-
Notifications
You must be signed in to change notification settings - Fork 690
feat: Unit tests for Packet Forward Middleware #8313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DeshErBojhaa
wants to merge
4
commits into
tamjid/pfm-homecoming
Choose a base branch
from
tamjid/pfm-homecoming-unit
base: tamjid/pfm-homecoming
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
modules/apps/packet-forward-middleware/ibc_middleware_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
package packetforward_test | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
packetforward "github.com/cosmos/ibc-go/v10/modules/apps/packet-forward-middleware" | ||
packetforwardkeeper "github.com/cosmos/ibc-go/v10/modules/apps/packet-forward-middleware/keeper" | ||
packetforwardtypes "github.com/cosmos/ibc-go/v10/modules/apps/packet-forward-middleware/types" | ||
transfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types" | ||
channeltypes "github.com/cosmos/ibc-go/v10/modules/core/04-channel/types" | ||
ibctesting "github.com/cosmos/ibc-go/v10/testing" | ||
) | ||
|
||
// CallbacksTestSuite defines the needed instances and methods to test callbacks | ||
type PFMTestSuite struct { | ||
suite.Suite | ||
|
||
coordinator *ibctesting.Coordinator | ||
|
||
chainA *ibctesting.TestChain | ||
chainB *ibctesting.TestChain | ||
chainC *ibctesting.TestChain | ||
|
||
pathAB *ibctesting.Path | ||
pathBC *ibctesting.Path | ||
} | ||
|
||
func TestPFMTestSuite(t *testing.T) { | ||
suite.Run(t, new(PFMTestSuite)) | ||
} | ||
|
||
// setupChains sets up a coordinator with 2 test chains. | ||
func (s *PFMTestSuite) setupChains() { | ||
s.coordinator = ibctesting.NewCoordinator(s.T(), 3) | ||
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(1)) | ||
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(2)) | ||
s.chainC = s.coordinator.GetChain(ibctesting.GetChainID(3)) | ||
|
||
s.pathAB = ibctesting.NewTransferPath(s.chainA, s.chainB) | ||
s.pathAB.Setup() | ||
|
||
s.pathBC = ibctesting.NewTransferPath(s.chainB, s.chainC) | ||
s.pathBC.Setup() | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket() { | ||
s.setupChains() | ||
|
||
ctx := s.chainA.GetContext() | ||
version := s.pathAB.EndpointA.GetChannel().Version | ||
relayerAddr := s.chainA.SenderAccount.GetAddress() | ||
|
||
// PacketForwardMiddleware | ||
pfm := s.pktForwardMiddleware(s.chainA, transfertypes.ModuleName) | ||
ack := pfm.OnRecvPacket(ctx, version, channeltypes.Packet{}, relayerAddr) | ||
s.Require().False(ack.Success()) | ||
|
||
expectedAck := &channeltypes.Acknowledgement{} | ||
err := s.chainA.Codec.UnmarshalJSON(ack.Acknowledgement(), expectedAck) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Equal("ABCI code: 12: error handling packet: see events for details", expectedAck.GetError()) | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket_Nomemo() { | ||
s.setupChains() | ||
|
||
ctx := s.chainA.GetContext() | ||
version := s.pathAB.EndpointA.GetChannel().Version | ||
relayerAddr := s.chainA.SenderAccount.GetAddress() | ||
receiverAddr := s.chainB.SenderAccount.GetAddress() | ||
|
||
packet := s.transferPacket(relayerAddr.String(), receiverAddr.String(), s.pathAB, 0, "{}") | ||
|
||
// PacketForwardMiddleware | ||
pfm := s.pktForwardMiddleware(s.chainA, transfertypes.ModuleName) | ||
ack := pfm.OnRecvPacket(ctx, version, packet, relayerAddr) | ||
s.Require().True(ack.Success()) | ||
|
||
expectedAck := &channeltypes.Acknowledgement{} | ||
err := s.chainA.Codec.UnmarshalJSON(ack.Acknowledgement(), expectedAck) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Equal("", expectedAck.GetError()) | ||
s.Require().ElementsMatch([]byte{1}, expectedAck.GetResult()) | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket_InvalidReceiver() { | ||
s.setupChains() | ||
|
||
ctx := s.chainA.GetContext() | ||
version := s.pathAB.EndpointA.GetChannel().Version | ||
relayerAddr := s.chainA.SenderAccount.GetAddress() | ||
|
||
packet := s.transferPacket(relayerAddr.String(), "", s.pathAB, 0, nil) | ||
|
||
// PacketForwardMiddleware | ||
pfm := s.pktForwardMiddleware(s.chainA, transfertypes.ModuleName) | ||
ack := pfm.OnRecvPacket(ctx, version, packet, relayerAddr) | ||
s.Require().False(ack.Success()) | ||
|
||
expectedAck := &channeltypes.Acknowledgement{} | ||
err := s.chainA.Codec.UnmarshalJSON(ack.Acknowledgement(), expectedAck) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Equal("ABCI code: 5: error handling packet: see events for details", expectedAck.GetError()) | ||
s.Require().Empty(expectedAck.GetResult()) | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket_NoForward() { | ||
s.setupChains() | ||
|
||
ctx := s.chainA.GetContext() | ||
version := s.pathAB.EndpointA.GetChannel().Version | ||
|
||
senderAddr := s.chainA.SenderAccount.GetAddress() | ||
receiverAddr := s.chainB.SenderAccount.GetAddress() | ||
|
||
packet := s.transferPacket(senderAddr.String(), receiverAddr.String(), s.pathAB, 0, nil) | ||
|
||
// PacketForwardMiddleware | ||
pfm := s.pktForwardMiddleware(s.chainA, transfertypes.ModuleName) | ||
ack := pfm.OnRecvPacket(ctx, version, packet, senderAddr) | ||
s.Require().True(ack.Success()) | ||
|
||
expectedAck := &channeltypes.Acknowledgement{} | ||
err := s.chainA.Codec.UnmarshalJSON(ack.Acknowledgement(), expectedAck) | ||
s.Require().NoError(err) | ||
s.Require().Equal("", expectedAck.GetError()) | ||
|
||
s.Require().Equal([]byte{1}, expectedAck.GetResult()) | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket_RecvPacketFailed() { | ||
s.setupChains() | ||
|
||
transferKeeper := s.chainA.GetSimApp().TransferKeeper | ||
ctx := s.chainA.GetContext() | ||
// Also can be done if send amount is 0 | ||
transferKeeper.SetParams(ctx, transfertypes.Params{ReceiveEnabled: false}) | ||
|
||
version := s.pathAB.EndpointA.GetChannel().Version | ||
|
||
senderAddr := s.chainA.SenderAccount.GetAddress() | ||
receiverAddr := s.chainB.SenderAccount.GetAddress() | ||
metadata := &packetforwardtypes.PacketMetadata{ | ||
Forward: &packetforwardtypes.ForwardMetadata{ | ||
Receiver: receiverAddr.String(), | ||
Port: s.pathAB.EndpointA.ChannelConfig.PortID, | ||
Channel: s.pathAB.EndpointA.ChannelID, | ||
}, | ||
} | ||
packet := s.transferPacket(senderAddr.String(), receiverAddr.String(), s.pathAB, 0, metadata) | ||
|
||
// PacketForwardMiddleware | ||
pfm := s.pktForwardMiddleware(s.chainA, transfertypes.ModuleName) | ||
ack := pfm.OnRecvPacket(ctx, version, packet, senderAddr) | ||
s.Require().False(ack.Success()) | ||
|
||
expectedAck := &channeltypes.Acknowledgement{} | ||
|
||
err := s.chainA.Codec.UnmarshalJSON(ack.Acknowledgement(), expectedAck) | ||
s.Require().NoError(err) | ||
log.Printf("Error: %s", expectedAck.GetError()) | ||
s.Require().Equal("packet-forward-middleware error: error receiving packet: ack error: {\"error\":\"ABCI code: 8: error handling packet: see events for details\"}", expectedAck.GetError()) | ||
|
||
s.Require().Equal([]byte(nil), expectedAck.GetResult()) | ||
} | ||
|
||
func (s *PFMTestSuite) TestOnRecvPacket_ForwardNoFee() { | ||
s.setupChains() | ||
|
||
senderAddr := s.chainA.SenderAccount.GetAddress() | ||
receiverAddr := s.chainC.SenderAccount.GetAddress() | ||
metadata := &packetforwardtypes.PacketMetadata{ | ||
Forward: &packetforwardtypes.ForwardMetadata{ | ||
Receiver: receiverAddr.String(), | ||
Port: s.pathBC.EndpointA.ChannelConfig.PortID, | ||
Channel: s.pathBC.EndpointA.ChannelID, | ||
}, | ||
} | ||
packet := s.transferPacket(senderAddr.String(), receiverAddr.String(), s.pathAB, 0, metadata) | ||
version := s.pathAB.EndpointA.GetChannel().Version | ||
ctxB := s.chainB.GetContext() | ||
|
||
pfmB := s.pktForwardMiddleware(s.chainB, transfertypes.ModuleName) | ||
ack := pfmB.OnRecvPacket(ctxB, version, packet, senderAddr) | ||
s.Require().Nil(ack) | ||
|
||
// Check that chain C has received the packet | ||
// | ||
ctxC := s.chainC.GetContext() | ||
packet = s.transferPacket(senderAddr.String(), receiverAddr.String(), s.pathBC, 0, nil) | ||
version = s.pathBC.EndpointA.GetChannel().Version | ||
|
||
pfmC := s.pktForwardMiddleware(s.chainC, transfertypes.ModuleName) | ||
ack = pfmC.OnRecvPacket(ctxC, version, packet, senderAddr) | ||
s.Require().NotNil(ack) | ||
|
||
// Ack on chainC | ||
packet = s.transferPacket(senderAddr.String(), receiverAddr.String(), s.pathBC, 1, nil) | ||
err := pfmC.OnAcknowledgementPacket(ctxC, version, packet, ack.Acknowledgement(), senderAddr) | ||
s.Require().NoError(err) | ||
|
||
// Ack on ChainB | ||
err = pfmB.OnAcknowledgementPacket(ctxB, version, packet, ack.Acknowledgement(), senderAddr) | ||
s.Require().NoError(err) | ||
} | ||
|
||
func (s *PFMTestSuite) pktForwardMiddleware(chain *ibctesting.TestChain, module string) packetforward.IBCMiddleware { | ||
Check failure on line 214 in modules/apps/packet-forward-middleware/ibc_middleware_test.go
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As linter says, PFM is always wrapping transfer so we don't need to do this |
||
pfmKeeper := chain.GetSimApp().PFMKeeper | ||
|
||
ibcModule, ok := chain.App.GetIBCKeeper().PortKeeper.Route(module) | ||
s.Require().True(ok) | ||
|
||
ibcMiddleware := packetforward.NewIBCMiddleware(ibcModule, &pfmKeeper, 0, packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp) | ||
return ibcMiddleware | ||
} | ||
|
||
func (s *PFMTestSuite) transferPacket(sender string, receiver string, path *ibctesting.Path, seq uint64, metadata any) channeltypes.Packet { | ||
s.T().Helper() | ||
tokenPacket := transfertypes.FungibleTokenPacketData{ | ||
Denom: "uatom", | ||
Amount: "100", | ||
Sender: sender, | ||
Receiver: receiver, | ||
} | ||
|
||
if metadata != nil { | ||
if mStr, ok := metadata.(string); ok { | ||
tokenPacket.Memo = mStr | ||
} else { | ||
memo, err := json.Marshal(metadata) | ||
s.Require().NoError(err) | ||
tokenPacket.Memo = string(memo) | ||
} | ||
} | ||
|
||
tokenData, err := transfertypes.ModuleCdc.MarshalJSON(&tokenPacket) | ||
s.Require().NoError(err) | ||
|
||
return channeltypes.Packet{ | ||
SourcePort: path.EndpointA.ChannelConfig.PortID, | ||
SourceChannel: path.EndpointA.ChannelID, | ||
DestinationPort: path.EndpointB.ChannelConfig.PortID, | ||
DestinationChannel: path.EndpointB.ChannelID, | ||
Data: tokenData, | ||
Sequence: seq, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a single
TestOnRecvPacket
with table test cases. See the ibc-go test cases for an example