From 7b508d08060003828ceb209835f6eb00d127dbf5 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 27 Jul 2023 12:55:10 +0200 Subject: [PATCH 1/4] ree --- baseapp/abci_utils.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/baseapp/abci_utils.go b/baseapp/abci_utils.go index d73fb003b46e..6dacc5407d8f 100644 --- a/baseapp/abci_utils.go +++ b/baseapp/abci_utils.go @@ -231,10 +231,12 @@ func (h DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand if (txSize + totalTxBytes) < req.MaxTxBytes { // If there is a max block gas limit, add the tx only if the limit has // not been met. - if maxBlockGas > 0 && (txGasLimit+totalTxGas) <= uint64(maxBlockGas) { - totalTxGas += txGasLimit - totalTxBytes += txSize - selectedTxs = append(selectedTxs, bz) + if maxBlockGas > 0 { + if (txGasLimit + totalTxGas) <= uint64(maxBlockGas) { + totalTxGas += txGasLimit + totalTxBytes += txSize + selectedTxs = append(selectedTxs, bz) + } } else { totalTxBytes += txSize selectedTxs = append(selectedTxs, bz) From fa147c318a35a6e0196877b21a7d753d389a69cf Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 27 Jul 2023 13:08:29 +0200 Subject: [PATCH 2/4] add test --- baseapp/abci_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 5458a85027c5..a2e6a39713c4 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -1573,6 +1573,47 @@ func TestABCI_PrepareProposal_BadEncoding(t *testing.T) { require.Equal(t, 1, len(resPrepareProposal.Txs)) } +func TestABCI_PrepareProposal_OverGasUnderBytes(t *testing.T) { + pool := mempool.NewSenderNonceMempool() + suite := NewBaseAppSuite(t, baseapp.SetMempool(pool)) + baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), NoopCounterServerImpl{}) + + // set max block gas limit to 99, this will allow 9 txs of 10 gas each, the last one should NOT + // be included + _, err := suite.baseApp.InitChain(&abci.RequestInitChain{ + ConsensusParams: &cmtproto.ConsensusParams{ + Block: &cmtproto.BlockParams{MaxGas: 99}, + }, + }) + require.NoError(t, err) + // insert 100 txs, each with a gas limit of 10 + _, _, addr := testdata.KeyTestPubAddr() + for i := int64(0); i < 100; i++ { + msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addr.String()} + msgs := []sdk.Msg{msg} + + builder := suite.txConfig.NewTxBuilder() + err = builder.SetMsgs(msgs...) + require.NoError(t, err) + builder.SetMemo("counter=" + strconv.FormatInt(i, 10) + "&failOnAnte=false") + builder.SetGasLimit(10) + setTxSignature(t, builder, uint64(i)) + + err := pool.Insert(sdk.Context{}, builder.GetTx()) + require.NoError(t, err) + } + + // ensure we only select transactions that fit within the block gas limit + res, err := suite.baseApp.PrepareProposal(&abci.RequestPrepareProposal{ + MaxTxBytes: 1_000_000, // large enough to ignore restriction + Height: 1, + }) + require.NoError(t, err) + + // Should include 9 transactions + require.Len(t, res.Txs, 9, "invalid number of transactions returned") +} + func TestABCI_PrepareProposal_MaxGas(t *testing.T) { pool := mempool.NewSenderNonceMempool() suite := NewBaseAppSuite(t, baseapp.SetMempool(pool)) From e27a52a1176fe64f7bf1cea4a1cf98f427753a83 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 27 Jul 2023 13:10:04 +0200 Subject: [PATCH 3/4] comment --- baseapp/abci_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index a2e6a39713c4..4e164b812be0 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -1578,13 +1578,13 @@ func TestABCI_PrepareProposal_OverGasUnderBytes(t *testing.T) { suite := NewBaseAppSuite(t, baseapp.SetMempool(pool)) baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), NoopCounterServerImpl{}) - // set max block gas limit to 99, this will allow 9 txs of 10 gas each, the last one should NOT - // be included + // set max block gas limit to 99, this will allow 9 txs of 10 gas each. _, err := suite.baseApp.InitChain(&abci.RequestInitChain{ ConsensusParams: &cmtproto.ConsensusParams{ Block: &cmtproto.BlockParams{MaxGas: 99}, }, }) + require.NoError(t, err) // insert 100 txs, each with a gas limit of 10 _, _, addr := testdata.KeyTestPubAddr() From 4c8b6d31798292282188079d47ad13ade68652fb Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 27 Jul 2023 13:12:57 +0200 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac42cc4973f3..4f3b4c522d14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (baseapp) [#17159](https://github.com/cosmos/cosmos-sdk/pull/17159) Validators can propose blocks that exceed the gas limit. * (x/group) [#17146](https://github.com/cosmos/cosmos-sdk/pull/17146) Rename x/group legacy ORM package's error codespace from "orm" to "legacy_orm", preventing collisions with ORM's error codespace "orm". ### API Breaking Changes