diff --git a/wallet/wallet.go b/wallet/wallet.go index b823d90..bd16d71 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -3214,7 +3214,7 @@ func (w *Wallet) SendOutputs(outputs []*wire.TxOut, account uint32, return nil, err } - txHash, err := w.publishTransaction(createdTx.Tx) + txHash, err := w.reliablyPublishTransaction(createdTx.Tx) if err != nil { 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 // of the wallet. func (w *Wallet) PublishTransaction(tx *wire.MsgTx) error { - _, err := w.publishTransaction(tx) + _, err := w.reliablyPublishTransaction(tx) return err } -// publishTransaction is the private version of PublishTransaction which -// contains the primary logic required for publishing a transaction, updating -// the relevant database state, and finally possible removing the transaction -// from the database (along with cleaning up all inputs used, and outputs -// created) if the transaction is rejected by the back end. -func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) { +// reliablyPublishTransaction is a superset of publishTransaction which contains +// the primary logic required for publishing a transaction, updating the +// relevant database state, and finally possible removing the transaction from +// the database (along with cleaning up all inputs used, and outputs created) if +// the transaction is rejected by the backend. +func (w *Wallet) reliablyPublishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) { chainClient, err := w.requireChainClient() if err != nil { 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) switch { case err == nil: @@ -3451,6 +3464,10 @@ func (w *Wallet) publishTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) { // accurate. dbErr := walletdb.Update(w.db, func(dbTx walletdb.ReadWriteTx) error { txmgrNs := dbTx.ReadWriteBucket(wtxmgrNamespaceKey) + txRec, err := wtxmgr.NewTxRecordFromMsgTx(tx, time.Now()) + if err != nil { + return err + } return w.TxStore.RemoveUnminedTx(txmgrNs, txRec) }) if dbErr != nil {