-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactor: factor out Begin(End)Block calls from simibc (#588)
* 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
1 parent
0c62722
commit daece1c
Showing
2 changed files
with
64 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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,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 | ||
} |
This file contains 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