Skip to content

Commit 96ca59b

Browse files
mergify[bot]colin-axner
andauthoredFeb 15, 2022
chore: add ParsePacketFromEvents testing helper function (#904) (#919)
## Description ref: #891 --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md). - [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [ ] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`) - [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [ ] Re-reviewed `Files changed` in the Github PR explorer - [ ] Review `Codecov Report` in the comment section below once CI passes (cherry picked from commit c27d5b5) Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6161

6262
### Improvements
6363

64+
* (testing) [\#904](https://github.com/cosmos/ibc-go/pull/904) Add `ParsePacketFromEvents` function to the testing package. Useful when sending/relaying packets via the testing package.
6465
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.
6566
* (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknowledgement via MsgRecvPacket events.
6667
* (connection) [\#721](https://github.com/cosmos/ibc-go/pull/721) Simplify connection handshake error messages when unpacking client state.

‎modules/apps/transfer/keeper/relay_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
186186
// send coin from chainB to chainA, receive them, acknowledge them, and send back to chainB
187187
coinFromBToA := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))
188188
transferMsg := types.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, coinFromBToA, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0)
189-
_, err := suite.chainB.SendMsgs(transferMsg)
189+
res, err := suite.chainB.SendMsgs(transferMsg)
190190
suite.Require().NoError(err) // message committed
191191

192-
// relay send packet
193-
fungibleTokenPacket := types.NewFungibleTokenPacketData(coinFromBToA.Denom, coinFromBToA.Amount.String(), suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
194-
packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, clienttypes.NewHeight(0, 110), 0)
192+
packet, err := ibctesting.ParsePacketFromEvents(res.GetEvents())
193+
suite.Require().NoError(err)
194+
195195
err = path.RelayPacket(packet)
196196
suite.Require().NoError(err) // relay committed
197197

‎modules/apps/transfer/transfer_test.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
1010
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
11-
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
1211
ibctesting "github.com/cosmos/ibc-go/v3/testing"
1312
)
1413

@@ -56,13 +55,13 @@ func (suite *TransferTestSuite) TestHandleMsgTransfer() {
5655

5756
// send from chainA to chainB
5857
msg := types.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coinToSendToB, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0)
59-
60-
_, err := suite.chainA.SendMsgs(msg)
58+
res, err := suite.chainA.SendMsgs(msg)
6159
suite.Require().NoError(err) // message committed
6260

61+
packet, err := ibctesting.ParsePacketFromEvents(res.GetEvents())
62+
suite.Require().NoError(err)
63+
6364
// relay send
64-
fungibleTokenPacket := types.NewFungibleTokenPacketData(coinToSendToB.Denom, coinToSendToB.Amount.String(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String())
65-
packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0)
6665
err = path.RelayPacket(packet)
6766
suite.Require().NoError(err) // relay committed
6867

@@ -82,18 +81,18 @@ func (suite *TransferTestSuite) TestHandleMsgTransfer() {
8281

8382
// send from chainB to chainC
8483
msg = types.NewMsgTransfer(pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, coinSentFromAToB, suite.chainB.SenderAccount.GetAddress().String(), suite.chainC.SenderAccount.GetAddress().String(), timeoutHeight, 0)
85-
86-
_, err = suite.chainB.SendMsgs(msg)
84+
res, err = suite.chainB.SendMsgs(msg)
8785
suite.Require().NoError(err) // message committed
8886

89-
// relay send
90-
// NOTE: fungible token is prefixed with the full trace in order to verify the packet commitment
91-
fullDenomPath := types.GetPrefixedDenom(pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, voucherDenomTrace.GetFullDenomPath())
92-
fungibleTokenPacket = types.NewFungibleTokenPacketData(voucherDenomTrace.GetFullDenomPath(), coinSentFromAToB.Amount.String(), suite.chainB.SenderAccount.GetAddress().String(), suite.chainC.SenderAccount.GetAddress().String())
93-
packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, timeoutHeight, 0)
87+
packet, err = ibctesting.ParsePacketFromEvents(res.GetEvents())
88+
suite.Require().NoError(err)
89+
9490
err = pathBtoC.RelayPacket(packet)
9591
suite.Require().NoError(err) // relay committed
9692

93+
// NOTE: fungible token is prefixed with the full trace in order to verify the packet commitment
94+
fullDenomPath := types.GetPrefixedDenom(pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, voucherDenomTrace.GetFullDenomPath())
95+
9796
coinSentFromBToC := sdk.NewCoin(types.ParseDenomTrace(fullDenomPath).IBCDenom(), amount)
9897
balance = suite.chainC.GetSimApp().BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), coinSentFromBToC.Denom)
9998

@@ -106,14 +105,12 @@ func (suite *TransferTestSuite) TestHandleMsgTransfer() {
106105

107106
// send from chainC back to chainB
108107
msg = types.NewMsgTransfer(pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, coinSentFromBToC, suite.chainC.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0)
109-
110-
_, err = suite.chainC.SendMsgs(msg)
108+
res, err = suite.chainC.SendMsgs(msg)
111109
suite.Require().NoError(err) // message committed
112110

113-
// relay send
114-
// NOTE: fungible token is prefixed with the full trace in order to verify the packet commitment
115-
fungibleTokenPacket = types.NewFungibleTokenPacketData(fullDenomPath, coinSentFromBToC.Amount.String(), suite.chainC.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String())
116-
packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, timeoutHeight, 0)
111+
packet, err = ibctesting.ParsePacketFromEvents(res.GetEvents())
112+
suite.Require().NoError(err)
113+
117114
err = pathBtoC.RelayPacket(packet)
118115
suite.Require().NoError(err) // relay committed
119116

‎testing/events.go

+60
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ibctesting
22

33
import (
44
"fmt"
5+
"strconv"
56

67
sdk "github.com/cosmos/cosmos-sdk/types"
78

@@ -56,6 +57,65 @@ func ParseChannelIDFromEvents(events sdk.Events) (string, error) {
5657
return "", fmt.Errorf("channel identifier event attribute not found")
5758
}
5859

60+
// ParsePacketFromEvents parses events emitted from a MsgRecvPacket and returns the
61+
// acknowledgement.
62+
func ParsePacketFromEvents(events sdk.Events) (channeltypes.Packet, error) {
63+
for _, ev := range events {
64+
if ev.Type == channeltypes.EventTypeSendPacket {
65+
packet := channeltypes.Packet{}
66+
for _, attr := range ev.Attributes {
67+
68+
switch string(attr.Key) {
69+
case channeltypes.AttributeKeyData:
70+
packet.Data = attr.Value
71+
72+
case channeltypes.AttributeKeySequence:
73+
seq, err := strconv.ParseUint(string(attr.Value), 10, 64)
74+
if err != nil {
75+
return channeltypes.Packet{}, err
76+
}
77+
78+
packet.Sequence = seq
79+
80+
case channeltypes.AttributeKeySrcPort:
81+
packet.SourcePort = string(attr.Value)
82+
83+
case channeltypes.AttributeKeySrcChannel:
84+
packet.SourceChannel = string(attr.Value)
85+
86+
case channeltypes.AttributeKeyDstPort:
87+
packet.DestinationPort = string(attr.Value)
88+
89+
case channeltypes.AttributeKeyDstChannel:
90+
packet.DestinationChannel = string(attr.Value)
91+
92+
case channeltypes.AttributeKeyTimeoutHeight:
93+
height, err := clienttypes.ParseHeight(string(attr.Value))
94+
if err != nil {
95+
return channeltypes.Packet{}, err
96+
}
97+
98+
packet.TimeoutHeight = height
99+
100+
case channeltypes.AttributeKeyTimeoutTimestamp:
101+
timestamp, err := strconv.ParseUint(string(attr.Value), 10, 64)
102+
if err != nil {
103+
return channeltypes.Packet{}, err
104+
}
105+
106+
packet.TimeoutTimestamp = timestamp
107+
108+
default:
109+
continue
110+
}
111+
}
112+
113+
return packet, nil
114+
}
115+
}
116+
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found")
117+
}
118+
59119
// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the
60120
// acknowledgement.
61121
func ParseAckFromEvents(events sdk.Events) ([]byte, error) {

0 commit comments

Comments
 (0)
Please sign in to comment.