Skip to content

Commit

Permalink
Support all upgrades in CalcBaseFee and CalcExtraPrefix (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored and ceyonur committed Feb 28, 2025
1 parent 098c255 commit bcec4a4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
28 changes: 23 additions & 5 deletions consensus/dummy/dynamic_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,41 @@ var (

// CalcExtraPrefix takes the previous header and the timestamp of its child
// block and calculates the expected extra prefix for the child block.
//
// CalcExtraPrefix should only be called if timestamp >= config.ApricotPhase3Timestamp
func CalcExtraPrefix(
config *params.ChainConfig,
feeConfig commontype.FeeConfig,
parent *types.Header,
timestamp uint64,
) ([]byte, error) {
window, err := calcFeeWindow(config, feeConfig, parent, timestamp)
return window.Bytes(), err
switch {
case config.IsSubnetEVM(timestamp):
window, err := calcFeeWindow(config, feeConfig, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate fee window: %w", err)
}
return window.Bytes(), nil
default:
// Prior to SubnetEVM there was no expected extra prefix.
return nil, nil
}
}

// CalcBaseFee takes the previous header and the timestamp of its child block
// and calculates the expected base fee for the child block.
//
// CalcBaseFee should only be called if timestamp >= config.IsSubnetEVMTimestamp
// Prior to SubnetEVM, the returned base fee will be nil.
func CalcBaseFee(config *params.ChainConfig, feeConfig commontype.FeeConfig, parent *types.Header, timestamp uint64) (*big.Int, error) {
switch {
case config.IsSubnetEVM(timestamp):
return calcBaseFeeWithWindow(config, feeConfig, parent, timestamp)
default:
// Prior to SubnetEVM the expected base fee is nil.
return nil, nil
}
}

// calcBaseFeeWithWindow should only be called if `timestamp` >= `config.SubnetEVMTimestamp`.
func calcBaseFeeWithWindow(config *params.ChainConfig, feeConfig commontype.FeeConfig, parent *types.Header, timestamp uint64) (*big.Int, error) {
// If the current block is the first EIP-1559 block, or it is the genesis block
// return the initial slice and initial base fee.
if !config.IsSubnetEVM(parent.Time) || parent.Number.Cmp(common.Big0) == 0 {
Expand Down
27 changes: 14 additions & 13 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,27 +379,28 @@ func (cm *chainMaker) makeHeader(parent *types.Block, gap uint64, state *state.S
ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(),
Difficulty: engine.CalcDifficulty(cm, time, parent.Header()),
GasLimit: parent.GasLimit(),
Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: time,
}
feeConfig, _, err := cm.GetFeeConfigAt(parent.Header())
if err != nil {
panic(err)
}
if cm.config.IsSubnetEVM(time) {
feeConfig, _, err := cm.GetFeeConfigAt(parent.Header())
if err != nil {
panic(err)
}
header.GasLimit = feeConfig.GasLimit.Uint64()
header.Extra, err = dummy.CalcExtraPrefix(cm.config, feeConfig, parent.Header(), time)
if err != nil {
panic(err)
}
header.BaseFee, err = dummy.CalcBaseFee(cm.config, feeConfig, parent.Header(), time)
if err != nil {
panic(err)
}
} else {
header.GasLimit = CalcGasLimit(parent.GasUsed(), parent.GasLimit(), parent.GasLimit(), parent.GasLimit())
}

header.Extra, err = dummy.CalcExtraPrefix(cm.config, feeConfig, parent.Header(), time)
if err != nil {
panic(err)
}
header.BaseFee, err = dummy.CalcBaseFee(cm.config, feeConfig, parent.Header(), time)
if err != nil {
panic(err)
}

if cm.config.IsCancun(header.Number, header.Time) {
var (
parentExcessBlobGas uint64
Expand Down
5 changes: 2 additions & 3 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,9 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
Time: parent.Time() + 10,
UncleHash: types.EmptyUncleHash,
}

header.Extra, _ = dummy.CalcExtraPrefix(config, config.FeeConfig, parent.Header(), header.Time)
header.BaseFee, _ = dummy.CalcBaseFee(config, config.FeeConfig, parent.Header(), header.Time)
if config.IsSubnetEVM(header.Time) {
header.Extra, _ = dummy.CalcExtraPrefix(config, config.FeeConfig, parent.Header(), header.Time)
header.BaseFee, _ = dummy.CalcBaseFee(config, config.FeeConfig, parent.Header(), header.Time)
header.BlockGasCost = big.NewInt(0)
}
var receipts []*types.Receipt
Expand Down
18 changes: 8 additions & 10 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,15 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
Time: timestamp,
}

if w.chainConfig.IsSubnetEVM(timestamp) {
var err error
header.Extra, err = dummy.CalcExtraPrefix(w.chainConfig, feeConfig, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new extra prefix: %w", err)
}
header.BaseFee, err = dummy.CalcBaseFee(w.chainConfig, feeConfig, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
}
header.Extra, err = dummy.CalcExtraPrefix(w.chainConfig, feeConfig, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new extra prefix: %w", err)
}
header.BaseFee, err = dummy.CalcBaseFee(w.chainConfig, feeConfig, parent, timestamp)
if err != nil {
return nil, fmt.Errorf("failed to calculate new base fee: %w", err)
}

// Apply EIP-4844, EIP-4788.
if w.chainConfig.IsCancun(header.Number, header.Time) {
var excessBlobGas uint64
Expand Down

0 comments on commit bcec4a4

Please sign in to comment.