diff --git a/consensus/blake3pow/consensus.go b/consensus/blake3pow/consensus.go index 63c9da0755..f1dc0901c6 100644 --- a/consensus/blake3pow/consensus.go +++ b/consensus/blake3pow/consensus.go @@ -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)) @@ -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 } diff --git a/consensus/consensus.go b/consensus/consensus.go index 0d892c6c81..8749db87ad 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -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 diff --git a/consensus/progpow/consensus.go b/consensus/progpow/consensus.go index 192a5297fa..80a5550520 100644 --- a/consensus/progpow/consensus.go +++ b/consensus/progpow/consensus.go @@ -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 } @@ -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] } @@ -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 } diff --git a/core/chain_indexer.go b/core/chain_indexer.go index 141ff28f54..bfec3c4c16 100644 --- a/core/chain_indexer.go +++ b/core/chain_indexer.go @@ -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 { diff --git a/core/core.go b/core/core.go index 3b9ae47b97..87b5d5927f 100644 --- a/core/core.go +++ b/core/core.go @@ -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 { diff --git a/core/evm.go b/core/evm.go index bfe2af1bb7..bcc44353ed 100644 --- a/core/evm.go +++ b/core/evm.go @@ -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 @@ -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 } diff --git a/core/genesis.go b/core/genesis.go index c1b9c8b765..2c5a71a09f 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -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() diff --git a/core/headerchain.go b/core/headerchain.go index b0a416f6fb..521ae4b86e 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -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) @@ -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 @@ -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 } @@ -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 } @@ -716,21 +717,6 @@ 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 { @@ -738,7 +724,7 @@ func (hc *HeaderChain) GetHeaderOrCandidate(hash common.Hash, number uint64) *ty 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 } @@ -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 { diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 488d9a8339..94c0c38c52 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -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 @@ -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 } @@ -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. diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index aa24d73019..dd38ef5ddc 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -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, @@ -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, diff --git a/core/slice.go b/core/slice.go index 2ce48dbe85..8f69cc566c 100644 --- a/core/slice.go +++ b/core/slice.go @@ -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 diff --git a/core/tx_pool.go b/core/tx_pool.go index 638f81a540..10981b6bf8 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -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 }