From fa65c1b5f776b193bb8e7c49c58fa9599d08aaee Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 20 May 2019 16:26:41 -0700 Subject: [PATCH] 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. --- wallet/wallet.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index 341ebfa..6b5e630 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -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) } }