Skip to content

Commit 8c6d5a8

Browse files
committed
fixup! rewind to last non verified block when restart fast node
1 parent be53153 commit 8c6d5a8

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

core/remote_state_verifier.go

+17-27
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,25 @@ func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure b
6262
if block == nil {
6363
return nil, ErrCurrentBlockNotFound
6464
}
65-
number := block.Number()
66-
for i := maxForkHeight; i >= 0; i-- {
67-
if new(big.Int).Sub(number, big.NewInt(int64(i))).Cmp(common.Big0) <= 0 {
68-
continue
69-
}
70-
oldBlock := blockchain.GetBlockByNumber(number.Uint64() - uint64(i))
71-
if oldBlock == nil {
72-
return nil, fmt.Errorf("block is nil, number: %d", number)
73-
}
74-
75-
// rewind to last non verified block
76-
blockchain.SetHead(oldBlock.NumberU64())
77-
block = oldBlock
78-
break
79-
}
8065

81-
number = block.Number()
82-
for i := maxForkHeight; i >= 0; i-- {
83-
if new(big.Int).Sub(number, big.NewInt(int64(i))).Cmp(common.Big0) <= 0 {
84-
continue
85-
}
86-
oldBlock := blockchain.GetBlockByNumber(number.Uint64() - uint64(i))
87-
if oldBlock == nil {
88-
return nil, fmt.Errorf("block is nil, number: %d", number)
66+
// rewind to last non verified block
67+
number := new(big.Int).Sub(block.Number(), big.NewInt(int64(maxForkHeight)))
68+
if number.Cmp(common.Big0) < 0 {
69+
blockchain.SetHead(0)
70+
} else {
71+
numberU64 := number.Uint64()
72+
blockchain.SetHead(numberU64)
73+
block := blockchain.GetBlockByNumber(numberU64)
74+
for i := 0; i < maxForkHeight; i++ {
75+
// When inserting a block,
76+
// the block before 11 blocks will be verified,
77+
// so the parent block of 11-22 will directly write the verification information.
78+
verifiedCache.Add(block.Hash(), true)
79+
block = blockchain.GetBlockByHash(block.ParentHash())
80+
if block == nil {
81+
return nil, fmt.Errorf("block is nil, number: %d", number)
82+
}
8983
}
90-
// When inserting a block,
91-
// the block before 11 blocks will be verified,
92-
// so the parent block of 11-22 will directly write the verification information.
93-
verifiedCache.Add(oldBlock.Hash(), true)
9484
}
9585

9686
vm := &remoteVerifyManager{

0 commit comments

Comments
 (0)