From 953d24273bab7282da3d62a3e2af755abf91f03a Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 24 May 2023 07:13:49 -0400 Subject: [PATCH] fix: ensure height is correct on first block (#16259) --- baseapp/abci.go | 14 ++++++++++++-- baseapp/abci_test.go | 17 ++++++++++++++++- baseapp/test_helpers.go | 4 ++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 3afa5c4bdc1a..7b850ee06f1a 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -71,6 +71,16 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams) } + defer func() { + // InitChain represents the state of the application BEFORE the first block, + // i.e. the genesis block. This means that when processing the app's InitChain + // handler, the block height is zero by default. However, after Commit is called + // the height needs to reflect the true block height. + initHeader.Height = req.InitialHeight + app.checkState.ctx = app.checkState.ctx.WithBlockHeader(initHeader) + app.deliverState.ctx = app.deliverState.ctx.WithBlockHeader(initHeader) + }() + if app.initChainer == nil { return } @@ -113,8 +123,8 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC appHash = emptyHash[:] } - // NOTE: We don't commit, but BeginBlock for block `initial_height` starts from this - // deliverState. + // NOTE: We don't commit, but BeginBlock for block InitialHeight starts from + // this deliverState. return abci.ResponseInitChain{ ConsensusParams: res.ConsensusParams, Validators: res.Validators, diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index e3793506866f..9349d149ed56 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -7,9 +7,9 @@ import ( "strings" "testing" - dbm "github.com/cometbft/cometbft-db" abci "github.com/cometbft/cometbft/abci/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + dbm "github.com/cometbft/cometbft-db" "github.com/cosmos/gogoproto/jsonpb" "github.com/stretchr/testify/require" @@ -39,6 +39,21 @@ func TestABCI_Info(t *testing.T) { require.Equal(t, suite.baseApp.AppVersion(), res.AppVersion) } +func TestABCI_First_block_Height(t *testing.T) { + suite := NewBaseAppSuite(t, baseapp.SetChainID("test-chain-id")) + app := suite.baseApp + + app.InitChain(abci.RequestInitChain{ + ChainId: "test-chain-id", + ConsensusParams: &cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}}, + InitialHeight: 1, + }) + _ = app.Commit() + + ctx := app.GetContextForCheckTx(nil) + require.Equal(t, int64(1), ctx.BlockHeight()) +} + func TestABCI_InitChain(t *testing.T) { name := t.Name() db := dbm.NewMemDB() diff --git a/baseapp/test_helpers.go b/baseapp/test_helpers.go index 8e2e2da77fab..7b100e24959f 100644 --- a/baseapp/test_helpers.go +++ b/baseapp/test_helpers.go @@ -53,3 +53,7 @@ func (app *BaseApp) NewUncachedContext(isCheckTx bool, header tmproto.Header) sd func (app *BaseApp) GetContextForDeliverTx(txBytes []byte) sdk.Context { return app.getContextForTx(runTxModeDeliver, txBytes) } + +func (app *BaseApp) GetContextForCheckTx(txBytes []byte) sdk.Context { + return app.getContextForTx(runTxModeCheck, txBytes) +}