Skip to content

Commit

Permalink
Refactor packet data unmarshalling to use specific version (#6354)
Browse files Browse the repository at this point in the history
* chore: specifically unmarshal v1 or v2 without brute force

* chore: fix TestPacketDataUnmarshalerInterface test in transfer module

* Pass dest values OnRecv, refactor GetExpectedEvents

* chore: fixing TestGetCallbackData test

* chore: fixed remaining tests in callbacks module

* test: simplify callbacks test, revert back to previous behaviour

* chore: fix test case name

* chore: addressing PR feedback

* chore: added docstring for unmarshalPacketDataBytesToICS20V2

---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Colin Axnér <25233464+colin-axner@users.noreply.github.com>
  • Loading branch information
3 people authored May 23, 2024
1 parent 8eae033 commit dbcff45
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 96 deletions.
4 changes: 2 additions & 2 deletions modules/apps/callbacks/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ func GetExpectedEvent(
gasMeter := storetypes.NewGasMeter(remainingGas)
ctx = ctx.WithGasMeter(gasMeter)

// Mock packet.
packet := channeltypes.NewPacket(data, 0, srcPortID, "", "", "", clienttypes.ZeroHeight(), 0)
if callbackType == types.CallbackTypeReceivePacket {
packet := channeltypes.NewPacket(data, seq, "", "", eventPortID, eventChannelID, clienttypes.ZeroHeight(), 0)
callbackData, err = types.GetDestCallbackData(ctx, packetDataUnmarshaler, packet, maxCallbackGas)
} else {
packet := channeltypes.NewPacket(data, seq, eventPortID, eventChannelID, "", "", clienttypes.ZeroHeight(), 0)
callbackData, err = types.GetSourceCallbackData(ctx, packetDataUnmarshaler, packet, maxCallbackGas)
}
if err != nil {
Expand Down
43 changes: 38 additions & 5 deletions modules/apps/callbacks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,13 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
}
}

func (s *CallbacksTestSuite) TestUnmarshalPacketData() {
func (s *CallbacksTestSuite) TestUnmarshalPacketDataV1() {
s.setupChains()
s.path.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort
s.path.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort
s.path.EndpointA.ChannelConfig.Version = transfertypes.V1
s.path.EndpointB.ChannelConfig.Version = transfertypes.V1
s.path.Setup()

// We will pass the function call down the transfer stack to the transfer module
// transfer stack UnmarshalPacketData call order: callbacks -> fee -> transfer
Expand Down Expand Up @@ -1006,15 +1011,43 @@ func (s *CallbacksTestSuite) TestUnmarshalPacketData() {
portID := s.path.EndpointA.ChannelConfig.PortID
channelID := s.path.EndpointA.ChannelID

// Unmarshal ICS20 v1 packet data
// Unmarshal ICS20 v1 packet data into v2 packet data
data := expPacketDataICS20V1.GetBytes()
packetData, err := unmarshalerStack.UnmarshalPacketData(s.chainA.GetContext(), portID, channelID, data)
s.Require().NoError(err)
s.Require().Equal(expPacketDataICS20V2, packetData)
}

func (s *CallbacksTestSuite) TestUnmarshalPacketDataV2() {
s.SetupTransferTest()

// We will pass the function call down the transfer stack to the transfer module
// transfer stack UnmarshalPacketData call order: callbacks -> fee -> transfer
transferStack, ok := s.chainA.App.GetIBCKeeper().PortKeeper.Route(transfertypes.ModuleName)
s.Require().True(ok)

// Unmarshal ICS20 v1 packet data
data = expPacketDataICS20V2.GetBytes()
packetData, err = unmarshalerStack.UnmarshalPacketData(s.chainA.GetContext(), portID, channelID, data)
unmarshalerStack, ok := transferStack.(types.CallbacksCompatibleModule)
s.Require().True(ok)

expPacketDataICS20V2 := transfertypes.FungibleTokenPacketDataV2{
Tokens: []transfertypes.Token{
{
Denom: ibctesting.TestCoin.Denom,
Amount: ibctesting.TestCoin.Amount.String(),
Trace: nil,
},
},
Sender: ibctesting.TestAccAddress,
Receiver: ibctesting.TestAccAddress,
Memo: fmt.Sprintf(`{"src_callback": {"address": "%s"}, "dest_callback": {"address":"%s"}}`, ibctesting.TestAccAddress, ibctesting.TestAccAddress),
}

portID := s.path.EndpointA.ChannelConfig.PortID
channelID := s.path.EndpointA.ChannelID

// Unmarshal ICS20 v2 packet data
data := expPacketDataICS20V2.GetBytes()
packetData, err := unmarshalerStack.UnmarshalPacketData(s.chainA.GetContext(), portID, channelID, data)
s.Require().NoError(err)
s.Require().Equal(expPacketDataICS20V2, packetData)
}
Expand Down
Loading

0 comments on commit dbcff45

Please sign in to comment.