Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Allow passing a context into GetHeader #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
txContext := core.NewEVMTxContext(msg)
evmContext := core.NewEVMBlockContext(header, b.blockchain, nil)
evmContext := core.NewEVMBlockContext(nil, header, b.blockchain, nil)
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
gasPool := new(core.GasPool).AddGas(math.MaxUint64)

Expand Down
4 changes: 2 additions & 2 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (beacon *Beacon) VerifyHeader(chain consensus.ChainHeaderReader, header *ty
return beacon.ethone.VerifyHeader(chain, header, seal)
}
// Short circuit if the parent is not known
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
parent := chain.GetHeader(nil, header.ParentHash, header.Number.Uint64()-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func (beacon *Beacon) verifyHeaders(chain consensus.ChainHeaderReader, headers [
if ancestor != nil {
parent = ancestor
} else {
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
parent = chain.GetHeader(nil, headers[0].ParentHash, headers[0].Number.Uint64()-1)
}
} else if headers[i-1].Hash() == headers[i].ParentHash {
parent = headers[i-1]
Expand Down
6 changes: 3 additions & 3 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
if len(parents) > 0 {
parent = parents[len(parents)-1]
} else {
parent = chain.GetHeader(header.ParentHash, number-1)
parent = chain.GetHeader(nil, header.ParentHash, number-1)
}
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor
Expand Down Expand Up @@ -421,7 +421,7 @@ func (c *Clique) snapshot(chain consensus.ChainHeaderReader, number uint64, hash
parents = parents[:len(parents)-1]
} else {
// No explicit parents (or no more left), reach out to the database
header = chain.GetHeader(hash, number)
header = chain.GetHeader(nil, hash, number)
if header == nil {
return nil, consensus.ErrUnknownAncestor
}
Expand Down Expand Up @@ -554,7 +554,7 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
header.MixDigest = common.Hash{}

// Ensure the timestamp has the correct delay
parent := chain.GetHeader(header.ParentHash, number-1)
parent := chain.GetHeader(nil, header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
Expand Down
3 changes: 2 additions & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package consensus

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -37,7 +38,7 @@ type ChainHeaderReader interface {
CurrentHeader() *types.Header

// GetHeader retrieves a block header from the database by hash and number.
GetHeader(hash common.Hash, number uint64) *types.Header
GetHeader(ctx context.Context, hash common.Hash, number uint64) *types.Header

// GetHeaderByNumber retrieves a block header from the database by number.
GetHeaderByNumber(number uint64) *types.Header
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (ethash *Ethash) VerifyHeader(chain consensus.ChainHeaderReader, header *ty
}
// Short circuit if the header is known, or its parent not
number := header.Number.Uint64()
if chain.GetHeader(header.Hash(), number) != nil {
if chain.GetHeader(nil, header.Hash(), number) != nil {
return nil
}
parent := chain.GetHeader(header.ParentHash, number-1)
parent := chain.GetHeader(nil, header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainHeaderReader, headers [
func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool, index int, unixNow int64) error {
var parent *types.Header
if index == 0 {
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
parent = chain.GetHeader(nil, headers[0].ParentHash, headers[0].Number.Uint64()-1)
} else if headers[index-1].Hash() == headers[index].ParentHash {
parent = headers[index-1]
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo

number, parent := block.NumberU64()-1, block.ParentHash()
for i := 0; i < 7; i++ {
ancestorHeader := chain.GetHeader(parent, number)
ancestorHeader := chain.GetHeader(nil, parent, number)
if ancestorHeader == nil {
break
}
Expand Down Expand Up @@ -590,7 +590,7 @@ func (ethash *Ethash) verifySeal(chain consensus.ChainHeaderReader, header *type
// Prepare implements consensus.Engine, initializing the difficulty field of a
// header to conform to the ethash protocol. The changes are done inline.
func (ethash *Ethash) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error {
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
parent := chain.GetHeader(nil, header.ParentHash, header.Number.Uint64()-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
start := time.Now()
parent := it.previous()
if parent == nil {
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
parent = bc.GetHeader(nil, block.ParentHash(), block.NumberU64()-1)
}
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
if err != nil {
Expand Down Expand Up @@ -1959,7 +1959,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
hashes = append(hashes, parent.Hash())
numbers = append(numbers, parent.Number.Uint64())

parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1)
parent = bc.GetHeader(nil, parent.ParentHash, parent.Number.Uint64()-1)
}
if parent == nil {
return it.index, errors.New("missing parent")
Expand Down
5 changes: 3 additions & 2 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -70,8 +71,8 @@ func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool {

// GetHeader retrieves a block header from the database by hash and number,
// caching it if found.
func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header {
return bc.hc.GetHeader(hash, number)
func (bc *BlockChain) GetHeader(_ context.Context, hash common.Hash, number uint64) *types.Header {
return bc.hc.GetHeader(nil, hash, number)
}

// GetHeaderByHash retrieves a block header from the database by hash, caching it if
Expand Down
15 changes: 9 additions & 6 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"context"
"fmt"
"math/big"

Expand Down Expand Up @@ -434,9 +435,11 @@ func (cr *fakeChainReader) Config() *params.ChainConfig {
return cr.config
}

func (cr *fakeChainReader) CurrentHeader() *types.Header { return nil }
func (cr *fakeChainReader) GetHeaderByNumber(number uint64) *types.Header { return nil }
func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *types.Header { return nil }
func (cr *fakeChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { return nil }
func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }
func (cr *fakeChainReader) GetTd(hash common.Hash, number uint64) *big.Int { return nil }
func (cr *fakeChainReader) CurrentHeader() *types.Header { return nil }
func (cr *fakeChainReader) GetHeaderByNumber(number uint64) *types.Header { return nil }
func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *types.Header { return nil }
func (cr *fakeChainReader) GetHeader(ctx context.Context, hash common.Hash, number uint64) *types.Header {
return nil
}
func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }
func (cr *fakeChainReader) GetTd(hash common.Hash, number uint64) *big.Int { return nil }
11 changes: 6 additions & 5 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -32,11 +33,11 @@ type ChainContext interface {
Engine() consensus.Engine

// GetHeader returns the header corresponding to the hash/number argument pair.
GetHeader(common.Hash, uint64) *types.Header
GetHeader(context.Context, common.Hash, uint64) *types.Header
}

// NewEVMBlockContext creates a new context for use in the EVM.
func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
func NewEVMBlockContext(ctx context.Context, header *types.Header, chain ChainContext, author *common.Address) vm.BlockContext {
var (
beneficiary common.Address
baseFee *big.Int
Expand All @@ -58,7 +59,7 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common
return vm.BlockContext{
CanTransfer: CanTransfer,
Transfer: Transfer,
GetHash: GetHashFn(header, chain),
GetHash: GetHashFn(ctx, header, chain),
Coinbase: beneficiary,
BlockNumber: new(big.Int).Set(header.Number),
Time: header.Time,
Expand All @@ -78,7 +79,7 @@ func NewEVMTxContext(msg *Message) vm.TxContext {
}

// GetHashFn returns a GetHashFunc which retrieves header hashes by number
func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
func GetHashFn(ctx context.Context, ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
// Cache will initially contain [refHash.parent],
// Then fill up with [refHash.p, refHash.pp, refHash.ppp, ...]
var cache []common.Hash
Expand All @@ -101,7 +102,7 @@ func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash
lastKnownNumber := ref.Number.Uint64() - uint64(len(cache))

for {
header := chain.GetHeader(lastKnownHash, lastKnownNumber)
header := chain.GetHeader(ctx, lastKnownHash, lastKnownNumber)
if header == nil {
break
}
Expand Down
15 changes: 8 additions & 7 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"context"
crand "crypto/rand"
"errors"
"fmt"
Expand Down Expand Up @@ -168,7 +169,7 @@ func (hc *HeaderChain) Reorg(headers []*types.Header) error {
break // It shouldn't be reached
}
headHash, headNumber = header.ParentHash, header.Number.Uint64()-1
header = hc.GetHeader(headHash, headNumber)
header = hc.GetHeader(nil, headHash, headNumber)
if header == nil {
return fmt.Errorf("missing parent %d %x", headNumber, headHash)
}
Expand Down Expand Up @@ -404,7 +405,7 @@ func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, ma
}
if ancestor == 1 {
// in this case it is cheaper to just read the header
if header := hc.GetHeader(hash, number); header != nil {
if header := hc.GetHeader(nil, hash, number); header != nil {
return header.ParentHash, number - 1
}
return common.Hash{}, 0
Expand All @@ -422,7 +423,7 @@ func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, ma
}
*maxNonCanonical--
ancestor--
header := hc.GetHeader(hash, number)
header := hc.GetHeader(nil, hash, number)
if header == nil {
return common.Hash{}, 0
}
Expand Down Expand Up @@ -450,7 +451,7 @@ func (hc *HeaderChain) GetTd(hash common.Hash, number uint64) *big.Int {

// GetHeader retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header {
func (hc *HeaderChain) GetHeader(_ context.Context, hash common.Hash, number uint64) *types.Header {
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header
Expand All @@ -471,7 +472,7 @@ func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header {
if number == nil {
return nil
}
return hc.GetHeader(hash, *number)
return hc.GetHeader(nil, hash, *number)
}

// HasHeader checks if a block header is present in the database or not.
Expand All @@ -491,7 +492,7 @@ func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header {
if hash == (common.Hash{}) {
return nil
}
return hc.GetHeader(hash, number)
return hc.GetHeader(nil, hash, number)
}

// GetHeadersFrom returns a contiguous segment of headers, in rlp-form, going
Expand Down Expand Up @@ -606,7 +607,7 @@ func (hc *HeaderChain) setHead(headBlock uint64, headTime uint64, updateFn Updat
num := hdr.Number.Uint64()

// Rewind chain to new head
parent := hc.GetHeader(hdr.ParentHash, num-1)
parent := hc.GetHeader(nil, hdr.ParentHash, num-1)
if parent == nil {
parent = hc.genesisHeader
}
Expand Down
2 changes: 1 addition & 1 deletion core/state_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb state.StateDBI, c
var (
header = block.Header()
gaspool = new(GasPool).AddGas(block.GasLimit())
blockContext = NewEVMBlockContext(header, p.bc, nil)
blockContext = NewEVMBlockContext(nil, header, p.bc, nil)
evm = vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
signer = types.MakeSigner(p.config, header.Number)
)
Expand Down
4 changes: 2 additions & 2 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb state.StateDBI, cfg
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
misc.ApplyDAOHardFork(statedb)
}
blockContext := NewEVMBlockContext(header, p.bc, nil)
blockContext := NewEVMBlockContext(nil, header, p.bc, nil)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
Expand Down Expand Up @@ -152,7 +152,7 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
return nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
blockContext := NewEVMBlockContext(nil, header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
}
2 changes: 1 addition & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (b *EthAPIBackend) GetEVM(ctx context.Context, msg *core.Message, state sta
vmConfig = b.eth.blockchain.GetVMConfig()
}
txContext := core.NewEVMTxContext(msg)
context := core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
context := core.NewEVMBlockContext(nil, header, b.eth.BlockChain(), nil)
return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), state.Error, nil
}

Expand Down
4 changes: 2 additions & 2 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func (api *ConsensusAPI) checkInvalidAncestor(check common.Hash, head common.Has
}
// If the last valid hash is the terminal pow block, return 0x0 for latest valid hash
lastValid := &invalid.ParentHash
if header := api.eth.BlockChain().GetHeader(invalid.ParentHash, invalid.Number.Uint64()-1); header != nil && header.Difficulty.Sign() != 0 {
if header := api.eth.BlockChain().GetHeader(nil, invalid.ParentHash, invalid.Number.Uint64()-1); header != nil && header.Difficulty.Sign() != 0 {
lastValid = &common.Hash{}
}
failure := "links to previously rejected block"
Expand Down Expand Up @@ -749,7 +749,7 @@ func (api *ConsensusAPI) heartbeat() {
current = head
)
for i := 0; i < 64; i++ {
parent := chain.GetHeader(current.ParentHash, current.Number.Uint64()-1)
parent := chain.GetHeader(nil, current.ParentHash, current.Number.Uint64()-1)
if parent == nil {
break
}
Expand Down
2 changes: 1 addition & 1 deletion eth/protocols/eth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func serviceNonContiguousBlockHeaderQuery(chain *core.BlockChain, query *GetBloc
query.Origin.Number = origin.Number.Uint64()
}
} else {
origin = chain.GetHeader(query.Origin.Hash, query.Origin.Number)
origin = chain.GetHeader(nil, query.Origin.Hash, query.Origin.Number)
}
} else {
origin = chain.GetHeaderByNumber(query.Origin.Number)
Expand Down
2 changes: 1 addition & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
for idx, tx := range block.Transactions() {
// Assemble the transaction call message and return if the requested offset
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil)
context := core.NewEVMBlockContext(nil, block.Header(), eth.blockchain, nil)
if idx == txIndex {
return msg, context, statedb, release, nil
}
Expand Down
Loading