github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/04-channel/keeper/keeper_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	types2 "github.com/fibonacci-chain/fbc/libs/tendermint/types"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    11  	ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing"
    12  )
    13  
    14  // KeeperTestSuite is a testing suite to test keeper functions.
    15  type KeeperTestSuite struct {
    16  	suite.Suite
    17  
    18  	coordinator *ibctesting.Coordinator
    19  
    20  	// testing chains used for convenience and readability
    21  	chainA ibctesting.TestChainI
    22  	chainB ibctesting.TestChainI
    23  }
    24  
    25  // TestKeeperTestSuite runs all the tests within this package.
    26  func TestKeeperTestSuite(t *testing.T) {
    27  	suite.Run(t, new(KeeperTestSuite))
    28  }
    29  
    30  // SetupTest creates a coordinator with 2 test chains.
    31  func (suite *KeeperTestSuite) SetupTest() {
    32  	types2.UnittestOnlySetMilestoneVenus1Height(-1)
    33  	suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
    34  	suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0))
    35  	suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1))
    36  	// commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1)
    37  	suite.coordinator.CommitNBlocks(suite.chainA, 2)
    38  	suite.coordinator.CommitNBlocks(suite.chainB, 2)
    39  }
    40  
    41  // TestSetChannel create clients and connections on both chains. It tests for the non-existence
    42  // and existence of a channel in INIT on chainA.
    43  func (suite *KeeperTestSuite) TestSetChannel() {
    44  	// create client and connections on both chains
    45  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
    46  	suite.coordinator.SetupConnections(path)
    47  
    48  	// check for channel to be created on chainA
    49  	_, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
    50  	suite.False(found)
    51  
    52  	path.SetChannelOrdered()
    53  
    54  	// init channel
    55  	err := path.EndpointA.ChanOpenInit()
    56  	suite.NoError(err)
    57  
    58  	storedChannel, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
    59  	// counterparty channel id is empty after open init
    60  	expectedCounterparty := types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, "")
    61  
    62  	suite.True(found)
    63  	suite.Equal(types.INIT, storedChannel.State)
    64  	suite.Equal(types.ORDERED, storedChannel.Ordering)
    65  	suite.Equal(expectedCounterparty, storedChannel.Counterparty)
    66  }
    67  
    68  // TestGetAllChannels creates multiple channels on chain A through various connections
    69  // and tests their retrieval. 2 channels are on connA0 and 1 channel is on connA1
    70  func (suite KeeperTestSuite) TestGetAllChannels() {
    71  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
    72  	suite.coordinator.Setup(path)
    73  	// channel0 on first connection on chainA
    74  	counterparty0 := types.Counterparty{
    75  		PortId:    path.EndpointB.ChannelConfig.PortID,
    76  		ChannelId: path.EndpointB.ChannelID,
    77  	}
    78  
    79  	// path1 creates a second channel on first connection on chainA
    80  	path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
    81  	path1.SetChannelOrdered()
    82  	path1.EndpointA.ClientID = path.EndpointA.ClientID
    83  	path1.EndpointB.ClientID = path.EndpointB.ClientID
    84  	path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID
    85  	path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID
    86  
    87  	suite.coordinator.CreateMockChannels(path1)
    88  	counterparty1 := types.Counterparty{
    89  		PortId:    path1.EndpointB.ChannelConfig.PortID,
    90  		ChannelId: path1.EndpointB.ChannelID,
    91  	}
    92  
    93  	path2 := ibctesting.NewPath(suite.chainA, suite.chainB)
    94  	suite.coordinator.SetupConnections(path2)
    95  
    96  	// path2 creates a second channel on chainA
    97  	err := path2.EndpointA.ChanOpenInit()
    98  	suite.Require().NoError(err)
    99  
   100  	// counterparty channel id is empty after open init
   101  	counterparty2 := types.Counterparty{
   102  		PortId:    path2.EndpointB.ChannelConfig.PortID,
   103  		ChannelId: "",
   104  	}
   105  
   106  	channel0 := types.NewChannel(
   107  		types.OPEN, types.UNORDERED,
   108  		counterparty0, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version,
   109  	)
   110  	channel1 := types.NewChannel(
   111  		types.OPEN, types.ORDERED,
   112  		counterparty1, []string{path1.EndpointA.ConnectionID}, path1.EndpointA.ChannelConfig.Version,
   113  	)
   114  	channel2 := types.NewChannel(
   115  		types.INIT, types.UNORDERED,
   116  		counterparty2, []string{path2.EndpointA.ConnectionID}, path2.EndpointA.ChannelConfig.Version,
   117  	)
   118  
   119  	expChannels := []types.IdentifiedChannel{
   120  		types.NewIdentifiedChannel(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel0),
   121  		types.NewIdentifiedChannel(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, channel1),
   122  		types.NewIdentifiedChannel(path2.EndpointA.ChannelConfig.PortID, path2.EndpointA.ChannelID, channel2),
   123  	}
   124  
   125  	ctxA := suite.chainA.GetContext()
   126  
   127  	channels := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllChannels(ctxA)
   128  	suite.Require().Len(channels, len(expChannels))
   129  	suite.Require().Equal(expChannels, channels)
   130  }
   131  
   132  // TestGetAllSequences sets all packet sequences for two different channels on chain A and
   133  // tests their retrieval.
   134  func (suite KeeperTestSuite) TestGetAllSequences() {
   135  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
   136  	suite.coordinator.Setup(path)
   137  
   138  	path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
   139  	path1.SetChannelOrdered()
   140  	path1.EndpointA.ClientID = path.EndpointA.ClientID
   141  	path1.EndpointB.ClientID = path.EndpointB.ClientID
   142  	path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID
   143  	path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID
   144  
   145  	suite.coordinator.CreateMockChannels(path1)
   146  
   147  	seq1 := types.NewPacketSequence(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1)
   148  	seq2 := types.NewPacketSequence(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2)
   149  	seq3 := types.NewPacketSequence(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 3)
   150  
   151  	// seq1 should be overwritten by seq2
   152  	expSeqs := []types.PacketSequence{seq2, seq3}
   153  
   154  	ctxA := suite.chainA.GetContext()
   155  
   156  	for _, seq := range []types.PacketSequence{seq1, seq2, seq3} {
   157  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceSend(ctxA, seq.PortId, seq.ChannelId, seq.Sequence)
   158  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(ctxA, seq.PortId, seq.ChannelId, seq.Sequence)
   159  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(ctxA, seq.PortId, seq.ChannelId, seq.Sequence)
   160  	}
   161  
   162  	sendSeqs := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketSendSeqs(ctxA)
   163  	recvSeqs := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketRecvSeqs(ctxA)
   164  	ackSeqs := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketAckSeqs(ctxA)
   165  	suite.Len(sendSeqs, 2)
   166  	suite.Len(recvSeqs, 2)
   167  	suite.Len(ackSeqs, 2)
   168  
   169  	suite.Equal(expSeqs, sendSeqs)
   170  	suite.Equal(expSeqs, recvSeqs)
   171  	suite.Equal(expSeqs, ackSeqs)
   172  }
   173  
   174  // TestGetAllPacketState creates a set of acks, packet commitments, and receipts on two different
   175  // channels on chain A and tests their retrieval.
   176  func (suite KeeperTestSuite) TestGetAllPacketState() {
   177  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
   178  	suite.coordinator.Setup(path)
   179  
   180  	path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
   181  	path1.EndpointA.ClientID = path.EndpointA.ClientID
   182  	path1.EndpointB.ClientID = path.EndpointB.ClientID
   183  	path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID
   184  	path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID
   185  
   186  	suite.coordinator.CreateMockChannels(path1)
   187  
   188  	// channel 0 acks
   189  	ack1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte("ack"))
   190  	ack2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("ack"))
   191  
   192  	// duplicate ack
   193  	ack2dup := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("ack"))
   194  
   195  	// channel 1 acks
   196  	ack3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte("ack"))
   197  
   198  	// create channel 0 receipts
   199  	receipt := string([]byte{byte(1)})
   200  	rec1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte(receipt))
   201  	rec2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte(receipt))
   202  
   203  	// channel 1 receipts
   204  	rec3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte(receipt))
   205  	rec4 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 2, []byte(receipt))
   206  
   207  	// channel 0 packet commitments
   208  	comm1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte("hash"))
   209  	comm2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("hash"))
   210  
   211  	// channel 1 packet commitments
   212  	comm3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte("hash"))
   213  	comm4 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 2, []byte("hash"))
   214  
   215  	expAcks := []types.PacketState{ack1, ack2, ack3}
   216  	expReceipts := []types.PacketState{rec1, rec2, rec3, rec4}
   217  	expCommitments := []types.PacketState{comm1, comm2, comm3, comm4}
   218  
   219  	ctxA := suite.chainA.GetContext()
   220  
   221  	// set acknowledgements
   222  	for _, ack := range []types.PacketState{ack1, ack2, ack2dup, ack3} {
   223  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(ctxA, ack.PortId, ack.ChannelId, ack.Sequence, ack.Data)
   224  	}
   225  
   226  	// set packet receipts
   227  	for _, rec := range expReceipts {
   228  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketReceipt(ctxA, rec.PortId, rec.ChannelId, rec.Sequence)
   229  	}
   230  
   231  	// set packet commitments
   232  	for _, comm := range expCommitments {
   233  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, comm.PortId, comm.ChannelId, comm.Sequence, comm.Data)
   234  	}
   235  
   236  	acks := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketAcks(ctxA)
   237  	receipts := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketReceipts(ctxA)
   238  	commitments := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketCommitments(ctxA)
   239  
   240  	suite.Require().Len(acks, len(expAcks))
   241  	suite.Require().Len(commitments, len(expCommitments))
   242  	suite.Require().Len(receipts, len(expReceipts))
   243  
   244  	suite.Require().Equal(expAcks, acks)
   245  	suite.Require().Equal(expReceipts, receipts)
   246  	suite.Require().Equal(expCommitments, commitments)
   247  }
   248  
   249  // TestSetSequence verifies that the keeper correctly sets the sequence counters.
   250  func (suite *KeeperTestSuite) TestSetSequence() {
   251  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
   252  	suite.coordinator.Setup(path)
   253  
   254  	ctxA := suite.chainA.GetContext()
   255  	one := uint64(1)
   256  
   257  	// initialized channel has next send seq of 1
   258  	seq, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   259  	suite.True(found)
   260  	suite.Equal(one, seq)
   261  
   262  	// initialized channel has next seq recv of 1
   263  	seq, found = suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   264  	suite.True(found)
   265  	suite.Equal(one, seq)
   266  
   267  	// initialized channel has next seq ack of
   268  	seq, found = suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   269  	suite.True(found)
   270  	suite.Equal(one, seq)
   271  
   272  	nextSeqSend, nextSeqRecv, nextSeqAck := uint64(10), uint64(10), uint64(10)
   273  	suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqSend)
   274  	suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqRecv)
   275  	suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqAck)
   276  
   277  	storedNextSeqSend, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   278  	suite.True(found)
   279  	suite.Equal(nextSeqSend, storedNextSeqSend)
   280  
   281  	storedNextSeqRecv, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   282  	suite.True(found)
   283  	suite.Equal(nextSeqRecv, storedNextSeqRecv)
   284  
   285  	storedNextSeqAck, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   286  	suite.True(found)
   287  	suite.Equal(nextSeqAck, storedNextSeqAck)
   288  }
   289  
   290  // TestGetAllPacketCommitmentsAtChannel verifies that the keeper returns all stored packet
   291  // commitments for a specific channel. The test will store consecutive commitments up to the
   292  // value of "seq" and then add non-consecutive up to the value of "maxSeq". A final commitment
   293  // with the value maxSeq + 1 is set on a different channel.
   294  func (suite *KeeperTestSuite) TestGetAllPacketCommitmentsAtChannel() {
   295  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
   296  	suite.coordinator.Setup(path)
   297  
   298  	// create second channel
   299  	path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
   300  	path1.SetChannelOrdered()
   301  	path1.EndpointA.ClientID = path.EndpointA.ClientID
   302  	path1.EndpointB.ClientID = path.EndpointB.ClientID
   303  	path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID
   304  	path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID
   305  
   306  	suite.coordinator.CreateMockChannels(path1)
   307  
   308  	ctxA := suite.chainA.GetContext()
   309  	expectedSeqs := make(map[uint64]bool)
   310  	hash := []byte("commitment")
   311  
   312  	seq := uint64(15)
   313  	maxSeq := uint64(25)
   314  	suite.Require().Greater(maxSeq, seq)
   315  
   316  	// create consecutive commitments
   317  	for i := uint64(1); i < seq; i++ {
   318  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, hash)
   319  		expectedSeqs[i] = true
   320  	}
   321  
   322  	// add non-consecutive commitments
   323  	for i := seq; i < maxSeq; i += 2 {
   324  		suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, hash)
   325  		expectedSeqs[i] = true
   326  	}
   327  
   328  	// add sequence on different channel/port
   329  	suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, maxSeq+1, hash)
   330  
   331  	commitments := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetAllPacketCommitmentsAtChannel(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   332  
   333  	suite.Equal(len(expectedSeqs), len(commitments))
   334  	// ensure above for loops occurred
   335  	suite.NotEqual(0, len(commitments))
   336  
   337  	// verify that all the packet commitments were stored
   338  	for _, packet := range commitments {
   339  		suite.True(expectedSeqs[packet.Sequence])
   340  		suite.Equal(path.EndpointA.ChannelConfig.PortID, packet.PortId)
   341  		suite.Equal(path.EndpointA.ChannelID, packet.ChannelId)
   342  		suite.Equal(hash, packet.Data)
   343  
   344  		// prevent duplicates from passing checks
   345  		expectedSeqs[packet.Sequence] = false
   346  	}
   347  }
   348  
   349  // TestSetPacketAcknowledgement verifies that packet acknowledgements are correctly
   350  // set in the keeper.
   351  func (suite *KeeperTestSuite) TestSetPacketAcknowledgement() {
   352  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
   353  	suite.coordinator.Setup(path)
   354  
   355  	ctxA := suite.chainA.GetContext()
   356  	seq := uint64(10)
   357  
   358  	storedAckHash, found := suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq)
   359  	suite.Require().False(found)
   360  	suite.Require().Nil(storedAckHash)
   361  
   362  	ackHash := []byte("ackhash")
   363  	suite.chainA.App().GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq, ackHash)
   364  
   365  	storedAckHash, found = suite.chainA.App().GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq)
   366  	suite.Require().True(found)
   367  	suite.Require().Equal(ackHash, storedAckHash)
   368  	suite.Require().True(suite.chainA.App().GetIBCKeeper().ChannelKeeper.HasPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq))
   369  }