Skip to content

Commit

Permalink
remove computeVirtualTxHash (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored Oct 7, 2021
1 parent 064aa42 commit 1f87f2e
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions modules/apps/27-interchain-accounts/keeper/relay.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package keeper

import (
"encoding/binary"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/crypto/tmhash"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
Expand All @@ -15,16 +12,16 @@ import (

// TODO: implement middleware functionality, this will allow us to use capabilities to
// manage helper module access to owner addresses they do not have capabilities for
func (k Keeper) TrySendTx(ctx sdk.Context, portID string, data interface{}, memo string) ([]byte, error) {
func (k Keeper) TrySendTx(ctx sdk.Context, portID string, data interface{}, memo string) (uint64, error) {
// Check for the active channel
activeChannelId, found := k.GetActiveChannel(ctx, portID)
if !found {
return nil, types.ErrActiveChannelNotFound
return 0, types.ErrActiveChannelNotFound
}

sourceChannelEnd, found := k.channelKeeper.GetChannel(ctx, portID, activeChannelId)
if !found {
return []byte{}, sdkerrors.Wrap(channeltypes.ErrChannelNotFound, activeChannelId)
return 0, sdkerrors.Wrap(channeltypes.ErrChannelNotFound, activeChannelId)
}

destinationPort := sourceChannelEnd.GetCounterparty().GetPortID()
Expand All @@ -41,9 +38,9 @@ func (k Keeper) createOutgoingPacket(
destinationChannel string,
data interface{},
memo string,
) ([]byte, error) {
) (uint64, error) {
if data == nil {
return []byte{}, types.ErrInvalidOutgoingData
return 0, types.ErrInvalidOutgoingData
}

var (
Expand All @@ -55,22 +52,22 @@ func (k Keeper) createOutgoingPacket(
case []sdk.Msg:
txBytes, err = k.SerializeCosmosTx(k.cdc, data)
default:
return nil, sdkerrors.Wrapf(types.ErrInvalidOutgoingData, "message type %T is not supported", data)
return 0, sdkerrors.Wrapf(types.ErrInvalidOutgoingData, "message type %T is not supported", data)
}

if err != nil {
return nil, sdkerrors.Wrap(err, "serialization of transaction data failed")
return 0, sdkerrors.Wrap(err, "serialization of transaction data failed")
}

channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
if !ok {
return []byte{}, sdkerrors.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
return 0, sdkerrors.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
}

// get the next sequence
sequence, found := k.channelKeeper.GetNextSequenceSend(ctx, sourcePort, sourceChannel)
if !found {
return []byte{}, channeltypes.ErrSequenceSendNotFound
return 0, channeltypes.ErrSequenceSendNotFound
}

packetData := types.InterchainAccountPacketData{
Expand All @@ -94,7 +91,11 @@ func (k Keeper) createOutgoingPacket(
timeoutTimestamp,
)

return k.ComputeVirtualTxHash(packetData.Data, packet.Sequence), k.channelKeeper.SendPacket(ctx, channelCap, packet)
if err := k.channelKeeper.SendPacket(ctx, channelCap, packet); err != nil {
return 0, err
}

return packet.Sequence, nil
}

// DeserializeCosmosTx unmarshals and unpacks a slice of transaction bytes
Expand Down Expand Up @@ -188,13 +189,6 @@ func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
return handler(ctx, msg)
}

// Compute the virtual tx hash that is used only internally.
func (k Keeper) ComputeVirtualTxHash(txBytes []byte, seq uint64) []byte {
bz := make([]byte, 8)
binary.LittleEndian.PutUint64(bz, seq)
return tmhash.SumTruncated(append(txBytes, bz...))
}

func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) error {
var data types.InterchainAccountPacketData

Expand Down Expand Up @@ -224,12 +218,12 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
switch ack.Response.(type) {
case *channeltypes.Acknowledgement_Error:
if k.hook != nil {
k.hook.OnTxFailed(ctx, packet.SourcePort, packet.SourceChannel, k.ComputeVirtualTxHash(data.Data, packet.Sequence), data.Data)
k.hook.OnTxFailed(ctx, packet.SourcePort, packet.SourceChannel, packet.Data, data.Data)
}
return nil
case *channeltypes.Acknowledgement_Result:
if k.hook != nil {
k.hook.OnTxSucceeded(ctx, packet.SourcePort, packet.SourceChannel, k.ComputeVirtualTxHash(data.Data, packet.Sequence), data.Data)
k.hook.OnTxSucceeded(ctx, packet.SourcePort, packet.SourceChannel, packet.Data, data.Data)
}
return nil
default:
Expand All @@ -241,7 +235,7 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac

func (k Keeper) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, data types.InterchainAccountPacketData) error {
if k.hook != nil {
k.hook.OnTxFailed(ctx, packet.SourcePort, packet.SourceChannel, k.ComputeVirtualTxHash(data.Data, packet.Sequence), data.Data)
k.hook.OnTxFailed(ctx, packet.SourcePort, packet.SourceChannel, packet.Data, data.Data)
}

return nil
Expand Down

0 comments on commit 1f87f2e

Please sign in to comment.