Skip to content

Commit

Permalink
fix(amplifier): allow incoming messages from IBC to be forwarded to w…
Browse files Browse the repository at this point in the history
…asm (#2156)
  • Loading branch information
jcs47 authored Jul 25, 2024
1 parent 8d22714 commit 35082d5
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 136 deletions.
56 changes: 35 additions & 21 deletions x/axelarnet/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/CosmWasm/wasmd/x/wasm"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/axelarnetwork/axelar-core/x/axelarnet/keeper"
"github.com/axelarnetwork/axelar-core/x/axelarnet/types"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
tss "github.com/axelarnetwork/axelar-core/x/tss/exported"
"github.com/axelarnetwork/utils/funcs"
)

Expand Down Expand Up @@ -50,7 +52,7 @@ func (f Fee) ValidateBasic() error {
return nil
}

func validateFee(ctx sdk.Context, n types.Nexus, b types.BankKeeper, token sdk.Coin, msgType nexus.MessageType, sourceChain nexus.Chain, fee Fee) error {
func validateFee(ctx sdk.Context, n types.Nexus, token sdk.Coin, msgType nexus.MessageType, sourceChain nexus.Chain, fee Fee) error {
if err := fee.ValidateBasic(); err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +134,7 @@ func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n ty

path := types.NewIBCPath(packet.GetDestPort(), packet.GetDestChannel())

if err := validateMessage(ctx, ibcK, n, b, path, msg, token); err != nil {
if err := validateMessage(ctx, ibcK, n, path, msg, token); err != nil {
ibcK.Logger(ctx).Debug(fmt.Sprintf("failed validating message: %s", err.Error()),
"src_channel", packet.GetSourceChannel(),
"dest_channel", packet.GetDestChannel(),
Expand Down Expand Up @@ -182,9 +184,10 @@ func OnRecvMessage(ctx sdk.Context, k keeper.Keeper, ibcK keeper.IBCKeeper, n ty
return ack
}

func validateMessage(ctx sdk.Context, ibcK keeper.IBCKeeper, n types.Nexus, b types.BankKeeper, ibcPath string, msg Message, token keeper.Coin) error {
srcChainName, ok := ibcK.GetChainNameByIBCPath(ctx, ibcPath)
if !ok {
func validateMessage(ctx sdk.Context, ibcK keeper.IBCKeeper, n types.Nexus, ibcPath string, msg Message, token keeper.Coin) error {
// validate source chain
srcChainName, srcChainFound := ibcK.GetChainNameByIBCPath(ctx, ibcPath)
if !srcChainFound {
return fmt.Errorf("unrecognized IBC path %s", ibcPath)
}
srcChain := funcs.MustOk(n.GetChain(ctx, srcChainName))
Expand All @@ -193,27 +196,26 @@ func validateMessage(ctx sdk.Context, ibcK keeper.IBCKeeper, n types.Nexus, b ty
return fmt.Errorf("chain %s registered for IBC path %s is deactivated", srcChain.Name, ibcPath)
}

if msg.Fee != nil {
err := validateFee(ctx, n, token.Coin, nexus.MessageType(msg.Type), srcChain, *msg.Fee)
if err != nil {
return err
}
}

// validate destination chain
destChainName := nexus.ChainName(msg.DestinationChain)
if err := destChainName.Validate(); err != nil {
return err
}

destChain, ok := n.GetChain(ctx, destChainName)
if !ok {
return fmt.Errorf("unrecognized destination chain %s", destChainName)
}

if !n.IsChainActivated(ctx, destChain) {
return fmt.Errorf("chain %s is deactivated", destChain.Name)
}

if err := n.ValidateAddress(ctx, nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}); err != nil {
return err
}
destChain, destChainFound := n.GetChain(ctx, destChainName)
if destChainFound { // if chain can't be found in the nexus module, leave rest of validation up to amplifier (unless it has tokens, see below)
if !n.IsChainActivated(ctx, destChain) {
return fmt.Errorf("chain %s is deactivated", destChain.Name)
}

if msg.Fee != nil {
err := validateFee(ctx, n, b, token.Coin, nexus.MessageType(msg.Type), srcChain, *msg.Fee)
if err != nil {
if err := n.ValidateAddress(ctx, nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}); err != nil {
return err
}
}
Expand All @@ -222,6 +224,12 @@ func validateMessage(ctx sdk.Context, ibcK keeper.IBCKeeper, n types.Nexus, b ty
case nexus.TypeGeneralMessage:
return nil
case nexus.TypeGeneralMessageWithToken, nexus.TypeSendToken:

// if destination chain is not found but has tokens, it should not be allowed to be sent to amplifier
if !destChainFound {
return fmt.Errorf("unrecognized destination chain %s", destChainName)
}

if !n.IsAssetRegistered(ctx, srcChain, token.GetDenom()) {
return fmt.Errorf("asset %s is not registered on chain %s", token.GetDenom(), srcChain.Name)
}
Expand All @@ -244,7 +252,13 @@ func handleMessage(ctx sdk.Context, n types.Nexus, b types.BankKeeper, sourceAdd
return err
}

destChain := funcs.MustOk(n.GetChain(ctx, nexus.ChainName(msg.DestinationChain)))
chainName := nexus.ChainName(msg.DestinationChain)
destChain, ok := n.GetChain(ctx, chainName)
if !ok {
// try forwarding it to wasm router if destination chain is not registered
destChain = nexus.Chain{Name: chainName, SupportsForeignAssets: false, KeyType: tss.None, Module: wasm.ModuleName}
}

recipient := nexus.CrossChainAddress{Chain: destChain, Address: msg.DestinationAddress}
m := nexus.NewGeneralMessage(
id,
Expand Down
Loading

0 comments on commit 35082d5

Please sign in to comment.