From 8788d378295b5f28e8f97338d607b0e8971610c2 Mon Sep 17 00:00:00 2001 From: Delweng Date: Wed, 2 Jul 2025 21:40:25 +0800 Subject: [PATCH] core: check rewind head is later than older Signed-off-by: Delweng --- core/blockchain.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index c97897cd70..fe6de0979e 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -735,6 +735,14 @@ func (bc *BlockChain) initializeHistoryPruning(latest uint64) error { // was snap synced or full synced and in which state, the method will try to // delete minimal data from disk whilst retaining chain consistency. func (bc *BlockChain) SetHead(head uint64) error { + // Only allowed to rewind to a block that is later than the oldest state block. + firstStateBlock, err := bc.triedb.FirstStateBlock() + if err != nil { + return err + } + if head < firstStateBlock { + return fmt.Errorf("cannot rewind to block %d, oldest available state is at block %d", head, firstStateBlock) + } if _, err := bc.setHeadBeyondRoot(head, 0, common.Hash{}, false); err != nil { return err }