From 4638464a46206fccec94a7788da0bbd74903bfb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20Axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 27 Jan 2022 14:05:25 +0100 Subject: [PATCH] chore: restructure code logically --- .../host/keeper/relay.go | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay.go b/modules/apps/27-interchain-accounts/host/keeper/relay.go index a499b6bc5f0..80d434309f4 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay.go @@ -9,32 +9,34 @@ import ( channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" ) -// AuthenticateTx ensures the provided msgs contain the correct interchain account signer address retrieved -// from state using the provided controller port identifier -func (k Keeper) AuthenticateTx(ctx sdk.Context, msgs []sdk.Msg, portID string) error { - interchainAccountAddr, found := k.GetInterchainAccountAddress(ctx, portID) - if !found { - return sdkerrors.Wrapf(icatypes.ErrInterchainAccountNotFound, "failed to retrieve interchain account on port %s", portID) +// OnRecvPacket handles a given interchain accounts packet on a destination host chain +func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) error { + var data icatypes.InterchainAccountPacketData + + if err := icatypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { + // UnmarshalJSON errors are indeterminate and therefore are not wrapped and included in failed acks + return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain account packet data") } - allowMsgs := k.GetAllowMessages(ctx) - for _, msg := range msgs { - if !types.ContainsMsgType(allowMsgs, msg) { - return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "message type not allowed: %s", sdk.MsgTypeURL(msg)) + switch data.Type { + case icatypes.EXECUTE_TX: + msgs, err := icatypes.DeserializeCosmosTx(k.cdc, data.Data) + if err != nil { + return err } - for _, signer := range msg.GetSigners() { - if interchainAccountAddr != signer.String() { - return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "unexpected signer address: expected %s, got %s", interchainAccountAddr, signer.String()) - } + if err = k.executeTx(ctx, packet.SourcePort, packet.DestinationPort, packet.DestinationChannel, msgs); err != nil { + return err } - } - return nil + return nil + default: + return icatypes.ErrUnknownDataType + } } func (k Keeper) executeTx(ctx sdk.Context, sourcePort, destPort, destChannel string, msgs []sdk.Msg) error { - if err := k.AuthenticateTx(ctx, msgs, sourcePort); err != nil { + if err := k.authenticateTx(ctx, msgs, sourcePort); err != nil { return err } @@ -58,6 +60,30 @@ func (k Keeper) executeTx(ctx sdk.Context, sourcePort, destPort, destChannel str return nil } +// authenticateTx ensures the provided msgs contain the correct interchain account signer address retrieved +// from state using the provided controller port identifier +func (k Keeper) authenticateTx(ctx sdk.Context, msgs []sdk.Msg, portID string) error { + interchainAccountAddr, found := k.GetInterchainAccountAddress(ctx, portID) + if !found { + return sdkerrors.Wrapf(icatypes.ErrInterchainAccountNotFound, "failed to retrieve interchain account on port %s", portID) + } + + allowMsgs := k.GetAllowMessages(ctx) + for _, msg := range msgs { + if !types.ContainsMsgType(allowMsgs, msg) { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "message type not allowed: %s", sdk.MsgTypeURL(msg)) + } + + for _, signer := range msg.GetSigners() { + if interchainAccountAddr != signer.String() { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "unexpected signer address: expected %s, got %s", interchainAccountAddr, signer.String()) + } + } + } + + return nil +} + // Attempts to get the message handler from the router and if found will then execute the message func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) error { handler := k.msgRouter.Handler(msg) @@ -75,29 +101,3 @@ func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) error { return nil } - -// OnRecvPacket handles a given interchain accounts packet on a destination host chain -func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) error { - var data icatypes.InterchainAccountPacketData - - if err := icatypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { - // UnmarshalJSON errors are indeterminate and therefore are not wrapped and included in failed acks - return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain account packet data") - } - - switch data.Type { - case icatypes.EXECUTE_TX: - msgs, err := icatypes.DeserializeCosmosTx(k.cdc, data.Data) - if err != nil { - return err - } - - if err = k.executeTx(ctx, packet.SourcePort, packet.DestinationPort, packet.DestinationChannel, msgs); err != nil { - return err - } - - return nil - default: - return icatypes.ErrUnknownDataType - } -}