From 2163249c04e90649a3e310011e9c93fb46f521a6 Mon Sep 17 00:00:00 2001 From: lightclient Date: Thu, 30 Jan 2025 09:11:55 -0700 Subject: [PATCH] consensus/misc/eip4844: use head's target blobs, not parent --- cmd/evm/internal/t8ntool/execution.go | 2 +- consensus/misc/eip4844/eip4844.go | 6 +++--- consensus/misc/eip4844/eip4844_test.go | 7 +++++-- core/chain_makers.go | 2 +- core/state_processor_test.go | 2 +- eth/gasprice/feehistory.go | 2 +- eth/tracers/internal/tracetest/util.go | 2 +- internal/ethapi/simulate.go | 2 +- miner/worker.go | 2 +- 9 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 38e6d11f98..02a6c0ccab 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -194,11 +194,11 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, ExcessBlobGas: pre.Env.ParentExcessBlobGas, BlobGasUsed: pre.Env.ParentBlobGasUsed, } - excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent) header := &types.Header{ Time: pre.Env.Timestamp, ExcessBlobGas: &excessBlobGas, } + excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent, header) vmContext.BlobBaseFee = eip4844.CalcBlobFee(chainConfig, header) } } diff --git a/consensus/misc/eip4844/eip4844.go b/consensus/misc/eip4844/eip4844.go index c6547349ae..6f9c311702 100644 --- a/consensus/misc/eip4844/eip4844.go +++ b/consensus/misc/eip4844/eip4844.go @@ -49,7 +49,7 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob) } // Verify the excessBlobGas is correct based on the parent header - expectedExcessBlobGas := CalcExcessBlobGas(config, parent) + expectedExcessBlobGas := CalcExcessBlobGas(config, parent, header) if *header.ExcessBlobGas != expectedExcessBlobGas { return fmt.Errorf("invalid excessBlobGas: have %d, want %d", *header.ExcessBlobGas, expectedExcessBlobGas) } @@ -58,9 +58,9 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade // CalcExcessBlobGas calculates the excess blob gas after applying the set of // blobs on top of the excess blob gas. -func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header) uint64 { +func CalcExcessBlobGas(config *params.ChainConfig, parent, header *types.Header) uint64 { var ( - target = config.TargetBlobsPerBlock(parent.Time) * params.BlobTxBlobGasPerBlob + target = config.TargetBlobsPerBlock(header.Time) * params.BlobTxBlobGasPerBlob parentExcessBlobGas uint64 parentBlobGasUsed uint64 ) diff --git a/consensus/misc/eip4844/eip4844_test.go b/consensus/misc/eip4844/eip4844_test.go index b903492e18..220916cba4 100644 --- a/consensus/misc/eip4844/eip4844_test.go +++ b/consensus/misc/eip4844/eip4844_test.go @@ -57,12 +57,15 @@ func TestCalcExcessBlobGas(t *testing.T) { } for i, tt := range tests { blobGasUsed := tt.blobs * params.BlobTxBlobGasPerBlob + head := &types.Header{ + Time: *config.CancunTime, + } parent := &types.Header{ - Time: *config.CancunTime, + Time: *config.CancunTime + 12, ExcessBlobGas: &tt.excess, BlobGasUsed: &blobGasUsed, } - result := CalcExcessBlobGas(config, parent) + result := CalcExcessBlobGas(config, parent, head) if result != tt.want { t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 86f68d07c2..b0bd806e12 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -598,7 +598,7 @@ func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engi } } if cm.config.IsCancun(header.Number, header.Time) { - excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parent.Header()) + excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parent.Header(), header) header.ExcessBlobGas = &excessBlobGas header.BlobGasUsed = new(uint64) header.ParentBeaconRoot = new(common.Hash) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index b17c0ad2b2..b19f05a6a2 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -407,7 +407,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr } header.Root = common.BytesToHash(hasher.Sum(nil)) if config.IsCancun(header.Number, header.Time) { - excess := eip4844.CalcExcessBlobGas(config, parent.Header()) + excess := eip4844.CalcExcessBlobGas(config, parent.Header(), header) used := uint64(nBlobs * params.BlobTxBlobGasPerBlob) header.ExcessBlobGas = &excess header.BlobGasUsed = &used diff --git a/eth/gasprice/feehistory.go b/eth/gasprice/feehistory.go index e4019afb56..e16254d438 100644 --- a/eth/gasprice/feehistory.go +++ b/eth/gasprice/feehistory.go @@ -97,7 +97,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) { // Fill in blob base fee and next blob base fee. if excessBlobGas := bf.header.ExcessBlobGas; excessBlobGas != nil { bf.results.blobBaseFee = eip4844.CalcBlobFee(config, bf.header) - excess := eip4844.CalcExcessBlobGas(config, bf.header) + excess := eip4844.CalcExcessBlobGas(config, bf.header, bf.header) next := &types.Header{Number: bf.header.Number, Time: bf.header.Time, ExcessBlobGas: &excess} bf.results.nextBlobBaseFee = eip4844.CalcBlobFee(config, next) } else { diff --git a/eth/tracers/internal/tracetest/util.go b/eth/tracers/internal/tracetest/util.go index e29144e04e..2843149f42 100644 --- a/eth/tracers/internal/tracetest/util.go +++ b/eth/tracers/internal/tracetest/util.go @@ -54,7 +54,7 @@ func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext { } if genesis.ExcessBlobGas != nil && genesis.BlobGasUsed != nil { - excess := eip4844.CalcExcessBlobGas(genesis.Config, genesis.ToBlock().Header()) + excess := eip4844.CalcExcessBlobGas(genesis.Config, genesis.ToBlock().Header(), genesis.ToBlock().Header()) header := &types.Header{ExcessBlobGas: &excess, Number: genesis.Config.LondonBlock, Time: *genesis.Config.CancunTime} context.BlobBaseFee = eip4844.CalcBlobFee(genesis.Config, header) } diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index c5364c62e9..eaa1e49f0e 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -159,7 +159,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header, if sim.chainConfig.IsCancun(header.Number, header.Time) { var excess uint64 if sim.chainConfig.IsCancun(parent.Number, parent.Time) { - excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent) + excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent, header) } header.ExcessBlobGas = &excess } diff --git a/miner/worker.go b/miner/worker.go index ef7829b009..d95b69396f 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -210,7 +210,7 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir if miner.chainConfig.IsCancun(header.Number, header.Time) { var excessBlobGas uint64 if miner.chainConfig.IsCancun(parent.Number, parent.Time) { - excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent) + excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent, header) } header.BlobGasUsed = new(uint64) header.ExcessBlobGas = &excessBlobGas