From 54b31c1a358bc01f8ab292280746ff167f9eef13 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 21 Feb 2018 16:09:29 -0800 Subject: [PATCH] wallet: remove conflicting double spend transactions on start up --- wallet/wallet.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index 239ef45..d08d27e 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -22,6 +22,7 @@ import ( "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" + "github.com/davecgh/go-spew/spew" "github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil/hdkeychain" @@ -2062,10 +2063,39 @@ func (w *Wallet) resendUnminedTxs() { for _, tx := range txs { resp, err := chainClient.SendRawTransaction(tx, false) if err != nil { - // TODO(jrick): Check error for if this tx is a double spend, - // remove it if so. log.Debugf("Could not resend transaction %v: %v", tx.TxHash(), err) + + // As the transaction was rejected, we'll attempt to + // remove the unmined transaction all together. + // Otherwise, we'll keep attempting to rebroadcast + // this, and we may be computing our balance + // incorrectly if this tx credits or debits to us. + err := walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { + txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey) + + txRec, err := wtxmgr.NewTxRecordFromMsgTx( + tx, time.Now(), + ) + if err != nil { + return err + } + + err = w.TxStore.RemoveUnminedTx(txmgrNs, txRec) + if err != nil { + return err + } + + return err + }) + if err != nil { + log.Warnf("unable to remove conflicting "+ + "tx %v: %v", tx.TxHash(), err) + continue + } + + log.Infof("Removed conflicting tx: %v", spew.Sdump(tx)) + continue } log.Debugf("Resent unmined transaction %v", resp)