From c125b59df49642db2c53a8004632e3e2bb32fa37 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 20 Sep 2018 18:59:02 -0700 Subject: [PATCH] wallet/wallet: refactor PublishTransaction to use unexported method In this commit, we refactor the logic outside of PublishTransaction into another unexported method. This will pave the road for unifying the logic between SendOutputs and PublishTransaction. --- wallet/wallet.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index e2ee0c8..d606351 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -3299,9 +3299,14 @@ func (w *Wallet) SignTransaction(tx *wire.MsgTx, hashType txscript.SigHashType, // This function is unstable and will be removed once syncing code is moved out // of the wallet. func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error { + _, err := w.publishTransaction(tx) + return err +} + +func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) { server, err := w.requireChainClient() if err != nil { - return err + return nil, err } // As we aim for this to be general reliable transaction broadcast API, @@ -3310,19 +3315,19 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error { // set of records. txRec, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now()) if err != nil { - return err + return nil, err } err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { return w.addRelevantTx(dbTx, txRec, nil) }) if err != nil { - return err + return nil, err } - _, err = server.SendRawTransaction(tx, false) + txid, err := server.SendRawTransaction(tx, false) switch { case err == nil: - return nil + return txid, nil // The following are errors returned from btcd's mempool. case strings.Contains(err.Error(), "spent"): @@ -3347,14 +3352,14 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error { return w.TxStore.RemoveUnminedTx(txmgrNs, txRec) }) if dbErr != nil { - return fmt.Errorf("unable to broadcast tx: %v, "+ + return nil, fmt.Errorf("unable to broadcast tx: %v, "+ "unable to remove invalid tx: %v", err, dbErr) } - return err + return nil, err default: - return err + return nil, err } }