Skip to content

Commit de68986

Browse files
authored
chore(core/types): remove Blocks's Timestamp() method (#1429)
- Use `core.NewBlockContext` to convert `types.Block` and `types.Header` for calls to `core.ApplyUpgrades`
1 parent f83437d commit de68986

File tree

10 files changed

+21
-16
lines changed

10 files changed

+21
-16
lines changed

core/chain_makers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
284284
b := &BlockGen{i: i, cm: cm, parent: parent, statedb: statedb, engine: engine}
285285
b.header = cm.makeHeader(parent, gap, statedb, b.engine)
286286

287-
err := ApplyUpgrades(config, &parent.Header().Time, b, statedb)
287+
blockContext := NewBlockContext(b.header.Number, b.header.Time)
288+
err := ApplyUpgrades(config, &parent.Header().Time, blockContext, statedb)
288289
if err != nil {
289290
return nil, nil, fmt.Errorf("failed to configure precompiles %w", err)
290291
}

core/genesis.go

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

279279
// Configure any stateful precompiles that should be enabled in the genesis.
280-
err = ApplyPrecompileActivations(g.Config, nil, types.NewBlockWithHeader(head), statedb)
280+
blockContext := NewBlockContext(head.Number, head.Time)
281+
err = ApplyPrecompileActivations(g.Config, nil, blockContext, statedb)
281282
if err != nil {
282283
panic(fmt.Sprintf("unable to configure precompiles in genesis block: %v", err))
283284
}

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/types/block.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
317317
func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
318318
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
319319
func (b *Block) Time() uint64 { return b.header.Time }
320-
func (b *Block) Timestamp() uint64 { return b.header.Time }
321320

322321
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
323322
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_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
@@ -228,7 +228,8 @@ func (w *worker) commitNewWork(predicateContext *precompileconfig.PredicateConte
228228
env.state.StopPrefetcher()
229229
}()
230230
// Configure any upgrades that should go into effect during this block.
231-
err = core.ApplyUpgrades(w.chainConfig, &parent.Time, types.NewBlockWithHeader(header), env.state)
231+
blockContext := core.NewBlockContext(header.Number, header.Time)
232+
err = core.ApplyUpgrades(w.chainConfig, &parent.Time, blockContext, env.state)
232233
if err != nil {
233234
log.Error("failed to configure precompiles mining new block", "parent", parent.Hash(), "number", header.Number, "timestamp", header.Time, "err", err)
234235
return nil, err

plugin/evm/block.go

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

155155
// ShouldVerifyWithContext implements the block.WithVerifyContext interface
156156
func (b *Block) ShouldVerifyWithContext(context.Context) (bool, error) {
157-
rules := params.GetRulesExtra(b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Timestamp()))
157+
rules := params.GetRulesExtra(b.vm.chainConfig.Rules(b.ethBlock.Number(), params.IsMergeTODO, b.ethBlock.Time()))
158158
predicates := rules.Predicaters
159159
// Short circuit early if there are no predicates to verify
160160
if len(predicates) == 0 {
@@ -221,7 +221,7 @@ func (b *Block) verify(predicateContext *precompileconfig.PredicateContext, writ
221221

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

227227
switch {

plugin/evm/vm_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ func TestTxAllowListDisablePrecompile(t *testing.T) {
24032403
require.Equal(t, signedTx0.Hash(), txs[0].Hash())
24042404

24052405
// verify the issued block is after the network upgrade
2406-
require.GreaterOrEqual(t, int64(block.Timestamp()), disableAllowListTimestamp.Unix())
2406+
require.GreaterOrEqual(t, int64(block.Time()), disableAllowListTimestamp.Unix())
24072407

24082408
<-newTxPoolHeadChan // wait for new head in tx pool
24092409

@@ -2783,7 +2783,7 @@ func TestRewardManagerPrecompileSetRewardAddress(t *testing.T) {
27832783
// to determine the coinbase for this block before full deactivation in the
27842784
// next block.
27852785
require.Equal(t, testAddr, ethBlock.Coinbase())
2786-
require.GreaterOrEqual(t, int64(ethBlock.Timestamp()), disableTime.Unix())
2786+
require.GreaterOrEqual(t, int64(ethBlock.Time()), disableTime.Unix())
27872787

27882788
vm.clock.Set(vm.clock.Time().Add(3 * time.Hour)) // let time pass to decrease gas price
27892789
// issue another block to verify that the reward manager is disabled
@@ -2803,7 +2803,7 @@ func TestRewardManagerPrecompileSetRewardAddress(t *testing.T) {
28032803
// reward manager was disabled at previous block
28042804
// so this block should revert back to enabling fee recipients
28052805
require.Equal(t, etherBase, ethBlock.Coinbase())
2806-
require.GreaterOrEqual(t, int64(ethBlock.Timestamp()), disableTime.Unix())
2806+
require.GreaterOrEqual(t, int64(ethBlock.Time()), disableTime.Unix())
28072807

28082808
// Verify that Blackhole has received fees
28092809
blkState, err = vm.blockChain.StateAt(ethBlock.Root())
@@ -2917,7 +2917,7 @@ func TestRewardManagerPrecompileAllowFeeRecipients(t *testing.T) {
29172917
require.Equal(t, newHead.Head.Hash(), common.Hash(blk.ID()))
29182918
ethBlock = blk.(*chain.BlockWrapper).Block.(*Block).ethBlock
29192919
require.Equal(t, etherBase, ethBlock.Coinbase()) // reward address was activated at previous block
2920-
require.GreaterOrEqual(t, int64(ethBlock.Timestamp()), disableTime.Unix())
2920+
require.GreaterOrEqual(t, int64(ethBlock.Time()), disableTime.Unix())
29212921

29222922
vm.clock.Set(vm.clock.Time().Add(3 * time.Hour)) // let time pass so that gas price is reduced
29232923
tx2 = types.NewTransaction(uint64(2), testEthAddrs[0], big.NewInt(2), 21000, big.NewInt(testMinGasPrice), nil)
@@ -2934,7 +2934,7 @@ func TestRewardManagerPrecompileAllowFeeRecipients(t *testing.T) {
29342934
require.Equal(t, newHead.Head.Hash(), common.Hash(blk.ID()))
29352935
ethBlock = blk.(*chain.BlockWrapper).Block.(*Block).ethBlock
29362936
require.Equal(t, constants.BlackholeAddr, ethBlock.Coinbase()) // reward address was activated at previous block
2937-
require.Greater(t, int64(ethBlock.Timestamp()), disableTime.Unix())
2937+
require.Greater(t, int64(ethBlock.Time()), disableTime.Unix())
29382938

29392939
// Verify that Blackhole has received fees
29402940
blkState, err = vm.blockChain.StateAt(ethBlock.Root())

plugin/evm/vm_upgrade_bytes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestVMUpgradeBytesPrecompile(t *testing.T) {
145145
assert.Equal(t, signedTx0.Hash(), txs[0].Hash())
146146

147147
// verify the issued block is after the network upgrade
148-
assert.GreaterOrEqual(t, int64(block.Timestamp()), disableAllowListTimestamp.Unix())
148+
assert.GreaterOrEqual(t, int64(block.Time()), disableAllowListTimestamp.Unix())
149149

150150
<-newTxPoolHeadChan // wait for new head in tx pool
151151

0 commit comments

Comments
 (0)