From f62a7e22c98bd64b65ec26c3f2a9ce0e816256d9 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 15 Apr 2025 15:36:43 +0200 Subject: [PATCH] alternative approach --- core/blockchain.go | 22 ++++++++++++++++++---- core/rawdb/accessors_chain.go | 6 ------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index d56996dadb..f315a4f71b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -513,19 +513,33 @@ func (bc *BlockChain) loadLastState() error { log.Warn("Empty database, resetting chain") return bc.Reset() } - // Make sure the entire head block is available - headBlock := bc.GetBlockByHash(head) + headHeader := bc.GetHeaderByHash(head) + if headHeader == nil { + // Corrupt or empty database, init from scratch + log.Warn("Head header missing, resetting chain", "hash", head) + return bc.Reset() + } + + var headBlock *types.Block + if cmp := headHeader.Number.Cmp(new(big.Int)); cmp == 1 { + // Make sure the entire head block is available. + headBlock = bc.GetBlockByHash(head) + } else if cmp == 0 { + // On a pruned node the block body might not be available. But a pruned + // block should never be the head block. The only exception is when, as + // a last resort, chain is reset to genesis. + headBlock = bc.genesisBlock + } if headBlock == nil { // Corrupt or empty database, init from scratch log.Warn("Head block missing, resetting chain", "hash", head) return bc.Reset() } // Everything seems to be fine, set as the head block - bc.currentBlock.Store(headBlock.Header()) + bc.currentBlock.Store(headHeader) headBlockGauge.Update(int64(headBlock.NumberU64())) // Restore the last known head header - headHeader := headBlock.Header() if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) { if header := bc.GetHeaderByHash(head); header != nil { headHeader = header diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 9868c47e62..2f62d86e4b 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -440,12 +440,6 @@ func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue // Check if the data is in ancients if isCanon(reader, number, hash) { data, _ = reader.Ancient(ChainFreezerBodiesTable, number) - // The freezer might be pruned. In the particular case of genesis, the block - // will be still available in kvdb. The full genesis block is needed on startup - // sometimes for repair. - if data == nil { - data, _ = db.Get(blockBodyKey(number, hash)) - } return nil } // If not, try reading from leveldb