From e6b9d0c2b68068574e5803cbcb3583c1b331bcde Mon Sep 17 00:00:00 2001 From: jwasinger Date: Wed, 9 Jul 2025 18:24:44 +0900 Subject: [PATCH] core,miner: implement EIP-7934 - RLP Execution Block Size Limit (#31990) This PR adds a block validation check for the maximum block size, as required by EIP-7934, and also applies a slightly lower size limit during block building. --------- Co-authored-by: spencer-tb Co-authored-by: Felix Lange Co-authored-by: Gary Rong --- core/block_validator.go | 4 +++ core/error.go | 4 +++ core/types/block_test.go | 56 +++++++++++++++++++++++++++++++++++++++ core/types/transaction.go | 8 ++++-- miner/worker.go | 52 ++++++++++++++++++++++++++++++------ params/protocol_params.go | 2 ++ 6 files changed, 116 insertions(+), 10 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index 591e472bc1..008444fbbc 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -49,6 +49,10 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain) *Bloc // header's transaction and uncle roots. The headers are assumed to be already // validated at this point. func (v *BlockValidator) ValidateBody(block *types.Block) error { + // check EIP 7934 RLP-encoded block size cap + if v.config.IsOsaka(block.Number(), block.Time()) && block.Size() > params.MaxBlockSize { + return ErrBlockOversized + } // Check whether the block is already imported. if v.bc.HasBlockAndState(block.Hash(), block.NumberU64()) { return ErrKnownBlock diff --git a/core/error.go b/core/error.go index c41a7f872c..b9e894bd9b 100644 --- a/core/error.go +++ b/core/error.go @@ -28,6 +28,10 @@ var ( // ErrNoGenesis is returned when there is no Genesis Block. ErrNoGenesis = errors.New("genesis not found in chain") + + // ErrBlockOversized is returned if the size of the RLP-encoded block + // exceeds the cap established by EIP 7934 + ErrBlockOversized = errors.New("block RLP-encoded size exceeds maximum") ) // List of evm-call-message pre-checking errors. All state transition messages will diff --git a/core/types/block_test.go b/core/types/block_test.go index 1f61be89a5..2130a2fcf3 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -24,11 +24,13 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/blocktest" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" + "github.com/holiman/uint256" ) // from bcValidBlockTest.json, "SimpleTx" @@ -194,6 +196,60 @@ func TestEIP2718BlockEncoding(t *testing.T) { } } +func TestEIP4844BlockEncoding(t *testing.T) { + // https://github.com/ethereum/tests/blob/develop/BlockchainTests/ValidBlocks/bcEIP4844-blobtransactions/blockWithAllTransactionTypes.json + blockEnc := common.FromHex("0xf90417f90244a05eb7f6da0f3e237c62bcae48b7fb5f4506d392616b62890429c8b76b4a1d4104a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794ba5e000000000000000000000000000000000000a011639dcca0b44f2acb5b630a82c8a69cb82742b3711383ec4e111a554d27aea5a05cb644f722e31f9792a8ef6e2a762334e1a862e8b40c1612e1e9507fd7121ef9a00c82719448356ba6807d6edfcd8e5aea575a5e97f36038ffb3e395749b26d41cb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800188016345785d8a00008301482082079e42a00000000000000000000000000000000000000000000000000000000000020000880000000000000000820314a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218302000080a00000000000000000000000000000000000000000000000000000000000000000f901cbf864808203e885e8d4a5100094100000000000000000000000000000000000000a01801ca09de4adda6288582a6700dbcd8eb70c0a4a7fc9487d965f7bf22424e0bd121095a01cdb078764cc3770d5db847e99e10333aa7c356247baaf09b03eae04d64e7926b86901f86601018203e885e8d4a5100094100000000000000000000000000000000000000a0380c080a025090740da12684493e4fb466a3979e365b194e8cf462edf3c2c3be2f130bb2ea034fa18fb4c1bff4d957d72e28535d27f1352517a942aeaca0ed944085f0cd8bbb86a02f8670102018203e885e8d4a5100094100000000000000000000000000000000000000a0580c080a0352a7be5002ce111bc5167f3addf97a75e2e0b810d826af71d2caae18aed284ea065d38f8a5c8948ce706842e8861fb21020b93a4d5e489162a0e6d419a457b735b88c03f8890103018203e885e8d4a5100094100000000000000000000000000000000000000a0780c00ae1a001a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8809f638144c46d5de7a9e630c0e7c5c63ae829ecfd8cc94715d9c29fe17c464de0a06c5fc54c3aa868ba35ef31a4e12431611631ab7bcdceb4214dd273d83f73b5e1c0c0") + var block Block + if err := rlp.DecodeBytes(blockEnc, &block); err != nil { + t.Fatal("decode error: ", err) + } + + check := func(f string, got, want interface{}) { + if !reflect.DeepEqual(got, want) { + t.Errorf("%s mismatch: got %v, want %v", f, got, want) + } + } + check("Difficulty", block.Difficulty(), big.NewInt(0)) + check("GasLimit", block.GasLimit(), hexutil.MustDecodeUint64("0x16345785d8a0000")) + check("GasUsed", block.GasUsed(), hexutil.MustDecodeUint64("0x14820")) + check("Coinbase", block.Coinbase(), common.HexToAddress("0xba5e000000000000000000000000000000000000")) + check("MixDigest", block.MixDigest(), common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000020000")) + check("Root", block.Root(), common.HexToHash("0x11639dcca0b44f2acb5b630a82c8a69cb82742b3711383ec4e111a554d27aea5")) + check("WithdrawalRoot", *block.Header().WithdrawalsHash, common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")) + check("Nonce", block.Nonce(), uint64(0)) + check("Time", block.Time(), hexutil.MustDecodeUint64("0x79e")) + check("Size", block.Size(), uint64(len(blockEnc))) + + // Create blob tx. + tx := NewTx(&BlobTx{ + ChainID: uint256.NewInt(1), + Nonce: 3, + To: common.HexToAddress("0x100000000000000000000000000000000000000a"), + Gas: hexutil.MustDecodeUint64("0xe8d4a51000"), + GasTipCap: uint256.MustFromHex("0x1"), + GasFeeCap: uint256.MustFromHex("0x3e8"), + BlobFeeCap: uint256.MustFromHex("0xa"), + BlobHashes: []common.Hash{ + common.BytesToHash(hexutil.MustDecode("0x01a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")), + }, + Value: uint256.MustFromHex("0x7"), + }) + sig := common.Hex2Bytes("00638144c46d5de7a9e630c0e7c5c63ae829ecfd8cc94715d9c29fe17c464de06c5fc54c3aa868ba35ef31a4e12431611631ab7bcdceb4214dd273d83f73b5e100") + tx, _ = tx.WithSignature(LatestSignerForChainID(big.NewInt(1)), sig) + + check("len(Transactions)", len(block.Transactions()), 4) + check("Transactions[3].Hash", block.Transactions()[3].Hash(), tx.Hash()) + check("Transactions[3].Type()", block.Transactions()[3].Type(), uint8(BlobTxType)) + + ourBlockEnc, err := rlp.EncodeToBytes(&block) + if err != nil { + t.Fatal("encode error: ", err) + } + if !bytes.Equal(ourBlockEnc, blockEnc) { + t.Errorf("encoded block mismatch:\ngot: %x\nwant: %x", ourBlockEnc, blockEnc) + } +} + func TestUncleHash(t *testing.T) { uncles := make([]*Header, 0) h := CalcUncleHash(uncles) diff --git a/core/types/transaction.go b/core/types/transaction.go index 934feb7353..733b6510f1 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -449,14 +449,18 @@ func (tx *Transaction) BlobGasFeeCapIntCmp(other *big.Int) int { // WithoutBlobTxSidecar returns a copy of tx with the blob sidecar removed. func (tx *Transaction) WithoutBlobTxSidecar() *Transaction { blobtx, ok := tx.inner.(*BlobTx) - if !ok { + if !ok || blobtx.Sidecar == nil { return tx } cpy := &Transaction{ inner: blobtx.withoutSidecar(), time: tx.time, } - // Note: tx.size cache not carried over because the sidecar is included in size! + if size := tx.size.Load(); size != 0 { + // The tx had a sidecar before, so we need to subtract it from the size. + scSize := rlp.ListSize(blobtx.Sidecar.encodedSize()) + cpy.size.Store(size - scSize) + } if h := tx.hash.Load(); h != nil { cpy.hash.Store(h) } diff --git a/miner/worker.go b/miner/worker.go index 9e60ce9a16..799107f3bf 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -50,6 +50,7 @@ type environment struct { signer types.Signer state *state.StateDB // apply state changes here tcount int // tx count in cycle + size uint64 // size of the block we are building gasPool *core.GasPool // available gas used to pack transactions coinbase common.Address evm *vm.EVM @@ -63,6 +64,11 @@ type environment struct { witness *stateless.Witness } +// txFits reports whether the transaction fits into the block size limit. +func (env *environment) txFitsSize(tx *types.Transaction) bool { + return env.size+tx.Size() < params.MaxBlockSize-maxBlockSizeBufferZone +} + const ( commitInterruptNone int32 = iota commitInterruptNewHead @@ -70,6 +76,11 @@ const ( commitInterruptTimeout ) +// Block size is capped by the protocol at params.MaxBlockSize. When producing blocks, we +// try to say below the size including a buffer zone, this is to avoid going over the +// maximum size with auxiliary data added into the block. +const maxBlockSizeBufferZone = 1_000_000 + // newPayloadResult is the result of payload generation. type newPayloadResult struct { err error @@ -95,12 +106,23 @@ type generateParams struct { } // generateWork generates a sealing block based on the given parameters. -func (miner *Miner) generateWork(params *generateParams, witness bool) *newPayloadResult { - work, err := miner.prepareWork(params, witness) +func (miner *Miner) generateWork(genParam *generateParams, witness bool) *newPayloadResult { + work, err := miner.prepareWork(genParam, witness) if err != nil { return &newPayloadResult{err: err} } - if !params.noTxs { + + // Check withdrawals fit max block size. + // Due to the cap on withdrawal count, this can actually never happen, but we still need to + // check to ensure the CL notices there's a problem if the withdrawal cap is ever lifted. + maxBlockSize := params.MaxBlockSize - maxBlockSizeBufferZone + if genParam.withdrawals.Size() > maxBlockSize { + return &newPayloadResult{err: errors.New("withdrawals exceed max block size")} + } + // Also add size of withdrawals to work block size. + work.size += uint64(genParam.withdrawals.Size()) + + if !genParam.noTxs { interrupt := new(atomic.Int32) timer := time.AfterFunc(miner.config.Recommit, func() { interrupt.Store(commitInterruptTimeout) @@ -112,8 +134,8 @@ func (miner *Miner) generateWork(params *generateParams, witness bool) *newPaylo log.Warn("Block building is interrupted", "allowance", common.PrettyDuration(miner.config.Recommit)) } } + body := types.Body{Transactions: work.txs, Withdrawals: genParam.withdrawals} - body := types.Body{Transactions: work.txs, Withdrawals: params.withdrawals} allLogs := make([]*types.Log, 0) for _, r := range work.receipts { allLogs = append(allLogs, r.Logs...) @@ -256,6 +278,7 @@ func (miner *Miner) makeEnv(parent *types.Header, header *types.Header, coinbase return &environment{ signer: types.MakeSigner(miner.chainConfig, header.Number, header.Time), state: state, + size: uint64(header.Size()), coinbase: coinbase, header: header, witness: state.Witness(), @@ -273,6 +296,7 @@ func (miner *Miner) commitTransaction(env *environment, tx *types.Transaction) e } env.txs = append(env.txs, tx) env.receipts = append(env.receipts, receipt) + env.size += tx.Size() env.tcount++ return nil } @@ -294,10 +318,12 @@ func (miner *Miner) commitBlobTransaction(env *environment, tx *types.Transactio if err != nil { return err } - env.txs = append(env.txs, tx.WithoutBlobTxSidecar()) + txNoBlob := tx.WithoutBlobTxSidecar() + env.txs = append(env.txs, txNoBlob) env.receipts = append(env.receipts, receipt) env.sidecars = append(env.sidecars, sc) env.blobs += len(sc.Blobs) + env.size += txNoBlob.Size() *env.header.BlobGasUsed += receipt.BlobGasUsed env.tcount++ return nil @@ -318,7 +344,11 @@ func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (* } func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *transactionsByPriceAndNonce, interrupt *atomic.Int32) error { - gasLimit := env.header.GasLimit + var ( + isOsaka = miner.chainConfig.IsOsaka(env.header.Number, env.header.Time) + isCancun = miner.chainConfig.IsCancun(env.header.Number, env.header.Time) + gasLimit = env.header.GasLimit + ) if env.gasPool == nil { env.gasPool = new(core.GasPool).AddGas(gasLimit) } @@ -374,7 +404,7 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran // Most of the blob gas logic here is agnostic as to if the chain supports // blobs or not, however the max check panics when called on a chain without // a defined schedule, so we need to verify it's safe to call. - if miner.chainConfig.IsCancun(env.header.Number, env.header.Time) { + if isCancun { left := eip4844.MaxBlobsPerBlock(miner.chainConfig, env.header.Time) - env.blobs if left < int(ltx.BlobGas/params.BlobTxBlobGasPerBlob) { log.Trace("Not enough blob space left for transaction", "hash", ltx.Hash, "left", left, "needed", ltx.BlobGas/params.BlobTxBlobGasPerBlob) @@ -391,8 +421,14 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran continue } + // if inclusion of the transaction would put the block size over the + // maximum we allow, don't add any more txs to the payload. + if !env.txFitsSize(tx) { + break + } + // Make sure all transactions after osaka have cell proofs - if miner.chainConfig.IsOsaka(env.header.Number, env.header.Time) { + if isOsaka { if sidecar := tx.BlobTxSidecar(); sidecar != nil { if sidecar.Version == 0 { log.Info("Including blob tx with v0 sidecar, recomputing proofs", "hash", ltx.Hash) diff --git a/params/protocol_params.go b/params/protocol_params.go index 2319019a95..f52bb27380 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -180,6 +180,8 @@ const ( BlobBaseCost = 1 << 13 // Base execution gas cost for a blob. HistoryServeWindow = 8192 // Number of blocks to serve historical block hashes for, EIP-2935. + + MaxBlockSize = 8_388_608 // maximum size of an RLP-encoded block ) // Bls12381G1MultiExpDiscountTable is the gas discount table for BLS12-381 G1 multi exponentiation operation