Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scan when rescan a reorg block, the wallet will stop sync #270

Merged
merged 8 commits into from
Nov 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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