Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getheaderbyhash #1773

Merged
merged 2 commits into from
May 21, 2024
Merged
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
6 changes: 2 additions & 4 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ func (blake3pow *Blake3pow) VerifyHeader(chain consensus.ChainHeaderReader, head
return nil
}
nodeCtx := blake3pow.config.NodeLocation.Context()
// Short circuit if the header is known, or its parent not
number := header.NumberU64(nodeCtx)
if chain.GetHeader(header.Hash(), number) != nil {
if chain.GetHeaderByHash(header.Hash()) != nil {
return nil
}
parent := chain.GetBlockByHash(header.ParentHash(nodeCtx))
Expand Down Expand Up @@ -206,7 +204,7 @@ func (blake3pow *Blake3pow) VerifyUncles(chain consensus.ChainReader, block *typ

number, parent := block.NumberU64(nodeCtx)-1, block.ParentHash(nodeCtx)
for i := 0; i < params.WorkSharesInclusionDepth; i++ {
ancestorHeader := chain.GetHeader(parent, number)
ancestorHeader := chain.GetHeaderByHash(parent)
if ancestorHeader == nil {
break
}
Expand Down
3 changes: 0 additions & 3 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ type ChainHeaderReader interface {
// CurrentHeader retrieves the current header from the local chain.
CurrentHeader() *types.WorkObject

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

// GetHeaderByNumber retrieves a block header from the database by number.
GetHeaderByNumber(number uint64) *types.WorkObject

Expand Down
9 changes: 4 additions & 5 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ func (progpow *Progpow) VerifyHeader(chain consensus.ChainHeaderReader, header *
return nil
}
// Short circuit if the header is known, or its parent not
number := header.NumberU64(nodeCtx)
if chain.GetHeader(header.Hash(), number) != nil {
if chain.GetHeaderByHash(header.Hash()) != nil {
return nil
}
parent := chain.GetHeader(header.ParentHash(nodeCtx), number-1)
parent := chain.GetBlockByHash(header.ParentHash(nodeCtx))
if parent == nil {
return consensus.ErrUnknownAncestor
}
Expand Down Expand Up @@ -178,7 +177,7 @@ func (progpow *Progpow) verifyHeaderWorker(chain consensus.ChainHeaderReader, he
nodeCtx := progpow.NodeLocation().Context()
var parent *types.WorkObject
if index == 0 {
parent = chain.GetHeader(headers[0].ParentHash(nodeCtx), headers[0].NumberU64(nodeCtx)-1)
parent = chain.GetHeaderByHash(headers[0].ParentHash(nodeCtx))
} else if headers[index-1].Hash() == headers[index].ParentHash(nodeCtx) {
parent = headers[index-1]
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func (progpow *Progpow) VerifyUncles(chain consensus.ChainReader, block *types.W

number, parent := block.NumberU64(nodeCtx)-1, block.ParentHash(nodeCtx)
for i := 0; i < params.WorkSharesInclusionDepth; i++ {
ancestorHeader := chain.GetHeader(parent, number)
ancestorHeader := chain.GetHeaderByHash(parent)
if ancestorHeader == nil {
break
}
Expand Down
2 changes: 1 addition & 1 deletion core/chain_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash, node
if hash == (common.Hash{}) {
return common.Hash{}, fmt.Errorf("canonical block #%d unknown", number)
}
header := rawdb.ReadHeader(c.chainDb, hash, number)
header := rawdb.ReadHeader(c.chainDb, hash)
if header == nil {
return common.Hash{}, fmt.Errorf("block #%d [%x..] not found", number, hash[:4])
} else if header.ParentHash(nodeCtx) != lastHead {
Expand Down
6 changes: 0 additions & 6 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,12 +839,6 @@ func (c *Core) CalcOrder(header *types.WorkObject) (*big.Int, int, error) {
return c.engine.CalcOrder(header)
}

// GetHeader retrieves a block header from the database by hash and number,
// caching it if found.
func (c *Core) GetHeader(hash common.Hash, number uint64) *types.WorkObject {
return c.sl.hc.GetHeader(hash, number)
}

// GetHeaderByHash retrieves a block header from the database by hash, caching it if
// found.
func (c *Core) GetHeaderByHash(hash common.Hash) *types.WorkObject {
Expand Down
5 changes: 1 addition & 4 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ type ChainContext interface {
// Engine retrieves the chain's consensus engine.
Engine() consensus.Engine

// GetHeader returns the hash corresponding to their hash.
GetHeader(common.Hash, uint64) *types.WorkObject

// GetHeader returns a block header from the database by hash.
// The header might not be on the canonical chain.
GetHeaderOrCandidate(common.Hash, uint64) *types.WorkObject
Expand Down Expand Up @@ -138,7 +135,7 @@ func GetHashFn(ref *types.WorkObject, chain ChainContext) func(n uint64) common.
lastKnownNumber := ref.NumberU64(chain.NodeCtx()) - uint64(len(cache))

for {
header := chain.GetHeader(lastKnownHash, lastKnownNumber)
header := chain.GetHeaderByHash(lastKnownHash)
if header == nil {
break
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, nodeLoca
}
// We have the genesis block in database(perhaps in ancient database)
// but the corresponding state is missing.
header := rawdb.ReadHeader(db, stored, 0)
header := rawdb.ReadHeader(db, stored)
if _, err := state.New(header.EVMRoot(), header.UTXORoot(), header.EtxSetRoot(), state.NewDatabaseWithConfig(db, nil), state.NewDatabaseWithConfig(db, nil), state.NewDatabaseWithConfig(db, nil), nil, nodeLocation, logger); err != nil {
if genesis == nil {
genesis = DefaultGenesisBlock()
Expand Down
34 changes: 10 additions & 24 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []co
chain := make([]common.Hash, 0, max)
for i := uint64(0); i < max; i++ {
next := header.ParentHash(hc.NodeCtx())
if header = hc.GetHeader(next, header.NumberU64(hc.NodeCtx())-1); header == nil {
if header = hc.GetHeaderByHash(next); header == nil {
break
}
chain = append(chain, next)
Expand All @@ -664,7 +664,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.GetHeaderByHash(hash); header != nil {
return header.ParentHash(hc.NodeCtx()), number - 1
}
return common.Hash{}, 0
Expand All @@ -682,7 +682,7 @@ func (hc *HeaderChain) GetAncestor(hash common.Hash, number, ancestor uint64, ma
}
*maxNonCanonical--
ancestor--
header := hc.GetHeader(hash, number)
header := hc.GetHeaderByHash(hash)
if header == nil {
return common.Hash{}, 0
}
Expand All @@ -696,18 +696,19 @@ func (hc *HeaderChain) WriteBlock(block *types.WorkObject) {
hc.bc.WriteBlock(block, hc.NodeCtx())
}

// 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.WorkObject {
// GetHeaderByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.WorkObject {
termini := hc.GetTerminiByHash(hash)
if termini == nil {
return nil
}

// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.WorkObject)
}
header := rawdb.ReadHeader(hc.headerDb, hash, number)
header := rawdb.ReadHeader(hc.headerDb, hash)
if header == nil {
return nil
}
Expand All @@ -716,29 +717,14 @@ func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.WorkObj
return header
}

// GetHeaderByHash retrieves a block header from the database by hash, caching it if
// found.
func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.WorkObject {
termini := hc.GetTerminiByHash(hash)
if termini == nil {
return nil
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}

return hc.GetHeader(hash, *number)
}

// GetHeaderOrCandidate retrieves a block header from the database by hash and number,
// caching it if found.
func (hc *HeaderChain) GetHeaderOrCandidate(hash common.Hash, number uint64) *types.WorkObject {
// Short circuit if the header's already in the cache, retrieve otherwise
if header, ok := hc.headerCache.Get(hash); ok {
return header.(*types.WorkObject)
}
header := rawdb.ReadHeader(hc.headerDb, hash, number)
header := rawdb.ReadHeader(hc.headerDb, hash)
if header == nil {
return nil
}
Expand Down Expand Up @@ -799,7 +785,7 @@ func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.WorkObject {
if hash == (common.Hash{}) {
return nil
}
return hc.GetHeader(hash, number)
return hc.GetHeaderByHash(hash)
}

func (hc *HeaderChain) GetCanonicalHash(number uint64) common.Hash {
Expand Down
12 changes: 6 additions & 6 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
}

// ReadHeader retrieves the block header corresponding to the hash.
func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.WorkObject {
func ReadHeader(db ethdb.Reader, hash common.Hash) *types.WorkObject {
wo := ReadWorkObjectHeaderOnly(db, hash, types.BlockObject)
if wo == nil || wo.Body() == nil || wo.Header() == nil {
// Try backup function
Expand Down Expand Up @@ -1116,23 +1116,23 @@ func ReadBadWorkObject(db ethdb.Reader, hash common.Hash) *types.WorkObject {
// FindCommonAncestor returns the last common ancestor of two block headers
func FindCommonAncestor(db ethdb.Reader, a, b *types.WorkObject, nodeCtx int) *types.WorkObject {
for bn := b.NumberU64(nodeCtx); a.NumberU64(nodeCtx) > bn; {
a = ReadHeader(db, a.ParentHash(nodeCtx), a.NumberU64(nodeCtx)-1)
a = ReadHeader(db, a.ParentHash(nodeCtx))
if a == nil {
return nil
}
}
for an := a.NumberU64(nodeCtx); an < b.NumberU64(nodeCtx); {
b = ReadHeader(db, b.ParentHash(nodeCtx), b.NumberU64(nodeCtx)-1)
b = ReadHeader(db, b.ParentHash(nodeCtx))
if b == nil {
return nil
}
}
for a.Hash() != b.Hash() {
a = ReadHeader(db, a.ParentHash(nodeCtx), a.NumberU64(nodeCtx)-1)
a = ReadHeader(db, a.ParentHash(nodeCtx))
if a == nil {
return nil
}
b = ReadHeader(db, b.ParentHash(nodeCtx), b.NumberU64(nodeCtx)-1)
b = ReadHeader(db, b.ParentHash(nodeCtx))
if b == nil {
return nil
}
Expand All @@ -1150,7 +1150,7 @@ func ReadHeadHeader(db ethdb.Reader) *types.WorkObject {
if headHeaderNumber == nil {
return nil
}
return ReadHeader(db, headHeaderHash, *headHeaderNumber)
return ReadHeader(db, headHeaderHash)
}

// ReadHeadBlock returns the current canonical head block.
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (f *freezer) freeze(db ethdb.KeyValueStore, nodeCtx int, location common.Lo
backoff = true
continue
}
head := ReadHeader(nfdb, hash, *number)
head := ReadHeader(nfdb, hash)
if head == nil {
f.logger.WithFields(log.Fields{
"number": *number,
Expand Down Expand Up @@ -487,7 +487,7 @@ func (f *freezer) freeze(db ethdb.KeyValueStore, nodeCtx int, location common.Lo
children := ReadAllHashes(db, tip)
for i := 0; i < len(children); i++ {
// Dig up the child and ensure it's dangling
child := ReadHeader(nfdb, children[i], tip)
child := ReadHeader(nfdb, children[i])
if child == nil {
f.logger.WithFields(log.Fields{
"number": tip,
Expand Down
2 changes: 1 addition & 1 deletion core/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ func (sl *Slice) cleanCacheAndDatabaseTillBlock(hash common.Hash) {
// delete the trie node for a given root of the header
rawdb.DeleteTrieNode(sl.sliceDb, header.EVMRoot())
badHashes = append(badHashes, header.Hash())
parent := sl.hc.GetHeader(header.ParentHash(nodeCtx), header.NumberU64(nodeCtx)-1)
parent := sl.hc.GetHeaderByHash(header.ParentHash(nodeCtx))
header = parent
if header.Hash() == hash || sl.hc.IsGenesisHash(header.Hash()) {
break
Expand Down
1 change: 0 additions & 1 deletion core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ type blockChain interface {
CheckIfEtxIsEligible(hash common.Hash, location common.Location) bool
Engine() consensus.Engine
GetHeaderOrCandidate(common.Hash, uint64) *types.WorkObject
GetHeader(common.Hash, uint64) *types.WorkObject
NodeCtx() int
GetHeaderByHash(common.Hash) *types.WorkObject
}
Expand Down
Loading