Skip to content

Commit

Permalink
Merge pull request #270 from jamesvan2019/scan
Browse files Browse the repository at this point in the history
Fix scan when rescan a reorg block, the wallet will stop sync
  • Loading branch information
dindinw authored Nov 8, 2022
2 parents 6dac2ce + b4ea9c2 commit 2ecad78
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions rpc/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func recoverFromReorg(chain *blockchain.BlockChain, minBlock, maxBlock uint64,
err))
return nil, cmds.ErrRPCBlockNotFound
}
jsonErr := descendantBlock(lastBlock, blk)
jsonErr := descendantBlock(chain, lastBlock, blk)
if jsonErr != nil {
return nil, jsonErr
}
Expand All @@ -52,13 +52,17 @@ func recoverFromReorg(chain *blockchain.BlockChain, minBlock, maxBlock uint64,

// descendantBlock returns the appropriate JSON-RPC error if a current block
// fetched during a reorganize is not a direct child of the parent block hash.
func descendantBlock(prevHash *hash.Hash, curBlock *types.SerializedBlock) error {
curHash := &curBlock.Block().Header.ParentRoot
if !prevHash.IsEqual(curHash) {
log.Error(fmt.Sprintf("Stopping rescan for reorged block %v "+
"(replaced by block %v)", prevHash, curHash))
return ErrRescanReorg
func descendantBlock(chain *blockchain.BlockChain, lastBlockHash *hash.Hash, curBlock *types.SerializedBlock) error {
preHash, err := chain.BlockHashByOrder(curBlock.Order() - 1)
if err != nil {
return fmt.Errorf("failed fetch order:%v", curBlock.Order()-1)
}
if !preHash.IsEqual(lastBlockHash) {
err = fmt.Errorf("stopping rescan for reorged block %v (replaced by block %v)", lastBlockHash, preHash)
log.Error(err.Error())
return err
}

return nil
}

Expand Down Expand Up @@ -190,7 +194,8 @@ fetchRange:
if i == 0 && lastBlockHash != nil {
// Ensure the new hashList is on the same fork
// as the last block from the old hashList.
jsonErr := descendantBlock(lastBlockHash, blk)
var jsonErr error
jsonErr = descendantBlock(chain, lastBlockHash, blk)
if jsonErr != nil {
return nil, nil, nil, jsonErr
}
Expand Down

0 comments on commit 2ecad78

Please sign in to comment.