mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
Merge pull request #627 from wpaulino/wallet-recovery-start-height
wallet: start recovery from the wallet's best height
This commit is contained in:
commit
46c0cf2a3f
1 changed files with 20 additions and 39 deletions
|
@ -359,10 +359,20 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
|
||||||
// arbitrary height, rather than all the blocks from genesis, so
|
// arbitrary height, rather than all the blocks from genesis, so
|
||||||
// we persist this height to ensure we don't store any blocks
|
// we persist this height to ensure we don't store any blocks
|
||||||
// before it.
|
// before it.
|
||||||
startHeight, _, err := w.getSyncRange(chainClient, birthdayStamp)
|
_, bestHeight, err := chainClient.GetBestBlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
startHeight := bestHeight - waddrmgr.MaxReorgDepth + 1
|
||||||
|
if startHeight < 0 {
|
||||||
|
startHeight = 0
|
||||||
|
}
|
||||||
|
if birthdayStamp.Height < startHeight {
|
||||||
|
startHeight = birthdayStamp.Height
|
||||||
|
}
|
||||||
|
|
||||||
|
// With the starting height obtained, get the remaining block
|
||||||
|
// details required by the wallet.
|
||||||
startHash, err := chainClient.GetBlockHash(int64(startHeight))
|
startHash, err := chainClient.GetBlockHash(int64(startHeight))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -643,23 +653,24 @@ func (w *Wallet) recovery(chainClient chain.Interface,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll then need to determine the range of our recovery. This properly
|
// Fetch the best height from the backend to determine when we should
|
||||||
// handles the case where we resume a previous recovery attempt after a
|
// stop.
|
||||||
// restart.
|
_, bestHeight, err := chainClient.GetBestBlock()
|
||||||
startHeight, bestHeight, err := w.getSyncRange(chainClient, birthdayBlock)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we can begin scanning the chain from the specified starting
|
// Now we can begin scanning the chain from the wallet's current tip to
|
||||||
// height. Since the recovery process itself acts as rescan, we'll also
|
// ensure we properly handle restarts. Since the recovery process itself
|
||||||
// update our wallet's synced state along the way to reflect the blocks
|
// acts as rescan, we'll also update our wallet's synced state along the
|
||||||
// we process and prevent rescanning them later on.
|
// way to reflect the blocks we process and prevent rescanning them
|
||||||
|
// later on.
|
||||||
//
|
//
|
||||||
// NOTE: We purposefully don't update our best height since we assume
|
// NOTE: We purposefully don't update our best height since we assume
|
||||||
// that a wallet rescan will be performed from the wallet's tip, which
|
// that a wallet rescan will be performed from the wallet's tip, which
|
||||||
// will be of bestHeight after completing the recovery process.
|
// will be of bestHeight after completing the recovery process.
|
||||||
var blocks []*waddrmgr.BlockStamp
|
var blocks []*waddrmgr.BlockStamp
|
||||||
|
startHeight := w.Manager.SyncedTo().Height + 1
|
||||||
for height := startHeight; height <= bestHeight; height++ {
|
for height := startHeight; height <= bestHeight; height++ {
|
||||||
hash, err := chainClient.GetBlockHash(int64(height))
|
hash, err := chainClient.GetBlockHash(int64(height))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -723,36 +734,6 @@ func (w *Wallet) recovery(chainClient chain.Interface,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSyncRange determines the best height range to sync with the chain to
|
|
||||||
// ensure we don't rescan blocks more than once.
|
|
||||||
func (w *Wallet) getSyncRange(chainClient chain.Interface,
|
|
||||||
birthdayBlock *waddrmgr.BlockStamp) (int32, int32, error) {
|
|
||||||
|
|
||||||
// The wallet requires to store up to MaxReorgDepth blocks, so we'll
|
|
||||||
// start from there, unless our birthday is before it.
|
|
||||||
_, bestHeight, err := chainClient.GetBestBlock()
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, err
|
|
||||||
}
|
|
||||||
startHeight := bestHeight - waddrmgr.MaxReorgDepth + 1
|
|
||||||
if startHeight < 0 {
|
|
||||||
startHeight = 0
|
|
||||||
}
|
|
||||||
if birthdayBlock.Height < startHeight {
|
|
||||||
startHeight = birthdayBlock.Height
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the wallet's tip has surpassed our starting height, then we'll
|
|
||||||
// start there as we don't need to rescan blocks we've already
|
|
||||||
// processed.
|
|
||||||
walletHeight := w.Manager.SyncedTo().Height
|
|
||||||
if walletHeight > startHeight {
|
|
||||||
startHeight = walletHeight
|
|
||||||
}
|
|
||||||
|
|
||||||
return startHeight, bestHeight, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// defaultScopeManagers fetches the ScopedKeyManagers from the wallet using the
|
// defaultScopeManagers fetches the ScopedKeyManagers from the wallet using the
|
||||||
// default set of key scopes.
|
// default set of key scopes.
|
||||||
func (w *Wallet) defaultScopeManagers() (
|
func (w *Wallet) defaultScopeManagers() (
|
||||||
|
|
Loading…
Add table
Reference in a new issue