-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nexus): add the wasm message route (#2023)
- Loading branch information
1 parent
c2bcf4c
commit 3472ac6
Showing
10 changed files
with
1,035 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/axelarnetwork/axelar-core/x/nexus/exported" | ||
types "github.com/axelarnetwork/axelar-core/x/nexus/types" | ||
) | ||
|
||
type request struct { | ||
RouteMessages []exported.WasmMessage `json:"route_messages"` | ||
} | ||
|
||
// NewMessageRoute creates a new message route | ||
func NewMessageRoute(nexus types.Nexus, account types.AccountKeeper, wasm types.WasmKeeper) exported.MessageRoute { | ||
return func(ctx sdk.Context, _ exported.RoutingContext, msg exported.GeneralMessage) error { | ||
if msg.Asset != nil { | ||
return fmt.Errorf("asset transfer is not supported") | ||
} | ||
|
||
gateway := nexus.GetParams(ctx).Gateway | ||
if gateway.Empty() { | ||
return fmt.Errorf("gateway is not set") | ||
} | ||
|
||
bz, err := json.Marshal(request{RouteMessages: []exported.WasmMessage{exported.FromGeneralMessage(msg)}}) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if _, err := wasm.Execute(ctx, gateway, account.GetModuleAddress(types.ModuleName), bz, sdk.NewCoins()); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
"github.com/axelarnetwork/axelar-core/testutils/fake" | ||
"github.com/axelarnetwork/axelar-core/testutils/rand" | ||
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported" | ||
"github.com/axelarnetwork/axelar-core/x/nexus/keeper" | ||
"github.com/axelarnetwork/axelar-core/x/nexus/types" | ||
"github.com/axelarnetwork/axelar-core/x/nexus/types/mock" | ||
. "github.com/axelarnetwork/utils/test" | ||
) | ||
|
||
func TestNewMessageRoute(t *testing.T) { | ||
var ( | ||
ctx sdk.Context | ||
route nexus.MessageRoute | ||
msg nexus.GeneralMessage | ||
|
||
nexusK *mock.NexusMock | ||
accountK *mock.AccountKeeperMock | ||
wasmK *mock.WasmKeeperMock | ||
gateway sdk.AccAddress | ||
) | ||
|
||
givenMessageRoute := Given("the message route", func() { | ||
ctx = sdk.NewContext(fake.NewMultiStore(), tmproto.Header{}, false, log.TestingLogger()) | ||
|
||
nexusK = &mock.NexusMock{} | ||
accountK = &mock.AccountKeeperMock{} | ||
wasmK = &mock.WasmKeeperMock{} | ||
|
||
route = keeper.NewMessageRoute(nexusK, accountK, wasmK) | ||
}) | ||
|
||
givenMessageRoute. | ||
When("the gateway is not set", func() { | ||
nexusK.GetParamsFunc = func(ctx sdk.Context) types.Params { return types.DefaultParams() } | ||
}). | ||
Then("should return error", func(t *testing.T) { | ||
assert.ErrorContains(t, route(ctx, nexus.RoutingContext{}, msg), "gateway is not set") | ||
}). | ||
Run(t) | ||
|
||
givenMessageRoute. | ||
When("the gateway is set", func() { | ||
nexusK.GetParamsFunc = func(ctx sdk.Context) types.Params { | ||
gateway = rand.AccAddr() | ||
|
||
params := types.DefaultParams() | ||
params.Gateway = gateway | ||
|
||
return params | ||
} | ||
}). | ||
Branch( | ||
When("the message has an asset", func() { | ||
msg = randMsg(nexus.Processing, true) | ||
}). | ||
Then("should return error", func(t *testing.T) { | ||
assert.ErrorContains(t, route(ctx, nexus.RoutingContext{}, msg), "asset transfer is not supported") | ||
}), | ||
|
||
When("the message has no asset", func() { | ||
msg = randMsg(nexus.Processing) | ||
}). | ||
Then("should execute the wasm message", func(t *testing.T) { | ||
moduleAddr := rand.AccAddr() | ||
accountK.GetModuleAddressFunc = func(_ string) sdk.AccAddress { return moduleAddr } | ||
|
||
wasmK.ExecuteFunc = func(_ sdk.Context, _, _ sdk.AccAddress, _ []byte, _ sdk.Coins) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
assert.NoError(t, route(ctx, nexus.RoutingContext{}, msg)) | ||
|
||
assert.Len(t, wasmK.ExecuteCalls(), 1) | ||
assert.Equal(t, wasmK.ExecuteCalls()[0].ContractAddress, gateway) | ||
assert.Equal(t, wasmK.ExecuteCalls()[0].Caller, moduleAddr) | ||
assert.Empty(t, wasmK.ExecuteCalls()[0].Coins) | ||
|
||
type req struct { | ||
RouteMessages []nexus.WasmMessage `json:"route_messages"` | ||
} | ||
|
||
var actual req | ||
assert.NoError(t, json.Unmarshal(wasmK.ExecuteCalls()[0].Msg, &actual)) | ||
assert.Len(t, actual.RouteMessages, 1) | ||
assert.Equal(t, nexus.FromGeneralMessage(msg), actual.RouteMessages[0]) | ||
}), | ||
). | ||
Run(t) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.