LCOV - code coverage report
Current view: top level - src/node - psbt.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 3 93 3.2 %
Date: 2023-11-10 23:46:46 Functions: 0 5 0.0 %
Branches: 2 143 1.4 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-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 <coins.h>
       6                 :            : #include <consensus/amount.h>
       7                 :            : #include <consensus/tx_verify.h>
       8                 :            : #include <node/psbt.h>
       9                 :            : #include <policy/policy.h>
      10                 :            : #include <policy/settings.h>
      11                 :            : #include <tinyformat.h>
      12                 :            : 
      13                 :            : #include <numeric>
      14                 :            : 
      15                 :            : namespace node {
      16                 :          0 : PSBTAnalysis AnalyzePSBT(PartiallySignedTransaction psbtx)
      17         [ +  - ]:          2 : {
      18         [ +  - ]:          2 :     // Go through each input and build status
      19                 :          0 :     PSBTAnalysis result;
      20                 :            : 
      21                 :          0 :     bool calc_fee = true;
      22                 :            : 
      23                 :          0 :     CAmount in_amt = 0;
      24                 :            : 
      25         [ #  # ]:          0 :     result.inputs.resize(psbtx.tx->vin.size());
      26                 :            : 
      27         [ #  # ]:          2 :     const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
      28                 :            : 
      29         [ #  # ]:          0 :     for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
      30                 :          0 :         PSBTInput& input = psbtx.inputs[i];
      31                 :          0 :         PSBTInputAnalysis& input_analysis = result.inputs[i];
      32                 :            : 
      33                 :            :         // We set next role here and ratchet backwards as required
      34                 :          0 :         input_analysis.next = PSBTRole::EXTRACTOR;
      35                 :            : 
      36                 :            :         // Check for a UTXO
      37         [ #  # ]:          0 :         CTxOut utxo;
      38 [ #  # ][ #  # ]:          0 :         if (psbtx.GetInputUTXO(utxo, i)) {
      39 [ #  # ][ #  # ]:          0 :             if (!MoneyRange(utxo.nValue) || !MoneyRange(in_amt + utxo.nValue)) {
         [ #  # ][ #  # ]
      40 [ #  # ][ #  # ]:          0 :                 result.SetInvalid(strprintf("PSBT is not valid. Input %u has invalid value", i));
      41                 :          0 :                 return result;
      42                 :            :             }
      43                 :          0 :             in_amt += utxo.nValue;
      44                 :          0 :             input_analysis.has_utxo = true;
      45                 :          0 :         } else {
      46 [ #  # ][ #  # ]:          0 :             if (input.non_witness_utxo && psbtx.tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) {
      47 [ #  # ][ #  # ]:          0 :                 result.SetInvalid(strprintf("PSBT is not valid. Input %u specifies invalid prevout", i));
      48                 :          0 :                 return result;
      49                 :            :             }
      50                 :          0 :             input_analysis.has_utxo = false;
      51                 :          0 :             input_analysis.is_final = false;
      52                 :          0 :             input_analysis.next = PSBTRole::UPDATER;
      53                 :          0 :             calc_fee = false;
      54                 :            :         }
      55                 :            : 
      56 [ #  # ][ #  # ]:          0 :         if (!utxo.IsNull() && utxo.scriptPubKey.IsUnspendable()) {
         [ #  # ][ #  # ]
      57 [ #  # ][ #  # ]:          0 :             result.SetInvalid(strprintf("PSBT is not valid. Input %u spends unspendable output", i));
      58                 :          0 :             return result;
      59                 :            :         }
      60                 :            : 
      61                 :            :         // Check if it is final
      62 [ #  # ][ #  # ]:          0 :         if (!PSBTInputSignedAndVerified(psbtx, i, &txdata)) {
                 [ #  # ]
      63                 :          0 :             input_analysis.is_final = false;
      64                 :            : 
      65                 :            :             // Figure out what is missing
      66         [ #  # ]:          0 :             SignatureData outdata;
      67         [ #  # ]:          0 :             bool complete = SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, 1, &outdata);
      68                 :            : 
      69                 :            :             // Things are missing
      70         [ #  # ]:          0 :             if (!complete) {
      71         [ #  # ]:          0 :                 input_analysis.missing_pubkeys = outdata.missing_pubkeys;
      72                 :          0 :                 input_analysis.missing_redeem_script = outdata.missing_redeem_script;
      73                 :          0 :                 input_analysis.missing_witness_script = outdata.missing_witness_script;
      74         [ #  # ]:          0 :                 input_analysis.missing_sigs = outdata.missing_sigs;
      75                 :            : 
      76                 :            :                 // If we are only missing signatures and nothing else, then next is signer
      77 [ #  # ][ #  # ]:          0 :                 if (outdata.missing_pubkeys.empty() && outdata.missing_redeem_script.IsNull() && outdata.missing_witness_script.IsNull() && !outdata.missing_sigs.empty()) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      78                 :          0 :                     input_analysis.next = PSBTRole::SIGNER;
      79                 :          0 :                 } else {
      80                 :          0 :                     input_analysis.next = PSBTRole::UPDATER;
      81                 :            :                 }
      82                 :          0 :             } else {
      83                 :          0 :                 input_analysis.next = PSBTRole::FINALIZER;
      84                 :            :             }
      85 [ #  # ][ #  # ]:          0 :         } else if (!utxo.IsNull()){
      86                 :          0 :             input_analysis.is_final = true;
      87                 :          0 :         }
      88         [ #  # ]:          0 :     }
      89                 :            : 
      90                 :            :     // Calculate next role for PSBT by grabbing "minimum" PSBTInput next role
      91                 :          0 :     result.next = PSBTRole::EXTRACTOR;
      92         [ #  # ]:          0 :     for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
      93                 :          0 :         PSBTInputAnalysis& input_analysis = result.inputs[i];
      94         [ #  # ]:          0 :         result.next = std::min(result.next, input_analysis.next);
      95                 :          0 :     }
      96         [ #  # ]:          0 :     assert(result.next > PSBTRole::CREATOR);
      97                 :            : 
      98         [ #  # ]:          0 :     if (calc_fee) {
      99                 :            :         // Get the output amount
     100         [ #  # ]:          0 :         CAmount out_amt = std::accumulate(psbtx.tx->vout.begin(), psbtx.tx->vout.end(), CAmount(0),
     101                 :          0 :             [](CAmount a, const CTxOut& b) {
     102 [ #  # ][ #  # ]:          0 :                 if (!MoneyRange(a) || !MoneyRange(b.nValue) || !MoneyRange(a + b.nValue)) {
                 [ #  # ]
     103                 :          0 :                     return CAmount(-1);
     104                 :            :                 }
     105                 :          0 :                 return a += b.nValue;
     106                 :          0 :             }
     107                 :            :         );
     108 [ #  # ][ #  # ]:          0 :         if (!MoneyRange(out_amt)) {
     109 [ #  # ][ #  # ]:          0 :             result.SetInvalid("PSBT is not valid. Output amount invalid");
     110                 :          0 :             return result;
     111                 :            :         }
     112                 :            : 
     113                 :            :         // Get the fee
     114                 :          0 :         CAmount fee = in_amt - out_amt;
     115                 :          0 :         result.fee = fee;
     116                 :            : 
     117                 :            :         // Estimate the size
     118         [ #  # ]:          0 :         CMutableTransaction mtx(*psbtx.tx);
     119                 :          0 :         CCoinsView view_dummy;
     120         [ #  # ]:          0 :         CCoinsViewCache view(&view_dummy);
     121                 :          0 :         bool success = true;
     122                 :            : 
     123         [ #  # ]:          0 :         for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
     124                 :          0 :             PSBTInput& input = psbtx.inputs[i];
     125         [ #  # ]:          0 :             Coin newcoin;
     126                 :            : 
     127 [ #  # ][ #  # ]:          0 :             if (!SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, nullptr, 1) || !psbtx.GetInputUTXO(newcoin.out, i)) {
         [ #  # ][ #  # ]
     128                 :          0 :                 success = false;
     129                 :          0 :                 break;
     130                 :            :             } else {
     131         [ #  # ]:          0 :                 mtx.vin[i].scriptSig = input.final_script_sig;
     132         [ #  # ]:          0 :                 mtx.vin[i].scriptWitness = input.final_script_witness;
     133                 :          0 :                 newcoin.nHeight = 1;
     134         [ #  # ]:          0 :                 view.AddCoin(psbtx.tx->vin[i].prevout, std::move(newcoin), true);
     135                 :            :             }
     136      [ #  #  # ]:          0 :         }
     137                 :            : 
     138         [ #  # ]:          0 :         if (success) {
     139         [ #  # ]:          0 :             CTransaction ctx = CTransaction(mtx);
     140 [ #  # ][ #  # ]:          0 :             size_t size(GetVirtualTransactionSize(ctx, GetTransactionSigOpCost(ctx, view, STANDARD_SCRIPT_VERIFY_FLAGS), ::nBytesPerSigOp));
     141                 :          0 :             result.estimated_vsize = size;
     142                 :            :             // Estimate fee rate
     143         [ #  # ]:          0 :             CFeeRate feerate(fee, size);
     144                 :          0 :             result.estimated_feerate = feerate;
     145                 :          0 :         }
     146                 :            : 
     147                 :          0 :     }
     148                 :            : 
     149                 :          0 :     return result;
     150         [ #  # ]:          0 : }
     151                 :            : } // namespace node

Generated by: LCOV version 1.14