Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d11ef8b
core,miner,params: implement EIP-7934 - RLP Execution Block Size Limit
jwasinger Jun 9, 2025
bbb603a
rename func
jwasinger Jun 9, 2025
6403291
goimports
jwasinger Jun 9, 2025
c14d331
simplification
jwasinger Jun 9, 2025
84fdfe1
remove outdated change
jwasinger Jun 9, 2025
e38f776
more updates
jwasinger Jun 9, 2025
568b9ac
add back some changes that were deleted
jwasinger Jun 9, 2025
b6e8755
fix withdrawal inclusion
jwasinger Jun 9, 2025
525f560
fix lint
jwasinger Jun 9, 2025
799a5b1
add comment for size check in body validation
jwasinger Jun 9, 2025
199faa7
make sure that we respect the buffer when adding transactions. comme…
jwasinger Jun 9, 2025
0293afe
ensure that all requested withdrawals are included in the block even …
jwasinger Jun 10, 2025
6caa1ec
comment phrasing
jwasinger Jun 10, 2025
7288192
consider tx without sidecar when accounting for the size it will cont…
jwasinger Jun 11, 2025
85290d0
don't calculate the current fork for every transaction that we attemp…
jwasinger Jun 11, 2025
517f8fa
correctly incorporate blob transaction size
jwasinger Jun 12, 2025
7754e77
params: fix 7934 size limit.
spencer-tb Jun 23, 2025
84609d8
miner: return error when withdrawals overshoot max block size
fjl Jul 8, 2025
4402a89
miner: check tx fits block size unconditionally
fjl Jul 8, 2025
f40396d
params: remove WithdrawalSize
fjl Jul 8, 2025
b66ed1b
core/types: add block encode test
rjl493456442 Jul 8, 2025
319fd95
miner: prevent double construction
rjl493456442 Jul 8, 2025
11f1b1f
params, miner: rename constants
fjl Jul 8, 2025
69fe54b
core/types: maintain tx size cache in WithoutBlobTxSidecar
fjl Jul 8, 2025
fd05c0a
miner: fix typo in comment
fjl Jul 8, 2025
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
4 changes: 4 additions & 0 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been verifying this, and this part is correct, but the Size method has some issues related with the new sidecar v2. I was thinking about fixing it in this PR before merging, but will separate this out.

if h := tx.hash.Load(); h != nil {
cpy.hash.Store(h)
}
Expand Down
52 changes: 44 additions & 8 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -63,13 +64,23 @@ 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
commitInterruptResubmit
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
Expand All @@ -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)
Expand All @@ -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...)
Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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
Expand Down