Skip to content

Commit

Permalink
Minor refactor: factor out Begin(End)Block calls from simibc (#588)
Browse files Browse the repository at this point in the history
* Factors out BeginBlock, EndBlock calls

* Adds comments

* Adds docstring to BeginBlock

Co-authored-by: Daniel <[email protected]>
Co-authored-by: Marius Poke <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2023
1 parent 0c62722 commit daece1c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
59 changes: 59 additions & 0 deletions testutil/simibc/chain_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package simibc

import (
"time"

channelkeeper "github.com/cosmos/ibc-go/v3/modules/core/04-channel/keeper"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// BeginBlock updates the current header and calls the app.BeginBlock() method.
// The new block height is the previous block height + 1.
// The new block time is the previous block time + dt.
func BeginBlock(c *ibctesting.TestChain, dt time.Duration) {

c.CurrentHeader = tmproto.Header{
ChainID: c.ChainID,
Height: c.App.LastBlockHeight() + 1,
AppHash: c.App.LastCommitID().Hash,
Time: c.CurrentHeader.Time.Add(dt),
ValidatorsHash: c.Vals.Hash(),
NextValidatorsHash: c.NextVals.Hash(),
}

_ = c.App.BeginBlock(abci.RequestBeginBlock{Header: c.CurrentHeader})
}

// EndBlock and calls the preCommitCallback before the app.Commit() is called.
func EndBlock(c *ibctesting.TestChain, preCommitCallback func()) (*ibctmtypes.Header, []channeltypes.Packet) {
ebRes := c.App.EndBlock(abci.RequestEndBlock{Height: c.CurrentHeader.Height})

/*
It is useful to call arbitrary code after ending the block but before
committing the block because the sdk.Context is cleared after committing.
*/
preCommitCallback()

c.App.Commit()

c.Vals = c.NextVals

c.NextVals = ibctesting.ApplyValSetChanges(c.T, c.Vals, ebRes.ValidatorUpdates)

c.LastHeader = c.CurrentTMClientHeader()

packets := []channeltypes.Packet{}

for _, e := range ebRes.Events {
if e.Type == channeltypes.EventTypeSendPacket {
packet, _ := channelkeeper.ReconstructPacketFromEvent(e)
packets = append(packets, packet)
}
}

return c.LastHeader, packets
}
45 changes: 5 additions & 40 deletions testutil/simibc/relayed_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import (

"testing"

channelkeeper "github.com/cosmos/ibc-go/v3/modules/core/04-channel/keeper"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// RelayedPath augments ibctesting.Path, giving fine-grained control
Expand Down Expand Up @@ -92,44 +88,13 @@ func (f *RelayedPath) DeliverAcks(chainID string, num int) {
// allowing access to the sdk.Context after EndBlock.
func (f *RelayedPath) EndAndBeginBlock(chainID string, dt time.Duration, preCommitCallback func()) {
c := f.Chain(chainID)

ebRes := c.App.EndBlock(abci.RequestEndBlock{Height: c.CurrentHeader.Height})

preCommitCallback()

c.App.Commit()

c.Vals = c.NextVals

c.NextVals = ibctesting.ApplyValSetChanges(c.T, c.Vals, ebRes.ValidatorUpdates)

c.LastHeader = c.CurrentTMClientHeader()

// Store header to be used in UpdateClient
f.clientHeaders[chainID] = append(f.clientHeaders[chainID], c.LastHeader)

for _, e := range ebRes.Events {
if e.Type == channeltypes.EventTypeSendPacket {
packet, _ := channelkeeper.ReconstructPacketFromEvent(e)
// Collect packets
f.Link.AddPacket(chainID, packet)
}
header, packets := EndBlock(c, preCommitCallback)
f.clientHeaders[chainID] = append(f.clientHeaders[chainID], header)
for _, p := range packets {
f.Link.AddPacket(chainID, p)
}

// Commit packets emmitted up to this point
f.Link.Commit(chainID)

// increment the current header
c.CurrentHeader = tmproto.Header{
ChainID: c.ChainID,
Height: c.App.LastBlockHeight() + 1,
AppHash: c.App.LastCommitID().Hash,
Time: c.CurrentHeader.Time.Add(dt),
ValidatorsHash: c.Vals.Hash(),
NextValidatorsHash: c.NextVals.Hash(),
}

_ = c.App.BeginBlock(abci.RequestBeginBlock{Header: c.CurrentHeader})
BeginBlock(c, dt)
}

func (f *RelayedPath) other(chainID string) string {
Expand Down

0 comments on commit daece1c

Please sign in to comment.