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.
This commit is contained in:
Wilmer Paulino 2018-09-20 18:59:02 -07:00
parent 06e70c0f08
commit c125b59df4
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -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 // This function is unstable and will be removed once syncing code is moved out
// of the wallet. // of the wallet.
func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error { 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() server, err := w.requireChainClient()
if err != nil { if err != nil {
return err return nil, err
} }
// As we aim for this to be general reliable transaction broadcast API, // 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. // set of records.
txRec, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now()) txRec, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil { if err != nil {
return err return nil, err
} }
err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { err = walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error {
return w.addRelevantTx(dbTx, txRec, nil) return w.addRelevantTx(dbTx, txRec, nil)
}) })
if err != nil { if err != nil {
return err return nil, err
} }
_, err = server.SendRawTransaction(tx, false) txid, err := server.SendRawTransaction(tx, false)
switch { switch {
case err == nil: case err == nil:
return nil return txid, nil
// The following are errors returned from btcd's mempool. // The following are errors returned from btcd's mempool.
case strings.Contains(err.Error(), "spent"): case strings.Contains(err.Error(), "spent"):
@ -3347,14 +3352,14 @@ func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error {
return w.TxStore.RemoveUnminedTx(txmgrNs, txRec) return w.TxStore.RemoveUnminedTx(txmgrNs, txRec)
}) })
if dbErr != nil { 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) "unable to remove invalid tx: %v", err, dbErr)
} }
return err return nil, err
default: default:
return err return nil, err
} }
} }