-
Notifications
You must be signed in to change notification settings - Fork 117
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
- Loading branch information
1 parent
4929397
commit be784b9
Showing
9 changed files
with
1,017 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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" | ||
) | ||
|
||
// 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 is not supported") | ||
} | ||
|
||
gateway := nexus.GetParams(ctx).Gateway | ||
if gateway.Empty() { | ||
return fmt.Errorf("gateway is not set") | ||
} | ||
|
||
bz, err := json.Marshal(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,96 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/axelarnetwork/axelar-core/testutils/fake" | ||
"github.com/axelarnetwork/axelar-core/testutils/rand" | ||
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" | ||
|
||
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 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) | ||
|
||
var actual nexus.WasmMessage | ||
assert.NoError(t, json.Unmarshal(wasmK.ExecuteCalls()[0].Msg, &actual)) | ||
assert.Equal(t, nexus.FromGeneralMessage(msg), actual) | ||
}), | ||
). | ||
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.