From 3c0cafa9e64a7045e059525fd7782f787afa1c92 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 23 May 2023 10:13:05 -0400 Subject: [PATCH 1/2] updates --- baseapp/abci.go | 14 ++++++++++++-- baseapp/abci_test.go | 27 ++++++++++++++++++++------- baseapp/test_helpers.go | 4 ++++ x/auth/ante/sigverify.go | 1 + 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 123f23197039..084838d603b9 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -74,6 +74,16 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC } } + 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 } @@ -119,8 +129,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 674a209b3a08..74f61c5aa11f 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -7,19 +7,17 @@ import ( "strings" "testing" - dbm "github.com/cosmos/cosmos-db" - - abci "github.com/cometbft/cometbft/abci/types" - cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cosmos/gogoproto/jsonpb" - "github.com/stretchr/testify/require" - errorsmod "cosmossdk.io/errors" "cosmossdk.io/log" pruningtypes "cosmossdk.io/store/pruning/types" "cosmossdk.io/store/snapshots" snapshottypes "cosmossdk.io/store/snapshots/types" storetypes "cosmossdk.io/store/types" + abci "github.com/cometbft/cometbft/abci/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + dbm "github.com/cosmos/cosmos-db" + "github.com/cosmos/gogoproto/jsonpb" + "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/baseapp" baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil" @@ -44,6 +42,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 d3f24d6ef419..37be5ed02c2a 100644 --- a/baseapp/test_helpers.go +++ b/baseapp/test_helpers.go @@ -55,3 +55,7 @@ func (app *BaseApp) NewUncachedContext(isCheckTx bool, header cmtproto.Header) s 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) +} diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 6021842b2a8f..16e042c0e9a1 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -11,6 +11,7 @@ import ( errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" txsigning "cosmossdk.io/x/tx/signing" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" From 50135af2dc19dcb4ce170ed77cb1c7de902c17da Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Tue, 23 May 2023 10:21:20 -0400 Subject: [PATCH 2/2] updates --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddccd6a9b001..20441f53c988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -239,6 +239,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (baseapp) [#16259](https://github.com/cosmos/cosmos-sdk/pull/16259) Ensure the `Context` block height is correct after `InitChain` and prior to the second block. * (x/staking) [#16043](https://github.com/cosmos/cosmos-sdk/pull/16043) Call `AfterUnbondingInitiated` hook for new unbonding entries only and fix `UnbondingDelegation` entries handling * (types) [#16010](https://github.com/cosmos/cosmos-sdk/pull/16010) Let `module.CoreAppModuleBasicAdaptor` fallback to legacy genesis handling. * (x/group) [#16017](https://github.com/cosmos/cosmos-sdk/pull/16017) Correctly apply account number in group v2 migration.