Skip to content

Commit 79bcb83

Browse files
committed
wallet: handle corner cases for reorg
1 parent b7013bf commit 79bcb83

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

wallet/wallet.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,23 @@ func (w *Wallet) syncWithChain(birthdayStamp *waddrmgr.BlockStamp) error {
430430
// before catching up with the rescan.
431431
rollback := false
432432
rollbackStamp := w.Manager.SyncedTo()
433+
434+
// Handle corner cases where the chain has reorged to a lower height
435+
// than the wallet had synced to. This could happen if the chain has
436+
// reorged to a shorter chain with difficulty greater than the current.
437+
// Most of the time, it's only temporary since the chain will grow
438+
// past the previous height anyway. In regtest, however, it burdens
439+
// the testing setup without this check.
440+
bestBlockHash, bestBlockHeight, err := chainClient.GetBestBlock()
441+
if err != nil {
442+
return err
443+
}
444+
if rollbackStamp.Height > bestBlockHeight {
445+
rollbackStamp.Hash = *bestBlockHash
446+
rollbackStamp.Height = bestBlockHeight
447+
rollback = true
448+
}
449+
433450
err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
434451
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
435452
txmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey)

0 commit comments

Comments
 (0)