Skip to content

Commit

Permalink
cleaning up AuthenticateTx and executeTx to reduce unnecessary comple…
Browse files Browse the repository at this point in the history
…xity
  • Loading branch information
damiannolan committed Oct 13, 2021
1 parent 4db5c83 commit 735e2db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
48 changes: 17 additions & 31 deletions modules/apps/27-interchain-accounts/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,60 +99,46 @@ func (k Keeper) DeserializeCosmosTx(_ sdk.Context, data []byte) ([]sdk.Msg, erro
return msgs, nil
}

func (k Keeper) AuthenticateTx(ctx sdk.Context, msgs []sdk.Msg, portId string) error {
seen := map[string]bool{}
var signers []sdk.AccAddress
for _, msg := range msgs {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, addr)
seen[addr.String()] = true
}
}
}

interchainAccountAddr, found := k.GetInterchainAccountAddress(ctx, portId)
// 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.ErrUnauthorized
}

for _, signer := range signers {
if interchainAccountAddr != signer.String() {
return sdkerrors.ErrUnauthorized
for _, msg := range msgs {
for _, signer := range msg.GetSigners() {
if interchainAccountAddr != signer.String() {
return sdkerrors.ErrUnauthorized
}
}
}

return nil
}

func (k Keeper) executeTx(ctx sdk.Context, sourcePort, destPort, destChannel string, msgs []sdk.Msg) error {
err := k.AuthenticateTx(ctx, msgs, sourcePort)
if err != nil {
if err := k.AuthenticateTx(ctx, msgs, sourcePort); err != nil {
return err
}

for _, msg := range msgs {
err := msg.ValidateBasic()
if err != nil {
if err := msg.ValidateBasic(); err != nil {
return err
}
}

cacheContext, writeFn := ctx.CacheContext()
// CacheContext returns a new context with the multi-store branched into a cached storage object
// writeCache is called only if all msgs succeed, performing state transitions atomically
cacheCtx, writeCache := ctx.CacheContext()
for _, msg := range msgs {
_, msgErr := k.executeMsg(cacheContext, msg)
if msgErr != nil {
err = msgErr
break
if _, err := k.executeMsg(cacheCtx, msg); err != nil {
return err
}
}

if err != nil {
return err
}

// Write the state transitions if all handlers succeed.
writeFn()
writeCache()

return nil
}
Expand Down
1 change: 0 additions & 1 deletion modules/apps/27-interchain-accounts/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (iapd InterchainAccountPacketData) ValidateBasic() error {
if len(iapd.Memo) > MaxMemoCharLength {
return sdkerrors.Wrapf(ErrInvalidOutgoingData, "packet data memo cannot be greater than %d characters", MaxMemoCharLength)
}
// TODO: add type validation when data type enum supports unspecified type

return nil
}
Expand Down

0 comments on commit 735e2db

Please sign in to comment.