diff --git a/cmd/gc/main.go b/cmd/gc/main.go index cb1797898042..f6c76581dec7 100644 --- a/cmd/gc/main.go +++ b/cmd/gc/main.go @@ -54,7 +54,7 @@ func main() { flag.Parse() db, _ := leveldb.New(*dir, ethconfig.Defaults.DatabaseCache, utils.MakeDatabaseHandles(0), "") lddb := rawdb.NewDatabase(db) - head := core.ReadHeadBlockHash(lddb) + head := rawdb.ReadHeadBlockHash(lddb) currentHeader := core.GetHeader(lddb, head, core.GetBlockNumber(lddb, head)) tridb := trie.NewDatabase(lddb) catchEventInterupt(db) diff --git a/core/blockchain.go b/core/blockchain.go index 8aef7d293a06..1877a95411fd 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -306,7 +306,7 @@ func (bc *BlockChain) addXDCxDb(XDCxDb ethdb.XDCxDatabase) { // assumes that the chain manager mutex is held. func (bc *BlockChain) loadLastState() error { // Restore the last known head block - head := ReadHeadBlockHash(bc.db) + head := rawdb.ReadHeadBlockHash(bc.db) if head == (common.Hash{}) { // Corrupt or empty database, init from scratch log.Warn("Empty database, resetting chain") diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d01c8bdc7906..8d63b4ca20c3 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -174,7 +174,7 @@ func TestLastBlock(t *testing.T) { if _, err := blockchain.InsertChain(blocks); err != nil { t.Fatalf("Failed to insert block: %v", err) } - if blocks[len(blocks)-1].Hash() != ReadHeadBlockHash(blockchain.db) { + if blocks[len(blocks)-1].Hash() != rawdb.ReadHeadBlockHash(blockchain.db) { t.Fatalf("Write/Get HeadBlockHash failed") } } diff --git a/core/database_util.go b/core/database_util.go index c71cde4625f7..5c6d3608645f 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -85,15 +85,6 @@ func GetBlockNumber(db rawdb.DatabaseReader, hash common.Hash) uint64 { return binary.BigEndian.Uint64(data) } -// ReadHeadBlockHash retrieves the hash of the current canonical head block. -func ReadHeadBlockHash(db rawdb.DatabaseReader) common.Hash { - data, _ := db.Get(headBlockKey) - if len(data) == 0 { - return common.Hash{} - } - return common.BytesToHash(data) -} - // GetTrieSyncProgress retrieves the number of tries nodes fast synced to allow // reportinc correct numbers across restarts. func GetTrieSyncProgress(db rawdb.DatabaseReader) uint64 { diff --git a/core/database_util_test.go b/core/database_util_test.go index ee74f67a2827..a33823133d51 100644 --- a/core/database_util_test.go +++ b/core/database_util_test.go @@ -240,7 +240,7 @@ func TestHeadStorage(t *testing.T) { if entry := rawdb.ReadHeadHeaderHash(db); entry != (common.Hash{}) { t.Fatalf("Non head header entry returned: %v", entry) } - if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) { + if entry := rawdb.ReadHeadBlockHash(db); entry != (common.Hash{}) { t.Fatalf("Non head block entry returned: %v", entry) } if entry := rawdb.ReadHeadFastBlockHash(db); entry != (common.Hash{}) { @@ -254,7 +254,7 @@ func TestHeadStorage(t *testing.T) { if entry := rawdb.ReadHeadHeaderHash(db); entry != blockHead.Hash() { t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash()) } - if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() { + if entry := rawdb.ReadHeadBlockHash(db); entry != blockFull.Hash() { t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash()) } if entry := rawdb.ReadHeadFastBlockHash(db); entry != blockFast.Hash() { diff --git a/core/headerchain.go b/core/headerchain.go index 155568a37ad4..6318ac9d0652 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -100,7 +100,7 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c } hc.currentHeader.Store(hc.genesisHeader) - if head := ReadHeadBlockHash(chainDb); head != (common.Hash{}) { + if head := rawdb.ReadHeadBlockHash(chainDb); head != (common.Hash{}) { if chead := hc.GetHeaderByHash(head); chead != nil { hc.currentHeader.Store(chead) } diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 5cc788e49165..e30619dd3685 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -110,6 +110,15 @@ func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) { } } +// ReadHeadBlockHash retrieves the hash of the current canonical head block. +func ReadHeadBlockHash(db DatabaseReader) common.Hash { + data, _ := db.Get(headBlockKey) + if len(data) == 0 { + return common.Hash{} + } + return common.BytesToHash(data) +} + // WriteHeadBlockHash stores the head block's hash. func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) { if err := db.Put(headBlockKey, hash.Bytes()); err != nil { diff --git a/eth/filters/bench_test.go b/eth/filters/bench_test.go index 612752ce3e9a..983481943112 100644 --- a/eth/filters/bench_test.go +++ b/eth/filters/bench_test.go @@ -71,7 +71,7 @@ func benchmarkBloomBits(b *testing.B, sectionSize uint64) { if err != nil { b.Fatalf("error opening database at %v: %v", benchDataDir, err) } - head := core.ReadHeadBlockHash(db) + head := rawdb.ReadHeadBlockHash(db) if head == (common.Hash{}) { b.Fatalf("chain data not found at %v", benchDataDir) } @@ -183,7 +183,7 @@ func BenchmarkNoBloomBits(b *testing.B) { if err != nil { b.Fatalf("error opening database at %v: %v", benchDataDir, err) } - head := core.ReadHeadBlockHash(db) + head := rawdb.ReadHeadBlockHash(db) if head == (common.Hash{}) { b.Fatalf("chain data not found at %v", benchDataDir) } diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index 0a8a5fd2465c..6b08f736e7e4 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -60,7 +60,7 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe var hash common.Hash var num uint64 if blockNr == rpc.LatestBlockNumber { - hash = core.ReadHeadBlockHash(b.db) + hash = rawdb.ReadHeadBlockHash(b.db) num = core.GetBlockNumber(b.db, hash) } else { num = uint64(blockNr)