wallet/wallet: use addRelevantTx when publishing transactions

In this commit, we simplify the logic when broadcasting transactions to
the greater network. Rather than special casing when running with a
Neutrino backend, we'll always add the transaction to the store as
relevant when attempting to broadcast it. This will properly insert it
into the store and update unconfirmed balances. In the event that the
transaction failed to broadcast, it can be removed from the store with
no side-effects, essentially acting as if the transaction was never
added to the store in the first place.
This commit is contained in:
Wilmer Paulino 2018-09-20 18:53:03 -07:00
parent 421298df22
commit 06e70c0f08
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -3312,9 +3312,8 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error {
if err != nil { if err != nil {
return err return err
} }
err = walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error {
txmgrNs := tx.ReadWriteBucket(wtxmgrNamespaceKey) return w.addRelevantTx(dbTx, txRec, nil)
return w.TxStore.InsertTx(txmgrNs, txRec, nil)
}) })
if err != nil { if err != nil {
return err return err
@ -3323,15 +3322,6 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error {
_, err = server.SendRawTransaction(tx, false) _, err = server.SendRawTransaction(tx, false)
switch { switch {
case err == nil: case err == nil:
switch w.chainClient.(type) {
// For neutrino we need to trigger adding relevant tx manually
// because for spv client - tx data isn't received from sync peer.
case *chain.NeutrinoClient:
return walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
return w.addRelevantTx(tx, txRec, nil)
})
}
return nil return nil
// The following are errors returned from btcd's mempool. // The following are errors returned from btcd's mempool.
@ -3348,14 +3338,12 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error {
case strings.Contains(err.Error(), "Missing inputs"): case strings.Contains(err.Error(), "Missing inputs"):
fallthrough fallthrough
case strings.Contains(err.Error(), "already in block chain"): case strings.Contains(err.Error(), "already in block chain"):
// If the transaction was rejected, then we'll remove it from // If the transaction was rejected, then we'll remove it from
// the txstore, as otherwise, we'll attempt to continually // the txstore, as otherwise, we'll attempt to continually
// re-broadcast it, and the utxo state of the wallet won't be // re-broadcast it, and the utxo state of the wallet won't be
// accurate. // accurate.
dbErr := walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { dbErr := walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error {
txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey) txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey)
return w.TxStore.RemoveUnminedTx(txmgrNs, txRec) return w.TxStore.RemoveUnminedTx(txmgrNs, txRec)
}) })
if dbErr != nil { if dbErr != nil {