Skip to content

Commit

Permalink
wallet: handle corner cases for reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 13, 2022
1 parent b7013bf commit 79bcb83
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,23 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
// before catching up with the rescan.
rollback := false
rollbackStamp := w.Manager.SyncedTo()

// Handle corner cases where the chain has reorged to a lower height
// than the wallet had synced to. This could happen if the chain has
// reorged to a shorter chain with difficulty greater than the current.
// Most of the time, it's only temporary since the chain will grow
// past the previous height anyway. In regtest, however, it burdens
// the testing setup without this check.
bestBlockHash, bestBlockHeight, err := chainClient.GetBestBlock()
if err != nil {
return err
}
if rollbackStamp.Height > bestBlockHeight {
rollbackStamp.Hash = *bestBlockHash
rollbackStamp.Height = bestBlockHeight
rollback = true
}

err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
txmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey)
Expand Down

0 comments on commit 79bcb83

Please sign in to comment.