Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 1465dc2

Browse files
authored
chore(core/types): remove Blocks's Timestamp() method (#749)
- Define `core.BlockContext` implementing `contract.ConfigurationBlockContext` (same as subnet-evm) - Use `core.NewBlockContext` to convert `types.Block` and `types.Header` for calls to `core.ApplyUpgrades`
1 parent 1755289 commit 1465dc2

File tree

9 files changed

+36
-10
lines changed

9 files changed

+36
-10
lines changed

core/genesis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *triedb.Database) *types.Blo
246246
}
247247

248248
// Configure any stateful precompiles that should be enabled in the genesis.
249-
err = ApplyPrecompileActivations(g.Config, nil, types.NewBlockWithHeader(head), statedb)
249+
blockContext := NewBlockContext(head.Number, head.Time)
250+
err = ApplyPrecompileActivations(g.Config, nil, blockContext, statedb)
250251
if err != nil {
251252
panic(fmt.Sprintf("unable to configure precompiles in genesis block: %v", err))
252253
}

core/state_processor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func (p *StateProcessor) Process(block *types.Block, parent *types.Header, state
7878
)
7979

8080
// Configure any upgrades that should go into effect during this block.
81-
err := ApplyUpgrades(p.config, &parent.Time, block, statedb)
81+
blockContext := NewBlockContext(block.Number(), block.Time())
82+
err := ApplyUpgrades(p.config, &parent.Time, blockContext, statedb)
8283
if err != nil {
8384
log.Error("failed to configure precompiles processing block", "hash", block.Hash(), "number", block.NumberU64(), "timestamp", block.Time(), "err", err)
8485
return nil, nil, 0, err

core/state_processor_ext.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package core
44
import (
55
"encoding/json"
66
"fmt"
7+
"math/big"
78

89
"github.com/ava-labs/coreth/core/extstate"
910
"github.com/ava-labs/coreth/core/state"
@@ -73,3 +74,23 @@ func ApplyPrecompileActivations(c *params.ChainConfig, parentTimestamp *uint64,
7374
func ApplyUpgrades(c *params.ChainConfig, parentTimestamp *uint64, blockContext contract.ConfigurationBlockContext, statedb *state.StateDB) error {
7475
return ApplyPrecompileActivations(c, parentTimestamp, blockContext, statedb)
7576
}
77+
78+
// BlockContext implements the `contract.ConfigurationBlockContext` interface.
79+
type BlockContext struct {
80+
number *big.Int
81+
timestamp uint64
82+
}
83+
84+
// NewBlockContext creates a new block context using the block number
85+
// and block time provided. This function is usually necessary to convert
86+
// a `*types.Block` to be passed as a `contract.ConfigurationBlockContext`
87+
// interface to [ApplyUpgrades].
88+
func NewBlockContext(number *big.Int, timestamp uint64) *BlockContext {
89+
return &BlockContext{
90+
number: number,
91+
timestamp: timestamp,
92+
}
93+
}
94+
95+
func (bc *BlockContext) Number() *big.Int { return bc.number }
96+
func (bc *BlockContext) Timestamp() uint64 { return bc.timestamp }

core/types/block.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
339339
func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
340340
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
341341
func (b *Block) Time() uint64 { return b.header.Time }
342-
func (b *Block) Timestamp() uint64 { return b.header.Time }
343342

344343
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
345344
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }

eth/state_accessor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ func (eth *Ethereum) StateAtNextBlock(ctx context.Context, parent *types.Block,
284284
}
285285

286286
// Apply upgrades here for the [nextBlock]
287-
err = core.ApplyUpgrades(eth.blockchain.Config(), &parent.Header().Time, nextBlock, statedb)
287+
blockContext := core.NewBlockContext(nextBlock.Number(), nextBlock.Time())
288+
err = core.ApplyUpgrades(eth.blockchain.Config(), &parent.Header().Time, blockContext, statedb)
288289
if err != nil {
289290
release()
290291
return nil, nil, err

eth/tracers/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,8 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
966966
config.BlockOverrides.Apply(&vmctx)
967967
// Apply all relevant upgrades from [originalTime] to the block time set in the override.
968968
// Should be applied before the state overrides.
969-
err = core.ApplyUpgrades(api.backend.ChainConfig(), &originalTime, block, statedb)
969+
blockContext := core.NewBlockContext(block.Number(), block.Time())
970+
err = core.ApplyUpgrades(api.backend.ChainConfig(), &originalTime, blockContext, statedb)
970971
if err != nil {
971972
return nil, err
972973
}

eth/tracers/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ func (b *testBackend) StateAtNextBlock(ctx context.Context, parent, nextBlock *t
181181
return nil, nil, err
182182
}
183183
// Apply upgrades to the parent state
184-
err = core.ApplyUpgrades(b.chainConfig, &parent.Header().Time, nextBlock, statedb)
184+
blockContext := core.NewBlockContext(nextBlock.Number(), nextBlock.Time())
185+
err = core.ApplyUpgrades(b.chainConfig, &parent.Header().Time, blockContext, statedb)
185186
if err != nil {
186187
release()
187188
return nil, nil, err

miner/worker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
212212
env.state.StopPrefetcher()
213213
}()
214214
// Configure any upgrades that should go into effect during this block.
215-
err = core.ApplyUpgrades(w.chainConfig, &parent.Time, types.NewBlockWithHeader(header), env.state)
215+
blockContext := core.NewBlockContext(header.Number, header.Time)
216+
err = core.ApplyUpgrades(w.chainConfig, &parent.Time, blockContext, env.state)
216217
if err != nil {
217218
log.Error("failed to configure precompiles mining new block", "parent", parent.Hash(), "number", header.Number, "timestamp", header.Time, "err", err)
218219
return nil, err

plugin/evm/block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (b *Block) Accept(context.Context) error {
151151
// Call Accept for relevant precompile logs. Note we do this prior to
152152
// calling Accept on the blockChain so any side effects (eg warp signatures)
153153
// take place before the accepted log is emitted to subscribers.
154-
rules := b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Timestamp())
154+
rules := b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Time())
155155
if err := b.handlePrecompileAccept(*params.GetRulesExtra(rules)); err != nil {
156156
return err
157157
}
@@ -279,7 +279,7 @@ func (b *Block) Verify(context.Context) error {
279279

280280
// ShouldVerifyWithContext implements the block.WithVerifyContext interface
281281
func (b *Block) ShouldVerifyWithContext(context.Context) (bool, error) {
282-
rules := params.GetRulesExtra(b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Timestamp()))
282+
rules := params.GetRulesExtra(b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Time()))
283283
predicates := rules.Predicaters
284284
// Short circuit early if there are no predicates to verify
285285
if len(predicates) == 0 {
@@ -360,7 +360,7 @@ func (b *Block) verify(predicateContext *precompileconfig.PredicateContext, writ
360360

361361
// verifyPredicates verifies the predicates in the block are valid according to predicateContext.
362362
func (b *Block) verifyPredicates(predicateContext *precompileconfig.PredicateContext) error {
363-
rules := b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Timestamp())
363+
rules := b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Time())
364364
rulesExtra := params.GetRulesExtra(rules)
365365

366366
switch {

0 commit comments

Comments
 (0)