From dcef172f6b3b2594f5478e223fbec9279971e5be Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 17 Apr 2015 16:12:04 -0400 Subject: [PATCH] Rollback txstore on disconnected blocks. Seems that this code got lost over time, and the only place where reorgs were handled were those that happened while wallet was disconnected. --- wallet/chainntfns.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/wallet/chainntfns.go b/wallet/chainntfns.go index 3d9b8be..4406d35 100644 --- a/wallet/chainntfns.go +++ b/wallet/chainntfns.go @@ -44,7 +44,7 @@ func (w *Wallet) handleChainNotifications() { case chain.BlockConnected: w.connectBlock(waddrmgr.BlockStamp(n)) case chain.BlockDisconnected: - w.disconnectBlock(waddrmgr.BlockStamp(n)) + err = w.disconnectBlock(waddrmgr.BlockStamp(n)) case chain.RecvTx: err = w.addReceivedTx(n.Tx, n.Block) case chain.RedeemingTx: @@ -84,9 +84,9 @@ func (w *Wallet) connectBlock(bs waddrmgr.BlockStamp) { // disconnectBlock handles a chain server reorganize by rolling back all // block history from the reorged block for a wallet in-sync with the chain // server. -func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) { +func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) error { if !w.ChainSynced() { - return + return nil } // Disconnect the last seen block from the manager if it matches the @@ -96,6 +96,10 @@ func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) { if iter.Prev() { prev := iter.BlockStamp() w.Manager.SetSyncedTo(&prev) + err := w.TxStore.Rollback(prev.Height) + if err != nil { + return err + } } else { // The reorg is farther back than the recently-seen list // of blocks has recorded, so set it to unsynced which @@ -108,6 +112,8 @@ func (w *Wallet) disconnectBlock(bs waddrmgr.BlockStamp) { w.notifyDisconnectedBlock(bs) w.notifyBalances(bs.Height - 1) + + return nil } func (w *Wallet) addReceivedTx(tx *btcutil.Tx, block *txstore.Block) error {