From 2adc53952b84a45ad2ca7ad9902c927571fc23b5 Mon Sep 17 00:00:00 2001 From: buddh0 Date: Tue, 10 Sep 2024 14:56:17 +0800 Subject: [PATCH] miner: limit block size to eth protocol msg size --- eth/protocols/eth/protocol.go | 3 ++- miner/bid_simulator.go | 9 +++++++++ miner/worker.go | 19 +++++++++++++++++++ params/protocol_params.go | 2 ++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/eth/protocols/eth/protocol.go b/eth/protocols/eth/protocol.go index d1e07df25c..46bc97fbb8 100644 --- a/eth/protocols/eth/protocol.go +++ b/eth/protocols/eth/protocol.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/forkid" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -46,7 +47,7 @@ var ProtocolVersions = []uint{ETH68} var protocolLengths = map[uint]uint64{ETH68: 17} // maxMessageSize is the maximum cap on the size of a protocol message. -const maxMessageSize = 10 * 1024 * 1024 +var maxMessageSize = params.MaxMessageSize const ( StatusMsg = 0x00 diff --git a/miner/bid_simulator.go b/miner/bid_simulator.go index f96d3c1a8a..40556e8d36 100644 --- a/miner/bid_simulator.go +++ b/miner/bid_simulator.go @@ -692,6 +692,14 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) { return } + // check bid size + if bidRuntime.env.size+blockReserveSize > params.MaxMessageSize { + log.Error("BidSimulator: failed to check bid size", "builder", bidRuntime.bid.Builder, + "bidHash", bidRuntime.bid.Hash(), "env.size", bidRuntime.env.size) + err = errors.New("invalid bid size") + return + } + bestBid := b.GetBestBid(parentHash) if bestBid == nil { log.Info("[BID RESULT]", "win", "true[first]", "builder", bidRuntime.bid.Builder, "hash", bidRuntime.bid.Hash().TerminalString()) @@ -857,6 +865,7 @@ func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *para env.receipts = append(env.receipts, receipt) } + r.env.size += uint32(tx.Size()) r.env.tcount++ return nil diff --git a/miner/worker.go b/miner/worker.go index 8f09819d7d..8513b6a9b6 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -70,6 +70,12 @@ const ( // the default to wait for the mev miner to finish waitMEVMinerEndTimeLimit = 50 * time.Millisecond + + // Reserve block size for the following 3 components: + // a. System transactions at the end of the block + // b. Seal in the block header + // c. Overhead from RLP encoding + blockReserveSize = 100 * 1024 ) var ( @@ -89,6 +95,7 @@ type environment struct { signer types.Signer state *state.StateDB // apply state changes here tcount int // tx count in cycle + size uint32 // almost accurate block size, gasPool *core.GasPool // available gas used to pack transactions coinbase common.Address @@ -105,6 +112,7 @@ func (env *environment) copy() *environment { signer: env.signer, state: env.state.Copy(), tcount: env.tcount, + size: env.size, coinbase: env.coinbase, header: types.CopyHeader(env.header), receipts: copyReceipts(env.receipts), @@ -895,6 +903,13 @@ LOOP: txs.Pop() continue } + // If we don't have enough size left for the next transaction, skip it. + if env.size+uint32(tx.Size())+blockReserveSize > params.MaxMessageSize { + log.Trace("Not enough size left for transaction", "hash", ltx.Hash, + "env.size", env.size, "needed", uint32(tx.Size())) + txs.Pop() + continue + } // Error may be ignored here. The error has already been checked // during transaction acceptance is the transaction pool. from, _ := types.Sender(env.signer, tx) @@ -920,6 +935,7 @@ LOOP: // Everything ok, collect the logs and shift in the next transaction from the same account coalescedLogs = append(coalescedLogs, logs...) env.tcount++ + env.size += uint32(tx.Size()) // size of BlobTxSidecar included txs.Shift() default: @@ -1055,6 +1071,9 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, w.chainConfig, vm.Config{}) core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state) } + + env.size = uint32(env.header.Size()) + return env, nil } diff --git a/params/protocol_params.go b/params/protocol_params.go index 65b2d942c1..b1f3513b77 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -23,6 +23,8 @@ import ( ) const ( + MaxMessageSize uint32 = 10 * 1024 * 1024 // MaxMessageSize is the maximum cap on the size of a eth protocol message. + GasLimitBoundDivisor uint64 = 256 // The bound divisor of the gas limit, used in update calculations. MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be. MaxGasLimit uint64 = 0x7fffffffffffffff // Maximum the gas limit (2^63-1).