From f6e7e7a4448fd518b386b98dc44a359924f61216 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 1de2dda73fb7..b9586969f308 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -69,6 +69,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 } @@ -111,8 +121,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 81c919ff7c51..f6613abb054c 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) +}