From 88736cf7b32670cd31c47732563a88dd66c6ce85 Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Mon, 12 Sep 2022 17:28:07 -0700 Subject: [PATCH 1/2] wallet: revert blockhash properly during reorg --- wallet/chainntfns.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wallet/chainntfns.go b/wallet/chainntfns.go index edad9e9..c63aca4 100644 --- a/wallet/chainntfns.go +++ b/wallet/chainntfns.go @@ -245,7 +245,7 @@ func (w *Wallet) disconnectBlock(dbtx walletdb.ReadWriteTx, b wtxmgr.BlockMeta) if err != nil { return err } - b.Hash = *hash + bs.Hash = *hash client := w.ChainClient() header, err := client.GetBlockHeader(hash) -- 2.45.3 From 50467d1824acd5fbeeba21de52555a33f5acf52e Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Mon, 12 Sep 2022 23:38:13 -0700 Subject: [PATCH 2/2] wallet: handle corner cases for reorg --- wallet/wallet.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/wallet/wallet.go b/wallet/wallet.go index 3b3e5c7..913440b 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -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) -- 2.45.3