Skip to content

Commit cd31f2d

Browse files
authored
all: change chain head markers from block to header (#26777)
1 parent e1b98f4 commit cd31f2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+462
-412
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis
9595
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{})
9696
backend.events = filters.NewEventSystem(backend.filterSystem, false)
9797

98-
backend.rollback(blockchain.CurrentBlock())
98+
header := backend.blockchain.CurrentBlock()
99+
block := backend.blockchain.GetBlock(header.Hash(), header.Number.Uint64())
100+
101+
backend.rollback(block)
99102
return backend
100103
}
101104

@@ -135,7 +138,10 @@ func (b *SimulatedBackend) Rollback() {
135138
b.mu.Lock()
136139
defer b.mu.Unlock()
137140

138-
b.rollback(b.blockchain.CurrentBlock())
141+
header := b.blockchain.CurrentBlock()
142+
block := b.blockchain.GetBlock(header.Hash(), header.Number.Uint64())
143+
144+
b.rollback(block)
139145
}
140146

141147
func (b *SimulatedBackend) rollback(parent *types.Block) {
@@ -174,7 +180,7 @@ func (b *SimulatedBackend) Fork(ctx context.Context, parent common.Hash) error {
174180

175181
// stateByBlockNumber retrieves a state by a given blocknumber.
176182
func (b *SimulatedBackend) stateByBlockNumber(ctx context.Context, blockNumber *big.Int) (*state.StateDB, error) {
177-
if blockNumber == nil || blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) == 0 {
183+
if blockNumber == nil || blockNumber.Cmp(b.blockchain.CurrentBlock().Number) == 0 {
178184
return b.blockchain.State()
179185
}
180186
block, err := b.blockByNumber(ctx, blockNumber)
@@ -303,7 +309,7 @@ func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (
303309
// (associated with its hash) if found without Lock.
304310
func (b *SimulatedBackend) blockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
305311
if number == nil || number.Cmp(b.pendingBlock.Number()) == 0 {
306-
return b.blockchain.CurrentBlock(), nil
312+
return b.blockByHash(ctx, b.blockchain.CurrentBlock().Hash())
307313
}
308314

309315
block := b.blockchain.GetBlockByNumber(uint64(number.Int64()))
@@ -431,7 +437,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM
431437
b.mu.Lock()
432438
defer b.mu.Unlock()
433439

434-
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
440+
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number) != 0 {
435441
return nil, errBlockNumberUnsupported
436442
}
437443
stateDB, err := b.blockchain.State()
@@ -455,7 +461,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu
455461
defer b.mu.Unlock()
456462
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
457463

458-
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
464+
res, err := b.callContract(ctx, call, b.pendingBlock.Header(), b.pendingState)
459465
if err != nil {
460466
return nil, err
461467
}
@@ -549,7 +555,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
549555
call.Gas = gas
550556

551557
snapshot := b.pendingState.Snapshot()
552-
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
558+
res, err := b.callContract(ctx, call, b.pendingBlock.Header(), b.pendingState)
553559
b.pendingState.RevertToSnapshot(snapshot)
554560

555561
if err != nil {
@@ -599,7 +605,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
599605

600606
// callContract implements common code between normal and pending contract calls.
601607
// state is modified during execution, make sure to copy it if necessary.
602-
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, stateDB *state.StateDB) (*core.ExecutionResult, error) {
608+
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, header *types.Header, stateDB *state.StateDB) (*core.ExecutionResult, error) {
603609
// Gas prices post 1559 need to be initialized
604610
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) {
605611
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
@@ -645,7 +651,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
645651
msg := callMsg{call}
646652

647653
txContext := core.NewEVMTxContext(msg)
648-
evmContext := core.NewEVMBlockContext(block.Header(), b.blockchain, nil)
654+
evmContext := core.NewEVMBlockContext(header, b.blockchain, nil)
649655
// Create a new environment which holds all relevant information
650656
// about the transaction and calling mechanisms.
651657
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
@@ -854,15 +860,9 @@ func (fb *filterBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNum
854860
case rpc.LatestBlockNumber:
855861
return fb.bc.CurrentHeader(), nil
856862
case rpc.FinalizedBlockNumber:
857-
if block := fb.bc.CurrentFinalizedBlock(); block != nil {
858-
return block.Header(), nil
859-
}
860-
return nil, errors.New("finalized block not found")
863+
return fb.bc.CurrentFinalBlock(), nil
861864
case rpc.SafeBlockNumber:
862-
if block := fb.bc.CurrentSafeBlock(); block != nil {
863-
return block.Header(), nil
864-
}
865-
return nil, errors.New("safe block not found")
865+
return fb.bc.CurrentSafeBlock(), nil
866866
default:
867867
return fb.bc.GetHeaderByNumber(uint64(number.Int64())), nil
868868
}

accounts/abi/bind/backends/simulated_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ func TestFork(t *testing.T) {
11891189
sim.Commit()
11901190
}
11911191
// 3.
1192-
if sim.blockchain.CurrentBlock().NumberU64() != uint64(n) {
1192+
if sim.blockchain.CurrentBlock().Number.Uint64() != uint64(n) {
11931193
t.Error("wrong chain length")
11941194
}
11951195
// 4.
@@ -1199,7 +1199,7 @@ func TestFork(t *testing.T) {
11991199
sim.Commit()
12001200
}
12011201
// 6.
1202-
if sim.blockchain.CurrentBlock().NumberU64() != uint64(n+1) {
1202+
if sim.blockchain.CurrentBlock().Number.Uint64() != uint64(n+1) {
12031203
t.Error("wrong chain length")
12041204
}
12051205
}
@@ -1344,7 +1344,7 @@ func TestCommitReturnValue(t *testing.T) {
13441344
sim := simTestBackend(testAddr)
13451345
defer sim.Close()
13461346

1347-
startBlockHeight := sim.blockchain.CurrentBlock().NumberU64()
1347+
startBlockHeight := sim.blockchain.CurrentBlock().Number.Uint64()
13481348

13491349
// Test if Commit returns the correct block hash
13501350
h1 := sim.Commit()

cmd/geth/chaincmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ func exportChain(ctx *cli.Context) error {
349349
if first < 0 || last < 0 {
350350
utils.Fatalf("Export error: block number must be greater than 0\n")
351351
}
352-
if head := chain.CurrentFastBlock(); uint64(last) > head.NumberU64() {
353-
utils.Fatalf("Export error: block number %d larger than head block %d\n", uint64(last), head.NumberU64())
352+
if head := chain.CurrentSnapBlock(); uint64(last) > head.Number.Uint64() {
353+
utils.Fatalf("Export error: block number %d larger than head block %d\n", uint64(last), head.Number.Uint64())
354354
}
355355
err = utils.ExportAppendChain(chain, fp, uint64(first), uint64(last))
356356
}

cmd/utils/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block
222222
head := chain.CurrentBlock()
223223
for i, block := range blocks {
224224
// If we're behind the chain head, only check block, state is available at head
225-
if head.NumberU64() > block.NumberU64() {
225+
if head.Number.Uint64() > block.NumberU64() {
226226
if !chain.HasBlock(block.Hash(), block.NumberU64()) {
227227
return blocks[i:]
228228
}

consensus/clique/clique_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestReimportMirroredState(t *testing.T) {
9393
if _, err := chain.InsertChain(blocks[:2]); err != nil {
9494
t.Fatalf("failed to insert initial blocks: %v", err)
9595
}
96-
if head := chain.CurrentBlock().NumberU64(); head != 2 {
96+
if head := chain.CurrentBlock().Number.Uint64(); head != 2 {
9797
t.Fatalf("chain head mismatch: have %d, want %d", head, 2)
9898
}
9999

@@ -106,7 +106,7 @@ func TestReimportMirroredState(t *testing.T) {
106106
if _, err := chain.InsertChain(blocks[2:]); err != nil {
107107
t.Fatalf("failed to insert final block: %v", err)
108108
}
109-
if head := chain.CurrentBlock().NumberU64(); head != 3 {
109+
if head := chain.CurrentBlock().Number.Uint64(); head != 3 {
110110
t.Fatalf("chain head mismatch: have %d, want %d", head, 3)
111111
}
112112
}

0 commit comments

Comments
 (0)