Skip to content

Commit

Permalink
stop SyncedTo at birthday for initialSync
Browse files Browse the repository at this point in the history
  • Loading branch information
roeierez committed Nov 27, 2018
1 parent 7ad4f1e commit bfa2e60
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions wallet/wallet.go
Expand Up @@ -347,7 +347,7 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
// We'll mark this as our first sync if we don't have any unspent
// outputs as known by the wallet. This'll allow us to skip a full
// rescan at this height, and instead wait for the backend to catch up.
isInitialSync := len(unspent) == 0
isInitialSync := birthdayStamp == nil

isRecovery := w.recoveryWindow > 0
birthday := w.Manager.Birthday()
Expand Down Expand Up @@ -504,6 +504,17 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
tx.Rollback()
return err
}

//only set this when birthday has been found
err = w.Manager.SetSyncedTo(ns, &waddrmgr.BlockStamp{
Hash: *hash,
Height: height,
Timestamp: timestamp,
})
if err != nil {
tx.Rollback()
return err
}
}

// If we are in recovery mode and the check
Expand All @@ -516,14 +527,16 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
}
}

err = w.Manager.SetSyncedTo(ns, &waddrmgr.BlockStamp{
Hash: *hash,
Height: height,
Timestamp: timestamp,
})
if err != nil {
tx.Rollback()
return err
if birthdayStamp == nil {
err = w.Manager.SetSyncedTo(ns, &waddrmgr.BlockStamp{
Hash: *hash,
Height: height,
Timestamp: timestamp,
})
if err != nil {
tx.Rollback()
return err
}
}

// If we are in recovery mode, attempt a recovery on
Expand Down Expand Up @@ -653,17 +666,24 @@ 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,
birthdayStamp.Hash)

err := walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
err = w.Manager.SetSyncedTo(ns, &waddrmgr.BlockStamp{
Hash: birthdayStamp.Hash,
Height: birthdayStamp.Height,
Timestamp: birthdayStamp.Timestamp,
})
if err != nil {
tx.Rollback()
return err
}
return w.Manager.SetBirthdayBlock(
ns, *birthdayStamp, true,
)
Expand All @@ -689,11 +709,8 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
// 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.
// Trigger rescan from the last sync point.
return w.rescanWithTarget(addrs, unspent, nil)
}

Expand Down

0 comments on commit bfa2e60

Please sign in to comment.