Skip to content

Commit

Permalink
wip: experiementing with transfer test
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton committed Sep 18, 2024
1 parent 7104c3e commit 444c0fc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
9 changes: 4 additions & 5 deletions modules/apps/transfer/keeper/relay_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
)

func (k Keeper) OnRecvPacketV2(ctx context.Context, packet channeltypes.PacketV2, payload channeltypes.Payload, data types.FungibleTokenPacketDataV2) error {
Expand Down Expand Up @@ -177,10 +176,10 @@ func (k Keeper) OnSendPacket(
packetData types.FungibleTokenPacketDataV2,
sender sdk.AccAddress,
) error {
_, ok := k.packetServerKeeper.GetCounterparty(ctx, sourceID)
if !ok {
return errorsmod.Wrap(packetservertypes.ErrCounterpartyNotFound, sourceID)
}
//_, ok := k.packetServerKeeper.GetCounterparty(ctx, sourceID)

Check failure on line 179 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 179 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//if !ok {

Check failure on line 180 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

comment-spacings: no space between comment delimiter and comment text (revive)

Check failure on line 180 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

comment-spacings: no space between comment delimiter and comment text (revive)
// return errorsmod.Wrap(packetservertypes.ErrCounterpartyNotFound, sourceID)
//}

Check failure on line 182 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

comment-spacings: no space between comment delimiter and comment text (revive)

Check failure on line 182 in modules/apps/transfer/keeper/relay_v2.go

View workflow job for this annotation

GitHub Actions / lint

comment-spacings: no space between comment delimiter and comment text (revive)

var coins sdk.Coins
for _, token := range packetData.Tokens {
Expand Down
8 changes: 5 additions & 3 deletions modules/apps/transfer/transfer_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func (suite *TransferV2TestSuite) TestHandleMsgV2Transfer() {
coinsSentFromAToB = coinsSentFromAToB.Add(coinSentFromAToB)
}

sender := suite.chainB.SenderAccount.GetAddress().String()
suite.Require().NoError(pathAToB.EndpointA.UpdateClient())
suite.Require().NoError(pathAToB.EndpointB.UpdateClient())

ftpd := types.FungibleTokenPacketDataV2{
Tokens: []types.Token{
{
Expand All @@ -101,7 +103,7 @@ func (suite *TransferV2TestSuite) TestHandleMsgV2Transfer() {
Amount: "100",
},
},
Sender: sender,
Sender: suite.chainB.SenderAccount.GetAddress().String(),
Receiver: suite.chainA.SenderAccount.GetAddress().String(),
Memo: "",
Forwarding: types.ForwardingPacketData{},
Expand All @@ -125,7 +127,7 @@ func (suite *TransferV2TestSuite) TestHandleMsgV2Transfer() {
},
},
},
Signer: sender,
Signer: suite.chainB.SenderAccount.GetAddress().String(),
}

res, err = suite.chainB.SendMsgs(msgSendPacket)
Expand Down
26 changes: 26 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"context"
"errors"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
packetservertypes "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types"
"strconv"
"strings"

Expand Down Expand Up @@ -849,3 +851,27 @@ func (k *Keeper) PruneAcknowledgements(ctx context.Context, portID, channelID st

return totalPruned, totalRemaining, nil
}

// GetV2Counterparty returns a version 2 counterparty for the given port and channel ID
// by converting the channel into a version 2 counterparty
func (k *Keeper) GetV2Counterparty(ctx context.Context, portID, channelID string) (packetservertypes.Counterparty, bool) {
channel, ok := k.GetChannel(ctx, portID, channelID)
if !ok {
return packetservertypes.Counterparty{}, false
}
// Do not allow channel to be converted into a version 2 counterparty
// if the channel is not OPEN or if it is ORDERED
if channel.State != types.OPEN || channel.Ordering == types.ORDERED {
return packetservertypes.Counterparty{}, false
}
connection, ok := k.connectionKeeper.GetConnection(ctx, channel.ConnectionHops[0])
if !ok {
return packetservertypes.Counterparty{}, false
}
merklePathPrefix := commitmenttypes.NewMerklePath(connection.Counterparty.Prefix.KeyPrefix, []byte(""))
counterparty := packetservertypes.Counterparty{
ClientId: connection.ClientId,
MerklePathPrefix: merklePathPrefix,
}
return counterparty, true
}
13 changes: 13 additions & 0 deletions modules/core/packet-server/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ func (k *Keeper) GetCounterparty(ctx context.Context, clientID string) (types.Co
k.cdc.MustUnmarshal(bz, &counterparty)
return counterparty, true
}

// GetCounterparty gets the Counterparty for a given client identifier.
func (k *Keeper) GetCounterpartyV2(ctx context.Context, portID, channelID, clientID string) (types.Counterparty, bool) {
store := k.ChannelStore(ctx, clientID)
bz := store.Get([]byte(types.CounterpartyKey))
if len(bz) == 0 {
return k.ChannelKeeper.GetV2Counterparty(ctx, portID, channelID)
}

var counterparty types.Counterparty
k.cdc.MustUnmarshal(bz, &counterparty)
return counterparty, true
}
21 changes: 10 additions & 11 deletions modules/core/packet-server/keeper/relay_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,35 @@ func (k Keeper) SendPacketV2(
data []channeltypes.PacketData,
) (uint64, error) {
// Lookup counterparty associated with our source channel to retrieve the destination channel
counterparty, ok := k.GetCounterparty(ctx, sourceID)
counterparty, ok := k.GetCounterpartyV2(ctx, "transfer", "channel-2", sourceID)
if !ok {
return 0, errorsmod.Wrap(types.ErrCounterpartyNotFound, sourceID)
}
destChannel := counterparty.ClientId
// retrieve the sequence send for this channel
// if no packets have been sent yet, initialize the sequence to 1.
sequence, found := k.ChannelKeeper.GetNextSequenceSend(ctx, host.SentinelV2PortID, sourceID)
sequence, found := k.ChannelKeeper.GetNextSequenceSend(ctx, host.SentinelV2PortID, counterparty.ClientId)
if !found {
sequence = 1
}

// construct packet from given fields and channel state
packet := channeltypesv2.NewPacketV2(sequence, sourceID, destChannel, timeoutTimestamp, data...)
packet := channeltypesv2.NewPacketV2(sequence, sourceID, counterparty.ClientId, timeoutTimestamp, data...)

if err := packet.ValidateBasic(); err != nil {
return 0, errorsmod.Wrapf(channeltypes.ErrInvalidPacket, "constructed packet failed basic validation: %v", err)
}

// check that the client of counterparty chain is still active
if status := k.ClientKeeper.GetClientStatus(ctx, sourceID); status != exported.Active {
return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", sourceID, status)
if status := k.ClientKeeper.GetClientStatus(ctx, counterparty.ClientId); status != exported.Active {
return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", counterparty.ClientId, status)
}

// retrieve latest height and timestamp of the client of counterparty chain
latestHeight := k.ClientKeeper.GetClientLatestHeight(ctx, sourceID)
latestHeight := k.ClientKeeper.GetClientLatestHeight(ctx, counterparty.ClientId)
if latestHeight.IsZero() {
return 0, errorsmod.Wrapf(clienttypes.ErrInvalidHeight, "cannot send packet using client (%s) with zero height", sourceID)
return 0, errorsmod.Wrapf(clienttypes.ErrInvalidHeight, "cannot send packet using client (%s) with zero height", counterparty.ClientId)
}
latestTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceID, latestHeight)
latestTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, counterparty.ClientId, latestHeight)
if err != nil {
return 0, err
}
Expand All @@ -67,8 +66,8 @@ func (k Keeper) SendPacketV2(
commitment := channeltypes.CommitPacketV2(packet)

// bump the sequence and set the packet commitment so it is provable by the counterparty
k.ChannelKeeper.SetNextSequenceSend(ctx, host.SentinelV2PortID, sourceID, sequence+1)
k.ChannelKeeper.SetPacketCommitment(ctx, host.SentinelV2PortID, sourceID, packet.GetSequence(), commitment)
k.ChannelKeeper.SetNextSequenceSend(ctx, host.SentinelV2PortID, counterparty.ClientId, sequence+1)
k.ChannelKeeper.SetPacketCommitment(ctx, host.SentinelV2PortID, counterparty.ClientId, packet.GetSequence(), commitment)
// k.Logger(ctx).Info("packet sent", "sequence", strconv.FormatUint(packet.Sequence, 10), "src_port", packetV2SentinelPort, "src_channel", packet.SourceChannel, "dst_port", packet.DestinationPort, "dst_channel", packet.DestinationChannel)

// channelkeeper.EmitSendPacketEventV2(ctx, packet, sentinelChannel(sourceID), timeoutHeight)
Expand Down
3 changes: 2 additions & 1 deletion modules/core/packet-server/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"context"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
Expand Down Expand Up @@ -44,6 +43,8 @@ type ChannelKeeper interface {
GetMultiAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64) (types.MultiAcknowledgement, bool)
// SetMultiAcknowledgement writes the multi ack under the multi ack path.
SetMultiAcknowledgement(ctx context.Context, portID, channelID string, sequence uint64, recvResults types.MultiAcknowledgement)

GetV2Counterparty(ctx context.Context, portID, channelID string) (Counterparty, bool)
}

type ClientKeeper interface {
Expand Down

0 comments on commit 444c0fc

Please sign in to comment.