Skip to content

Commit

Permalink
test: added packet data forwards compatibility test (#6089)
Browse files Browse the repository at this point in the history
* test: added packet data backwards compatibility test

* imp: fixed test case

* imp: use expError

---------

Co-authored-by: Carlos Rodriguez <[email protected]>
Co-authored-by: Colin Axnér <[email protected]>
  • Loading branch information
3 people authored May 22, 2024
1 parent 23f6751 commit af57c5d
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions modules/apps/transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
)

Expand Down Expand Up @@ -913,3 +914,88 @@ func (suite *KeeperTestSuite) TestOnTimeoutPacketSetsTotalEscrowAmountForSourceI
totalEscrowChainB = suite.chainB.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(suite.chainB.GetContext(), coin.GetDenom())
suite.Require().Equal(sdkmath.ZeroInt(), totalEscrowChainB.Amount)
}

func (suite *KeeperTestSuite) TestPacketForwardsCompatibility() {
// We are testing a scenario where a packet in the future has a new populated
// field called "new_field". And this packet is being sent to this module which
// doesn't have this field in the packet data. The module should be able to handle
// this packet without any issues.

var packetData []byte

testCases := []struct {
msg string
malleate func()
expError error
}{
{
"success: new field",
func() {
jsonString := fmt.Sprintf(`{"denom":"denom","amount":"100","sender":"%s","receiver":"%s","memo":"memo","new_field":"value"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
nil,
},
{
"success: no new field with memo",
func() {
jsonString := fmt.Sprintf(`{"denom":"denom","amount":"100","sender":"%s","receiver":"%s","memo":"memo"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
nil,
},
{
"success: no new field without memo",
func() {
jsonString := fmt.Sprintf(`{"denom":"denom","amount":"100","sender":"%s","receiver":"%s"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
nil,
},
{
"failure: invalid packet data",
func() {
packetData = []byte("invalid packet data")
},
ibcerrors.ErrUnknownRequest,
},
{
"failure: missing field",
func() {
jsonString := fmt.Sprintf(`{"amount":"100","sender":%s","receiver":"%s"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
ibcerrors.ErrUnknownRequest,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.msg, func() {
suite.SetupTest() // reset
packetData = nil

path := ibctesting.NewTransferPath(suite.chainA, suite.chainB)
path.Setup()

tc.malleate()

timeoutHeight := suite.chainB.GetTimeoutHeight()

seq, err := path.EndpointB.SendPacket(timeoutHeight, 0, packetData)
suite.Require().NoError(err)

packet := channeltypes.NewPacket(packetData, seq, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, timeoutHeight, 0)

// receive packet on chainA
err = path.RelayPacket(packet)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
} else {
suite.Require().ErrorContains(err, tc.expError.Error())
}
})
}
}

0 comments on commit af57c5d

Please sign in to comment.