Skip to content

Commit bcec4a4

Browse files
StephenButtolphceyonur
authored andcommitted
Support all upgrades in CalcBaseFee and CalcExtraPrefix (#804)
1 parent 098c255 commit bcec4a4

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

consensus/dummy/dynamic_fees.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,41 @@ var (
2323

2424
// CalcExtraPrefix takes the previous header and the timestamp of its child
2525
// block and calculates the expected extra prefix for the child block.
26-
//
27-
// CalcExtraPrefix should only be called if timestamp >= config.ApricotPhase3Timestamp
2826
func CalcExtraPrefix(
2927
config *params.ChainConfig,
3028
feeConfig commontype.FeeConfig,
3129
parent *types.Header,
3230
timestamp uint64,
3331
) ([]byte, error) {
34-
window, err := calcFeeWindow(config, feeConfig, parent, timestamp)
35-
return window.Bytes(), err
32+
switch {
33+
case config.IsSubnetEVM(timestamp):
34+
window, err := calcFeeWindow(config, feeConfig, parent, timestamp)
35+
if err != nil {
36+
return nil, fmt.Errorf("failed to calculate fee window: %w", err)
37+
}
38+
return window.Bytes(), nil
39+
default:
40+
// Prior to SubnetEVM there was no expected extra prefix.
41+
return nil, nil
42+
}
3643
}
3744

3845
// CalcBaseFee takes the previous header and the timestamp of its child block
3946
// and calculates the expected base fee for the child block.
4047
//
41-
// CalcBaseFee should only be called if timestamp >= config.IsSubnetEVMTimestamp
48+
// Prior to SubnetEVM, the returned base fee will be nil.
4249
func CalcBaseFee(config *params.ChainConfig, feeConfig commontype.FeeConfig, parent *types.Header, timestamp uint64) (*big.Int, error) {
50+
switch {
51+
case config.IsSubnetEVM(timestamp):
52+
return calcBaseFeeWithWindow(config, feeConfig, parent, timestamp)
53+
default:
54+
// Prior to SubnetEVM the expected base fee is nil.
55+
return nil, nil
56+
}
57+
}
58+
59+
// calcBaseFeeWithWindow should only be called if `timestamp` >= `config.SubnetEVMTimestamp`.
60+
func calcBaseFeeWithWindow(config *params.ChainConfig, feeConfig commontype.FeeConfig, parent *types.Header, timestamp uint64) (*big.Int, error) {
4361
// If the current block is the first EIP-1559 block, or it is the genesis block
4462
// return the initial slice and initial base fee.
4563
if !config.IsSubnetEVM(parent.Time) || parent.Number.Cmp(common.Big0) == 0 {

core/chain_makers.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,27 +379,28 @@ func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.S
379379
ParentHash: parent.Hash(),
380380
Coinbase: parent.Coinbase(),
381381
Difficulty: engine.CalcDifficulty(cm, time, parent.Header()),
382-
GasLimit: parent.GasLimit(),
383382
Number: new(big.Int).Add(parent.Number(), common.Big1),
384383
Time: time,
385384
}
385+
feeConfig, _, err := cm.GetFeeConfigAt(parent.Header())
386+
if err != nil {
387+
panic(err)
388+
}
386389
if cm.config.IsSubnetEVM(time) {
387-
feeConfig, _, err := cm.GetFeeConfigAt(parent.Header())
388-
if err != nil {
389-
panic(err)
390-
}
391390
header.GasLimit = feeConfig.GasLimit.Uint64()
392-
header.Extra, err = dummy.CalcExtraPrefix(cm.config, feeConfig, parent.Header(), time)
393-
if err != nil {
394-
panic(err)
395-
}
396-
header.BaseFee, err = dummy.CalcBaseFee(cm.config, feeConfig, parent.Header(), time)
397-
if err != nil {
398-
panic(err)
399-
}
400391
} else {
401392
header.GasLimit = CalcGasLimit(parent.GasUsed(), parent.GasLimit(), parent.GasLimit(), parent.GasLimit())
402393
}
394+
395+
header.Extra, err = dummy.CalcExtraPrefix(cm.config, feeConfig, parent.Header(), time)
396+
if err != nil {
397+
panic(err)
398+
}
399+
header.BaseFee, err = dummy.CalcBaseFee(cm.config, feeConfig, parent.Header(), time)
400+
if err != nil {
401+
panic(err)
402+
}
403+
403404
if cm.config.IsCancun(header.Number, header.Time) {
404405
var (
405406
parentExcessBlobGas uint64

core/state_processor_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,9 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
444444
Time: parent.Time() + 10,
445445
UncleHash: types.EmptyUncleHash,
446446
}
447-
447+
header.Extra, _ = dummy.CalcExtraPrefix(config, config.FeeConfig, parent.Header(), header.Time)
448+
header.BaseFee, _ = dummy.CalcBaseFee(config, config.FeeConfig, parent.Header(), header.Time)
448449
if config.IsSubnetEVM(header.Time) {
449-
header.Extra, _ = dummy.CalcExtraPrefix(config, config.FeeConfig, parent.Header(), header.Time)
450-
header.BaseFee, _ = dummy.CalcBaseFee(config, config.FeeConfig, parent.Header(), header.Time)
451450
header.BlockGasCost = big.NewInt(0)
452451
}
453452
var receipts []*types.Receipt

miner/worker.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,15 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
167167
Time: timestamp,
168168
}
169169

170-
if w.chainConfig.IsSubnetEVM(timestamp) {
171-
var err error
172-
header.Extra, err = dummy.CalcExtraPrefix(w.chainConfig, feeConfig, parent, timestamp)
173-
if err != nil {
174-
return nil, fmt.Errorf("failed to calculate new extra prefix: %w", err)
175-
}
176-
header.BaseFee, err = dummy.CalcBaseFee(w.chainConfig, feeConfig, parent, timestamp)
177-
if err != nil {
178-
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
179-
}
170+
header.Extra, err = dummy.CalcExtraPrefix(w.chainConfig, feeConfig, parent, timestamp)
171+
if err != nil {
172+
return nil, fmt.Errorf("failed to calculate new extra prefix: %w", err)
173+
}
174+
header.BaseFee, err = dummy.CalcBaseFee(w.chainConfig, feeConfig, parent, timestamp)
175+
if err != nil {
176+
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
180177
}
178+
181179
// Apply EIP-4844, EIP-4788.
182180
if w.chainConfig.IsCancun(header.Number, header.Time) {
183181
var excessBlobGas uint64

0 commit comments

Comments
 (0)