wallet: store reorg safe height upon initial sync

Currently, wallet rescans start from its known tip of the chain. Since
we no longer store blocks all the way from genesis to the tip of the
chain, performing a rescan would cause us to scan blocks all the way
from genesis, which we want to avoid. To prevent this, we set the
wallet's tip to be the current reorg safe height. This ensures that
we're unable to scan any blocks before it, and that we maintain
MaxReorgDepth blocks stored.
This commit is contained in:
Wilmer Paulino 2019-05-20 16:26:41 -07:00
parent e754478496
commit fa65c1b5f7
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -354,13 +354,39 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
err)
}
// We'll also determine our initial sync starting height. This
// is needed as the wallet can now begin storing blocks from an
// arbitrary height, rather than all the blocks from genesis, so
// we persist this height to ensure we don't store any blocks
// before it.
startHeight, _, err := w.getSyncRange(chainClient, birthdayStamp)
if err != nil {
return err
}
startHash, err := chainClient.GetBlockHash(int64(startHeight))
if err != nil {
return err
}
startHeader, err := chainClient.GetBlockHeader(startHash)
if err != nil {
return err
}
err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
err := w.Manager.SetSyncedTo(ns, &waddrmgr.BlockStamp{
Hash: *startHash,
Height: startHeight,
Timestamp: startHeader.Timestamp,
})
if err != nil {
return err
}
return w.Manager.SetBirthdayBlock(ns, *birthdayStamp, true)
})
if err != nil {
return fmt.Errorf("unable to write birthday block: %v",
err)
return fmt.Errorf("unable to persist initial sync "+
"data: %v", err)
}
}