From 467525bceae33895c80f39492dc2c90c2a8b422c Mon Sep 17 00:00:00 2001 From: mpoke Date: Tue, 28 Jun 2022 11:10:06 +0200 Subject: [PATCH 1/2] store sent packets --- testing/chain.go | 39 +++++++++++++++++++++++++++++++++ testing/chain_test.go | 50 +++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/testing/chain.go b/testing/chain.go index 63396921f0e..3633880e14c 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -27,6 +27,8 @@ import ( tmversion "github.com/tendermint/tendermint/version" clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" + channelkeeper "github.com/cosmos/ibc-go/v3/modules/core/04-channel/keeper" + channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" host "github.com/cosmos/ibc-go/v3/modules/core/24-host" "github.com/cosmos/ibc-go/v3/modules/core/exported" @@ -70,6 +72,10 @@ type TestChain struct { // the new PrivValidator entry. Signers map[string]tmtypes.PrivValidator + // SentPackets is a map from packet sequences to sent packets, + // reconstructed from emitted events of type SendPacketEvent + SentPackets map[uint64]channeltypes.Packet + // autogenerated sender private key SenderPrivKey cryptotypes.PrivKey SenderAccount authtypes.AccountI @@ -144,6 +150,7 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appIniter AppInite Vals: valSet, NextVals: valSet, Signers: signers, + SentPackets: make(map[uint64]channeltypes.Packet), SenderPrivKey: senderAccs[0].SenderPrivKey, SenderAccount: senderAccs[0].SenderAccount, SenderAccounts: senderAccs, @@ -261,6 +268,13 @@ func (chain *TestChain) QueryConsensusStateProof(clientID string) ([]byte, clien return proofConsensus, consensusHeight } +// GetSentPacket returns the sent packet with `sequence` (if any), +// reconstructed from emitted events of type SendPacketEvent +func (chain *TestChain) GetSentPacket(sequence uint64) (packet channeltypes.Packet, found bool) { + packet, found = chain.SentPackets[sequence] + return +} + // NextBlock sets the last header to the current header and increments the current header to be // at the next block height. It does not update the time as that is handled by the Coordinator. // It will call Endblock and Commit and apply the validator set changes to the next validators @@ -270,6 +284,15 @@ func (chain *TestChain) QueryConsensusStateProof(clientID string) ([]byte, clien func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit, abci.ResponseBeginBlock) { ebRes := chain.App.EndBlock(abci.RequestEndBlock{Height: chain.CurrentHeader.Height}) + // store packets sent during EndBlock + for _, event := range ebRes.Events { + if event.Type == channeltypes.EventTypeSendPacket { + packet, err := channelkeeper.ReconstructPacketFromEvent(event) + require.NoError(chain.T, err) + chain.SentPackets[packet.Sequence] = packet + } + } + cRes := chain.App.Commit() // set the last header to the current header @@ -294,6 +317,14 @@ func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit, } bbRes := chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) + // store packets sent during BeginBlock + for _, event := range bbRes.Events { + if event.Type == channeltypes.EventTypeSendPacket { + packet, err := channelkeeper.ReconstructPacketFromEvent(event) + require.NoError(chain.T, err) + chain.SentPackets[packet.Sequence] = packet + } + } return ebRes, cRes, bbRes } @@ -324,6 +355,14 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { if err != nil { return nil, err } + // store packets sent during the execution of this transaction + for _, event := range r.Events { + if event.Type == channeltypes.EventTypeSendPacket { + packet, err := channelkeeper.ReconstructPacketFromEvent(event) + require.NoError(chain.T, err) + chain.SentPackets[packet.Sequence] = packet + } + } // NextBlock calls app.Commit() chain.NextBlock() diff --git a/testing/chain_test.go b/testing/chain_test.go index 64ddc6c751e..290e63eb4c9 100644 --- a/testing/chain_test.go +++ b/testing/chain_test.go @@ -1,38 +1,28 @@ package ibctesting_test -import ( - "testing" +// func TestChangeValSet(t *testing.T) { +// coord := ibctesting.NewCoordinator(t, 2) +// chainA := coord.GetChain(ibctesting.GetChainID(1)) +// chainB := coord.GetChain(ibctesting.GetChainID(2)) - "github.com/stretchr/testify/require" +// path := ibctesting.NewPath(chainA, chainB) +// coord.Setup(path) - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking/types" - ibctesting "github.com/cosmos/ibc-go/v3/testing" -) +// amount, ok := sdk.NewIntFromString("10000000000000000000") +// require.True(t, ok) +// amount2, ok := sdk.NewIntFromString("30000000000000000000") +// require.True(t, ok) -func TestChangeValSet(t *testing.T) { - coord := ibctesting.NewCoordinator(t, 2) - chainA := coord.GetChain(ibctesting.GetChainID(1)) - chainB := coord.GetChain(ibctesting.GetChainID(2)) +// val := chainA.App.GetStakingKeeper().GetValidators(chainA.GetContext(), 4) - path := ibctesting.NewPath(chainA, chainB) - coord.Setup(path) +// chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[1].SenderAccount.GetAddress(), +// amount, types.Unbonded, val[1], true) +// chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[3].SenderAccount.GetAddress(), +// amount2, types.Unbonded, val[3], true) - amount, ok := sdk.NewIntFromString("10000000000000000000") - require.True(t, ok) - amount2, ok := sdk.NewIntFromString("30000000000000000000") - require.True(t, ok) +// coord.CommitBlock(chainA) - val := chainA.App.GetStakingKeeper().GetValidators(chainA.GetContext(), 4) - - chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[1].SenderAccount.GetAddress(), - amount, types.Unbonded, val[1], true) - chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[3].SenderAccount.GetAddress(), - amount2, types.Unbonded, val[3], true) - - coord.CommitBlock(chainA) - - // verify that update clients works even after validator update goes into effect - path.EndpointB.UpdateClient() - path.EndpointB.UpdateClient() -} +// // verify that update clients works even after validator update goes into effect +// path.EndpointB.UpdateClient() +// path.EndpointB.UpdateClient() +// } From a8a6073a44ed47705b20c186b8c24e726d9a7933 Mon Sep 17 00:00:00 2001 From: mpoke Date: Tue, 28 Jun 2022 12:49:26 +0200 Subject: [PATCH 2/2] add changes from review --- testing/chain.go | 37 ++++++++++++++++--------------------- testing/chain_test.go | 28 ---------------------------- 2 files changed, 16 insertions(+), 49 deletions(-) delete mode 100644 testing/chain_test.go diff --git a/testing/chain.go b/testing/chain.go index 3633880e14c..f760882048a 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -275,6 +275,18 @@ func (chain *TestChain) GetSentPacket(sequence uint64) (packet channeltypes.Pack return } +// setSentPacketsFromEvents stores the sent packet reconstructed +// from emitted events of type SendPacketEvent +func (chain *TestChain) setSentPacketsFromEvents(events []abci.Event) { + for _, event := range events { + if event.Type == channeltypes.EventTypeSendPacket { + packet, err := channelkeeper.ReconstructPacketFromEvent(event) + require.NoError(chain.T, err) + chain.SentPackets[packet.Sequence] = packet + } + } +} + // NextBlock sets the last header to the current header and increments the current header to be // at the next block height. It does not update the time as that is handled by the Coordinator. // It will call Endblock and Commit and apply the validator set changes to the next validators @@ -285,13 +297,7 @@ func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit, ebRes := chain.App.EndBlock(abci.RequestEndBlock{Height: chain.CurrentHeader.Height}) // store packets sent during EndBlock - for _, event := range ebRes.Events { - if event.Type == channeltypes.EventTypeSendPacket { - packet, err := channelkeeper.ReconstructPacketFromEvent(event) - require.NoError(chain.T, err) - chain.SentPackets[packet.Sequence] = packet - } - } + chain.setSentPacketsFromEvents(ebRes.Events) cRes := chain.App.Commit() @@ -318,13 +324,8 @@ func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit, bbRes := chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) // store packets sent during BeginBlock - for _, event := range bbRes.Events { - if event.Type == channeltypes.EventTypeSendPacket { - packet, err := channelkeeper.ReconstructPacketFromEvent(event) - require.NoError(chain.T, err) - chain.SentPackets[packet.Sequence] = packet - } - } + chain.setSentPacketsFromEvents(bbRes.Events) + return ebRes, cRes, bbRes } @@ -356,13 +357,7 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { return nil, err } // store packets sent during the execution of this transaction - for _, event := range r.Events { - if event.Type == channeltypes.EventTypeSendPacket { - packet, err := channelkeeper.ReconstructPacketFromEvent(event) - require.NoError(chain.T, err) - chain.SentPackets[packet.Sequence] = packet - } - } + chain.setSentPacketsFromEvents(r.Events) // NextBlock calls app.Commit() chain.NextBlock() diff --git a/testing/chain_test.go b/testing/chain_test.go deleted file mode 100644 index 290e63eb4c9..00000000000 --- a/testing/chain_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package ibctesting_test - -// func TestChangeValSet(t *testing.T) { -// coord := ibctesting.NewCoordinator(t, 2) -// chainA := coord.GetChain(ibctesting.GetChainID(1)) -// chainB := coord.GetChain(ibctesting.GetChainID(2)) - -// path := ibctesting.NewPath(chainA, chainB) -// coord.Setup(path) - -// amount, ok := sdk.NewIntFromString("10000000000000000000") -// require.True(t, ok) -// amount2, ok := sdk.NewIntFromString("30000000000000000000") -// require.True(t, ok) - -// val := chainA.App.GetStakingKeeper().GetValidators(chainA.GetContext(), 4) - -// chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[1].SenderAccount.GetAddress(), -// amount, types.Unbonded, val[1], true) -// chainA.App.GetStakingKeeper().Delegate(chainA.GetContext(), chainA.SenderAccounts[3].SenderAccount.GetAddress(), -// amount2, types.Unbonded, val[3], true) - -// coord.CommitBlock(chainA) - -// // verify that update clients works even after validator update goes into effect -// path.EndpointB.UpdateClient() -// path.EndpointB.UpdateClient() -// }