Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Nov 7, 2023
1 parent b0acbb0 commit c5a42a0
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 27 deletions.
2 changes: 1 addition & 1 deletion x/axelarnet/keeper/message_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func escrowAssetToMessageSender(
asset := sdk.NewCoin(exported.NativeAsset, sdk.OneInt())
sender := routingCtx.Sender

if routingCtx.FeeGranter != nil {
if !routingCtx.FeeGranter.Empty() {
req := types.RouteMessageRequest{
Sender: routingCtx.Sender,
ID: msg.ID,
Expand Down
212 changes: 207 additions & 5 deletions x/axelarnet/keeper/message_route_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,218 @@
package keeper_test

import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"

"github.com/axelarnetwork/axelar-core/testutils/rand"
"github.com/axelarnetwork/axelar-core/x/axelarnet/exported"
"github.com/axelarnetwork/axelar-core/x/axelarnet/keeper"
"github.com/axelarnetwork/axelar-core/x/axelarnet/types"
"github.com/axelarnetwork/axelar-core/x/axelarnet/types/mock"
evmtestutils "github.com/axelarnetwork/axelar-core/x/evm/types/testutils"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
nexustestutils "github.com/axelarnetwork/axelar-core/x/nexus/exported/testutils"
"github.com/axelarnetwork/utils/funcs"
"github.com/axelarnetwork/utils/slices"
. "github.com/axelarnetwork/utils/test"
)

func randPayload() []byte {
bytesType := funcs.Must(abi.NewType("bytes", "bytes", nil))
stringType := funcs.Must(abi.NewType("string", "string", nil))
stringArrayType := funcs.Must(abi.NewType("string[]", "string[]", nil))

argNum := int(rand.I64Between(1, 10))

var args abi.Arguments
for i := 0; i < argNum; i += 1 {
args = append(args, abi.Argument{Type: stringType})
}

schema := abi.Arguments{{Type: stringType}, {Type: stringArrayType}, {Type: stringArrayType}, {Type: bytesType}}
payload := funcs.Must(
schema.Pack(
rand.StrBetween(5, 10),
slices.Expand2(func() string { return rand.Str(5) }, argNum),
slices.Expand2(func() string { return "string" }, argNum),
funcs.Must(args.Pack(slices.Expand2(func() interface{} { return "string" }, argNum)...)),
),
)

return append(funcs.Must(hexutil.Decode(types.CosmWasmV1)), payload...)
}

func randMsg(status nexus.GeneralMessage_Status, payload []byte, token ...*sdk.Coin) nexus.GeneralMessage {
var asset *sdk.Coin
if len(token) > 0 {
asset = token[0]
}

return nexus.GeneralMessage{
ID: rand.NormalizedStr(10),
Sender: nexus.CrossChainAddress{
Chain: nexustestutils.RandomChain(),
Address: rand.NormalizedStr(42),
},
Recipient: nexus.CrossChainAddress{
Chain: nexustestutils.RandomChain(),
Address: rand.NormalizedStr(42),
},
PayloadHash: evmtestutils.RandomHash().Bytes(),
Status: status,
Asset: asset,
SourceTxID: evmtestutils.RandomHash().Bytes(),
SourceTxIndex: uint64(rand.I64Between(0, 100)),
}
}

func TestNewMessageRoute(t *testing.T) {
// var (
// route nexus.MessageRoute
// )
var (
ctx sdk.Context
routingCtx nexus.RoutingContext
msg nexus.GeneralMessage
route nexus.MessageRoute

k keeper.Keeper
feegrantK *mock.FeegrantKeeperMock
ibcK *mock.IBCKeeperMock
bankK *mock.BankKeeperMock
nexusK *mock.NexusMock
accountK *mock.AccountKeeperMock
)

givenMessageRoute := Given("the message route", func() {
ctx, k, _, feegrantK = setup()

ibcK = &mock.IBCKeeperMock{}
bankK = &mock.BankKeeperMock{}
nexusK = &mock.NexusMock{}
accountK = &mock.AccountKeeperMock{}

route = keeper.NewMessageRoute(k, ibcK, feegrantK, bankK, nexusK, accountK)
})

givenMessageRoute.
When("payload is nil", func() {
routingCtx = nexus.RoutingContext{Payload: nil}
}).
Then("should return error", func(t *testing.T) {
assert.ErrorContains(t, route(ctx, routingCtx, msg), "payload is required")
}).
Run(t)

givenMessageRoute.
When("the message cannot be translated", func() {
routingCtx = nexus.RoutingContext{
Sender: rand.AccAddr(),
FeeGranter: nil,
Payload: rand.Bytes(100),
}
msg = randMsg(nexus.Processing, routingCtx.Payload)
}).
Then("should return error", func(t *testing.T) {
assert.ErrorContains(t, route(ctx, routingCtx, msg), "invalid payload")
}).
Run(t)

whenTheMessageCanBeTranslated := When("the message can be translated", func() {
routingCtx = nexus.RoutingContext{
Sender: rand.AccAddr(),
Payload: randPayload(),
}
})

givenMessageRoute.
When2(whenTheMessageCanBeTranslated).
When("the message has no token transfer", func() {
msg = randMsg(nexus.Processing, routingCtx.Payload)
}).
Branch(
When("the fee granter is not set", func() {
routingCtx.FeeGranter = nil
}).
Then("should deduct the fee from the sender", func(t *testing.T) {
bankK.SendCoinsFunc = func(_ sdk.Context, _, _ sdk.AccAddress, _ sdk.Coins) error { return nil }
ibcK.SendMessageFunc = func(_ context.Context, _ nexus.CrossChainAddress, _ sdk.Coin, _, _ string) error {
return nil
}

assert.NoError(t, route(ctx, routingCtx, msg))

assert.Len(t, bankK.SendCoinsCalls(), 1)
assert.Equal(t, routingCtx.Sender, bankK.SendCoinsCalls()[0].FromAddr)
assert.Equal(t, types.AxelarGMPAccount, bankK.SendCoinsCalls()[0].ToAddr)
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(exported.NativeAsset, sdk.OneInt())), bankK.SendCoinsCalls()[0].Amt)

assert.Len(t, ibcK.SendMessageCalls(), 1)
assert.Equal(t, msg.Recipient, ibcK.SendMessageCalls()[0].Recipient)
assert.Equal(t, sdk.NewCoin(exported.NativeAsset, sdk.OneInt()), ibcK.SendMessageCalls()[0].Asset)
assert.Equal(t, msg.ID, ibcK.SendMessageCalls()[0].ID)
}),

When("the fee granter is set", func() {
routingCtx.FeeGranter = rand.AccAddr()
}).
Then("should deduct the fee from the fee granter", func(t *testing.T) {
feegrantK.UseGrantedFeesFunc = func(_ sdk.Context, granter, _ sdk.AccAddress, _ sdk.Coins, _ []sdk.Msg) error {
return nil
}
bankK.SendCoinsFunc = func(_ sdk.Context, _, _ sdk.AccAddress, _ sdk.Coins) error { return nil }
ibcK.SendMessageFunc = func(_ context.Context, _ nexus.CrossChainAddress, _ sdk.Coin, _, _ string) error {
return nil
}

assert.NoError(t, route(ctx, routingCtx, msg))

assert.Len(t, feegrantK.UseGrantedFeesCalls(), 1)
assert.Equal(t, routingCtx.FeeGranter, feegrantK.UseGrantedFeesCalls()[0].Granter)
assert.Equal(t, routingCtx.Sender, feegrantK.UseGrantedFeesCalls()[0].Grantee)
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(exported.NativeAsset, sdk.OneInt())), feegrantK.UseGrantedFeesCalls()[0].Fee)

assert.Len(t, bankK.SendCoinsCalls(), 1)
assert.Equal(t, routingCtx.FeeGranter, bankK.SendCoinsCalls()[0].FromAddr)
assert.Equal(t, types.AxelarGMPAccount, bankK.SendCoinsCalls()[0].ToAddr)
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(exported.NativeAsset, sdk.OneInt())), bankK.SendCoinsCalls()[0].Amt)

assert.Len(t, ibcK.SendMessageCalls(), 1)
assert.Equal(t, msg.Recipient, ibcK.SendMessageCalls()[0].Recipient)
assert.Equal(t, sdk.NewCoin(exported.NativeAsset, sdk.OneInt()), ibcK.SendMessageCalls()[0].Asset)
assert.Equal(t, msg.ID, ibcK.SendMessageCalls()[0].ID)
}),
).
Run(t)

givenMessageRoute.
When2(whenTheMessageCanBeTranslated).
When("the message has token transfer", func() {
coin := rand.Coin()
msg = randMsg(nexus.Processing, routingCtx.Payload, &coin)
}).
Then("should deduct from the corresponding account", func(t *testing.T) {
nexusK.GetChainByNativeAssetFunc = func(_ sdk.Context, _ string) (nexus.Chain, bool) {
return exported.Axelarnet, true
}
bankK.SendCoinsFunc = func(_ sdk.Context, _, _ sdk.AccAddress, _ sdk.Coins) error { return nil }
ibcK.SendMessageFunc = func(_ context.Context, _ nexus.CrossChainAddress, _ sdk.Coin, _, _ string) error {
return nil
}

assert.NoError(t, route(ctx, routingCtx, msg))

assert.Len(t, bankK.SendCoinsCalls(), 1)
assert.Equal(t, types.GetEscrowAddress(msg.Asset.Denom), bankK.SendCoinsCalls()[0].FromAddr)
assert.Equal(t, types.AxelarGMPAccount, bankK.SendCoinsCalls()[0].ToAddr)
assert.Equal(t, sdk.NewCoins(*msg.Asset), bankK.SendCoinsCalls()[0].Amt)

// givenMessageRoute := Given("the message route", func() {
// })
assert.Len(t, ibcK.SendMessageCalls(), 1)
assert.Equal(t, msg.Recipient, ibcK.SendMessageCalls()[0].Recipient)
assert.Equal(t, *msg.Asset, ibcK.SendMessageCalls()[0].Asset)
assert.Equal(t, msg.ID, ibcK.SendMessageCalls()[0].ID)
}).
Run(t)
}
2 changes: 1 addition & 1 deletion x/axelarnet/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (s msgServer) RouteMessage(c context.Context, req *types.RouteMessageReques
FeeGranter: req.Feegranter,
Payload: req.Payload,
}
if err := s.nexus.RouteMessage(ctx, routingCtx, req.ID); err != nil {
if err := s.nexus.RouteMessage(ctx, req.ID, routingCtx); err != nil {
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions x/axelarnet/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,16 +989,16 @@ func TestRouteMessage(t *testing.T) {

givenMsgServer.
When("route message successfully", func() {
nexusK.RouteMessageFunc = func(_ sdk.Context, _ nexus.RoutingContext, _ string) error { return nil }
nexusK.RouteMessageFunc = func(_ sdk.Context, _ string, _ ...nexus.RoutingContext) error { return nil }
}).
Then("should route the correct message", func(t *testing.T) {
_, err := server.RouteMessage(sdk.WrapSDKContext(ctx), &req)

assert.NoError(t, err)
assert.Len(t, nexusK.RouteMessageCalls(), 1)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx.Sender, req.Sender)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx.FeeGranter, req.Feegranter)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx.Payload, req.Payload)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx[0].Sender, req.Sender)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx[0].FeeGranter, req.Feegranter)
assert.Equal(t, nexusK.RouteMessageCalls()[0].RoutingCtx[0].Payload, req.Payload)
assert.Equal(t, nexusK.RouteMessageCalls()[0].ID, req.ID)
}).
Run(t)
Expand Down
2 changes: 1 addition & 1 deletion x/axelarnet/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Nexus interface {
SetMessageFailed(ctx sdk.Context, id string) error
GenerateMessageID(ctx sdk.Context) (string, []byte, uint64)
ValidateAddress(ctx sdk.Context, address nexus.CrossChainAddress) error
RouteMessage(ctx sdk.Context, routingCtx nexus.RoutingContext, id string) error
RouteMessage(ctx sdk.Context, id string, routingCtx ...nexus.RoutingContext) error
}

// BankKeeper defines the expected interface contract the vesting module requires
Expand Down
Loading

0 comments on commit c5a42a0

Please sign in to comment.