From 33a0a065f9f95b0221ff4926b7084981fec6b6ec Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 19 Jul 2021 13:26:52 +0200 Subject: [PATCH] wallet: use walletdb.Update in FundPsbt To make sure we don't create any manual DB transactions, we refactor FundPsbt to use walletdb.Update instead. --- wallet/psbt.go | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/wallet/psbt.go b/wallet/psbt.go index 4088a90..3df32cf 100644 --- a/wallet/psbt.go +++ b/wallet/psbt.go @@ -186,33 +186,28 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, keyScope *waddrmgr.KeyScope, inputSource := constantInputSource(credits) // We also need a change source which needs to be able to insert - // a new change addresse into the database. - dbtx, err := w.db.BeginReadWriteTx() - if err != nil { - return 0, err - } - _, changeSource, err := w.addrMgrWithChangeSource( - dbtx, keyScope, account, - ) - if err != nil { - return 0, err - } + // a new change address into the database. + err = walletdb.Update(w.db, func(dbtx walletdb.ReadWriteTx) error { + _, changeSource, err := w.addrMgrWithChangeSource( + dbtx, keyScope, account, + ) + if err != nil { + return err + } - // Ask the txauthor to create a transaction with our selected - // coins. This will perform fee estimation and add a change - // output if necessary. - tx, err = txauthor.NewUnsignedTransaction( - txOut, feeSatPerKB, inputSource, changeSource, - ) - if err != nil { - _ = dbtx.Rollback() - return 0, fmt.Errorf("fee estimation not successful: "+ - "%v", err) - } + // Ask the txauthor to create a transaction with our + // selected coins. This will perform fee estimation and + // add a change output if necessary. + tx, err = txauthor.NewUnsignedTransaction( + txOut, feeSatPerKB, inputSource, changeSource, + ) + if err != nil { + return fmt.Errorf("fee estimation not "+ + "successful: %v", err) + } - // The transaction could be created, let's commit the DB TX to - // store the change address (if one was created). - err = dbtx.Commit() + return nil + }) if err != nil { return 0, fmt.Errorf("could not add change address to "+ "database: %v", err)