Skip to content

Commit 82397d6

Browse files
Added optional packet metadata to the packet and message types (cosmos#2305)
* added optional packet metadata to the packet and message types * added docs * breaking the api (backports should add a utility function for this) * adding nil metadata on all the calls * added metadata to the cli * added events * breaking api for FungibleTokenPacketData * hex encoding metadata * added abstraction * fixed bad merge * added tests with metadata * added missing metadata to packet for recv * cleaning up metadata on every test * reset metadata * added metadata flag * lint * Update modules/apps/transfer/client/cli/tx.go Co-authored-by: Damian Nolan <[email protected]> * fixed bad call in tests Co-authored-by: Damian Nolan <[email protected]>
1 parent be46857 commit 82397d6

File tree

24 files changed

+264
-101
lines changed

24 files changed

+264
-101
lines changed

docs/apps/transfer/events.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ order: 5
2323
| fungible_token_packet | denom | {denom} |
2424
| fungible_token_packet | amount | {amount} |
2525
| fungible_token_packet | success | {ackSuccess} |
26+
| fungible_token_packet | metadata | {metadata} |
2627
| denomination_trace | trace_hash | {hex_hash} |
2728

2829
## `OnAcknowledgePacket` callback
@@ -34,6 +35,7 @@ order: 5
3435
| fungible_token_packet | receiver | {receiver} |
3536
| fungible_token_packet | denom | {denom} |
3637
| fungible_token_packet | amount | {amount} |
38+
| fungible_token_packet | metadata | {metadata} |
3739
| fungible_token_packet | acknowledgement | {ack.String()} |
3840
| fungible_token_packet | success | error | {ack.Response} |
3941

@@ -45,3 +47,4 @@ order: 5
4547
| fungible_token_packet | refund_receiver | {receiver} |
4648
| fungible_token_packet | denom | {denom} |
4749
| fungible_token_packet | amount | {amount} |
50+
| fungible_token_packet | metadata | {metadata} |

docs/apps/transfer/messages.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type MsgTransfer struct {
1717
Receiver string
1818
TimeoutHeight ibcexported.Height
1919
TimeoutTimestamp uint64
20+
Metadata []byte
2021
}
2122
```
2223

docs/ibc/proto-docs.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,7 @@ https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transf
22552255
| `receiver` | [string](#string) | | the recipient address on the destination chain |
22562256
| `timeout_height` | [ibc.core.client.v1.Height](#ibc.core.client.v1.Height) | | Timeout height relative to the current block height. The timeout is disabled when set to 0. |
22572257
| `timeout_timestamp` | [uint64](#uint64) | | Timeout timestamp in absolute nanoseconds since unix epoch. The timeout is disabled when set to 0. |
2258+
| `metadata` | [bytes](#bytes) | | optional metadata |
22582259

22592260

22602261

@@ -2269,7 +2270,7 @@ MsgTransferResponse defines the Msg/Transfer response type.
22692270

22702271
| Field | Type | Label | Description |
22712272
| ----- | ---- | ----- | ----------- |
2272-
| `sequence` | [uint64](#uint64) | | sequence number of the packet on the channel. |
2273+
| `sequence` | [uint64](#uint64) | | sequence number of the transfer packet sent |
22732274

22742275

22752276

@@ -2316,6 +2317,7 @@ https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transf
23162317
| `amount` | [string](#string) | | the token amount to be transferred |
23172318
| `sender` | [string](#string) | | the sender address |
23182319
| `receiver` | [string](#string) | | the recipient address on the destination chain |
2320+
| `metadata` | [bytes](#bytes) | | optional metadata |
23192321

23202322

23212323

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ require (
4343
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
4444
github.com/bgentry/speakeasy v0.1.0 // indirect
4545
github.com/btcsuite/btcd v0.22.1 // indirect
46-
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
46+
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
4747
github.com/cespare/xxhash v1.1.0 // indirect
4848
github.com/cespare/xxhash/v2 v2.1.2 // indirect
4949
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOC
180180
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
181181
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
182182
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
183+
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
183184
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
184-
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
185-
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
186185
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
187186
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
188187
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=

modules/apps/29-fee/transfer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (suite *FeeTestSuite) TestFeeTransfer() {
3030

3131
msgs := []sdk.Msg{
3232
types.NewMsgPayPacketFee(fee, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, suite.chainA.SenderAccount.GetAddress().String(), nil),
33-
transfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coin, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(1, 100), 0),
33+
transfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coin, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(1, 100), 0, nil),
3434
}
3535
res, err := suite.chainA.SendMsgs(msgs...)
3636
suite.Require().NoError(err) // message committed

modules/apps/transfer/client/cli/tx.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
flagPacketTimeoutHeight = "packet-timeout-height"
2323
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
2424
flagAbsoluteTimeouts = "absolute-timeouts"
25+
flagMetadata = "metadata"
2526
)
2627

2728
// NewTransferTxCmd returns the command to create a NewMsgTransfer transaction
@@ -76,6 +77,11 @@ corresponding to the counterparty channel. Any timeout set to 0 is disabled.`),
7677
return err
7778
}
7879

80+
metadataStr, err := cmd.Flags().GetString(flagMetadata)
81+
if err != nil {
82+
return err
83+
}
84+
7985
// if the timeouts are not absolute, retrieve latest block height and block timestamp
8086
// for the consensus state connected to the destination port/channel
8187
if !absoluteTimeouts {
@@ -111,7 +117,7 @@ corresponding to the counterparty channel. Any timeout set to 0 is disabled.`),
111117
}
112118

113119
msg := types.NewMsgTransfer(
114-
srcPort, srcChannel, coin, sender, receiver, timeoutHeight, timeoutTimestamp,
120+
srcPort, srcChannel, coin, sender, receiver, timeoutHeight, timeoutTimestamp, []byte(metadataStr),
115121
)
116122
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
117123
},
@@ -120,6 +126,7 @@ corresponding to the counterparty channel. Any timeout set to 0 is disabled.`),
120126
cmd.Flags().String(flagPacketTimeoutHeight, types.DefaultRelativePacketTimeoutHeight, "Packet timeout block height. The timeout is disabled when set to 0-0.")
121127
cmd.Flags().Uint64(flagPacketTimeoutTimestamp, types.DefaultRelativePacketTimeoutTimestamp, "Packet timeout timestamp in nanoseconds from now. Default is 10 minutes. The timeout is disabled when set to 0.")
122128
cmd.Flags().Bool(flagAbsoluteTimeouts, false, "Timeout flags are used as absolute timeouts.")
129+
cmd.Flags().String(flagMetadata, "", "Metadata to be sent along with the packet. The CLI accepts only strings here but you can construct a packet with arbitrary bytes via code.")
123130
flags.AddTxFlagsToCmd(cmd)
124131

125132
return cmd

modules/apps/transfer/ibc_module.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transfer
22

33
import (
4+
"encoding/hex"
45
"fmt"
56
"math"
67
"strings"
@@ -194,6 +195,7 @@ func (im IBCModule) OnRecvPacket(
194195
sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver),
195196
sdk.NewAttribute(types.AttributeKeyDenom, data.Denom),
196197
sdk.NewAttribute(types.AttributeKeyAmount, data.Amount),
198+
sdk.NewAttribute(types.AttributeKeyMetadata, hex.EncodeToString(data.Metadata)),
197199
sdk.NewAttribute(types.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
198200
}
199201

@@ -240,6 +242,7 @@ func (im IBCModule) OnAcknowledgementPacket(
240242
sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver),
241243
sdk.NewAttribute(types.AttributeKeyDenom, data.Denom),
242244
sdk.NewAttribute(types.AttributeKeyAmount, data.Amount),
245+
sdk.NewAttribute(types.AttributeKeyMetadata, hex.EncodeToString(data.Metadata)),
243246
sdk.NewAttribute(types.AttributeKeyAck, ack.String()),
244247
),
245248
)
@@ -286,6 +289,7 @@ func (im IBCModule) OnTimeoutPacket(
286289
sdk.NewAttribute(types.AttributeKeyRefundReceiver, data.Sender),
287290
sdk.NewAttribute(types.AttributeKeyRefundDenom, data.Denom),
288291
sdk.NewAttribute(types.AttributeKeyRefundAmount, data.Amount),
292+
sdk.NewAttribute(types.AttributeKeyMetadata, hex.EncodeToString(data.Metadata)),
289293
),
290294
)
291295

modules/apps/transfer/keeper/mbt_relay_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ func FungibleTokenPacketFromTla(packet TlaFungibleTokenPacket) FungibleTokenPack
146146
DenomFromTla(packet.Data.Denom),
147147
packet.Data.Amount,
148148
AddressFromString(packet.Data.Sender),
149-
AddressFromString(packet.Data.Receiver)),
149+
AddressFromString(packet.Data.Receiver),
150+
nil),
150151
}
151152
}
152153

@@ -346,7 +347,8 @@ func (suite *KeeperTestSuite) TestModelBasedRelay() {
346347
sender,
347348
tc.packet.Data.Receiver,
348349
clienttypes.NewHeight(1, 110),
349-
0)
350+
0,
351+
nil)
350352
}
351353
case "OnRecvPacket":
352354
err = suite.chainB.GetSimApp().TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, tc.packet.Data)

modules/apps/transfer/keeper/msg_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.
2121

2222
sequence, err := k.sendTransfer(
2323
ctx, msg.SourcePort, msg.SourceChannel, msg.Token, sender, msg.Receiver, msg.TimeoutHeight, msg.TimeoutTimestamp,
24-
)
24+
msg.Metadata)
2525
if err != nil {
2626
return nil, err
2727
}

0 commit comments

Comments
 (0)