LCOV - code coverage report
Current view: top level - src/wallet - feebumper.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 87.0 % 200 174
Test Date: 2024-11-04 05:10:19 Functions: 85.7 % 7 6
Branches: 52.3 % 348 182

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <common/system.h>
       6                 :             : #include <consensus/validation.h>
       7                 :             : #include <interfaces/chain.h>
       8                 :             : #include <node/types.h>
       9                 :             : #include <policy/fees.h>
      10                 :             : #include <policy/policy.h>
      11                 :             : #include <util/moneystr.h>
      12                 :             : #include <util/rbf.h>
      13                 :             : #include <util/translation.h>
      14                 :             : #include <wallet/coincontrol.h>
      15                 :             : #include <wallet/feebumper.h>
      16                 :             : #include <wallet/fees.h>
      17                 :             : #include <wallet/receive.h>
      18                 :             : #include <wallet/spend.h>
      19                 :             : #include <wallet/wallet.h>
      20                 :             : 
      21                 :             : namespace wallet {
      22                 :             : //! Check whether transaction has descendant in wallet or mempool, or has been
      23                 :             : //! mined, or conflicts with a mined transaction. Return a feebumper::Result.
      24                 :         223 : static feebumper::Result PreconditionChecks(const CWallet& wallet, const CWalletTx& wtx, bool require_mine, std::vector<bilingual_str>& errors) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
      25                 :             : {
      26         [ +  + ]:         223 :     if (wallet.HasWalletSpend(wtx.tx)) {
      27         [ +  - ]:           6 :         errors.push_back(Untranslated("Transaction has descendants in the wallet"));
      28                 :           2 :         return feebumper::Result::INVALID_PARAMETER;
      29                 :             :     }
      30                 :             : 
      31                 :         221 :     {
      32         [ +  + ]:         221 :         if (wallet.chain().hasDescendantsInMempool(wtx.GetHash())) {
      33         [ +  - ]:           3 :             errors.push_back(Untranslated("Transaction has descendants in the mempool"));
      34                 :           1 :             return feebumper::Result::INVALID_PARAMETER;
      35                 :             :         }
      36                 :             :     }
      37                 :             : 
      38         [ -  + ]:         220 :     if (wallet.GetTxDepthInMainChain(wtx) != 0) {
      39         [ #  # ]:           0 :         errors.push_back(Untranslated("Transaction has been mined, or is conflicted with a mined transaction"));
      40                 :           0 :         return feebumper::Result::WALLET_ERROR;
      41                 :             :     }
      42                 :             : 
      43         [ +  + ]:         220 :     if (!SignalsOptInRBF(*wtx.tx)) {
      44         [ +  - ]:           6 :         errors.push_back(Untranslated("Transaction is not BIP 125 replaceable"));
      45                 :           2 :         return feebumper::Result::WALLET_ERROR;
      46                 :             :     }
      47                 :             : 
      48         [ +  + ]:         218 :     if (wtx.mapValue.count("replaced_by_txid")) {
      49   [ +  -  +  -  :           4 :         errors.push_back(strprintf(Untranslated("Cannot bump transaction %s which was already bumped by transaction %s"), wtx.GetHash().ToString(), wtx.mapValue.at("replaced_by_txid")));
          +  -  +  -  +  
                      - ]
      50                 :           1 :         return feebumper::Result::WALLET_ERROR;
      51                 :             :     }
      52                 :             : 
      53         [ +  + ]:         217 :     if (require_mine) {
      54                 :             :         // check that original tx consists entirely of our inputs
      55                 :             :         // if not, we can't bump the fee, because the wallet has no way of knowing the value of the other inputs (thus the fee)
      56   [ -  +  -  - ]:         113 :         isminefilter filter = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
      57         [ +  + ]:         113 :         if (!AllInputsMine(wallet, *wtx.tx, filter)) {
      58         [ +  - ]:           3 :             errors.push_back(Untranslated("Transaction contains inputs that don't belong to this wallet"));
      59                 :           1 :             return feebumper::Result::WALLET_ERROR;
      60                 :             :         }
      61                 :             :     }
      62                 :             : 
      63                 :             :     return feebumper::Result::OK;
      64                 :             : }
      65                 :             : 
      66                 :             : //! Check if the user provided a valid feeRate
      67                 :          31 : static feebumper::Result CheckFeeRate(const CWallet& wallet, const CMutableTransaction& mtx, const CFeeRate& newFeerate, const int64_t maxTxSize, CAmount old_fee, std::vector<bilingual_str>& errors)
      68                 :             : {
      69                 :             :     // check that fee rate is higher than mempool's minimum fee
      70                 :             :     // (no point in bumping fee if we know that the new tx won't be accepted to the mempool)
      71                 :             :     // This may occur if the user set fee_rate or paytxfee too low, if fallbackfee is too low, or, perhaps,
      72                 :             :     // in a rare situation where the mempool minimum fee increased significantly since the fee estimation just a
      73                 :             :     // moment earlier. In this case, we report an error to the user, who may adjust the fee.
      74                 :          31 :     CFeeRate minMempoolFeeRate = wallet.chain().mempoolMinFee();
      75                 :             : 
      76         [ -  + ]:          31 :     if (newFeerate.GetFeePerK() < minMempoolFeeRate.GetFeePerK()) {
      77         [ #  # ]:           0 :         errors.push_back(strprintf(
      78   [ #  #  #  # ]:           0 :             Untranslated("New fee rate (%s) is lower than the minimum fee rate (%s) to get into the mempool -- "),
      79         [ #  # ]:           0 :             FormatMoney(newFeerate.GetFeePerK()),
      80         [ #  # ]:           0 :             FormatMoney(minMempoolFeeRate.GetFeePerK())));
      81                 :           0 :         return feebumper::Result::WALLET_ERROR;
      82                 :             :     }
      83                 :             : 
      84                 :          31 :     std::vector<COutPoint> reused_inputs;
      85         [ +  - ]:          31 :     reused_inputs.reserve(mtx.vin.size());
      86         [ +  + ]:          70 :     for (const CTxIn& txin : mtx.vin) {
      87         [ +  - ]:          39 :         reused_inputs.push_back(txin.prevout);
      88                 :             :     }
      89                 :             : 
      90         [ +  - ]:          31 :     std::optional<CAmount> combined_bump_fee = wallet.chain().calculateCombinedBumpFee(reused_inputs, newFeerate);
      91         [ -  + ]:          31 :     if (!combined_bump_fee.has_value()) {
      92   [ #  #  #  #  :           0 :         errors.push_back(strprintf(Untranslated("Failed to calculate bump fees, because unconfirmed UTXOs depend on enormous cluster of unconfirmed transactions.")));
                   #  # ]
      93                 :             :     }
      94   [ +  -  +  - ]:          31 :     CAmount new_total_fee = newFeerate.GetFee(maxTxSize) + combined_bump_fee.value();
      95                 :             : 
      96         [ +  - ]:          31 :     CFeeRate incrementalRelayFee = wallet.chain().relayIncrementalFee();
      97                 :             : 
      98                 :             :     // Min total fee is old fee + relay fee
      99         [ +  - ]:          31 :     CAmount minTotalFee = old_fee + incrementalRelayFee.GetFee(maxTxSize);
     100                 :             : 
     101         [ +  + ]:          31 :     if (new_total_fee < minTotalFee) {
     102   [ +  -  +  -  :          44 :         errors.push_back(strprintf(Untranslated("Insufficient total fee %s, must be at least %s (oldFee %s + incrementalFee %s)"),
                   +  - ]
     103   [ +  -  +  -  :          22 :             FormatMoney(new_total_fee), FormatMoney(minTotalFee), FormatMoney(old_fee), FormatMoney(incrementalRelayFee.GetFee(maxTxSize))));
          +  -  +  -  +  
                      - ]
     104                 :          11 :         return feebumper::Result::INVALID_PARAMETER;
     105                 :             :     }
     106                 :             : 
     107         [ +  - ]:          20 :     CAmount requiredFee = GetRequiredFee(wallet, maxTxSize);
     108         [ -  + ]:          20 :     if (new_total_fee < requiredFee) {
     109   [ #  #  #  #  :           0 :         errors.push_back(strprintf(Untranslated("Insufficient total fee (cannot be less than required fee %s)"),
                   #  # ]
     110         [ #  # ]:           0 :             FormatMoney(requiredFee)));
     111                 :           0 :         return feebumper::Result::INVALID_PARAMETER;
     112                 :             :     }
     113                 :             : 
     114                 :             :     // Check that in all cases the new fee doesn't violate maxTxFee
     115                 :          20 :     const CAmount max_tx_fee = wallet.m_default_max_tx_fee;
     116         [ +  + ]:          20 :     if (new_total_fee > max_tx_fee) {
     117   [ +  -  +  -  :           4 :         errors.push_back(strprintf(Untranslated("Specified or calculated fee %s is too high (cannot be higher than -maxtxfee %s)"),
                   +  - ]
     118   [ +  -  +  - ]:           2 :             FormatMoney(new_total_fee), FormatMoney(max_tx_fee)));
     119                 :           1 :         return feebumper::Result::WALLET_ERROR;
     120                 :             :     }
     121                 :             : 
     122                 :             :     return feebumper::Result::OK;
     123                 :          31 : }
     124                 :             : 
     125                 :          88 : static CFeeRate EstimateFeeRate(const CWallet& wallet, const CWalletTx& wtx, const CAmount old_fee, const CCoinControl& coin_control)
     126                 :             : {
     127                 :             :     // Get the fee rate of the original transaction. This is calculated from
     128                 :             :     // the tx fee/vsize, so it may have been rounded down. Add 1 satoshi to the
     129                 :             :     // result.
     130                 :          88 :     int64_t txSize = GetVirtualTransactionSize(*(wtx.tx));
     131                 :          88 :     CFeeRate feerate(old_fee, txSize);
     132                 :          88 :     feerate += CFeeRate(1);
     133                 :             : 
     134                 :             :     // The node has a configurable incremental relay fee. Increment the fee by
     135                 :             :     // the minimum of that and the wallet's conservative
     136                 :             :     // WALLET_INCREMENTAL_RELAY_FEE value to future proof against changes to
     137                 :             :     // network wide policy for incremental relay fee that our node may not be
     138                 :             :     // aware of. This ensures we're over the required relay fee rate
     139                 :             :     // (Rule 4).  The replacement tx will be at least as large as the
     140                 :             :     // original tx, so the total fee will be greater (Rule 3)
     141                 :          88 :     CFeeRate node_incremental_relay_fee = wallet.chain().relayIncrementalFee();
     142         [ +  - ]:          88 :     CFeeRate wallet_incremental_relay_fee = CFeeRate(WALLET_INCREMENTAL_RELAY_FEE);
     143         [ +  - ]:         176 :     feerate += std::max(node_incremental_relay_fee, wallet_incremental_relay_fee);
     144                 :             : 
     145                 :             :     // Fee rate must also be at least the wallet's GetMinimumFeeRate
     146                 :          88 :     CFeeRate min_feerate(GetMinimumFeeRate(wallet, coin_control, /*feeCalc=*/nullptr));
     147                 :             : 
     148                 :             :     // Set the required fee rate for the replacement transaction in coin control.
     149         [ +  + ]:          88 :     return std::max(feerate, min_feerate);
     150                 :             : }
     151                 :             : 
     152                 :             : namespace feebumper {
     153                 :             : 
     154                 :           0 : bool TransactionCanBeBumped(const CWallet& wallet, const uint256& txid)
     155                 :             : {
     156                 :           0 :     LOCK(wallet.cs_wallet);
     157         [ #  # ]:           0 :     const CWalletTx* wtx = wallet.GetWalletTx(txid);
     158         [ #  # ]:           0 :     if (wtx == nullptr) return false;
     159                 :             : 
     160                 :           0 :     std::vector<bilingual_str> errors_dummy;
     161         [ #  # ]:           0 :     feebumper::Result res = PreconditionChecks(wallet, *wtx, /* require_mine=*/ true, errors_dummy);
     162                 :           0 :     return res == feebumper::Result::OK;
     163                 :           0 : }
     164                 :             : 
     165                 :         129 : Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCoinControl& coin_control, std::vector<bilingual_str>& errors,
     166                 :             :                                  CAmount& old_fee, CAmount& new_fee, CMutableTransaction& mtx, bool require_mine, const std::vector<CTxOut>& outputs, std::optional<uint32_t> original_change_index)
     167                 :             : {
     168                 :             :     // For now, cannot specify both new outputs to use and an output index to send change
     169   [ +  +  +  + ]:         129 :     if (!outputs.empty() && original_change_index.has_value()) {
     170         [ +  - ]:           3 :         errors.push_back(Untranslated("The options 'outputs' and 'original_change_index' are incompatible. You can only either specify a new set of outputs, or designate a change output to be recycled."));
     171                 :           1 :         return Result::INVALID_PARAMETER;
     172                 :             :     }
     173                 :             : 
     174                 :             :     // We are going to modify coin control later, copy to reuse
     175                 :         128 :     CCoinControl new_coin_control(coin_control);
     176                 :             : 
     177         [ +  - ]:         128 :     LOCK(wallet.cs_wallet);
     178                 :         128 :     errors.clear();
     179         [ +  - ]:         128 :     auto it = wallet.mapWallet.find(txid);
     180         [ -  + ]:         128 :     if (it == wallet.mapWallet.end()) {
     181   [ #  #  #  # ]:           0 :         errors.push_back(Untranslated("Invalid or non-wallet transaction id"));
     182                 :           0 :         return Result::INVALID_ADDRESS_OR_KEY;
     183                 :             :     }
     184         [ +  + ]:         128 :     const CWalletTx& wtx = it->second;
     185                 :             : 
     186                 :             :     // Make sure that original_change_index is valid
     187   [ +  +  +  + ]:         128 :     if (original_change_index.has_value() && original_change_index.value() >= wtx.tx->vout.size()) {
     188   [ +  -  +  - ]:           3 :         errors.push_back(Untranslated("Change position is out of range"));
     189                 :           1 :         return Result::INVALID_PARAMETER;
     190                 :             :     }
     191                 :             : 
     192                 :             :     // Retrieve all of the UTXOs and add them to coin control
     193                 :             :     // While we're here, calculate the input amount
     194                 :         127 :     std::map<COutPoint, Coin> coins;
     195                 :         127 :     CAmount input_value = 0;
     196                 :         127 :     std::vector<CTxOut> spent_outputs;
     197         [ +  + ]:         410 :     for (const CTxIn& txin : wtx.tx->vin) {
     198         [ +  - ]:         283 :         coins[txin.prevout]; // Create empty map entry keyed by prevout.
     199                 :             :     }
     200         [ +  - ]:         127 :     wallet.chain().findCoins(coins);
     201         [ +  + ]:         409 :     for (const CTxIn& txin : wtx.tx->vin) {
     202         [ +  - ]:         283 :         const Coin& coin = coins.at(txin.prevout);
     203         [ +  + ]:         283 :         if (coin.out.IsNull()) {
     204   [ +  -  +  -  :           3 :             errors.push_back(Untranslated(strprintf("%s:%u is already spent", txin.prevout.hash.GetHex(), txin.prevout.n)));
                   +  - ]
     205                 :           1 :             return Result::MISC_ERROR;
     206                 :             :         }
     207         [ +  - ]:         282 :         PreselectedInput& preset_txin = new_coin_control.Select(txin.prevout);
     208   [ +  -  +  + ]:         282 :         if (!wallet.IsMine(txin.prevout)) {
     209         [ +  - ]:           3 :             preset_txin.SetTxOut(coin.out);
     210                 :             :         }
     211                 :         282 :         input_value += coin.out.nValue;
     212         [ +  - ]:         282 :         spent_outputs.push_back(coin.out);
     213                 :             :     }
     214                 :             : 
     215                 :             :     // Figure out if we need to compute the input weight, and do so if necessary
     216                 :         126 :     PrecomputedTransactionData txdata;
     217         [ +  - ]:         126 :     txdata.Init(*wtx.tx, std::move(spent_outputs), /* force=*/ true);
     218         [ +  + ]:         408 :     for (unsigned int i = 0; i < wtx.tx->vin.size(); ++i) {
     219         [ +  - ]:         282 :         const CTxIn& txin = wtx.tx->vin.at(i);
     220         [ +  - ]:         282 :         const Coin& coin = coins.at(txin.prevout);
     221                 :             : 
     222   [ +  -  +  + ]:         282 :         if (new_coin_control.IsExternalSelected(txin.prevout)) {
     223                 :             :             // For external inputs, we estimate the size using the size of this input
     224                 :           3 :             int64_t input_weight = GetTransactionInputWeight(txin);
     225                 :             :             // Because signatures can have different sizes, we need to figure out all of the
     226                 :             :             // signature sizes and replace them with the max sized signature.
     227                 :             :             // In order to do this, we verify the script with a special SignatureChecker which
     228                 :             :             // will observe the signatures verified and record their sizes.
     229                 :           3 :             SignatureWeights weights;
     230         [ +  - ]:           3 :             TransactionSignatureChecker tx_checker(wtx.tx.get(), i, coin.out.nValue, txdata, MissingDataBehavior::FAIL);
     231         [ +  - ]:           3 :             SignatureWeightChecker size_checker(weights, tx_checker);
     232         [ +  - ]:           3 :             VerifyScript(txin.scriptSig, coin.out.scriptPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, size_checker);
     233                 :             :             // Add the difference between max and current to input_weight so that it represents the largest the input could be
     234         [ +  - ]:           3 :             input_weight += weights.GetWeightDiffToMax();
     235         [ +  - ]:           3 :             new_coin_control.SetInputWeight(txin.prevout, input_weight);
     236                 :           3 :         }
     237                 :             :     }
     238                 :             : 
     239         [ +  - ]:         126 :     Result result = PreconditionChecks(wallet, wtx, require_mine, errors);
     240         [ +  + ]:         126 :     if (result != Result::OK) {
     241                 :             :         return result;
     242                 :             :     }
     243                 :             : 
     244                 :             :     // Calculate the old output amount.
     245                 :         119 :     CAmount output_value = 0;
     246         [ +  + ]:         447 :     for (const auto& old_output : wtx.tx->vout) {
     247                 :         328 :         output_value += old_output.nValue;
     248                 :             :     }
     249                 :             : 
     250                 :         119 :     old_fee = input_value - output_value;
     251                 :             : 
     252                 :             :     // Fill in recipients (and preserve a single change key if there
     253                 :             :     // is one). If outputs vector is non-empty, replace original
     254                 :             :     // outputs with its contents, otherwise use original outputs.
     255                 :         119 :     std::vector<CRecipient> recipients;
     256                 :         119 :     CAmount new_outputs_value = 0;
     257         [ +  + ]:         119 :     const auto& txouts = outputs.empty() ? wtx.tx->vout : outputs;
     258         [ +  + ]:         343 :     for (size_t i = 0; i < txouts.size(); ++i) {
     259         [ +  - ]:         224 :         const CTxOut& output = txouts.at(i);
     260                 :         224 :         CTxDestination dest;
     261         [ +  - ]:         224 :         ExtractDestination(output.scriptPubKey, dest);
     262   [ +  +  +  -  :         224 :         if (original_change_index.has_value() ?  original_change_index.value() == i : OutputIsChange(wallet, output)) {
                   +  + ]
     263         [ +  - ]:         110 :             new_coin_control.destChange = dest;
     264                 :             :         } else {
     265         [ +  - ]:         114 :             CRecipient recipient = {dest, output.nValue, false};
     266         [ +  - ]:         114 :             recipients.push_back(recipient);
     267                 :         114 :         }
     268                 :         224 :         new_outputs_value += output.nValue;
     269                 :         224 :     }
     270                 :             : 
     271                 :             :     // If no recipients, means that we are sending coins to a change address
     272         [ +  + ]:         119 :     if (recipients.empty()) {
     273                 :             :         // Just as a sanity check, ensure that the change address exist
     274         [ +  - ]:           5 :         if (std::get_if<CNoDestination>(&new_coin_control.destChange)) {
     275   [ #  #  #  #  :           0 :             errors.emplace_back(Untranslated("Unable to create transaction. Transaction must have at least one recipient"));
                   #  # ]
     276                 :           0 :             return Result::INVALID_PARAMETER;
     277                 :             :         }
     278                 :             : 
     279                 :             :         // Add change as recipient with SFFO flag enabled, so fees are deduced from it.
     280                 :             :         // If the output differs from the original tx output (because the user customized it) a new change output will be created.
     281   [ +  -  +  - ]:          10 :         recipients.emplace_back(CRecipient{new_coin_control.destChange, new_outputs_value, /*fSubtractFeeFromAmount=*/true});
     282                 :           5 :         new_coin_control.destChange = CNoDestination();
     283                 :             :     }
     284                 :             : 
     285         [ +  + ]:         119 :     if (coin_control.m_feerate) {
     286                 :             :         // The user provided a feeRate argument.
     287                 :             :         // We calculate this here to avoid compiler warning on the cs_wallet lock
     288                 :             :         // We need to make a temporary transaction with no input witnesses as the dummy signer expects them to be empty for external inputs
     289         [ +  - ]:          31 :         CMutableTransaction temp_mtx{*wtx.tx};
     290         [ +  + ]:          70 :         for (auto& txin : temp_mtx.vin) {
     291                 :          39 :             txin.scriptSig.clear();
     292                 :          39 :             txin.scriptWitness.SetNull();
     293                 :             :         }
     294         [ +  - ]:          31 :         temp_mtx.vout = txouts;
     295   [ +  -  +  - ]:          31 :         const int64_t maxTxSize{CalculateMaximumSignedTxSize(CTransaction(temp_mtx), &wallet, &new_coin_control).vsize};
     296         [ +  - ]:          31 :         Result res = CheckFeeRate(wallet, temp_mtx, *new_coin_control.m_feerate, maxTxSize, old_fee, errors);
     297         [ +  + ]:          31 :         if (res != Result::OK) {
     298                 :          12 :             return res;
     299                 :             :         }
     300                 :          31 :     } else {
     301                 :             :         // The user did not provide a feeRate argument
     302         [ +  - ]:          88 :         new_coin_control.m_feerate = EstimateFeeRate(wallet, wtx, old_fee, new_coin_control);
     303                 :             :     }
     304                 :             : 
     305                 :             :     // Fill in required inputs we are double-spending(all of them)
     306                 :             :     // N.B.: bip125 doesn't require all the inputs in the replaced transaction to be
     307                 :             :     // used in the replacement transaction, but it's very important for wallets to make
     308                 :             :     // sure that happens. If not, it would be possible to bump a transaction A twice to
     309                 :             :     // A2 and A3 where A2 and A3 don't conflict (or alternatively bump A to A2 and A2
     310                 :             :     // to A3 where A and A3 don't conflict). If both later get confirmed then the sender
     311                 :             :     // has accidentally double paid.
     312         [ +  + ]:         367 :     for (const auto& inputs : wtx.tx->vin) {
     313         [ +  - ]:         260 :         new_coin_control.Select(COutPoint(inputs.prevout));
     314                 :             :     }
     315                 :         107 :     new_coin_control.m_allow_other_inputs = true;
     316                 :             : 
     317                 :             :     // We cannot source new unconfirmed inputs(bip125 rule 2)
     318                 :         107 :     new_coin_control.m_min_depth = 1;
     319                 :             : 
     320         [ +  - ]:         107 :     auto res = CreateTransaction(wallet, recipients, /*change_pos=*/std::nullopt, new_coin_control, false);
     321         [ +  + ]:         107 :     if (!res) {
     322   [ +  -  +  -  :          21 :         errors.push_back(Untranslated("Unable to create transaction.") + Untranslated(" ") + util::ErrorString(res));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     323                 :           3 :         return Result::WALLET_ERROR;
     324                 :             :     }
     325                 :             : 
     326                 :         104 :     const auto& txr = *res;
     327                 :             :     // Write back new fee if successful
     328                 :         104 :     new_fee = txr.fee;
     329                 :             : 
     330                 :             :     // Write back transaction
     331         [ +  - ]:         104 :     mtx = CMutableTransaction(*txr.tx);
     332                 :             : 
     333                 :         104 :     return Result::OK;
     334                 :         374 : }
     335                 :             : 
     336                 :          97 : bool SignTransaction(CWallet& wallet, CMutableTransaction& mtx) {
     337                 :          97 :     LOCK(wallet.cs_wallet);
     338                 :             : 
     339   [ +  -  +  + ]:          97 :     if (wallet.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
     340                 :             :         // Make a blank psbt
     341         [ +  - ]:           1 :         PartiallySignedTransaction psbtx(mtx);
     342                 :             : 
     343                 :             :         // First fill transaction with our data without signing,
     344                 :             :         // so external signers are not asked to sign more than once.
     345                 :           1 :         bool complete;
     346         [ +  - ]:           1 :         wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
     347         [ +  - ]:           1 :         auto err{wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, true /* sign */, false  /* bip32derivs */)};
     348         [ +  - ]:           1 :         if (err) return false;
     349         [ +  - ]:           1 :         complete = FinalizeAndExtractPSBT(psbtx, mtx);
     350                 :           1 :         return complete;
     351                 :           1 :     } else {
     352         [ +  - ]:          96 :         return wallet.SignTransaction(mtx);
     353                 :             :     }
     354                 :          97 : }
     355                 :             : 
     356                 :          97 : Result CommitTransaction(CWallet& wallet, const uint256& txid, CMutableTransaction&& mtx, std::vector<bilingual_str>& errors, uint256& bumped_txid)
     357                 :             : {
     358                 :          97 :     LOCK(wallet.cs_wallet);
     359         [ +  - ]:          97 :     if (!errors.empty()) {
     360                 :             :         return Result::MISC_ERROR;
     361                 :             :     }
     362   [ -  +  +  - ]:          97 :     auto it = txid.IsNull() ? wallet.mapWallet.end() : wallet.mapWallet.find(txid);
     363         [ -  + ]:          97 :     if (it == wallet.mapWallet.end()) {
     364   [ #  #  #  # ]:           0 :         errors.push_back(Untranslated("Invalid or non-wallet transaction id"));
     365                 :           0 :         return Result::MISC_ERROR;
     366                 :             :     }
     367         [ +  - ]:          97 :     const CWalletTx& oldWtx = it->second;
     368                 :             : 
     369                 :             :     // make sure the transaction still has no descendants and hasn't been mined in the meantime
     370         [ +  - ]:          97 :     Result result = PreconditionChecks(wallet, oldWtx, /* require_mine=*/ false, errors);
     371         [ +  - ]:          97 :     if (result != Result::OK) {
     372                 :             :         return result;
     373                 :             :     }
     374                 :             : 
     375                 :             :     // commit/broadcast the tx
     376         [ +  - ]:          97 :     CTransactionRef tx = MakeTransactionRef(std::move(mtx));
     377         [ +  - ]:          97 :     mapValue_t mapValue = oldWtx.mapValue;
     378   [ +  -  +  -  :          97 :     mapValue["replaces_txid"] = oldWtx.GetHash().ToString();
                   +  - ]
     379                 :             : 
     380   [ +  -  +  -  :         388 :     wallet.CommitTransaction(tx, std::move(mapValue), oldWtx.vOrderForm);
                   +  - ]
     381                 :             : 
     382                 :             :     // mark the original tx as bumped
     383         [ +  - ]:          97 :     bumped_txid = tx->GetHash();
     384   [ +  -  -  + ]:          97 :     if (!wallet.MarkReplaced(oldWtx.GetHash(), bumped_txid)) {
     385   [ #  #  #  # ]:           0 :         errors.push_back(Untranslated("Created new bumpfee transaction but could not mark the original transaction as replaced"));
     386                 :             :     }
     387                 :          97 :     return Result::OK;
     388         [ +  - ]:         291 : }
     389                 :             : 
     390                 :             : } // namespace feebumper
     391                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1