From 4f5baed780ba77a6af6914e62b71ad240906ebea Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 15 Nov 2018 13:58:07 -0800 Subject: [PATCH] wallet/wallet: prevent always rescanning from birthday block In this commit, we address an issue with the wallet where it would always request a rescan from the birthday block. This is very crucial for older wallets, as it'll potentially go through thousands of blocks. To address this, we'll now only request a rescan from our birthday if we're recovering our wallet from our seed, the birthday block was rolled back, or if we're performing our initial sync. Otherwise, we'll request a rescan from tip. --- wallet/wallet.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index 059dd6e..8455975 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -653,8 +653,10 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error { // If a birthday stamp was found during the initial sync and the // rollback causes us to revert it, update the birthday stamp so that it // points at the new tip. + birthdayRollback := false if birthdayStamp != nil && rollbackStamp.Height <= birthdayStamp.Height { birthdayStamp = &rollbackStamp + birthdayRollback = true log.Debugf("Found new birthday block after rollback: "+ "height=%d, hash=%v", birthdayStamp.Height, @@ -681,7 +683,16 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error { return err } - return w.rescanWithTarget(addrs, unspent, birthdayStamp) + // If this was our initial sync, we're recovering from our seed, or our + // birthday was rolled back due to a chain reorg, we'll dispatch a + // rescan from our birthday block to ensure we detect all relevant + // on-chain events from this point. + if isInitialSync || isRecovery || birthdayRollback { + return w.rescanWithTarget(addrs, unspent, birthdayStamp) + } + + // Otherwise, we'll rescan from tip. + return w.rescanWithTarget(addrs, unspent, nil) } // defaultScopeManagers fetches the ScopedKeyManagers from the wallet using the