Skip to content

Commit ed1d46b

Browse files
authored
consensus/misc/eip4844: more changes for blob gas calculation (#31128)
This PR changes the signature of `CalcExcessBlobGas` to take in just the header timestamp instead of the whole object. It also adds a sanity check for the parent->child block order to `VerifyEIP4844Header`.
1 parent c4ad459 commit ed1d46b

File tree

9 files changed

+18
-18
lines changed

9 files changed

+18
-18
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
198198
Time: pre.Env.Timestamp,
199199
ExcessBlobGas: &excessBlobGas,
200200
}
201-
excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent, header)
201+
excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent, header.Time)
202202
vmContext.BlobBaseFee = eip4844.CalcBlobFee(chainConfig, header)
203203
}
204204
}

consensus/misc/eip4844/eip4844.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ var (
3434
// if the current block contains no transactions, the excessBlobGas is updated
3535
// accordingly.
3636
func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Header) error {
37+
if header.Number.Uint64() != parent.Number.Uint64()+1 {
38+
panic("bad header pair")
39+
}
3740
// Verify the header is not malformed
3841
if header.ExcessBlobGas == nil {
3942
return errors.New("header is missing excessBlobGas")
@@ -50,7 +53,7 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
5053
return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob)
5154
}
5255
// Verify the excessBlobGas is correct based on the parent header
53-
expectedExcessBlobGas := CalcExcessBlobGas(config, parent, header)
56+
expectedExcessBlobGas := CalcExcessBlobGas(config, parent, header.Time)
5457
if *header.ExcessBlobGas != expectedExcessBlobGas {
5558
return fmt.Errorf("invalid excessBlobGas: have %d, want %d", *header.ExcessBlobGas, expectedExcessBlobGas)
5659
}
@@ -59,9 +62,8 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
5962

6063
// CalcExcessBlobGas calculates the excess blob gas after applying the set of
6164
// blobs on top of the excess blob gas.
62-
func CalcExcessBlobGas(config *params.ChainConfig, parent, header *types.Header) uint64 {
65+
func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTimestamp uint64) uint64 {
6366
var (
64-
targetGas = uint64(targetBlobsPerBlock(config, header.Time)) * params.BlobTxBlobGasPerBlob
6567
parentExcessBlobGas uint64
6668
parentBlobGasUsed uint64
6769
)
@@ -70,6 +72,7 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent, header *types.Header)
7072
parentBlobGasUsed = *parent.BlobGasUsed
7173
}
7274
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
75+
targetGas := uint64(targetBlobsPerBlock(config, headTimestamp)) * params.BlobTxBlobGasPerBlob
7376
if excessBlobGas < targetGas {
7477
return 0
7578
}

consensus/misc/eip4844/eip4844_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ func TestCalcExcessBlobGas(t *testing.T) {
5757
}
5858
for i, tt := range tests {
5959
blobGasUsed := uint64(tt.blobs) * params.BlobTxBlobGasPerBlob
60-
head := &types.Header{
61-
Time: *config.CancunTime,
62-
}
63-
parent := &types.Header{
64-
Time: *config.CancunTime,
60+
header := &types.Header{
6561
ExcessBlobGas: &tt.excess,
6662
BlobGasUsed: &blobGasUsed,
6763
}
68-
result := CalcExcessBlobGas(config, parent, head)
64+
result := CalcExcessBlobGas(config, header, *config.CancunTime)
6965
if result != tt.want {
7066
t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want)
7167
}

core/chain_makers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,25 +582,26 @@ func GenerateVerkleChainWithGenesis(genesis *Genesis, engine consensus.Engine, n
582582

583583
func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
584584
time := parent.Time() + 10 // block time is fixed at 10 seconds
585+
parentHeader := parent.Header()
585586
header := &types.Header{
586587
Root: state.IntermediateRoot(cm.config.IsEIP158(parent.Number())),
587588
ParentHash: parent.Hash(),
588589
Coinbase: parent.Coinbase(),
589-
Difficulty: engine.CalcDifficulty(cm, time, parent.Header()),
590+
Difficulty: engine.CalcDifficulty(cm, time, parentHeader),
590591
GasLimit: parent.GasLimit(),
591592
Number: new(big.Int).Add(parent.Number(), common.Big1),
592593
Time: time,
593594
}
594595

595596
if cm.config.IsLondon(header.Number) {
596-
header.BaseFee = eip1559.CalcBaseFee(cm.config, parent.Header())
597+
header.BaseFee = eip1559.CalcBaseFee(cm.config, parentHeader)
597598
if !cm.config.IsLondon(parent.Number()) {
598599
parentGasLimit := parent.GasLimit() * cm.config.ElasticityMultiplier()
599600
header.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)
600601
}
601602
}
602603
if cm.config.IsCancun(header.Number, header.Time) {
603-
excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parent.Header(), header)
604+
excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parentHeader, time)
604605
header.ExcessBlobGas = &excessBlobGas
605606
header.BlobGasUsed = new(uint64)
606607
header.ParentBeaconRoot = new(common.Hash)

core/state_processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
407407
}
408408
header.Root = common.BytesToHash(hasher.Sum(nil))
409409
if config.IsCancun(header.Number, header.Time) {
410-
excess := eip4844.CalcExcessBlobGas(config, parent.Header(), header)
410+
excess := eip4844.CalcExcessBlobGas(config, parent.Header(), header.Time)
411411
used := uint64(nBlobs * params.BlobTxBlobGasPerBlob)
412412
header.ExcessBlobGas = &excess
413413
header.BlobGasUsed = &used

eth/gasprice/feehistory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
9797
// Fill in blob base fee and next blob base fee.
9898
if excessBlobGas := bf.header.ExcessBlobGas; excessBlobGas != nil {
9999
bf.results.blobBaseFee = eip4844.CalcBlobFee(config, bf.header)
100-
excess := eip4844.CalcExcessBlobGas(config, bf.header, bf.header)
100+
excess := eip4844.CalcExcessBlobGas(config, bf.header, bf.header.Time)
101101
next := &types.Header{Number: bf.header.Number, Time: bf.header.Time, ExcessBlobGas: &excess}
102102
bf.results.nextBlobBaseFee = eip4844.CalcBlobFee(config, next)
103103
} else {

eth/tracers/internal/tracetest/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
5555

5656
if genesis.ExcessBlobGas != nil && genesis.BlobGasUsed != nil {
5757
header := &types.Header{Number: genesis.Config.LondonBlock, Time: *genesis.Config.CancunTime}
58-
excess := eip4844.CalcExcessBlobGas(genesis.Config, header, genesis.ToBlock().Header())
58+
excess := eip4844.CalcExcessBlobGas(genesis.Config, header, genesis.Timestamp)
5959
header.ExcessBlobGas = &excess
6060
context.BlobBaseFee = eip4844.CalcBlobFee(genesis.Config, header)
6161
}

internal/ethapi/simulate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
159159
if sim.chainConfig.IsCancun(header.Number, header.Time) {
160160
var excess uint64
161161
if sim.chainConfig.IsCancun(parent.Number, parent.Time) {
162-
excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent, header)
162+
excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent, header.Time)
163163
}
164164
header.ExcessBlobGas = &excess
165165
}

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir
210210
if miner.chainConfig.IsCancun(header.Number, header.Time) {
211211
var excessBlobGas uint64
212212
if miner.chainConfig.IsCancun(parent.Number, parent.Time) {
213-
excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent, header)
213+
excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent, timestamp)
214214
}
215215
header.BlobGasUsed = new(uint64)
216216
header.ExcessBlobGas = &excessBlobGas

0 commit comments

Comments
 (0)