Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txpool: Respect updates to block gas limit #9363

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion erigon-lib/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,37 @@ func (p *TxPool) OnNewBlock(ctx context.Context, stateChanges *remote.StateChang
pendingBlobFee := stateChanges.PendingBlobFeePerGas
p.setBlobFee(pendingBlobFee)

p.blockGasLimit.Store(stateChanges.BlockGasLimit)
oldGasLimit := p.blockGasLimit.Swap(stateChanges.BlockGasLimit)
if oldGasLimit != stateChanges.BlockGasLimit {
p.all.ascendAll(func(mt *metaTx) bool {
var updated bool
if mt.Tx.Gas < stateChanges.BlockGasLimit {
updated = (mt.subPool & NotTooMuchGas) > 0
mt.subPool |= NotTooMuchGas
} else {
updated = (mt.subPool & NotTooMuchGas) == 0
mt.subPool &^= NotTooMuchGas
}

if mt.Tx.Traced {
p.logger.Info("TX TRACING: on block gas limit update", "idHash", fmt.Sprintf("%x", mt.Tx.IDHash), "senderId", mt.Tx.SenderID, "nonce", mt.Tx.Nonce, "subPool", mt.currentSubPool, "updated", updated)
}

if !updated {
return true
}

switch mt.currentSubPool {
case PendingSubPool:
p.pending.Updated(mt)
case BaseFeeSubPool:
p.baseFee.Updated(mt)
case QueuedSubPool:
p.queued.Updated(mt)
}
return true
})
}

for i, txn := range unwindBlobTxs.Txs {
if txn.Type == types.BlobTxType {
Expand Down
73 changes: 73 additions & 0 deletions erigon-lib/txpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,76 @@ func TestBlobSlots(t *testing.T) {
assert.Equal(txpoolcfg.BlobPoolOverflow, reason, reason.String())
}
}

func TestGasLimitChanged(t *testing.T) {
assert, require := assert.New(t), require.New(t)
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)

cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil, nil, nil, fixedgas.DefaultMaxBlobsPerBlock, nil, log.New())
assert.NoError(err)
require.True(pool != nil)
ctx := context.Background()
var stateVersionID uint64 = 0
pendingBaseFee := uint64(200000)
// start blocks from 0, set empty hash - then kvcache will also work on this
h1 := gointerfaces.ConvertHashToH256([32]byte{})
var addr [20]byte
addr[0] = 1
v := make([]byte, types.EncodeSenderLengthForStorage(2, *uint256.NewInt(1 * common.Ether)))
types.EncodeSender(2, *uint256.NewInt(1 * common.Ether), v)
tx, err := db.BeginRw(ctx)
require.NoError(err)
defer tx.Rollback()

change := &remote.StateChangeBatch{
StateVersionId: stateVersionID,
PendingBlockBaseFee: pendingBaseFee,
BlockGasLimit: 50_000,
ChangeBatch: []*remote.StateChange{
{BlockHeight: 0, BlockHash: h1},
},
}
change.ChangeBatch[0].Changes = append(change.ChangeBatch[0].Changes, &remote.AccountChange{
Action: remote.Action_UPSERT,
Address: gointerfaces.ConvertAddressToH160(addr),
Data: v,
})
err = pool.OnNewBlock(ctx, change, types.TxSlots{}, types.TxSlots{}, types.TxSlots{}, tx)
assert.NoError(err)

var txSlots types.TxSlots
txSlot1 := &types.TxSlot{
Tip: *uint256.NewInt(300000),
FeeCap: *uint256.NewInt(300000),
Gas: 100_000,
Nonce: 3,
}
txSlot1.IDHash[0] = 1
txSlots.Append(txSlot1, addr[:], true)

reasons, err := pool.AddLocalTxs(ctx, txSlots, tx)
assert.NoError(err)
for _, reason := range reasons {
assert.Equal(txpoolcfg.Success, reason, reason.String())
}

mtx, ok := pool.byHash[string(txSlot1.IDHash[:])]
assert.True(ok)
assert.Zero(mtx.subPool&NotTooMuchGas, "Should be insufficient block space for the tx")

change.ChangeBatch[0].Changes = nil
change.BlockGasLimit = 150_000
err = pool.OnNewBlock(ctx, change, types.TxSlots{}, types.TxSlots{}, types.TxSlots{}, tx)
assert.NoError(err)

assert.NotZero(mtx.subPool&NotTooMuchGas, "Should now have block space for the tx")

change.BlockGasLimit = 50_000
err = pool.OnNewBlock(ctx, change, types.TxSlots{}, types.TxSlots{}, types.TxSlots{}, tx)
assert.NoError(err)

assert.Zero(mtx.subPool&NotTooMuchGas, "Should now have block space (again) for the tx")
}
Loading