Skip to content

Commit

Permalink
Merge pull request cosmos#5 from informalsystems/marius/sent-packet-list
Browse files Browse the repository at this point in the history
Store sent packets within ibc-go testing framework
  • Loading branch information
mpoke authored Jun 28, 2022
2 parents 83aa279 + a8a6073 commit c48f8e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
34 changes: 34 additions & 0 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -261,6 +268,25 @@ 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
}

// 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
Expand All @@ -270,6 +296,9 @@ 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
chain.setSentPacketsFromEvents(ebRes.Events)

cRes := chain.App.Commit()

// set the last header to the current header
Expand All @@ -294,6 +323,9 @@ func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit,
}

bbRes := chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader})
// store packets sent during BeginBlock
chain.setSentPacketsFromEvents(bbRes.Events)

return ebRes, cRes, bbRes
}

Expand Down Expand Up @@ -324,6 +356,8 @@ 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
chain.setSentPacketsFromEvents(r.Events)

// NextBlock calls app.Commit()
chain.NextBlock()
Expand Down
38 changes: 0 additions & 38 deletions testing/chain_test.go

This file was deleted.

0 comments on commit c48f8e2

Please sign in to comment.