wallet/chainntfns: prevent sanity check if correctness of birthday block has been verified

In this commit, we prevent any further sanity check attempts by the
wallet if its correctness has previously been verified. We do this to
ensure we don't unnecessarily attempt to find a new candidate.
This commit is contained in:
Wilmer Paulino 2018-11-15 17:59:45 -08:00
parent 7c377b2906
commit f9df4908b3
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -360,12 +360,15 @@ func (w *Wallet) birthdaySanityCheck() (*waddrmgr.BlockStamp, error) {
} }
// We'll then fetch our wallet's birthday timestamp and block. // We'll then fetch our wallet's birthday timestamp and block.
birthdayTimestamp := w.Manager.Birthday() var (
var birthdayBlock waddrmgr.BlockStamp birthdayTimestamp = w.Manager.Birthday()
birthdayBlock waddrmgr.BlockStamp
birthdayBlockVerified bool
)
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error { err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
var err error var err error
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
birthdayBlock, err = w.Manager.BirthdayBlock(ns) birthdayBlock, birthdayBlockVerified, err = w.Manager.BirthdayBlock(ns)
return err return err
}) })
@ -380,6 +383,17 @@ func (w *Wallet) birthdaySanityCheck() (*waddrmgr.BlockStamp, error) {
return nil, err return nil, err
} }
// If the birthday block has already been verified to be correct, we can
// exit our sanity check to prevent potentially fetching a better
// candidate.
if birthdayBlockVerified {
log.Debugf("Birthday block has already been verified: "+
"height=%d, hash=%v", birthdayBlock.Height,
birthdayBlock.Hash)
return &birthdayBlock, nil
}
log.Debugf("Starting sanity check for the wallet's birthday block "+ log.Debugf("Starting sanity check for the wallet's birthday block "+
"from: height=%d, hash=%v", birthdayBlock.Height, "from: height=%d, hash=%v", birthdayBlock.Height,
birthdayBlock.Hash) birthdayBlock.Hash)
@ -505,21 +519,15 @@ func (w *Wallet) birthdaySanityCheck() (*waddrmgr.BlockStamp, error) {
timestampDelta = birthdayTimestamp.Sub(header.Timestamp) timestampDelta = birthdayTimestamp.Sub(header.Timestamp)
} }
// At this point, we've found a valid candidate that satisfies our // At this point, we've found a new, better candidate, so we'll write it
// conditions above. If this is our current birthday block, then we can // to disk.
// exit to avoid the additional database transaction.
if candidate.Hash.IsEqual(&birthdayBlock.Hash) {
return &candidate, nil
}
// Otherwise, we have a new, better candidate, so we'll write it to
// disk.
log.Debugf("Found a new valid wallet birthday block: height=%d, hash=%v", log.Debugf("Found a new valid wallet birthday block: height=%d, hash=%v",
candidate.Height, candidate.Hash) candidate.Height, candidate.Hash)
err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
if err := w.Manager.SetBirthdayBlock(ns, candidate); err != nil { err := w.Manager.SetBirthdayBlock(ns, candidate, true)
if err != nil {
return err return err
} }
return w.Manager.SetSyncedTo(ns, &candidate) return w.Manager.SetSyncedTo(ns, &candidate)