From 419381b7499b02d88af1f758b52653aed07f5bd0 Mon Sep 17 00:00:00 2001 From: Bjarne Magnussen Date: Fri, 4 Jun 2021 08:07:24 +0200 Subject: [PATCH] wallet: call `InsertTxCheckIfExists` to add a relevant tx Let the method `addRelevantTx` use `InsertTxCheckIfExists` to insert a relevant transaction. If the transaction has already been recorded, the method call `addRelevantTx` will return early and not proceed with duplicating work of checking every output and duplicating the tx notification to the notification server. --- wallet/chainntfns.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wallet/chainntfns.go b/wallet/chainntfns.go index 990d8bc..4182ad2 100644 --- a/wallet/chainntfns.go +++ b/wallet/chainntfns.go @@ -280,11 +280,19 @@ func (w *Wallet) addRelevantTx(dbtx walletdb.ReadWriteTx, rec *wtxmgr.TxRecord, // relevant. This assumption will not hold true when SPV support is // added, but until then, simply insert the transaction because there // should either be one or more relevant inputs or outputs. - err := w.TxStore.InsertTx(txmgrNs, rec, block) + exists, err := w.TxStore.InsertTxCheckIfExists(txmgrNs, rec, block) if err != nil { return err } + // If the transaction has already been recorded, we can return early. + // Note: Returning here is safe as we're within the context of an atomic + // database transaction, so we don't need to worry about the MarkUsed + // calls below. + if exists { + return nil + } + // Check every output to determine whether it is controlled by a wallet // key. If so, mark the output as a credit. for i, output := range rec.MsgTx.TxOut {