Skip to content

Commit

Permalink
fix tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Sep 24, 2024
1 parent 501f5c3 commit ed144a3
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 76 deletions.
3 changes: 1 addition & 2 deletions x/axelarnet/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"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"
"github.com/axelarnetwork/axelar-core/x/axelarnet/types/testutils"
axelartestutils "github.com/axelarnetwork/axelar-core/x/axelarnet/types/testutils"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/utils/math"
Expand Down Expand Up @@ -108,7 +107,7 @@ func TestEndBlocker(t *testing.T) {
return false
}

transfer := testutils.RandomIBCTransfer()
transfer := axelartestutils.RandomIBCTransfer()
bz, _ := transfer.Marshal()
if err := value.Unmarshal(bz); err != nil {
panic(err)
Expand Down
3 changes: 1 addition & 2 deletions x/axelarnet/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
abci "github.com/tendermint/tendermint/proto/tendermint/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/axelarnetwork/axelar-core/testutils/fake"
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestQuerier_PendingIBCTransferCount(t *testing.T) {
querier = keeper.NewGRPCQuerier(k, n)
}).
When("IBC transfer counts are queried", func() {
ctx := sdk.NewContext(fake.NewMultiStore(), abci.Header{}, false, log.TestingLogger())
ctx := sdk.NewContext(fake.NewMultiStore(), tmproto.Header{}, false, log.TestingLogger())
var err error
response, err = querier.PendingIBCTransferCount(sdk.WrapSDKContext(ctx), &types.PendingIBCTransferCountRequest{})
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion x/axelarnet/keeper/ibc_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (i IBCKeeper) SendMessage(c context.Context, recipient nexus.CrossChainAddr
return err
}

msg := ibctypes.NewMsgTransfer(portID, channelID, asset, types.AxelarGMPAccount.String(), recipient.Address, height, 0)
msg := ibctypes.NewMsgTransfer(portID, channelID, asset, types.AxelarIBCAccount.String(), recipient.Address, height, 0)
msg.Memo = payload

res, err := i.ibcTransferK.Transfer(c, msg)
Expand Down
4 changes: 2 additions & 2 deletions x/axelarnet/keeper/message_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func escrowAssetToMessageSender(
sender = routingCtx.FeeGranter
}

return asset, bankK.SendCoins(ctx, sender, types.AxelarGMPAccount, sdk.NewCoins(asset))
return asset, bankK.SendCoins(ctx, sender, types.AxelarIBCAccount, sdk.NewCoins(asset))
case nexus.TypeGeneralMessageWithToken:
coin, err := nexustypes.NewCoin(ctx, nexusK, ibcK, bankK, *msg.Asset)
if err != nil {
return sdk.Coin{}, err
}

return coin.GetOriginalCoin(ctx), coin.Unlock(ctx, types.AxelarGMPAccount)
return coin.GetOriginalCoin(ctx), coin.Unlock(ctx, types.AxelarIBCAccount)
default:
return sdk.Coin{}, fmt.Errorf("unrecognized message type")
}
Expand Down
18 changes: 9 additions & 9 deletions x/axelarnet/keeper/message_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"context"
"crypto/sha256"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -53,6 +54,8 @@ func randMsg(status nexus.GeneralMessage_Status, payload []byte, token ...*sdk.C
asset = token[0]
}

payloadHash := sha256.Sum256(payload)

return nexus.GeneralMessage{
ID: rand.NormalizedStr(10),
Sender: nexus.CrossChainAddress{
Expand All @@ -63,7 +66,7 @@ func randMsg(status nexus.GeneralMessage_Status, payload []byte, token ...*sdk.C
Chain: nexustestutils.RandomChain(),
Address: rand.NormalizedStr(42),
},
PayloadHash: evmtestutils.RandomHash().Bytes(),
PayloadHash: payloadHash[:],
Status: status,
Asset: asset,
SourceTxID: evmtestutils.RandomHash().Bytes(),
Expand All @@ -78,28 +81,25 @@ func TestNewMessageRoute(t *testing.T) {
msg nexus.GeneralMessage
route nexus.MessageRoute

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

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

ibcK = &mock.IBCKeeperMock{}
bankK = &mock.BankKeeperMock{}
nexusK = &mock.NexusMock{}
accountK = &mock.AccountKeeperMock{}
stakingK = &mock.StakingKeeperMock{}
stakingK.BondDenomFunc = func(ctx sdk.Context) string {
return exported.NativeAsset
}

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

givenMessageRoute.
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestNewMessageRoute(t *testing.T) {

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, types.AxelarIBCAccount, 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)
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestNewMessageRoute(t *testing.T) {

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, types.AxelarIBCAccount, 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)
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestNewMessageRoute(t *testing.T) {

assert.Len(t, bankK.SendCoinsCalls(), 1)
assert.Equal(t, types.GetEscrowAddress(msg.Asset.Denom), bankK.SendCoinsCalls()[0].FromAddr)

Check failure on line 213 in x/axelarnet/keeper/message_route_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: types.GetEscrowAddress
assert.Equal(t, types.AxelarGMPAccount, bankK.SendCoinsCalls()[0].ToAddr)
assert.Equal(t, types.AxelarIBCAccount, bankK.SendCoinsCalls()[0].ToAddr)
assert.Equal(t, sdk.NewCoins(*msg.Asset), bankK.SendCoinsCalls()[0].Amt)

assert.Len(t, ibcK.SendMessageCalls(), 1)
Expand Down
4 changes: 2 additions & 2 deletions x/axelarnet/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ func (s msgServer) RouteIBCTransfers(c context.Context, _ *types.RouteIBCTransfe
continue
}

if err := coin.Unlock(ctx, types.AxelarGMPAccount); err != nil {
if err := coin.Unlock(ctx, types.AxelarIBCAccount); err != nil {
s.Logger(ctx).Error(fmt.Sprintf("failed to route IBC transfer %s: %s", p.String(), err))
continue
}

funcs.MustNoErr(s.EnqueueIBCTransfer(ctx, types.NewIBCTransfer(types.AxelarGMPAccount, p.Recipient.Address, coin.GetOriginalCoin(ctx), portID, channelID, p.ID)))
funcs.MustNoErr(s.EnqueueIBCTransfer(ctx, types.NewIBCTransfer(types.AxelarIBCAccount, p.Recipient.Address, coin.GetOriginalCoin(ctx), portID, channelID, p.ID)))
s.nexus.ArchivePendingTransfer(ctx, p)
}
}
Expand Down
29 changes: 8 additions & 21 deletions x/axelarnet/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestHandleMsgLink(t *testing.T) {
k.InitGenesis(ctx, types.DefaultGenesisState())
nexusK = &mock.NexusMock{}
ibcK := keeper.NewIBCKeeper(k, &mock.IBCTransferKeeperMock{})
server = keeper.NewMsgServerImpl(k, nexusK, &mock.BankKeeperMock{}, &mock.AccountKeeperMock{}, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, &mock.BankKeeperMock{}, ibcK)
})

whenChainIsRegistered := When("chain is registered", func() {
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestHandleMsgConfirmDeposit(t *testing.T) {
},
}
ibcK := keeper.NewIBCKeeper(k, transferK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, &mock.AccountKeeperMock{}, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, ibcK)
})

recipientIsFound := When("recipient is found", func() {
Expand Down Expand Up @@ -387,13 +387,8 @@ func TestHandleMsgExecutePendingTransfers(t *testing.T) {
}
bankK = &mock.BankKeeperMock{}
transferK = &mock.IBCTransferKeeperMock{}
accountK := &mock.AccountKeeperMock{
GetModuleAddressFunc: func(moduleName string) sdk.AccAddress {
return rand.AccAddr()
},
}
ibcK := keeper.NewIBCKeeper(k, transferK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, accountK, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, ibcK)
})

whenAssetOriginsFromExternalCosmosChain := When("asset is from external cosmos chain", func() {
Expand Down Expand Up @@ -595,13 +590,8 @@ func TestHandleMsgRouteIBCTransfers(t *testing.T) {
}
bankK = &mock.BankKeeperMock{}
transferK = &mock.IBCTransferKeeperMock{}
accountK := &mock.AccountKeeperMock{
GetModuleAddressFunc: func(string) sdk.AccAddress {
return rand.AccAddr()
},
}
ibcK := keeper.NewIBCKeeper(k, transferK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, accountK, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, ibcK)
})

whenAssetOriginsFromExternalCosmosChain := When("asset is from external cosmos chain", func() {
Expand Down Expand Up @@ -726,7 +716,6 @@ func TestRetryIBCTransfer(t *testing.T) {
n *mock.NexusMock
b *mock.BankKeeperMock
i *mock.IBCTransferKeeperMock
a *mock.AccountKeeperMock
channelK *mock.ChannelKeeperMock
ctx sdk.Context
chain nexus.Chain
Expand All @@ -750,7 +739,6 @@ func TestRetryIBCTransfer(t *testing.T) {
funcs.MustNoErr(k.SetChainByIBCPath(ctx, path, cosmosChain.Name))

b = &mock.BankKeeperMock{}
a = &mock.AccountKeeperMock{}
i = &mock.IBCTransferKeeperMock{
SendTransferFunc: func(sdk.Context, string, string, sdk.Coin, sdk.AccAddress, string, clienttypes.Height, uint64) error {
return nil
Expand All @@ -774,7 +762,7 @@ func TestRetryIBCTransfer(t *testing.T) {
}

ibcK := keeper.NewIBCKeeper(k, i)
server = keeper.NewMsgServerImpl(k, n, b, a, ibcK)
server = keeper.NewMsgServerImpl(k, n, b, ibcK)
})

requestIsMade := When("a retry failed transfer request is made", func() {
Expand Down Expand Up @@ -858,7 +846,7 @@ func TestAddCosmosBasedChain(t *testing.T) {
},
}
ibcK := keeper.NewIBCKeeper(k, &mock.IBCTransferKeeperMock{})
server = keeper.NewMsgServerImpl(k, nexusK, &mock.BankKeeperMock{}, &mock.AccountKeeperMock{}, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, &mock.BankKeeperMock{}, ibcK)
})

addChainRequest := When("an add cosmos based chain request is created", func() {
Expand Down Expand Up @@ -984,8 +972,7 @@ func TestRouteMessage(t *testing.T) {
nexusK = &mock.NexusMock{}
ibcK := keeper.NewIBCKeeper(k, &mock.IBCTransferKeeperMock{})
bankK := &mock.BankKeeperMock{}
accountK := &mock.AccountKeeperMock{}
server = keeper.NewMsgServerImpl(k, nexusK, bankK, accountK, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, bankK, ibcK)
})

givenMsgServer.
Expand Down Expand Up @@ -1022,7 +1009,7 @@ func TestHandleCallContract(t *testing.T) {
nexusK = &mock.NexusMock{}
ibcK := keeper.NewIBCKeeper(k, &mock.IBCTransferKeeperMock{})
b = &mock.BankKeeperMock{}
server = keeper.NewMsgServerImpl(k, nexusK, b, &mock.AccountKeeperMock{}, ibcK)
server = keeper.NewMsgServerImpl(k, nexusK, b, ibcK)
count := 0
nexusK.GenerateMessageIDFunc = func(ctx sdk.Context) (string, []byte, uint64) {
count++
Expand Down
14 changes: 7 additions & 7 deletions x/axelarnet/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n ty
}

// Skip if packet not sent to Axelar message sender account.
if data.GetReceiver() != types.AxelarGMPAccount.String() {
if data.GetReceiver() != types.AxelarIBCAccount.String() {
// Rate limit non-GMP IBC transfers
// IBC receives are rate limited on the from direction (tokens coming from the source chain).
if err := r.RateLimitPacket(ctx, packet, nexus.TransferDirectionFrom, types.NewIBCPath(packet.GetDestPort(), packet.GetDestChannel())); err != nil {
Expand Down Expand Up @@ -160,7 +160,7 @@ func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n ty
case nexus.TypeSendToken:
// Send token is already rate limited in nexus.EnqueueTransfer
rateLimitPacket = false
err = handleTokenSent(ctx, n, b, sourceAddress, msg, token)
err = handleTokenSent(ctx, n, sourceAddress, msg, token)
default:
err = sdkerrors.Wrapf(types.ErrGeneralMessage, "unrecognized Message type")
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func handleMessageWithToken(ctx sdk.Context, n types.Nexus, b types.BankKeeper,
return err
}

if err = token.Lock(ctx, types.AxelarGMPAccount); err != nil {
if err = token.Lock(ctx, types.AxelarIBCAccount); err != nil {
return err
}

Expand Down Expand Up @@ -323,11 +323,11 @@ func handleMessageWithToken(ctx sdk.Context, n types.Nexus, b types.BankKeeper,
return n.SetNewMessage(ctx, m)
}

func handleTokenSent(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAddress nexus.CrossChainAddress, msg Message, token nexustypes.Coin) error {
func handleTokenSent(ctx sdk.Context, n types.Nexus, sourceAddress nexus.CrossChainAddress, msg Message, token nexustypes.Coin) error {
destChain := funcs.MustOk(n.GetChain(ctx, nexus.ChainName(msg.DestinationChain)))
crossChainAddr := nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}

if err := token.Lock(ctx, types.AxelarGMPAccount); err != nil {
if err := token.Lock(ctx, types.AxelarIBCAccount); err != nil {
return err
}

Expand Down Expand Up @@ -416,12 +416,12 @@ func deductFee(ctx sdk.Context, b types.BankKeeper, fee *Fee, token nexustypes.C
// subtract fee from transfer value
token.Amount = token.Amount.Sub(feeAmount)

return token, b.SendCoins(ctx, types.AxelarGMPAccount, recipient, sdk.NewCoins(coin))
return token, b.SendCoins(ctx, types.AxelarIBCAccount, recipient, sdk.NewCoins(coin))
}

// validateReceiver rejects uppercase GMP account address
func validateReceiver(receiver string) error {
if strings.ToUpper(receiver) == receiver && types.AxelarGMPAccount.Equals(funcs.Must(sdk.AccAddressFromBech32(receiver))) {
if strings.ToUpper(receiver) == receiver && types.AxelarIBCAccount.Equals(funcs.Must(sdk.AccAddressFromBech32(receiver))) {
return fmt.Errorf("uppercase GMP account address is not allowed")
}

Expand Down
14 changes: 7 additions & 7 deletions x/axelarnet/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestHandleMessage(t *testing.T) {
whenPacketReceiverIsGMPAccount := givenPacketWithMessage.
When("receiver is gmp account", func() {
ics20Packet = ibctransfertypes.NewFungibleTokenPacketData(
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarGMPAccount.String(),
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarIBCAccount.String(),
)
ics20Packet.Memo = string(funcs.Must(json.Marshal(message)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestHandleMessageWithToken(t *testing.T) {
denom = rand.Denom(5, 10)
amount = strconv.FormatInt(rand.PosI64(), 10)
ics20Packet = ibctransfertypes.NewFungibleTokenPacketData(
denom, amount, rand.AccAddr().String(), types.AxelarGMPAccount.String(),
denom, amount, rand.AccAddr().String(), types.AxelarIBCAccount.String(),
)
ics20Packet.Memo = string(funcs.Must(json.Marshal(message)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestHandleMessageWithToken(t *testing.T) {
})
b = &mock.BankKeeperMock{
SpendableBalanceFunc: func(ctx sdk.Context, addr sdk.AccAddress, d string) sdk.Coin {
if addr.Equals(types.AxelarGMPAccount) {
if addr.Equals(types.AxelarIBCAccount) {
return sdk.NewCoin(d, funcs.MustOk(sdk.NewIntFromString(amount)).Sub(feeAmount))
}
return sdk.NewCoin(d, sdk.ZeroInt())
Expand Down Expand Up @@ -678,7 +678,7 @@ func TestHandleSendToken(t *testing.T) {
denom = rand.Denom(5, 10)
amount = strconv.FormatInt(rand.PosI64(), 10)
ics20Packet = ibctransfertypes.NewFungibleTokenPacketData(
denom, amount, rand.AccAddr().String(), types.AxelarGMPAccount.String(),
denom, amount, rand.AccAddr().String(), types.AxelarIBCAccount.String(),
)
ics20Packet.Memo = string(funcs.Must(json.Marshal(message)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
Expand Down Expand Up @@ -741,7 +741,7 @@ func TestHandleSendToken(t *testing.T) {
})
b = &mock.BankKeeperMock{
SpendableBalanceFunc: func(ctx sdk.Context, addr sdk.AccAddress, d string) sdk.Coin {
if addr.Equals(types.AxelarGMPAccount) {
if addr.Equals(types.AxelarIBCAccount) {
return sdk.NewCoin(d, funcs.MustOk(sdk.NewIntFromString(amount)))
}
return sdk.NewCoin(d, sdk.ZeroInt())
Expand Down Expand Up @@ -908,7 +908,7 @@ func TestTokenAndDestChainNotFound(t *testing.T) {
whenPacketReceiverIsGMPWithTokenAccount := givenPacketWithMessage.
When("receiver is gmp with token account", func() {
ics20Packet = ibctransfertypes.NewFungibleTokenPacketData(
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarGMPAccount.String(),
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarIBCAccount.String(),
)
ics20Packet.Memo = string(funcs.Must(json.Marshal(gmpWithToken)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
Expand All @@ -917,7 +917,7 @@ func TestTokenAndDestChainNotFound(t *testing.T) {
whenPacketReceiverIsSendTokenAccount := givenPacketWithMessage.
When("receiver is send token account", func() {
ics20Packet = ibctransfertypes.NewFungibleTokenPacketData(
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarGMPAccount.String(),
rand.Denom(5, 10), strconv.FormatInt(rand.PosI64(), 10), rand.AccAddr().String(), types.AxelarIBCAccount.String(),
)
ics20Packet.Memo = string(funcs.Must(json.Marshal(sendToken)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
Expand Down
Loading

0 comments on commit ed144a3

Please sign in to comment.