Skip to content

Commit

Permalink
Merge branch 'main' into axe-1400
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth authored Aug 23, 2023
2 parents 0278a08 + 70b9030 commit 3ad7084
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion x/axelarnet/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func (s msgServer) CallContract(c context.Context, req *types.CallContractReques
})

if req.Fee != nil {
err := s.bank.SendCoins(ctx, req.Sender, req.Fee.Recipient, sdk.NewCoins(req.Fee.Amount))
token, err := NewCoin(ctx, s.ibcK, s.nexus, req.Fee.Amount)
if err != nil {
return nil, sdkerrors.Wrap(err, "unrecognized fee denom")
}

err = s.bank.SendCoins(ctx, req.Sender, req.Fee.Recipient, sdk.NewCoins(req.Fee.Amount))
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to transfer fee")
}
Expand All @@ -98,6 +103,7 @@ func (s msgServer) CallContract(c context.Context, req *types.CallContractReques
MessageID: msgID,
Recipient: req.Fee.Recipient,
Fee: req.Fee.Amount,
Asset: token.GetDenom(),
}
if req.Fee.RefundRecipient != nil {
feePaidEvent.RefundRecipient = req.Fee.RefundRecipient.String()
Expand Down
2 changes: 1 addition & 1 deletion x/axelarnet/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@ func TestHandleCallContract(t *testing.T) {
return fmt.Sprintf("%s-%x", hex.EncodeToString(hash[:]), count), hash[:], uint64(count)
}
b.SendCoinsFunc = func(sdk.Context, sdk.AccAddress, sdk.AccAddress, sdk.Coins) error { return nil }
nexusK.GetChainByNativeAssetFunc = func(_ sdk.Context, asset string) (nexus.Chain, bool) { return exported.Axelarnet, true }
})

whenChainIsRegistered := When("chain is registered", func() {
Expand Down Expand Up @@ -1325,7 +1326,6 @@ func TestHandleCallContract(t *testing.T) {
evmtestutils.RandomAddress().Hex(),
rand.BytesBetween(5, 1000),
&types.Fee{Amount: rand.Coin(), Recipient: rand.AccAddr()})

})

callFails := Then("call contract request fails", func(t *testing.T) {
Expand Down
19 changes: 17 additions & 2 deletions x/axelarnet/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package axelarnet
import (
"encoding/json"
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -93,7 +94,7 @@ type Message struct {

// OnRecvMessage handles general message from a cosmos chain
func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n types.Nexus, b types.BankKeeper, r RateLimiter, packet ibcexported.PacketI) ibcexported.Acknowledgement {
// the acknowledgement is considered successful if it is a ResultAcknowledgement,
// The acknowledgement is considered successful if it is a ResultAcknowledgement,
// follow ibc transfer convention, put byte(1) in ResultAcknowledgement to indicate success.
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})

Expand All @@ -102,7 +103,12 @@ func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n ty
return channeltypes.NewErrorAcknowledgement(err)
}

// skip if packet not sent to Axelar message sender account
err = validateReceiver(data.GetReceiver())
if err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}

// Skip if packet not sent to Axelar message sender account.
if data.GetReceiver() != types.AxelarGMPAccount.String() {
// Rate limit non-GMP IBC transfers
// IBC receives are rate limited on the Incoming direction (tokens coming in to Axelar hub).
Expand Down Expand Up @@ -398,3 +404,12 @@ func deductFee(ctx sdk.Context, b types.BankKeeper, fee *Fee, token keeper.Coin,

return token, b.SendCoins(ctx, types.AxelarGMPAccount, 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))) {
return fmt.Errorf("uppercase GMP account address is not allowed")
}

return nil
}
22 changes: 21 additions & 1 deletion x/axelarnet/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -83,13 +84,14 @@ func TestHandleMessage(t *testing.T) {
return srcChain, true
default:
return nexus.Chain{}, false

}
},
ValidateAddressFunc: func(ctx sdk.Context, address nexus.CrossChainAddress) error {
switch address.Chain.Module {
case evmtypes.ModuleName:
return evmKeeper.NewAddressValidator()(ctx, address)
case exported.ModuleName:
return keeper.NewAddressValidator(k)(ctx, address)
default:
return fmt.Errorf("module not found")
}
Expand Down Expand Up @@ -379,9 +381,27 @@ func TestHandleMessage(t *testing.T) {
}).
Run(t)

whenMessageIsValid.
When("receiver is in uppercase", func() {
ics20Packet.Receiver = strings.ToUpper(ics20Packet.Receiver)
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)
}).
Then("should return ack error", func(t *testing.T) { ackError() }).
Run(t)

whenMessageIsValid.
When("dest chain is cosmos", func() {
funcs.MustNoErr(k.SetCosmosChain(ctx, types.CosmosChain{
Name: destChain.Name,
IBCPath: types.NewIBCPath(ibctransfertypes.PortID, axelartestutils.RandomChannel()),
AddrPrefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
}))
message.DestinationAddress = rand.AccAddr().String()
ics20Packet.Memo = string(funcs.Must(json.Marshal(message)))
packet = axelartestutils.RandomPacket(ics20Packet, ibctransfertypes.PortID, sourceChannel, ibctransfertypes.PortID, receiverChannel)

destChain.Module = exported.ModuleName
isChainFound(destChain, true)()
}).
When("fee denom is registered", isAssetRegistered(true)).
When("message with fee", func() {
Expand Down

0 comments on commit 3ad7084

Please sign in to comment.