From 0fd6227a06bda30166d5ef43046b5ea36ccf3645 Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Thu, 6 Jul 2023 15:33:10 +0200 Subject: [PATCH] fix: remove setting of finalizeBlockState in FinalizeBlock + fix initialHeight + add ProcessProposal in tests/sims (#16794) Co-authored-by: Aleksandr Bezobchuk --- baseapp/abci.go | 54 +++++++---------- baseapp/abci_test.go | 28 ++++++++- baseapp/baseapp_test.go | 26 +++++++- baseapp/msg_service_router_test.go | 2 + baseapp/streaming_test.go | 3 +- server/mock/app_test.go | 9 ++- simapp/test_helpers.go | 1 - tests/e2e/server/export_test.go | 4 ++ tests/integration/bank/app_test.go | 60 ++++++++++++++++--- .../distribution/keeper/msg_server_test.go | 13 ++-- .../slashing/keeper/keeper_test.go | 4 +- .../store/rootmulti/rollback_test.go | 15 +++-- testutil/integration/example_test.go | 2 +- testutil/integration/options.go | 12 +++- testutil/integration/router.go | 7 +++ testutil/sims/tx_helpers.go | 3 +- x/simulation/simulate.go | 16 +++++ x/slashing/app_test.go | 5 +- x/staking/app_test.go | 8 +++ 19 files changed, 206 insertions(+), 66 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index 3ef7fa77c090..dc36a9ab4e81 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -45,13 +45,15 @@ func (app *BaseApp) InitChain(req *abci.RequestInitChain) (*abci.ResponseInitCha // On a new chain, we consider the init chain block height as 0, even though // req.InitialHeight is 1 by default. initHeader := cmtproto.Header{ChainID: req.ChainId, Time: req.Time} - app.initialHeight = req.InitialHeight app.logger.Info("InitChain", "initialHeight", req.InitialHeight, "chainID", req.ChainId) // 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 app.initialHeight == 0 { // If initial height is 0, set it to 1 + app.initialHeight = 1 + } // if req.InitialHeight is > 1, then we set the initial version on all stores if req.InitialHeight > 1 { @@ -675,46 +677,32 @@ func (app *BaseApp) FinalizeBlock(req *abci.RequestFinalizeBlock) (*abci.Respons AppHash: app.LastCommitID().Hash, } - // Initialize the FinalizeBlock state. If this is the first block, it should - // already be initialized in InitChain. Otherwise app.finalizeBlockState will be - // nil, since it is reset on Commit. - if app.finalizeBlockState == nil { - app.setState(execModeFinalize, header) - } else { - // In the first block, app.finalizeBlockState.ctx will already be initialized - // by InitChain. Context is now updated with Header information. - app.finalizeBlockState.ctx = app.finalizeBlockState.ctx. - WithBlockHeader(header). - WithBlockHeight(req.Height). - WithHeaderInfo(coreheader.Info{ - ChainID: app.chainID, - Height: req.Height, - Time: req.Time, - Hash: req.Hash, - AppHash: app.LastCommitID().Hash, - }) - } - - gasMeter := app.getBlockGasMeter(app.finalizeBlockState.ctx) - + // app.finalizeBlockState.ctx will already be initialized + // by InitChain or by ProcessProposal. Context is now updated with Header information. app.finalizeBlockState.ctx = app.finalizeBlockState.ctx. - WithBlockGasMeter(gasMeter). + WithBlockHeader(header). WithHeaderHash(req.Hash). - WithConsensusParams(app.GetConsensusParams(app.finalizeBlockState.ctx)). - WithVoteInfos(req.DecidedLastCommit.Votes). - WithExecMode(sdk.ExecModeFinalize). WithHeaderInfo(coreheader.Info{ ChainID: app.chainID, Height: req.Height, Time: req.Time, Hash: req.Hash, AppHash: app.LastCommitID().Hash, - }).WithCometInfo(cometInfo{ - Misbehavior: req.Misbehavior, - ValidatorsHash: req.NextValidatorsHash, - ProposerAddress: req.ProposerAddress, - LastCommit: req.DecidedLastCommit, - }) + }). + WithConsensusParams(app.GetConsensusParams(app.finalizeBlockState.ctx)). + WithVoteInfos(req.DecidedLastCommit.Votes). + WithExecMode(sdk.ExecModeFinalize). + WithCometInfo(cometInfo{ + Misbehavior: req.Misbehavior, + ValidatorsHash: req.NextValidatorsHash, + ProposerAddress: req.ProposerAddress, + LastCommit: req.DecidedLastCommit, + }) + + gasMeter := app.getBlockGasMeter(app.finalizeBlockState.ctx) + + app.finalizeBlockState.ctx = app.finalizeBlockState.ctx. + WithBlockGasMeter(gasMeter) if app.checkState != nil { app.checkState.ctx = app.checkState.ctx. diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 5487f5c371d6..778373571ca0 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -164,6 +164,8 @@ func TestABCI_InitChain(t *testing.T) { require.Equal(t, value, resQ.Value) // commit and ensure we can still query + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: app.LastBlockHeight() + 1}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) _, err = app.Commit() @@ -578,6 +580,12 @@ func TestABCI_FinalizeBlock_DeliverTx(t *testing.T) { txs = append(txs, txBytes) } + _, err := suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{ + Height: int64(blockN) + 1, + Txs: txs, + }) + require.NoError(t, err) + res, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{ Height: int64(blockN) + 1, Txs: txs, @@ -729,6 +737,8 @@ func TestABCI_Query_SimulateTx(t *testing.T) { require.Equal(t, result.Events, simRes.Result.Events) require.True(t, bytes.Equal(result.Data, simRes.Result.Data)) + _, err = suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: count}) + require.NoError(t, err) _, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: count}) require.NoError(t, err) _, err = suite.baseApp.Commit() @@ -897,6 +907,10 @@ func TestABCI_TxGasLimits(t *testing.T) { }) require.NoError(t, err) + _, err = suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{ + Height: 1, + }) + require.NoError(t, err) _, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{ Height: 1, }) @@ -934,6 +948,12 @@ func TestABCI_TxGasLimits(t *testing.T) { } // Deliver the txs + _, err = suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{ + Height: 2, + Txs: txs, + }) + require.NoError(t, err) + res, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{ Height: 2, Txs: txs, @@ -1299,7 +1319,9 @@ func TestPrepareCheckStateCalledWithCheckState(t *testing.T) { wasPrepareCheckStateCalled = true }) - _, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) + _, err := app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) _, err = app.Commit() require.NoError(t, err) @@ -1323,7 +1345,9 @@ func TestPrecommiterCalledWithDeliverState(t *testing.T) { wasPrecommiterCalled = true }) - _, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) + _, err := app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) _, err = app.Commit() require.NoError(t, err) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index ea7c9f5ed66b..9388badfa324 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -138,7 +138,13 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun txs = append(txs, txBytes) } - _, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{ + _, err := suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{ + Height: height, + Txs: txs, + }) + require.NoError(t, err) + + _, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{ Height: height, Txs: txs, }) @@ -190,6 +196,8 @@ func TestLoadVersion(t *testing.T) { require.Equal(t, emptyCommitID, lastID) // execute a block, collect commit ID + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) commitID1 := storetypes.CommitID{Version: 1, Hash: res.AppHash} @@ -197,6 +205,8 @@ func TestLoadVersion(t *testing.T) { require.NoError(t, err) // execute a block, collect commit ID + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 2}) + require.NoError(t, err) res, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 2}) require.NoError(t, err) commitID2 := storetypes.CommitID{Version: 2, Hash: res.AppHash} @@ -220,6 +230,8 @@ func TestLoadVersion(t *testing.T) { testLoadVersionHelper(t, app, int64(1), commitID1) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 2}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 2}) require.NoError(t, err) _, err = app.Commit() @@ -308,6 +320,8 @@ func TestSetLoader(t *testing.T) { require.Nil(t, err) // "execute" one block + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 2}) + require.NoError(t, err) res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 2}) require.NoError(t, err) require.NotNil(t, res.AppHash) @@ -357,6 +371,8 @@ func TestLoadVersionInvalid(t *testing.T) { err = app.LoadVersion(-1) require.Error(t, err) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) commitID1 := storetypes.CommitID{Version: 1, Hash: res.AppHash} @@ -576,11 +592,15 @@ func TestABCI_CreateQueryContext(t *testing.T) { name := t.Name() app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil) - _, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) + _, err := app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) _, err = app.Commit() require.NoError(t, err) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 2}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 2}) require.NoError(t, err) _, err = app.Commit() @@ -666,6 +686,8 @@ func TestLoadVersionPruning(t *testing.T) { // Commit seven blocks, of which 7 (latest) is kept in addition to 6, 5 // (keep recent) and 3 (keep every). for i := int64(1); i <= 7; i++ { + _, err := app.ProcessProposal(&abci.RequestProcessProposal{Height: i}) + require.NoError(t, err) res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: i}) require.NoError(t, err) _, err = app.Commit() diff --git a/baseapp/msg_service_router_test.go b/baseapp/msg_service_router_test.go index 23acbe99409b..ec87767ebd01 100644 --- a/baseapp/msg_service_router_test.go +++ b/baseapp/msg_service_router_test.go @@ -115,6 +115,8 @@ func TestMsgService(t *testing.T) { app.MsgServiceRouter(), testdata.MsgServerImpl{}, ) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: 1}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) diff --git a/baseapp/streaming_test.go b/baseapp/streaming_test.go index 68ab1320ef1f..2f5953047e6b 100644 --- a/baseapp/streaming_test.go +++ b/baseapp/streaming_test.go @@ -69,7 +69,7 @@ func TestABCI_MultiListener_StateChanges(t *testing.T) { var expectedChangeSet []*storetypes.StoreKVPair // create final block context state - _, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: int64(blockN) + 1, Txs: txs}) + _, err := suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: int64(blockN) + 1, Txs: txs}) require.NoError(t, err) for i := 0; i < txPerHeight; i++ { @@ -133,6 +133,7 @@ func Test_Ctx_with_StreamingManager(t *testing.T) { for blockN := 0; blockN < nBlocks; blockN++ { + suite.baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: int64(blockN) + 1}) suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: int64(blockN) + 1}) ctx := getFinalizeBlockStateCtx(suite.baseApp) diff --git a/server/mock/app_test.go b/server/mock/app_test.go index bb5bd906cc2e..f7253a17dab3 100644 --- a/server/mock/app_test.go +++ b/server/mock/app_test.go @@ -60,7 +60,7 @@ func TestInitApp(t *testing.T) { require.Equal(t, []byte("bar"), qres.Value) } -func TestDeliverTx(t *testing.T) { +func TestFinalizeBlock(t *testing.T) { app := SetupApp(t) key := "my-special-key" @@ -72,6 +72,13 @@ func TestDeliverTx(t *testing.T) { tx := NewTx(key, value, randomAccounts[0].Address) txBytes := tx.GetSignBytes() + _, err := app.ProcessProposal(&abci.RequestProcessProposal{ + Hash: []byte("apphash"), + Height: 1, + Txs: [][]byte{txBytes}, + }) + require.NoError(t, err) + res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{ Hash: []byte("apphash"), Height: 1, diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index 7043f2eba3bf..594619c8e8d1 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -70,7 +70,6 @@ func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptio Address: acc.GetAddress().String(), Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))), } - app := NewSimApp(options.Logger, options.DB, nil, true, options.AppOpts) genesisState := app.DefaultGenesis() genesisState, err = simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, []authtypes.GenesisAccount{acc}, balance) diff --git a/tests/e2e/server/export_test.go b/tests/e2e/server/export_test.go index 45616e7029b5..fac1514e3c88 100644 --- a/tests/e2e/server/export_test.go +++ b/tests/e2e/server/export_test.go @@ -95,6 +95,10 @@ func TestExportCmd_Height(t *testing.T) { // Fast forward to block `tc.fastForward`. for i := int64(2); i <= tc.fastForward; i++ { + app.ProcessProposal(&abci.RequestProcessProposal{ + Height: i, + }) + app.FinalizeBlock(&abci.RequestFinalizeBlock{ Height: i, }) diff --git a/tests/integration/bank/app_test.go b/tests/integration/bank/app_test.go index da860ca21e8b..cf0a883dd8bd 100644 --- a/tests/integration/bank/app_test.go +++ b/tests/integration/bank/app_test.go @@ -141,7 +141,7 @@ func checkBalance(t *testing.T, baseApp *baseapp.BaseApp, addr sdk.AccAddress, b t.Helper() ctxCheck := baseApp.NewContext(true) keeperBalances := keeper.GetAllBalances(ctxCheck, addr) - require.True(t, balances.Equal(keeperBalances)) + require.Truef(t, balances.Equal(keeperBalances), "expected %v, got %v", balances, keeperBalances) } func TestSendNotEnoughBalance(t *testing.T) { @@ -152,10 +152,16 @@ func TestSendNotEnoughBalance(t *testing.T) { genAccs := []authtypes.GenesisAccount{acc} s := createTestSuite(t, genAccs) baseApp := s.App.BaseApp - ctx := baseApp.NewContext(false) + _, err := baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) + + // context must be taken after InitChain/ProcessProposal, otherwise it's nil + ctx := baseApp.NewContext(false) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) - _, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) + + // FinalizeBlock call is needed because `app.finalizeBlockState.ms.Write()` is called + _, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) require.NoError(t, err) _, err = baseApp.Commit() require.NoError(t, err) @@ -170,6 +176,9 @@ func TestSendNotEnoughBalance(t *testing.T) { sendMsg := types.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)}) header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1} txConfig := moduletestutil.MakeTestTxConfig() + + _, err = baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) _, _, err = simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, []sdk.Msg{sendMsg}, "", []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1) require.Error(t, err) @@ -191,10 +200,13 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { genAccs := []authtypes.GenesisAccount{acc} s := createTestSuite(t, genAccs) baseApp := s.App.BaseApp + + _, err := baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) ctx := baseApp.NewContext(false) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67)))) - _, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) + _, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) require.NoError(t, err) _, err = baseApp.Commit() require.NoError(t, err) @@ -250,7 +262,11 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { t.Logf("testing %s", tc.desc) header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1} txConfig := moduletestutil.MakeTestTxConfig() - _, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + + _, err := baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) + require.NoError(t, err) + + _, _, err = simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) if tc.expPass { require.NoError(t, err) } else { @@ -274,11 +290,14 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { genAccs := []authtypes.GenesisAccount{acc1, acc2} s := createTestSuite(t, genAccs) baseApp := s.App.BaseApp - ctx := baseApp.NewContext(false) + _, err := baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) + + ctx := baseApp.NewContext(false) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - _, err := baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) + _, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) require.NoError(t, err) _, err = baseApp.Commit() require.NoError(t, err) @@ -302,7 +321,11 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { for _, tc := range testCases { header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1} txConfig := moduletestutil.MakeTestTxConfig() - _, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + + _, err := baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) + + _, _, err = simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) require.NoError(t, err) for _, eb := range tc.expectedBalances { @@ -320,8 +343,10 @@ func TestMsgMultiSendDependent(t *testing.T) { genAccs := []authtypes.GenesisAccount{acc1, acc2} s := createTestSuite(t, genAccs) baseApp := s.App.BaseApp - ctx := baseApp.NewContext(false) + _, err = baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: baseApp.LastBlockHeight() + 1}) + require.NoError(t, err) + ctx := baseApp.NewContext(false) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) _, err = baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: baseApp.LastBlockHeight() + 1}) require.NoError(t, err) @@ -357,6 +382,10 @@ func TestMsgMultiSendDependent(t *testing.T) { for _, tc := range testCases { header := cmtproto.Header{Height: baseApp.LastBlockHeight() + 1} txConfig := moduletestutil.MakeTestTxConfig() + + _, err = baseApp.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) + require.NoError(t, err) + _, _, err := simtestutil.SignCheckDeliver(t, txConfig, baseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) require.NoError(t, err) @@ -372,8 +401,17 @@ func TestMsgSetSendEnabled(t *testing.T) { genAccs := []authtypes.GenesisAccount{acc1} s := createTestSuite(t, genAccs) + _, err := s.App.ProcessProposal(&abci.RequestProcessProposal{Height: s.App.LastBlockHeight() + 1}) + require.NoError(t, err) + ctx := s.App.BaseApp.NewContext(false) require.NoError(t, testutil.FundAccount(ctx, s.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 101)))) + + _, err = s.App.FinalizeBlock(&abci.RequestFinalizeBlock{Height: s.App.LastBlockHeight() + 1}) + require.NoError(t, err) + _, err = s.App.Commit() + require.NoError(t, err) + addr1Str := addr1.String() govAddr := s.BankKeeper.GetAuthority() goodGovProp, err := govv1.NewMsgSubmitProposal( @@ -436,6 +474,10 @@ func TestMsgSetSendEnabled(t *testing.T) { t.Run(tc.desc, func(tt *testing.T) { header := cmtproto.Header{Height: s.App.LastBlockHeight() + 1} txGen := moduletestutil.MakeTestTxConfig() + + _, err = s.App.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) + require.NoError(t, err) + _, _, err = simtestutil.SignCheckDeliver(tt, txGen, s.App.BaseApp, header, tc.msgs, "", []uint64{0}, tc.accSeqs, tc.expSimPass, tc.expPass, priv1) if len(tc.expInError) > 0 { require.Error(tt, err) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 3315e2f7a62e..20f0df11f0f3 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -285,6 +285,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, + integration.WithAutomaticProcessProposal(), integration.WithAutomaticFinalizeBlock(), integration.WithAutomaticCommit(), ) @@ -435,7 +436,7 @@ func TestMsgSetWithdrawAddress(t *testing.T) { tc.preRun() res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { @@ -531,7 +532,7 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { @@ -634,7 +635,7 @@ func TestMsgFundCommunityPool(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { @@ -762,7 +763,7 @@ func TestMsgUpdateParams(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { @@ -841,7 +842,7 @@ func TestMsgCommunityPoolSpend(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { @@ -943,7 +944,7 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) { t.Run(tc.name, func(t *testing.T) { res, err := f.app.RunMsg( tc.msg, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) if tc.expErr { diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index 8ee839168d4b..e85516278bd7 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -190,7 +190,7 @@ func TestUnJailNotBonded(t *testing.T) { } _, err = f.app.RunMsg( &msgUnjail, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) assert.ErrorContains(t, err, "cannot be unjailed") @@ -206,7 +206,7 @@ func TestUnJailNotBonded(t *testing.T) { // verify we can immediately unjail _, err = f.app.RunMsg( &msgUnjail, - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), integration.WithAutomaticCommit(), ) assert.NilError(t, err) diff --git a/tests/integration/store/rootmulti/rollback_test.go b/tests/integration/store/rootmulti/rollback_test.go index 9e4454bdcb1b..0680818523e4 100644 --- a/tests/integration/store/rootmulti/rollback_test.go +++ b/tests/integration/store/rootmulti/rollback_test.go @@ -1,6 +1,7 @@ package rootmulti_test import ( + "encoding/json" "fmt" "testing" @@ -24,16 +25,21 @@ func TestRollback(t *testing.T) { } app := simapp.NewSimappWithCustomOptions(t, false, options) ver0 := app.LastBlockHeight() + appStateBz, _ := json.Marshal(app.DefaultGenesis()) + + app.InitChain(&abci.RequestInitChain{ + ConsensusParams: simtestutil.DefaultConsensusParams, + AppStateBytes: appStateBz, + InitialHeight: 1, + }) + // commit 10 blocks for i := int64(1); i <= 10; i++ { header := cmtproto.Header{ Height: ver0 + i, AppHash: app.LastCommitID().Hash, } - - app.FinalizeBlock(&abci.RequestFinalizeBlock{ - Height: header.Height, - }) + app.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) ctx := app.NewContextLegacy(false, header) store := ctx.KVStore(app.GetKey("bank")) store.Set([]byte("key"), []byte(fmt.Sprintf("value%d", i))) @@ -63,6 +69,7 @@ func TestRollback(t *testing.T) { Height: ver0 + i, AppHash: app.LastCommitID().Hash, } + app.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: header.Height}) ctx := app.NewContextLegacy(false, header) store := ctx.KVStore(app.GetKey("bank")) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 83712071b72f..bc8cbcd48441 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -167,7 +167,7 @@ func Example_oneModule() { Params: params, }, // this allows to the begin and end blocker of the module before and after the message - integration.WithAutomaticFinalizeBlock(), + integration.WithAutomaticProcessProposal(), // this allows to commit the state after the message integration.WithAutomaticCommit(), ) diff --git a/testutil/integration/options.go b/testutil/integration/options.go index d2a6aacdb001..955475736dad 100644 --- a/testutil/integration/options.go +++ b/testutil/integration/options.go @@ -2,13 +2,21 @@ package integration // Config is the configuration for the integration app. type Config struct { - AutomaticFinalizeBlock bool - AutomaticCommit bool + AutomaticProcessProposal bool + AutomaticFinalizeBlock bool + AutomaticCommit bool } // Option is a function that can be used to configure the integration app. type Option func(*Config) +// WithAutomaticProcessProposal calls ABCI process proposal. +func WithAutomaticProcessProposal() Option { + return func(cfg *Config) { + cfg.AutomaticProcessProposal = true + } +} + // WithAutomaticFinalizeBlock calls ABCI finalize block. func WithAutomaticFinalizeBlock() Option { return func(cfg *Config) { diff --git a/testutil/integration/router.go b/testutil/integration/router.go index 4270d493ad8c..2ffa702ed9af 100644 --- a/testutil/integration/router.go +++ b/testutil/integration/router.go @@ -132,6 +132,13 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) { defer app.Commit() } + if cfg.AutomaticProcessProposal { + height := app.LastBlockHeight() + 1 + if _, err := app.ProcessProposal(&cmtabcitypes.RequestProcessProposal{Height: height}); err != nil { + return nil, fmt.Errorf("failed to run process proposal: %w", err) + } + } + if cfg.AutomaticFinalizeBlock { height := app.LastBlockHeight() + 1 if _, err := app.FinalizeBlock(&cmtabcitypes.RequestFinalizeBlock{Height: height}); err != nil { diff --git a/testutil/sims/tx_helpers.go b/testutil/sims/tx_helpers.go index 9c8244c347d6..361f6cd01c14 100644 --- a/testutil/sims/tx_helpers.go +++ b/testutil/sims/tx_helpers.go @@ -141,7 +141,8 @@ func SignCheckDeliver( require.False(t, finalizeSuccess) } - app.Commit() + _, err = app.Commit() + require.NoError(t, err) gInfo := sdk.GasInfo{GasWanted: uint64(txResult.GasWanted), GasUsed: uint64(txResult.GasUsed)} txRes := sdk.Result{Data: txResult.Data, Log: txResult.Log, Events: txResult.Events} diff --git a/x/simulation/simulate.go b/x/simulation/simulate.go index 3a118dde7134..0add06f4f7ec 100644 --- a/x/simulation/simulate.go +++ b/x/simulation/simulate.go @@ -42,6 +42,7 @@ func initChain( ChainId: chainID, ConsensusParams: consensusParams, Time: genesisTimestamp, + InitialHeight: int64(config.InitialBlockHeight), } res, err := app.InitChain(&req) if err != nil { @@ -180,6 +181,21 @@ func SimulateFromSeed( // Run the BeginBlock handler logWriter.AddEntry(BeginBlockEntry(blockHeight)) + // Run ProcessProposal to remain compliant with the ABCI spec + _, err := app.ProcessProposal(&abci.RequestProcessProposal{ + Txs: finalizeBlockReq.Txs, + ProposedLastCommit: finalizeBlockReq.DecidedLastCommit, + Misbehavior: finalizeBlockReq.Misbehavior, + Hash: finalizeBlockReq.Hash, + Height: finalizeBlockReq.Height, + Time: finalizeBlockReq.Time, + NextValidatorsHash: finalizeBlockReq.NextValidatorsHash, + ProposerAddress: finalizeBlockReq.ProposerAddress, + }) + if err != nil { + return true, params, err + } + res, err := app.FinalizeBlock(finalizeBlockReq) if err != nil { return true, params, err diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 6c42e1dc9d4e..f3299d1848b9 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -91,7 +91,10 @@ func TestSlashingMsgs(t *testing.T) { require.NoError(t, err) require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr1))) - app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: app.LastBlockHeight() + 1}) + require.NoError(t, err) + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) + require.NoError(t, err) ctxCheck = baseApp.NewContext(true) validator, err := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1)) diff --git a/x/staking/app_test.go b/x/staking/app_test.go index 04a026780d53..87c4ae36d676 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -79,6 +79,8 @@ func TestStakingMsgs(t *testing.T) { require.NoError(t, err) require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.Equal(bankKeeper.GetAllBalances(ctxCheck, addr1))) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: app.LastBlockHeight() + 1}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) ctxCheck = app.BaseApp.NewContext(true) @@ -89,6 +91,8 @@ func TestStakingMsgs(t *testing.T) { require.Equal(t, types.Bonded, validator.Status) require.True(math.IntEq(t, bondTokens, validator.BondedTokens())) + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: app.LastBlockHeight() + 1}) + require.NoError(t, err) _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1}) require.NoError(t, err) @@ -110,6 +114,8 @@ func TestStakingMsgs(t *testing.T) { delegateMsg := types.NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin) header = cmtproto.Header{Height: app.LastBlockHeight() + 1} + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) + require.NoError(t, err) _, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2) require.NoError(t, err) @@ -121,6 +127,8 @@ func TestStakingMsgs(t *testing.T) { // begin unbonding beginUnbondingMsg := types.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondCoin) header = cmtproto.Header{Height: app.LastBlockHeight() + 1} + _, err = app.ProcessProposal(&abci.RequestProcessProposal{Height: header.Height}) + require.NoError(t, err) _, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2) require.NoError(t, err)