Skip to content

Commit bd61e84

Browse files
mergify[bot]Devon Bear
and
Devon Bear
authored
fix(abci): Validators can propose blocks that exceed the gas limit. (backport #17159) (#17160)
Co-authored-by: Devon Bear <[email protected]>
1 parent abd0bd5 commit bd61e84

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4545

4646
### Bug Fixes
4747

48+
* (baseapp) [#17159](https://github.com/cosmos/cosmos-sdk/pull/17159) Validators can propose blocks that exceed the gas limit.
4849
* (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".
4950

5051
### API Breaking Changes

baseapp/abci_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,47 @@ func TestABCI_PrepareProposal_BadEncoding(t *testing.T) {
15691569
require.Equal(t, 1, len(resPrepareProposal.Txs))
15701570
}
15711571

1572+
func TestABCI_PrepareProposal_OverGasUnderBytes(t *testing.T) {
1573+
pool := mempool.NewSenderNonceMempool()
1574+
suite := NewBaseAppSuite(t, baseapp.SetMempool(pool))
1575+
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), NoopCounterServerImpl{})
1576+
1577+
// set max block gas limit to 99, this will allow 9 txs of 10 gas each.
1578+
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
1579+
ConsensusParams: &cmtproto.ConsensusParams{
1580+
Block: &cmtproto.BlockParams{MaxGas: 99},
1581+
},
1582+
})
1583+
1584+
require.NoError(t, err)
1585+
// insert 100 txs, each with a gas limit of 10
1586+
_, _, addr := testdata.KeyTestPubAddr()
1587+
for i := int64(0); i < 100; i++ {
1588+
msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addr.String()}
1589+
msgs := []sdk.Msg{msg}
1590+
1591+
builder := suite.txConfig.NewTxBuilder()
1592+
err = builder.SetMsgs(msgs...)
1593+
require.NoError(t, err)
1594+
builder.SetMemo("counter=" + strconv.FormatInt(i, 10) + "&failOnAnte=false")
1595+
builder.SetGasLimit(10)
1596+
setTxSignature(t, builder, uint64(i))
1597+
1598+
err := pool.Insert(sdk.Context{}, builder.GetTx())
1599+
require.NoError(t, err)
1600+
}
1601+
1602+
// ensure we only select transactions that fit within the block gas limit
1603+
res, err := suite.baseApp.PrepareProposal(&abci.RequestPrepareProposal{
1604+
MaxTxBytes: 1_000_000, // large enough to ignore restriction
1605+
Height: 1,
1606+
})
1607+
require.NoError(t, err)
1608+
1609+
// Should include 9 transactions
1610+
require.Len(t, res.Txs, 9, "invalid number of transactions returned")
1611+
}
1612+
15721613
func TestABCI_PrepareProposal_MaxGas(t *testing.T) {
15731614
pool := mempool.NewSenderNonceMempool()
15741615
suite := NewBaseAppSuite(t, baseapp.SetMempool(pool))

baseapp/abci_utils.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,12 @@ func (h DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand
231231
if (txSize + totalTxBytes) < req.MaxTxBytes {
232232
// If there is a max block gas limit, add the tx only if the limit has
233233
// not been met.
234-
if maxBlockGas > 0 && (txGasLimit+totalTxGas) <= uint64(maxBlockGas) {
235-
totalTxGas += txGasLimit
236-
totalTxBytes += txSize
237-
selectedTxs = append(selectedTxs, bz)
234+
if maxBlockGas > 0 {
235+
if (txGasLimit + totalTxGas) <= uint64(maxBlockGas) {
236+
totalTxGas += txGasLimit
237+
totalTxBytes += txSize
238+
selectedTxs = append(selectedTxs, bz)
239+
}
238240
} else {
239241
totalTxBytes += txSize
240242
selectedTxs = append(selectedTxs, bz)

0 commit comments

Comments
 (0)