From f95585b40d0f72e25b73d81df662e2e5ce755442 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 11 Apr 2023 11:49:42 -0400 Subject: [PATCH] fix: use initialHeight correctly (#15789) --- CHANGELOG.md | 1 + baseapp/abci.go | 17 ++++++++++------- baseapp/abci_test.go | 1 + baseapp/baseapp.go | 20 +++++++++++--------- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f4b09d7fa..16317daa03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -173,6 +173,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (baseapp) [#15789](https://github.com/cosmos/cosmos-sdk/pull/15789) Ensure `PrepareProposal` and `ProcessProposal` respect `InitialHeight` set by CometBFT when set to a value greater than 1. * (types) [#15691](https://github.com/cosmos/cosmos-sdk/pull/15691) Make Coin.Validate() check that .Amount is not nil * (types) [#15433](https://github.com/cosmos/cosmos-sdk/pull/15433) Allow disabling of account address caches (for printing bech32 account addresses). * (x/auth) [#15059](https://github.com/cosmos/cosmos-sdk/pull/15059) `ante.CountSubKeys` returns 0 when passing a nil `Pubkey`. diff --git a/baseapp/abci.go b/baseapp/abci.go index d5fbf416d6..8dd1aaec0b 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -48,13 +48,14 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC app.logger.Info("InitChain", "initialHeight", req.InitialHeight, "chainID", req.ChainId) - // If req.InitialHeight is > 1, then we set the initial version in the - // stores. + // Set the initial height, which will be used to determine if we are proposing + // or processing the first block or not. + app.initialHeight = req.InitialHeight + + // if req.InitialHeight is > 1, then we set the initial version on all stores if req.InitialHeight > 1 { - app.initialHeight = req.InitialHeight - initHeader = cmtproto.Header{ChainID: req.ChainId, Height: req.InitialHeight, Time: req.Time} - err := app.cms.SetInitialVersion(req.InitialHeight) - if err != nil { + initHeader.Height = req.InitialHeight + if err := app.cms.SetInitialVersion(req.InitialHeight); err != nil { panic(err) } } @@ -1017,11 +1018,13 @@ func SplitABCIQueryPath(requestPath string) (path []string) { // ProcessProposal. We use deliverState on the first block to be able to access // any state changes made in InitChain. func (app *BaseApp) getContextForProposal(ctx sdk.Context, height int64) sdk.Context { - if height == 1 { + if height == app.initialHeight { ctx, _ = app.deliverState.ctx.CacheContext() + // clear all context data set during InitChain to avoid inconsistent behavior ctx = ctx.WithBlockHeader(cmtproto.Header{}) return ctx } + return ctx } diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index b4e4abca02..cc762dad42 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -1407,6 +1407,7 @@ func TestABCI_Proposal_Read_State_PrepareProposal(t *testing.T) { suite := NewBaseAppSuite(t, setInitChainerOpt, prepareOpt) suite.baseApp.InitChain(abci.RequestInitChain{ + InitialHeight: 1, ConsensusParams: &cmtproto.ConsensusParams{}, }) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 680decf8b8..bf29ecc83f 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -493,19 +493,21 @@ func (app *BaseApp) validateHeight(req abci.RequestBeginBlock) error { return fmt.Errorf("invalid height: %d", req.Header.Height) } - // expectedHeight holds the expected height to validate. + lastBlockHeight := app.LastBlockHeight() + + // expectedHeight holds the expected height to validate var expectedHeight int64 - if app.LastBlockHeight() == 0 && app.initialHeight > 1 { - // In this case, we're validating the first block of the chain (no - // previous commit). The height we're expecting is the initial height. + if lastBlockHeight == 0 && app.initialHeight > 1 { + // In this case, we're validating the first block of the chain, i.e no + // previous commit. The height we're expecting is the initial height. expectedHeight = app.initialHeight } else { // This case can mean two things: - // - either there was already a previous commit in the store, in which - // case we increment the version from there, - // - or there was no previous commit, and initial version was not set, - // in which case we start at version 1. - expectedHeight = app.LastBlockHeight() + 1 + // + // - Either there was already a previous commit in the store, in which + // case we increment the version from there. + // - Or there was no previous commit, in which case we start at version 1. + expectedHeight = lastBlockHeight + 1 } if req.Header.Height != expectedHeight {