Skip to content

Commit

Permalink
chore: restructure code logically
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner committed Jan 27, 2022
1 parent 640ba14 commit 4638464
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions modules/apps/27-interchain-accounts/host/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}
}

0 comments on commit 4638464

Please sign in to comment.