diff --git a/PENDING.md b/PENDING.md index 338afcbacda9..721ae1d43688 100644 --- a/PENDING.md +++ b/PENDING.md @@ -44,6 +44,7 @@ * #3808 `gaiad` and `gaiacli` integration tests use ./build/ binaries. ### SDK +* #3801 `baseapp` saftey improvements ### Tendermint diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index b6694a503209..8b30f4a7395c 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -287,12 +287,25 @@ func (app *BaseApp) storeConsensusParams(consensusParams *abci.ConsensusParams) mainStore.Set(mainConsensusParamsKey, consensusParamsBz) } -// getMaximumBlockGas gets the maximum gas from the consensus params. -func (app *BaseApp) getMaximumBlockGas() (maxGas uint64) { +// getMaximumBlockGas gets the maximum gas from the consensus params. It panics +// if maximum block gas is less than negative one and returns zero if negative +// one. +func (app *BaseApp) getMaximumBlockGas() uint64 { if app.consensusParams == nil || app.consensusParams.BlockSize == nil { return 0 } - return uint64(app.consensusParams.BlockSize.MaxGas) + + maxGas := app.consensusParams.BlockSize.MaxGas + switch { + case maxGas < -1: + panic(fmt.Sprintf("invalid maximum block gas: %d", maxGas)) + + case maxGas == -1: + return 0 + + default: + return uint64(maxGas) + } } // ---------------------------------------------------------------------------- @@ -510,6 +523,19 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res } } +func (app *BaseApp) validateHeight(req abci.RequestBeginBlock) error { + if req.Header.Height < 1 { + return fmt.Errorf("invalid height: %d", req.Header.Height) + } + + prevHeight := app.LastBlockHeight() + if req.Header.Height != prevHeight+1 { + return fmt.Errorf("invalid height: %d; expected: %d", req.Header.Height, prevHeight+1) + } + + return nil +} + // BeginBlock implements the ABCI application interface. func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { if app.cms.TracingEnabled() { @@ -518,6 +544,10 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg )) } + if err := app.validateHeight(req); err != nil { + panic(err) + } + // Initialize the DeliverTx state. If this is the first block, it should // already be initialized in InitChain. Otherwise app.deliverState will be // nil, since it is reset on Commit. diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index c1fca6f91939..4a4639f72167 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -296,6 +296,7 @@ func TestInitChainer(t *testing.T) { // stores are mounted and private members are set - sealing baseapp err := app.LoadLatestVersion(capKey) // needed to make stores non-nil require.Nil(t, err) + require.Equal(t, int64(0), app.LastBlockHeight()) app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}"), ChainId: "test-chain-id"}) // must have valid JSON genesis file, even if empty @@ -308,6 +309,7 @@ func TestInitChainer(t *testing.T) { app.Commit() res = app.Query(query) + require.Equal(t, int64(1), app.LastBlockHeight()) require.Equal(t, value, res.Value) // reload app @@ -316,14 +318,17 @@ func TestInitChainer(t *testing.T) { app.MountStores(capKey, capKey2) err = app.LoadLatestVersion(capKey) // needed to make stores non-nil require.Nil(t, err) + require.Equal(t, int64(1), app.LastBlockHeight()) // ensure we can still query after reloading res = app.Query(query) require.Equal(t, value, res.Value) // commit and ensure we can still query - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: app.LastBlockHeight() + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) app.Commit() + res = app.Query(query) require.Equal(t, value, res.Value) } @@ -514,7 +519,6 @@ func TestCheckTx(t *testing.T) { app := setupBaseApp(t, anteOpt, routerOpt) nTxs := int64(5) - app.InitChain(abci.RequestInitChain{}) // Create same codec used in txDecoder @@ -536,7 +540,8 @@ func TestCheckTx(t *testing.T) { require.Equal(t, nTxs, storedCounter) // If a block is committed, CheckTx state should be reset. - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) app.EndBlock(abci.RequestEndBlock{}) app.Commit() @@ -567,16 +572,22 @@ func TestDeliverTx(t *testing.T) { nBlocks := 3 txPerHeight := 5 + for blockN := 0; blockN < nBlocks; blockN++ { - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: int64(blockN) + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) + for i := 0; i < txPerHeight; i++ { counter := int64(blockN*txPerHeight + i) tx := newTxCounter(counter, counter) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) require.NoError(t, err) + res := app.DeliverTx(txBytes) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) } + app.EndBlock(abci.RequestEndBlock{}) app.Commit() } @@ -610,48 +621,47 @@ func TestMultiMsgDeliverTx(t *testing.T) { // run a multi-msg tx // with all msgs the same route - { - app.BeginBlock(abci.RequestBeginBlock{}) - tx := newTxCounter(0, 0, 1, 2) - txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) - require.NoError(t, err) - res := app.DeliverTx(txBytes) - require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - store := app.deliverState.ctx.KVStore(capKey1) + header := abci.Header{Height: 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) + tx := newTxCounter(0, 0, 1, 2) + txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) + require.NoError(t, err) + res := app.DeliverTx(txBytes) + require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) + + store := app.deliverState.ctx.KVStore(capKey1) - // tx counter only incremented once - txCounter := getIntFromStore(store, anteKey) - require.Equal(t, int64(1), txCounter) + // tx counter only incremented once + txCounter := getIntFromStore(store, anteKey) + require.Equal(t, int64(1), txCounter) - // msg counter incremented three times - msgCounter := getIntFromStore(store, deliverKey) - require.Equal(t, int64(3), msgCounter) - } + // msg counter incremented three times + msgCounter := getIntFromStore(store, deliverKey) + require.Equal(t, int64(3), msgCounter) // replace the second message with a msgCounter2 - { - tx := newTxCounter(1, 3) - tx.Msgs = append(tx.Msgs, msgCounter2{0}) - tx.Msgs = append(tx.Msgs, msgCounter2{1}) - txBytes, err := codec.MarshalBinaryLengthPrefixed(tx) - require.NoError(t, err) - res := app.DeliverTx(txBytes) - require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - store := app.deliverState.ctx.KVStore(capKey1) + tx = newTxCounter(1, 3) + tx.Msgs = append(tx.Msgs, msgCounter2{0}) + tx.Msgs = append(tx.Msgs, msgCounter2{1}) + txBytes, err = codec.MarshalBinaryLengthPrefixed(tx) + require.NoError(t, err) + res = app.DeliverTx(txBytes) + require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) + + store = app.deliverState.ctx.KVStore(capKey1) - // tx counter only incremented once - txCounter := getIntFromStore(store, anteKey) - require.Equal(t, int64(2), txCounter) + // tx counter only incremented once + txCounter = getIntFromStore(store, anteKey) + require.Equal(t, int64(2), txCounter) - // original counter increments by one - // new counter increments by two - msgCounter := getIntFromStore(store, deliverKey) - require.Equal(t, int64(4), msgCounter) - msgCounter2 := getIntFromStore(store, deliverKey2) - require.Equal(t, int64(2), msgCounter2) - } + // original counter increments by one + // new counter increments by two + msgCounter = getIntFromStore(store, deliverKey) + require.Equal(t, int64(4), msgCounter) + msgCounter2 := getIntFromStore(store, deliverKey2) + require.Equal(t, int64(2), msgCounter2) } // Interleave calls to Check and Deliver and ensure @@ -692,7 +702,8 @@ func TestSimulateTx(t *testing.T) { nBlocks := 3 for blockN := 0; blockN < nBlocks; blockN++ { count := int64(blockN + 1) - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: count} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) tx := newTxCounter(count, count) txBytes, err := cdc.MarshalBinaryLengthPrefixed(tx) @@ -737,7 +748,9 @@ func TestRunInvalidTransaction(t *testing.T) { } app := setupBaseApp(t, anteOpt, routerOpt) - app.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) // Transaction with no messages { @@ -847,7 +860,8 @@ func TestTxGasLimits(t *testing.T) { app := setupBaseApp(t, anteOpt, routerOpt) - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) testCases := []struct { tx *txTest @@ -962,7 +976,8 @@ func TestMaxBlockGasLimits(t *testing.T) { tx := tc.tx // reset the block gas - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: app.LastBlockHeight() + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) // execute the transaction multiple times for j := 0; j < tc.numDelivers; j++ { @@ -1005,7 +1020,9 @@ func TestBaseAppAnteHandler(t *testing.T) { app.InitChain(abci.RequestInitChain{}) registerTestCodec(cdc) - app.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: app.LastBlockHeight() + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) // execute a tx that will fail ante handler execution // @@ -1112,7 +1129,9 @@ func TestGasConsumptionBadTx(t *testing.T) { }) app.InitChain(abci.RequestInitChain{}) - app.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: app.LastBlockHeight() + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) tx := newTxCounter(5, 0) tx.setFailOnAnte(true) @@ -1174,7 +1193,9 @@ func TestQuery(t *testing.T) { require.Equal(t, 0, len(res.Value)) // query is still empty after a DeliverTx before we commit - app.BeginBlock(abci.RequestBeginBlock{}) + header := abci.Header{Height: app.LastBlockHeight() + 1} + app.BeginBlock(abci.RequestBeginBlock{Header: header}) + resTx = app.Deliver(tx) require.True(t, resTx.IsOK(), fmt.Sprintf("%v", resTx)) res = app.Query(query) @@ -1216,3 +1237,19 @@ func TestP2PQuery(t *testing.T) { res = app.Query(idQuery) require.Equal(t, uint32(4), res.Code) } + +func TestGetMaximumBlockGas(t *testing.T) { + app := setupBaseApp(t) + + app.setConsensusParams(&abci.ConsensusParams{BlockSize: &abci.BlockSizeParams{MaxGas: 0}}) + require.Equal(t, uint64(0), app.getMaximumBlockGas()) + + app.setConsensusParams(&abci.ConsensusParams{BlockSize: &abci.BlockSizeParams{MaxGas: -1}}) + require.Equal(t, uint64(0), app.getMaximumBlockGas()) + + app.setConsensusParams(&abci.ConsensusParams{BlockSize: &abci.BlockSizeParams{MaxGas: 5000000}}) + require.Equal(t, uint64(5000000), app.getMaximumBlockGas()) + + app.setConsensusParams(&abci.ConsensusParams{BlockSize: &abci.BlockSizeParams{MaxGas: -5000000}}) + require.Panics(t, func() { app.getMaximumBlockGas() }) +} diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 8703d71ec7fb..951a1746932b 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -123,7 +123,8 @@ func TestSendNotEnoughBalance(t *testing.T) { origSeq := res1.GetSequence() sendMsg := NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 100)}) - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{sendMsg}, []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{sendMsg}, []uint64{origAccNum}, []uint64{origSeq}, false, false, priv1) mock.CheckBalance(t, mapp, addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 67)}) @@ -173,7 +174,8 @@ func TestMsgMultiSendWithAccounts(t *testing.T) { } for _, tc := range testCases { - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) for _, eb := range tc.expectedBalances { mock.CheckBalance(t, mapp, eb.addr, eb.coins) @@ -212,7 +214,8 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { } for _, tc := range testCases { - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) for _, eb := range tc.expectedBalances { mock.CheckBalance(t, mapp, eb.addr, eb.coins) @@ -241,7 +244,7 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) { testCases := []appTestCase{ { msgs: []sdk.Msg{multiSendMsg3}, - accNums: []uint64{0, 0}, + accNums: []uint64{0, 2}, accSeqs: []uint64{0, 0}, expSimPass: true, expPass: true, @@ -256,7 +259,8 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) { } for _, tc := range testCases { - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) for _, eb := range tc.expectedBalances { mock.CheckBalance(t, mapp, eb.addr, eb.coins) @@ -289,7 +293,7 @@ func TestMsgMultiSendDependent(t *testing.T) { }, { msgs: []sdk.Msg{multiSendMsg4}, - accNums: []uint64{0}, + accNums: []uint64{1}, accSeqs: []uint64{0}, expSimPass: true, expPass: true, @@ -301,7 +305,8 @@ func TestMsgMultiSendDependent(t *testing.T) { } for _, tc := range testCases { - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, tc.msgs, tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) for _, eb := range tc.expectedBalances { mock.CheckBalance(t, mapp, eb.addr, eb.coins) diff --git a/x/gov/endblocker_test.go b/x/gov/endblocker_test.go index d4847b764f66..624fc3e54d90 100644 --- a/x/gov/endblocker_test.go +++ b/x/gov/endblocker_test.go @@ -13,7 +13,10 @@ import ( func TestTickExpiredDepositPeriod(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) keeper.ck.SetSendEnabled(ctx, true) govHandler := NewHandler(keeper) @@ -56,7 +59,10 @@ func TestTickExpiredDepositPeriod(t *testing.T) { func TestTickMultipleExpiredDepositPeriod(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) keeper.ck.SetSendEnabled(ctx, true) govHandler := NewHandler(keeper) @@ -113,7 +119,10 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) { func TestTickPassedDepositPeriod(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) keeper.ck.SetSendEnabled(ctx, true) govHandler := NewHandler(keeper) @@ -156,7 +165,10 @@ func TestTickPassedDepositPeriod(t *testing.T) { func TestTickPassedVotingPeriod(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) SortAddresses(addrs) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) keeper.ck.SetSendEnabled(ctx, true) govHandler := NewHandler(keeper) diff --git a/x/gov/genesis_test.go b/x/gov/genesis_test.go index 5624c58ae6cd..ef19aa300146 100644 --- a/x/gov/genesis_test.go +++ b/x/gov/genesis_test.go @@ -27,7 +27,10 @@ func TestEqualProposals(t *testing.T) { // Generate mock app and keepers mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil) SortAddresses(addrs) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) // Create two proposals @@ -56,11 +59,13 @@ func TestEqualProposals(t *testing.T) { } func TestImportExportQueues(t *testing.T) { - // Generate mock app and keepers mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil) SortAddresses(addrs) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) // Create two proposals, put the second into the voting period @@ -82,7 +87,9 @@ func TestImportExportQueues(t *testing.T) { genState := ExportGenesis(ctx, keeper) mapp2, keeper2, _, _, _, _ := getMockApp(t, 2, genState, genAccs) - mapp2.BeginBlock(abci.RequestBeginBlock{}) + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp2.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx2 := mapp2.BaseApp.NewContext(false, abci.Header{}) // Jump the time forward past the DepositPeriod and VotingPeriod diff --git a/x/gov/keeper_test.go b/x/gov/keeper_test.go index 6fdf6d4e7915..b7694f8da53a 100644 --- a/x/gov/keeper_test.go +++ b/x/gov/keeper_test.go @@ -13,7 +13,10 @@ import ( func TestGetSetProposal(t *testing.T) { mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) @@ -26,7 +29,10 @@ func TestGetSetProposal(t *testing.T) { func TestIncrementProposalNumber(t *testing.T) { mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) @@ -41,9 +47,11 @@ func TestIncrementProposalNumber(t *testing.T) { func TestActivateVotingPeriod(t *testing.T) { mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) - ctx := mapp.BaseApp.NewContext(false, abci.Header{}) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) require.True(t, proposal.GetVotingStartTime().Equal(time.Time{})) @@ -63,9 +71,11 @@ func TestActivateVotingPeriod(t *testing.T) { func TestDeposits(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil) SortAddresses(addrs) - mapp.BeginBlock(abci.RequestBeginBlock{}) - ctx := mapp.BaseApp.NewContext(false, abci.Header{}) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) proposalID := proposal.GetProposalID() @@ -149,9 +159,11 @@ func TestDeposits(t *testing.T) { func TestVotes(t *testing.T) { mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil) SortAddresses(addrs) - mapp.BeginBlock(abci.RequestBeginBlock{}) - ctx := mapp.BaseApp.NewContext(false, abci.Header{}) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText) proposalID := proposal.GetProposalID() @@ -204,7 +216,10 @@ func TestVotes(t *testing.T) { func TestProposalQueues(t *testing.T) { mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) mapp.InitChainer(ctx, abci.RequestInitChain{}) diff --git a/x/gov/tally_test.go b/x/gov/tally_test.go index cf89293eeb1c..47077d52d8d7 100644 --- a/x/gov/tally_test.go +++ b/x/gov/tally_test.go @@ -38,7 +38,10 @@ func createValidators(t *testing.T, stakingHandler sdk.Handler, ctx sdk.Context, func TestTallyNoOneVotes(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -63,7 +66,10 @@ func TestTallyNoOneVotes(t *testing.T) { func TestTallyNoQuorum(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -89,7 +95,10 @@ func TestTallyNoQuorum(t *testing.T) { func TestTallyOnlyValidatorsAllYes(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -119,7 +128,10 @@ func TestTallyOnlyValidatorsAllYes(t *testing.T) { func TestTallyOnlyValidators51No(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -148,7 +160,10 @@ func TestTallyOnlyValidators51No(t *testing.T) { func TestTallyOnlyValidators51Yes(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -180,7 +195,10 @@ func TestTallyOnlyValidators51Yes(t *testing.T) { func TestTallyOnlyValidatorsVetoed(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -212,7 +230,10 @@ func TestTallyOnlyValidatorsVetoed(t *testing.T) { func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -244,7 +265,10 @@ func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) { func TestTallyOnlyValidatorsAbstainFails(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -276,7 +300,10 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) { func TestTallyOnlyValidatorsNonVoter(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -306,7 +333,10 @@ func TestTallyOnlyValidatorsNonVoter(t *testing.T) { func TestTallyDelgatorOverride(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -344,7 +374,10 @@ func TestTallyDelgatorOverride(t *testing.T) { func TestTallyDelgatorInherit(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -380,7 +413,10 @@ func TestTallyDelgatorInherit(t *testing.T) { func TestTallyDelgatorMultipleOverride(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -420,7 +456,10 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) { func TestTallyDelgatorMultipleInherit(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) @@ -471,7 +510,10 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) { func TestTallyJailedValidator(t *testing.T) { mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) + ctx := mapp.BaseApp.NewContext(false, abci.Header{}) stakingHandler := staking.NewHandler(sk) diff --git a/x/ibc/app_test.go b/x/ibc/app_test.go index 0b1e7d9da7cc..ca51ae9d8b6f 100644 --- a/x/ibc/app_test.go +++ b/x/ibc/app_test.go @@ -72,10 +72,17 @@ func TestIBCMsgs(t *testing.T) { Sequence: 0, } - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{transferMsg}, []uint64{0}, []uint64{0}, true, true, priv1) + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{transferMsg}, []uint64{0}, []uint64{0}, true, true, priv1) mock.CheckBalance(t, mapp, addr1, emptyCoins) - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{transferMsg}, []uint64{0}, []uint64{1}, false, false, priv1) - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{receiveMsg}, []uint64{0}, []uint64{2}, true, true, priv1) + + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{transferMsg}, []uint64{0}, []uint64{1}, false, false, priv1) + + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{receiveMsg}, []uint64{0}, []uint64{2}, true, true, priv1) mock.CheckBalance(t, mapp, addr1, coins) - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{receiveMsg}, []uint64{0}, []uint64{2}, false, false, priv1) + + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{receiveMsg}, []uint64{0}, []uint64{2}, false, false, priv1) } diff --git a/x/mock/app_test.go b/x/mock/app_test.go index a101168381e1..7a2b6adfd41e 100644 --- a/x/mock/app_test.go +++ b/x/mock/app_test.go @@ -60,15 +60,17 @@ func TestCheckAndDeliverGenTx(t *testing.T) { acct := mApp.AccountKeeper.GetAccount(ctxCheck, addrs[0]) require.Equal(t, accs[0], acct.(*auth.BaseAccount)) + header := abci.Header{Height: mApp.LastBlockHeight() + 1} SignCheckDeliver( - t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{msg}, + t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{msg}, []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence()}, true, true, privKeys[0], ) // Signing a tx with the wrong privKey should result in an auth error + header = abci.Header{Height: mApp.LastBlockHeight() + 1} res := SignCheckDeliver( - t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{msg}, + t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{msg}, []uint64{accs[1].GetAccountNumber()}, []uint64{accs[1].GetSequence() + 1}, true, false, privKeys[1], ) @@ -77,8 +79,9 @@ func TestCheckAndDeliverGenTx(t *testing.T) { require.Equal(t, sdk.CodespaceRoot, res.Codespace) // Resigning the tx with the correct privKey should result in an OK result + header = abci.Header{Height: mApp.LastBlockHeight() + 1} SignCheckDeliver( - t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{msg}, + t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{msg}, []uint64{accs[0].GetAccountNumber()}, []uint64{accs[0].GetSequence() + 1}, true, true, privKeys[0], ) diff --git a/x/mock/test_utils.go b/x/mock/test_utils.go index 9032d1f3d6db..2b5978823363 100644 --- a/x/mock/test_utils.go +++ b/x/mock/test_utils.go @@ -72,9 +72,10 @@ func CheckGenTx( // block commitment with the given transaction. A test assertion is made using // the parameter 'expPass' against the result. A corresponding result is // returned. -func SignCheckDeliver(t *testing.T, cdc *codec.Codec, app *baseapp.BaseApp, - msgs []sdk.Msg, accNums, seq []uint64, expSimPass, expPass bool, - priv ...crypto.PrivKey) sdk.Result { +func SignCheckDeliver( + t *testing.T, cdc *codec.Codec, app *baseapp.BaseApp, header abci.Header, msgs []sdk.Msg, + accNums, seq []uint64, expSimPass, expPass bool, priv ...crypto.PrivKey, +) sdk.Result { tx := GenTx(msgs, accNums, seq, priv...) @@ -91,7 +92,7 @@ func SignCheckDeliver(t *testing.T, cdc *codec.Codec, app *baseapp.BaseApp, } // Simulate a sending a transaction and committing a block - app.BeginBlock(abci.RequestBeginBlock{}) + app.BeginBlock(abci.RequestBeginBlock{Header: header}) res = app.Deliver(tx) if expPass { diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index 6f15e4aeef93..94d53f5889c4 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -111,9 +111,13 @@ func TestSlashingMsgs(t *testing.T) { createValidatorMsg := staking.NewMsgCreateValidator( sdk.ValAddress(addr1), priv1.PubKey(), bondCoin, description, commission, sdk.OneInt(), ) - mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1) + + header := abci.Header{Height: mapp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1) mock.CheckBalance(t, mapp, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) - mapp.BeginBlock(abci.RequestBeginBlock{}) + + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) validator := checkValidator(t, mapp, stakingKeeper, addr1, true) require.Equal(t, sdk.ValAddress(addr1), validator.OperatorAddress) @@ -125,7 +129,8 @@ func TestSlashingMsgs(t *testing.T) { checkValidatorSigningInfo(t, mapp, keeper, sdk.ConsAddress(addr1), false) // unjail should fail with unknown validator - res := mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, []sdk.Msg{unjailMsg}, []uint64{0}, []uint64{1}, false, false, priv1) + header = abci.Header{Height: mapp.LastBlockHeight() + 1} + res := mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{unjailMsg}, []uint64{0}, []uint64{1}, false, false, priv1) require.EqualValues(t, CodeValidatorNotJailed, res.Code) require.EqualValues(t, DefaultCodespace, res.Codespace) } diff --git a/x/staking/app_test.go b/x/staking/app_test.go index d4481cef73dc..8eea9daf9cae 100644 --- a/x/staking/app_test.go +++ b/x/staking/app_test.go @@ -122,22 +122,28 @@ func TestStakingMsgs(t *testing.T) { sdk.ValAddress(addr1), priv1.PubKey(), bondCoin, description, commissionMsg, sdk.OneInt(), ) - mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1) + header := abci.Header{Height: mApp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1) mock.CheckBalance(t, mApp, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) - mApp.BeginBlock(abci.RequestBeginBlock{}) + + header = abci.Header{Height: mApp.LastBlockHeight() + 1} + mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) validator := checkValidator(t, mApp, keeper, sdk.ValAddress(addr1), true) require.Equal(t, sdk.ValAddress(addr1), validator.OperatorAddress) require.Equal(t, sdk.Bonded, validator.Status) require.True(sdk.IntEq(t, bondTokens, validator.BondedTokens())) - mApp.BeginBlock(abci.RequestBeginBlock{}) + header = abci.Header{Height: mApp.LastBlockHeight() + 1} + mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) // edit the validator description = NewDescription("bar_moniker", "", "", "") editValidatorMsg := NewMsgEditValidator(sdk.ValAddress(addr1), description, nil, nil) - mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{editValidatorMsg}, []uint64{0}, []uint64{1}, true, true, priv1) + header = abci.Header{Height: mApp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{editValidatorMsg}, []uint64{0}, []uint64{1}, true, true, priv1) + validator = checkValidator(t, mApp, keeper, sdk.ValAddress(addr1), true) require.Equal(t, description, validator.Description) @@ -145,13 +151,15 @@ func TestStakingMsgs(t *testing.T) { mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin}) delegateMsg := NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin) - mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{delegateMsg}, []uint64{0}, []uint64{0}, true, true, priv2) + header = abci.Header{Height: mApp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{delegateMsg}, []uint64{1}, []uint64{0}, true, true, priv2) mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin.Sub(bondCoin)}) checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), true, bondTokens.ToDec()) // begin unbonding beginUnbondingMsg := NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondTokens.ToDec()) - mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{beginUnbondingMsg}, []uint64{0}, []uint64{1}, true, true, priv2) + header = abci.Header{Height: mApp.LastBlockHeight() + 1} + mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, []uint64{1}, []uint64{1}, true, true, priv2) // delegation should exist anymore checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), false, sdk.Dec{})