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.
This commit is contained in:
Oliver Gugger 2021-07-19 13:26:52 +02:00
parent 60a04006d6
commit 33a0a065f9
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -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)