wallet/wallet: refactor sending transaction to backend into own method

We do this in order to be able to reuse the new publishTransaction
method within other parts of the wallet in order to consolidate all
error string-matching within one place.
This commit is contained in:
Wilmer Paulino 2019-02-20 12:55:45 -08:00
parent b51c1adeee
commit 7e00d1843e
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -3214,7 +3214,7 @@ func (w *Wallet) SendOutputs(outputs []*wire.TxOut, account uint32,
return nil, err return nil, err
} }
txHash, err := w.publishTransaction(createdTx.Tx) txHash, err := w.reliablyPublishTransaction(createdTx.Tx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3370,16 +3370,16 @@ 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) _, err := w.reliablyPublishTransaction(tx)
return err return err
} }
// publishTransaction is the private version of PublishTransaction which // reliablyPublishTransaction is a superset of publishTransaction which contains
// contains the primary logic required for publishing a transaction, updating // the primary logic required for publishing a transaction, updating the
// the relevant database state, and finally possible removing the transaction // relevant database state, and finally possible removing the transaction from
// from the database (along with cleaning up all inputs used, and outputs // the database (along with cleaning up all inputs used, and outputs created) if
// created) if the transaction is rejected by the back end. // the transaction is rejected by the backend.
func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) { func (w *Wallet) reliablyPublishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
chainClient, err := w.requireChainClient() chainClient, err := w.requireChainClient()
if err != nil { if err != nil {
return nil, err return nil, err
@ -3426,6 +3426,19 @@ func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
} }
} }
return w.publishTransaction(tx)
}
// publishTransaction attempts to send an unconfirmed transaction to the
// wallet's current backend. In the event that sending the transaction fails for
// whatever reason, it will be removed from the wallet's unconfirmed transaction
// store.
func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
chainClient, err := w.requireChainClient()
if err != nil {
return nil, err
}
txid, err := chainClient.SendRawTransaction(tx, false) txid, err := chainClient.SendRawTransaction(tx, false)
switch { switch {
case err == nil: case err == nil:
@ -3451,6 +3464,10 @@ func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
// 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)
txRec, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil {
return err
}
return w.TxStore.RemoveUnminedTx(txmgrNs, txRec) return w.TxStore.RemoveUnminedTx(txmgrNs, txRec)
}) })
if dbErr != nil { if dbErr != nil {