Skip to content

Commit f3d4634

Browse files
nicolaslaramergify[bot]
authored andcommitted
Added optional packet metadata to the packet and message types (#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 <damiannolan@gmail.com> * fixed bad call in tests Co-authored-by: Damian Nolan <damiannolan@gmail.com> (cherry picked from commit 82397d6) # Conflicts: # go.mod # go.sum # modules/apps/29-fee/transfer_test.go # modules/apps/transfer/keeper/mbt_relay_test.go # modules/apps/transfer/keeper/relay_test.go # modules/apps/transfer/types/packet.pb.go # modules/apps/transfer/types/tx.pb.go # proto/ibc/applications/interchain_accounts/controller/v1/tx.proto
1 parent c65a546 commit f3d4634

File tree

24 files changed

+356
-38
lines changed

24 files changed

+356
-38
lines changed

docs/apps/transfer/events.md

+3
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

+1
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

+2
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transf
21462146
| `receiver` | [string](#string) | | the recipient address on the destination chain |
21472147
| `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. |
21482148
| `timeout_timestamp` | [uint64](#uint64) | | Timeout timestamp in absolute nanoseconds since unix epoch. The timeout is disabled when set to 0. |
2149+
| `metadata` | [bytes](#bytes) | | optional metadata |
21492150

21502151

21512152

@@ -2207,6 +2208,7 @@ https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transf
22072208
| `amount` | [string](#string) | | the token amount to be transferred |
22082209
| `sender` | [string](#string) | | the sender address |
22092210
| `receiver` | [string](#string) | | the recipient address on the destination chain |
2211+
| `metadata` | [bytes](#bytes) | | optional metadata |
22102212

22112213

22122214

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ require (
3535
github.com/beorn7/perks v1.0.1 // indirect
3636
github.com/bgentry/speakeasy v0.1.0 // indirect
3737
github.com/btcsuite/btcd v0.22.1 // indirect
38+
<<<<<<< HEAD
39+
=======
40+
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
41+
>>>>>>> 82397d6 (Added optional packet metadata to the packet and message types (#2305))
3842
github.com/cespare/xxhash v1.1.0 // indirect
3943
github.com/cespare/xxhash/v2 v2.1.2 // indirect
4044
github.com/coinbase/rosetta-sdk-go v0.7.0 // indirect

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46f
130130
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
131131
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
132132
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
133+
<<<<<<< HEAD
134+
=======
135+
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
136+
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
137+
>>>>>>> 82397d6 (Added optional packet metadata to the packet and message types (#2305))
133138
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
134139
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
135140
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=

modules/apps/29-fee/transfer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ 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+
<<<<<<< HEAD
3334
transfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coin, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 100), 0),
35+
=======
36+
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),
37+
>>>>>>> 82397d6 (Added optional packet metadata to the packet and message types (#2305))
3438
}
3539
res, err := suite.chainA.SendMsgs(msgs...)
3640
suite.Require().NoError(err) // message committed

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

+8-1
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

+4
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ func FungibleTokenPacketFromTla(packet TlaFungibleTokenPacket) FungibleTokenPack
145145
DenomFromTla(packet.Data.Denom),
146146
packet.Data.Amount,
147147
AddressFromString(packet.Data.Sender),
148-
AddressFromString(packet.Data.Receiver)),
148+
AddressFromString(packet.Data.Receiver),
149+
nil),
149150
}
150151
}
151152

@@ -344,8 +345,14 @@ func (suite *KeeperTestSuite) TestModelBasedRelay() {
344345
sdk.NewCoin(denom, amount),
345346
sender,
346347
tc.packet.Data.Receiver,
348+
<<<<<<< HEAD
347349
clienttypes.NewHeight(0, 110),
348350
0)
351+
=======
352+
clienttypes.NewHeight(1, 110),
353+
0,
354+
nil)
355+
>>>>>>> 82397d6 (Added optional packet metadata to the packet and message types (#2305))
349356
}
350357
case "OnRecvPacket":
351358
err = suite.chainB.GetSimApp().TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, tc.packet.Data)

modules/apps/transfer/keeper/msg_server.go

+1-1
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
}

modules/apps/transfer/keeper/msg_server_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
5454
path.EndpointA.ChannelID,
5555
coin, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(),
5656
suite.chainB.GetTimeoutHeight(), 0, // only use timeout height
57+
[]byte("custom metadata"),
5758
)
5859

5960
tc.malleate()

modules/apps/transfer/keeper/relay.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (k Keeper) SendTransfer(
5959
receiver string,
6060
timeoutHeight clienttypes.Height,
6161
timeoutTimestamp uint64,
62+
metadata []byte,
6263
) error {
6364
_, err := k.sendTransfer(
6465
ctx,
@@ -69,6 +70,7 @@ func (k Keeper) SendTransfer(
6970
receiver,
7071
timeoutHeight,
7172
timeoutTimestamp,
73+
metadata,
7274
)
7375
return err
7476
}
@@ -83,6 +85,7 @@ func (k Keeper) sendTransfer(
8385
receiver string,
8486
timeoutHeight clienttypes.Height,
8587
timeoutTimestamp uint64,
88+
metadata []byte,
8689
) (uint64, error) {
8790
if !k.GetSendEnabled(ctx) {
8891
return 0, types.ErrSendDisabled
@@ -173,7 +176,7 @@ func (k Keeper) sendTransfer(
173176
}
174177

175178
packetData := types.NewFungibleTokenPacketData(
176-
fullDenomPath, token.Amount.String(), sender.String(), receiver,
179+
fullDenomPath, token.Amount.String(), sender.String(), receiver, metadata,
177180
)
178181

179182
packet := channeltypes.NewPacket(

0 commit comments

Comments
 (0)