Merge pull request #718 from wpaulino/relevant-addr-spend

wallet: only request spend notification for our outputs
This commit is contained in:
Olaoluwa Osuntokun 2020-11-05 16:52:34 -08:00 committed by GitHub
commit 9bd6d26f83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3463,7 +3463,35 @@ func (w *Wallet) reliablyPublishTransaction(tx *wire.MsgTx,
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Along the way, we'll extract our relevant destination addresses from
// the transaction.
var ourAddrs []btcutil.Address
err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error {
txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey)
for _, txOut := range tx.TxOut {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
txOut.PkScript, w.chainParams,
)
if err != nil {
// Non-standard outputs can safely be skipped because
// they're not supported by the wallet.
continue
}
for _, addr := range addrs {
// Skip any addresses which are not relevant to
// us.
_, err := w.Manager.Address(txmgrNs, addr)
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
continue
}
if err != nil {
return err
}
ourAddrs = append(ourAddrs, addr)
}
}
if err := w.addRelevantTx(dbTx, txRec, nil); err != nil { if err := w.addRelevantTx(dbTx, txRec, nil); err != nil {
return err return err
} }
@ -3475,7 +3503,6 @@ func (w *Wallet) reliablyPublishTransaction(tx *wire.MsgTx,
// If there is a label we should write, get the namespace key // If there is a label we should write, get the namespace key
// and record it in the tx store. // and record it in the tx store.
txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey)
return w.TxStore.PutTxLabel(txmgrNs, tx.TxHash(), label) return w.TxStore.PutTxLabel(txmgrNs, tx.TxHash(), label)
}) })
if err != nil { if err != nil {
@ -3485,28 +3512,9 @@ func (w *Wallet) reliablyPublishTransaction(tx *wire.MsgTx,
// We'll also ask to be notified of the transaction once it confirms // We'll also ask to be notified of the transaction once it confirms
// on-chain. This is done outside of the database transaction to prevent // on-chain. This is done outside of the database transaction to prevent
// backend interaction within it. // backend interaction within it.
// if err := chainClient.NotifyReceived(ourAddrs); err != nil {
// NOTE: In some cases, it's possible that the transaction to be
// broadcast is not directly relevant to the user's wallet, e.g.,
// multisig. In either case, we'll still ask to be notified of when it
// confirms to maintain consistency.
//
// TODO(wilmer): import script as external if the address does not
// belong to the wallet to handle confs during restarts?
for _, txOut := range tx.TxOut {
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
txOut.PkScript, w.chainParams,
)
if err != nil {
// Non-standard outputs can safely be skipped because
// they're not supported by the wallet.
continue
}
if err := chainClient.NotifyReceived(addrs); err != nil {
return nil, err return nil, err
} }
}
return w.publishTransaction(tx) return w.publishTransaction(tx)
} }