LCOV - code coverage report
Current view: top level - src - core_io.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 76.6 % 278 213
Test Date: 2026-06-05 07:17:20 Functions: 82.4 % 17 14
Branches: 43.8 % 642 281

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-present 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 <core_io.h>
       6                 :             : 
       7                 :             : #include <addresstype.h>
       8                 :             : #include <coins.h>
       9                 :             : #include <consensus/amount.h>
      10                 :             : #include <consensus/consensus.h>
      11                 :             : #include <consensus/validation.h>
      12                 :             : #include <crypto/hex_base.h>
      13                 :             : #include <key_io.h>
      14                 :             : #include <prevector.h>
      15                 :             : #include <primitives/block.h>
      16                 :             : #include <primitives/transaction.h>
      17                 :             : #include <script/descriptor.h>
      18                 :             : #include <script/interpreter.h>
      19                 :             : #include <script/script.h>
      20                 :             : #include <script/signingprovider.h>
      21                 :             : #include <script/solver.h>
      22                 :             : #include <serialize.h>
      23                 :             : #include <streams.h>
      24                 :             : #include <tinyformat.h>
      25                 :             : #include <uint256.h>
      26                 :             : #include <undo.h>
      27                 :             : #include <univalue.h>
      28                 :             : #include <util/check.h>
      29                 :             : #include <util/result.h>
      30                 :             : #include <util/strencodings.h>
      31                 :             : #include <util/string.h>
      32                 :             : #include <util/translation.h>
      33                 :             : 
      34                 :             : #include <algorithm>
      35                 :             : #include <compare>
      36                 :             : #include <cstdint>
      37                 :             : #include <exception>
      38                 :             : #include <functional>
      39                 :             : #include <map>
      40                 :             : #include <memory>
      41                 :             : #include <optional>
      42                 :             : #include <span>
      43                 :             : #include <stdexcept>
      44                 :             : #include <string>
      45                 :             : #include <utility>
      46                 :             : #include <vector>
      47                 :             : 
      48                 :             : using util::SplitString;
      49                 :             : 
      50                 :             : namespace {
      51                 :             : class OpCodeParser
      52                 :             : {
      53                 :             : private:
      54                 :             :     std::map<std::string, opcodetype> mapOpNames;
      55                 :             : 
      56                 :             : public:
      57                 :           3 :     OpCodeParser()
      58                 :           3 :     {
      59         [ +  + ]:         561 :         for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
      60                 :             :             // Allow OP_RESERVED to get into mapOpNames
      61         [ +  + ]:         558 :             if (op < OP_NOP && op != OP_RESERVED) {
      62                 :         288 :                 continue;
      63                 :             :             }
      64                 :             : 
      65         [ +  - ]:         270 :             std::string strName = GetOpName(static_cast<opcodetype>(op));
      66         [ -  + ]:         270 :             if (strName == "OP_UNKNOWN") {
      67                 :           0 :                 continue;
      68                 :             :             }
      69         [ +  - ]:         270 :             mapOpNames[strName] = static_cast<opcodetype>(op);
      70                 :             :             // Convenience: OP_ADD and just ADD are both recognized:
      71   [ -  +  +  - ]:         270 :             if (strName.starts_with("OP_")) {
      72   [ +  -  +  - ]:         270 :                 mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
      73                 :             :             }
      74                 :         270 :         }
      75                 :           3 :     }
      76                 :        3921 :     opcodetype Parse(const std::string& s) const
      77                 :             :     {
      78                 :        3921 :         auto it = mapOpNames.find(s);
      79   [ +  +  +  - ]:        3921 :         if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
      80                 :        3920 :         return it->second;
      81                 :             :     }
      82                 :             : };
      83                 :             : 
      84                 :        3921 : opcodetype ParseOpCode(const std::string& s)
      85                 :             : {
      86   [ +  +  +  -  :        3921 :     static const OpCodeParser ocp;
                   +  - ]
      87                 :        3921 :     return ocp.Parse(s);
      88                 :             : }
      89                 :             : 
      90                 :             : } // namespace
      91                 :             : 
      92                 :        2758 : CScript ParseScript(const std::string& s)
      93                 :             : {
      94                 :        2758 :     CScript result;
      95                 :             : 
      96   [ -  +  +  - ]:        2758 :     std::vector<std::string> words = SplitString(s, " \t\n");
      97                 :             : 
      98         [ +  + ]:       15005 :     for (const std::string& w : words) {
      99         [ +  + ]:       12250 :         if (w.empty()) {
     100                 :             :             // Empty string, ignore. (SplitString doesn't combine multiple separators)
     101   [ -  +  +  -  :       12028 :         } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
                   +  + ]
     102   [ +  +  +  -  :        7572 :                    (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
             +  -  +  - ]
     103                 :             :         {
     104                 :             :             // Number
     105         [ -  + ]:        4616 :             const auto num{ToIntegral<int64_t>(w)};
     106                 :             : 
     107                 :             :             // limit the range of numbers ParseScript accepts in decimal
     108                 :             :             // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
     109   [ +  +  +  +  :        4616 :             if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
                   -  + ]
     110                 :           2 :                 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
     111         [ +  - ]:           2 :                                          "range -0xFFFFFFFF...0xFFFFFFFF");
     112                 :             :             }
     113                 :             : 
     114         [ +  - ]:       12247 :             result << num.value();
     115   [ -  +  +  +  :       14147 :         } else if (w.starts_with("0x") && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
          +  -  +  -  -  
          +  +  -  +  -  
             +  +  -  - ]
     116                 :             :             // Raw hex data, inserted NOT pushed onto stack:
     117   [ -  +  +  -  :        4490 :             std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
             -  +  +  - ]
     118                 :        2245 :             result.insert(result.end(), raw.begin(), raw.end());
     119   [ -  +  +  -  :        7412 :         } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
             +  +  -  + ]
     120                 :             :             // Single-quoted string, pushed as data. NOTE: this is poor-man's
     121                 :             :             // parsing, spaces/tabs/newlines in single-quoted strings won't work.
     122         [ +  - ]:        1246 :             std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
     123         [ -  + ]:        1246 :             result << value;
     124                 :        1246 :         } else {
     125                 :             :             // opcode, e.g. OP_ADD or ADD:
     126   [ +  +  +  - ]:        3921 :             result << ParseOpCode(w);
     127                 :             :         }
     128                 :             :     }
     129                 :             : 
     130                 :        2755 :     return result;
     131                 :        2758 : }
     132                 :             : 
     133                 :             : /// Check that all of the input and output scripts of a transaction contain valid opcodes
     134                 :           7 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
     135                 :             : {
     136                 :             :     // Check input scripts for non-coinbase txs
     137         [ +  - ]:          14 :     if (!CTransaction(tx).IsCoinBase()) {
     138   [ -  +  +  + ]:          11 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     139   [ +  -  +  +  :           4 :             if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
                   +  - ]
     140                 :             :                 return false;
     141                 :             :             }
     142                 :             :         }
     143                 :             :     }
     144                 :             :     // Check output scripts
     145   [ -  +  +  + ]:          11 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     146   [ +  +  -  +  :           5 :         if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
                   -  - ]
     147                 :             :             return false;
     148                 :             :         }
     149                 :             :     }
     150                 :             : 
     151                 :             :     return true;
     152                 :             : }
     153                 :             : 
     154                 :           9 : static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
     155                 :             : {
     156                 :             :     // General strategy:
     157                 :             :     // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
     158                 :             :     //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
     159                 :             :     //   0-input 1-output incomplete transaction).
     160                 :             :     //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
     161                 :             :     //     disables extended if false).
     162                 :             :     //   - Ignore serializations that do not fully consume the hex string.
     163                 :             :     // - If neither succeeds, fail.
     164                 :             :     // - If only one succeeds, return that one.
     165                 :             :     // - If both decode attempts succeed:
     166                 :             :     //   - If only one passes the CheckTxScriptsSanity check, return that one.
     167                 :             :     //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
     168                 :             : 
     169         [ +  - ]:           9 :     CMutableTransaction tx_extended, tx_legacy;
     170                 :           9 :     bool ok_extended = false, ok_legacy = false;
     171                 :             : 
     172                 :             :     // Try decoding with extended serialization support, and remember if the result successfully
     173                 :             :     // consumes the entire input.
     174         [ +  + ]:           9 :     if (try_witness) {
     175         [ -  + ]:           5 :         SpanReader ssData{tx_data};
     176                 :           5 :         try {
     177         [ +  + ]:           5 :             ssData >> TX_WITH_WITNESS(tx_extended);
     178         [ +  - ]:           3 :             if (ssData.empty()) ok_extended = true;
     179         [ -  + ]:           2 :         } catch (const std::exception&) {
     180                 :             :             // Fall through.
     181                 :           2 :         }
     182                 :             :     }
     183                 :             : 
     184                 :             :     // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
     185                 :             :     // don't bother decoding the other way.
     186   [ +  -  -  + ]:           5 :     if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
     187                 :           3 :         tx = std::move(tx_extended);
     188                 :           3 :         return true;
     189                 :             :     }
     190                 :             : 
     191                 :             :     // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
     192         [ +  + ]:           6 :     if (try_no_witness) {
     193         [ -  + ]:           5 :         SpanReader ssData{tx_data};
     194                 :           5 :         try {
     195         [ +  + ]:           5 :             ssData >> TX_NO_WITNESS(tx_legacy);
     196         [ +  - ]:           4 :             if (ssData.empty()) ok_legacy = true;
     197         [ -  + ]:           1 :         } catch (const std::exception&) {
     198                 :             :             // Fall through.
     199                 :           1 :         }
     200                 :             :     }
     201                 :             : 
     202                 :             :     // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
     203                 :             :     // at this point that extended decoding either failed or doesn't pass the sanity check.
     204   [ +  -  +  + ]:           5 :     if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
     205                 :           3 :         tx = std::move(tx_legacy);
     206                 :           3 :         return true;
     207                 :             :     }
     208                 :             : 
     209                 :             :     // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
     210         [ -  + ]:           3 :     if (ok_extended) {
     211                 :           0 :         tx = std::move(tx_extended);
     212                 :           0 :         return true;
     213                 :             :     }
     214                 :             : 
     215                 :             :     // If legacy decoding succeeded and extended didn't, return the legacy one.
     216         [ +  + ]:           3 :     if (ok_legacy) {
     217                 :           1 :         tx = std::move(tx_legacy);
     218                 :           1 :         return true;
     219                 :             :     }
     220                 :             : 
     221                 :             :     // If none succeeded, we failed.
     222                 :             :     return false;
     223                 :          18 : }
     224                 :             : 
     225                 :          11 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
     226                 :             : {
     227   [ -  +  +  + ]:          11 :     if (!IsHex(hex_tx)) {
     228                 :             :         return false;
     229                 :             :     }
     230                 :             : 
     231         [ -  + ]:           9 :     std::vector<unsigned char> txData(ParseHex(hex_tx));
     232         [ +  - ]:           9 :     return DecodeTx(tx, txData, try_no_witness, try_witness);
     233                 :           9 : }
     234                 :             : 
     235                 :           0 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
     236                 :             : {
     237   [ #  #  #  # ]:           0 :     if (!IsHex(hex_header)) return false;
     238                 :             : 
     239         [ #  # ]:           0 :     const std::vector<unsigned char> header_data{ParseHex(hex_header)};
     240                 :           0 :     try {
     241   [ #  #  #  # ]:           0 :         SpanReader{header_data} >> header;
     242         [ -  - ]:           0 :     } catch (const std::exception&) {
     243                 :           0 :         return false;
     244                 :           0 :     }
     245                 :             :     return true;
     246                 :           0 : }
     247                 :             : 
     248                 :          10 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
     249                 :             : {
     250   [ -  +  +  - ]:          10 :     if (!IsHex(strHexBlk))
     251                 :             :         return false;
     252                 :             : 
     253         [ -  + ]:          10 :     std::vector<unsigned char> blockData(ParseHex(strHexBlk));
     254                 :          10 :     try {
     255   [ -  +  +  - ]:          20 :         SpanReader{blockData} >> TX_WITH_WITNESS(block);
     256                 :             :     }
     257         [ -  - ]:           0 :     catch (const std::exception&) {
     258                 :           0 :         return false;
     259                 :           0 :     }
     260                 :             : 
     261                 :             :     return true;
     262                 :          10 : }
     263                 :             : 
     264                 :           0 : util::Result<int> SighashFromStr(const std::string& sighash)
     265                 :             : {
     266                 :           0 :     static const std::map<std::string, int> map_sighash_values = {
     267         [ #  # ]:           0 :         {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
     268                 :           0 :         {std::string("ALL"), int(SIGHASH_ALL)},
     269                 :           0 :         {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
     270                 :           0 :         {std::string("NONE"), int(SIGHASH_NONE)},
     271                 :           0 :         {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
     272                 :           0 :         {std::string("SINGLE"), int(SIGHASH_SINGLE)},
     273                 :           0 :         {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
     274   [ #  #  #  #  :           0 :     };
             #  #  #  # ]
     275                 :           0 :     const auto& it = map_sighash_values.find(sighash);
     276         [ #  # ]:           0 :     if (it != map_sighash_values.end()) {
     277                 :           0 :         return it->second;
     278                 :             :     } else {
     279   [ #  #  #  # ]:           0 :         return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
     280                 :             :     }
     281   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     282                 :             : 
     283                 :          96 : UniValue ValueFromAmount(const CAmount amount)
     284                 :             : {
     285                 :          96 :     static_assert(COIN > 1);
     286                 :          96 :     int64_t quotient = amount / COIN;
     287                 :          96 :     int64_t remainder = amount % COIN;
     288         [ +  + ]:          96 :     if (amount < 0) {
     289                 :           6 :         quotient = -quotient;
     290                 :           6 :         remainder = -remainder;
     291                 :             :     }
     292                 :           6 :     return UniValue(UniValue::VNUM,
     293                 :          96 :             strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
     294                 :             : }
     295                 :             : 
     296                 :         268 : std::string FormatScript(const CScript& script)
     297                 :             : {
     298         [ +  + ]:         268 :     std::string ret;
     299         [ +  + ]:         536 :     CScript::const_iterator it = script.begin();
     300                 :             :     opcodetype op;
     301         [ +  + ]:         867 :     while (it != script.end()) {
     302                 :         599 :         CScript::const_iterator it2 = it;
     303                 :         599 :         std::vector<unsigned char> vch;
     304   [ +  -  +  - ]:         599 :         if (script.GetOp(it, op, vch)) {
     305         [ +  + ]:         599 :             if (op == OP_0) {
     306         [ +  - ]:          63 :                 ret += "0 ";
     307                 :          63 :                 continue;
     308   [ +  +  -  + ]:         536 :             } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
     309         [ +  - ]:         126 :                 ret += strprintf("%i ", op - OP_1NEGATE - 1);
     310                 :          63 :                 continue;
     311         [ +  + ]:         473 :             } else if (op >= OP_NOP && op <= OP_NOP10) {
     312         [ +  - ]:         178 :                 std::string str(GetOpName(op));
     313   [ +  -  +  -  :         178 :                 if (str.substr(0, 3) == std::string("OP_")) {
                   +  - ]
     314   [ +  -  -  + ]:         534 :                     ret += str.substr(3, std::string::npos) + " ";
     315                 :         178 :                     continue;
     316                 :             :                 }
     317                 :         178 :             }
     318   [ -  +  +  - ]:         295 :             if (vch.size() > 0) {
     319   [ +  -  +  -  :         885 :                 ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
                   +  - ]
     320   [ +  -  +  - ]:        1180 :                                                HexStr(std::vector<uint8_t>(it - vch.size(), it)));
     321                 :             :             } else {
     322   [ #  #  #  #  :           0 :                 ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
                   #  # ]
     323                 :             :             }
     324                 :         295 :             continue;
     325                 :         295 :         }
     326   [ #  #  #  #  :           0 :         ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
                   #  # ]
     327                 :           0 :         break;
     328                 :         599 :     }
     329   [ +  +  +  - ]:         509 :     return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
     330                 :         268 : }
     331                 :             : 
     332                 :             : const std::map<unsigned char, std::string> mapSigHashTypes = {
     333                 :             :     {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
     334                 :             :     {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
     335                 :             :     {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
     336                 :             :     {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
     337                 :             :     {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
     338                 :             :     {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
     339                 :             : };
     340                 :             : 
     341                 :           0 : std::string SighashToStr(unsigned char sighash_type)
     342                 :             : {
     343                 :           0 :     const auto& it = mapSigHashTypes.find(sighash_type);
     344         [ #  # ]:           0 :     if (it == mapSigHashTypes.end()) return "";
     345         [ #  # ]:           0 :     return it->second;
     346                 :             : }
     347                 :             : 
     348                 :             : /**
     349                 :             :  * Create the assembly string representation of a CScript object.
     350                 :             :  * @param[in] script    CScript object to convert into the asm string representation.
     351                 :             :  * @param[in] fAttemptSighashDecode    Whether to attempt to decode sighash types on data within the script that matches the format
     352                 :             :  *                                     of a signature. Only pass true for scripts you believe could contain signatures. For example,
     353                 :             :  *                                     pass false, or omit the this argument (defaults to false), for scriptPubKeys.
     354                 :             :  */
     355                 :          24 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
     356                 :             : {
     357         [ +  + ]:          24 :     std::string str;
     358                 :          24 :     opcodetype opcode;
     359                 :          24 :     std::vector<unsigned char> vch;
     360         [ +  + ]:          48 :     CScript::const_iterator pc = script.begin();
     361   [ +  +  +  + ]:         148 :     while (pc < script.end()) {
     362         [ +  + ]:          50 :         if (!str.empty()) {
     363         [ +  - ]:          26 :             str += " ";
     364                 :             :         }
     365   [ +  -  -  + ]:          50 :         if (!script.GetOp(pc, opcode, vch)) {
     366         [ -  - ]:          24 :             str += "[error]";
     367                 :             :             return str;
     368                 :             :         }
     369         [ +  + ]:          50 :         if (0 <= opcode && opcode <= OP_PUSHDATA4) {
     370   [ -  +  -  + ]:          38 :             if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
     371   [ #  #  #  # ]:           0 :                 str += strprintf("%d", CScriptNum(vch, false).getint());
     372                 :             :             } else {
     373                 :             :                 // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
     374   [ +  +  +  - ]:          38 :                 if (fAttemptSighashDecode && !script.IsUnspendable()) {
     375         [ +  - ]:          20 :                     std::string strSigHashDecode;
     376                 :             :                     // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
     377                 :             :                     // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
     378                 :             :                     // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
     379                 :             :                     // checks in CheckSignatureEncoding.
     380   [ +  -  +  + ]:          20 :                     if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
     381                 :           8 :                         const unsigned char chSigHashType = vch.back();
     382                 :           8 :                         const auto it = mapSigHashTypes.find(chSigHashType);
     383         [ +  - ]:           8 :                         if (it != mapSigHashTypes.end()) {
     384         [ +  - ]:          16 :                             strSigHashDecode = "[" + it->second + "]";
     385                 :           8 :                             vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
     386                 :             :                         }
     387                 :             :                     }
     388   [ -  +  +  -  :          40 :                     str += HexStr(vch) + strSigHashDecode;
                   +  - ]
     389                 :          20 :                 } else {
     390         [ +  - ]:          36 :                     str += HexStr(vch);
     391                 :             :                 }
     392                 :             :             }
     393                 :             :         } else {
     394         [ +  - ]:          24 :             str += GetOpName(opcode);
     395                 :             :         }
     396                 :             :     }
     397                 :             :     return str;
     398                 :          24 : }
     399                 :             : 
     400                 :           6 : std::string EncodeHexTx(const CTransaction& tx)
     401                 :             : {
     402                 :           6 :     DataStream ssTx;
     403         [ +  - ]:           6 :     ssTx << TX_WITH_WITNESS(tx);
     404   [ -  +  +  - ]:           6 :     return HexStr(ssTx);
     405                 :           6 : }
     406                 :             : 
     407                 :           2 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
     408                 :             : {
     409                 :           2 :     CTxDestination address;
     410                 :             : 
     411   [ +  -  +  -  :           4 :     out.pushKV("asm", ScriptToAsmStr(script));
             +  -  +  - ]
     412         [ +  - ]:           2 :     if (include_address) {
     413   [ +  -  +  -  :           4 :         out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
          +  -  +  -  +  
                -  +  - ]
     414                 :             :     }
     415         [ +  - ]:           2 :     if (include_hex) {
     416   [ +  -  +  -  :           6 :         out.pushKV("hex", HexStr(script));
          +  -  +  -  +  
                      - ]
     417                 :             :     }
     418                 :             : 
     419                 :           2 :     std::vector<std::vector<unsigned char>> solns;
     420         [ +  - ]:           2 :     const TxoutType type{Solver(script, solns)};
     421                 :             : 
     422   [ +  -  +  -  :           2 :     if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
             +  -  +  - ]
     423   [ +  -  +  -  :           4 :         out.pushKV("address", EncodeDestination(address));
             +  -  +  - ]
     424                 :             :     }
     425   [ +  -  +  -  :           4 :     out.pushKV("type", GetTxnOutputType(type));
             +  -  +  - ]
     426                 :           2 : }
     427                 :             : 
     428                 :           2 : void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, const CTxUndo* txundo, TxVerbosity verbosity, std::function<bool(const CTxOut&)> is_change_func)
     429                 :             : {
     430                 :           2 :     CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
     431                 :             : 
     432   [ +  -  +  -  :           4 :     entry.pushKV("txid", tx.GetHash().GetHex());
                   +  - ]
     433   [ +  -  +  -  :           4 :     entry.pushKV("hash", tx.GetWitnessHash().GetHex());
                   +  - ]
     434   [ +  -  +  - ]:           4 :     entry.pushKV("version", tx.version);
     435   [ +  -  +  - ]:           4 :     entry.pushKV("size", tx.ComputeTotalSize());
     436   [ +  -  +  - ]:           4 :     entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
     437   [ +  -  +  - ]:           4 :     entry.pushKV("weight", GetTransactionWeight(tx));
     438   [ +  -  +  - ]:           4 :     entry.pushKV("locktime", tx.nLockTime);
     439                 :             : 
     440                 :           2 :     UniValue vin{UniValue::VARR};
     441   [ -  +  +  - ]:           2 :     vin.reserve(tx.vin.size());
     442                 :             : 
     443                 :             :     // If available, use Undo data to calculate the fee. Note that txundo == nullptr
     444                 :             :     // for coinbase transactions and for transactions where undo data is unavailable.
     445                 :             :     const bool have_undo = txundo != nullptr;
     446                 :             :     CAmount amt_total_in = 0;
     447                 :           4 :     CAmount amt_total_out = 0;
     448                 :             : 
     449   [ -  +  +  + ]:           4 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     450                 :           2 :         const CTxIn& txin = tx.vin[i];
     451                 :           2 :         UniValue in(UniValue::VOBJ);
     452         [ -  + ]:           2 :         if (tx.IsCoinBase()) {
     453   [ #  #  #  #  :           0 :             in.pushKV("coinbase", HexStr(txin.scriptSig));
          #  #  #  #  #  
                      # ]
     454                 :             :         } else {
     455   [ +  -  +  -  :           4 :             in.pushKV("txid", txin.prevout.hash.GetHex());
             +  -  +  - ]
     456   [ +  -  +  -  :           4 :             in.pushKV("vout", txin.prevout.n);
                   +  - ]
     457                 :           2 :             UniValue o(UniValue::VOBJ);
     458   [ +  -  +  -  :           4 :             o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
             +  -  +  - ]
     459   [ -  +  +  -  :           6 :             o.pushKV("hex", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     460   [ +  -  +  - ]:           4 :             in.pushKV("scriptSig", std::move(o));
     461                 :           2 :         }
     462         [ -  + ]:           2 :         if (!tx.vin[i].scriptWitness.IsNull()) {
     463                 :           0 :             UniValue txinwitness(UniValue::VARR);
     464   [ #  #  #  # ]:           0 :             txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
     465         [ #  # ]:           0 :             for (const auto& item : tx.vin[i].scriptWitness.stack) {
     466   [ #  #  #  #  :           0 :                 txinwitness.push_back(HexStr(item));
             #  #  #  # ]
     467                 :             :             }
     468   [ #  #  #  # ]:           0 :             in.pushKV("txinwitness", std::move(txinwitness));
     469                 :           0 :         }
     470         [ -  + ]:           2 :         if (have_undo) {
     471         [ #  # ]:           0 :             const Coin& prev_coin = txundo->vprevout[i];
     472                 :           0 :             const CTxOut& prev_txout = prev_coin.out;
     473                 :             : 
     474                 :           0 :             amt_total_in += prev_txout.nValue;
     475                 :             : 
     476         [ #  # ]:           0 :             if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
     477                 :           0 :                 UniValue o_script_pub_key(UniValue::VOBJ);
     478         [ #  # ]:           0 :                 ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
     479                 :             : 
     480                 :           0 :                 UniValue p(UniValue::VOBJ);
     481   [ #  #  #  #  :           0 :                 p.pushKV("generated", prev_coin.IsCoinBase());
                   #  # ]
     482   [ #  #  #  #  :           0 :                 p.pushKV("height", prev_coin.nHeight);
                   #  # ]
     483   [ #  #  #  #  :           0 :                 p.pushKV("value", ValueFromAmount(prev_txout.nValue));
                   #  # ]
     484   [ #  #  #  # ]:           0 :                 p.pushKV("scriptPubKey", std::move(o_script_pub_key));
     485   [ #  #  #  # ]:           0 :                 in.pushKV("prevout", std::move(p));
     486                 :           0 :             }
     487                 :             :         }
     488   [ +  -  +  -  :           4 :         in.pushKV("sequence", txin.nSequence);
                   +  - ]
     489         [ +  - ]:           2 :         vin.push_back(std::move(in));
     490                 :           2 :     }
     491   [ +  -  +  - ]:           4 :     entry.pushKV("vin", std::move(vin));
     492                 :             : 
     493                 :           2 :     UniValue vout(UniValue::VARR);
     494   [ -  +  +  - ]:           2 :     vout.reserve(tx.vout.size());
     495   [ -  +  +  + ]:           4 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     496                 :           2 :         const CTxOut& txout = tx.vout[i];
     497                 :             : 
     498                 :           2 :         UniValue out(UniValue::VOBJ);
     499                 :             : 
     500   [ +  -  +  -  :           4 :         out.pushKV("value", ValueFromAmount(txout.nValue));
                   +  - ]
     501   [ +  -  +  -  :           4 :         out.pushKV("n", i);
                   +  - ]
     502                 :             : 
     503                 :           2 :         UniValue o(UniValue::VOBJ);
     504         [ +  - ]:           2 :         ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
     505   [ +  -  +  - ]:           4 :         out.pushKV("scriptPubKey", std::move(o));
     506                 :             : 
     507   [ -  +  -  -  :           2 :         if (is_change_func && is_change_func(txout)) {
                   -  - ]
     508   [ #  #  #  #  :           0 :             out.pushKV("ischange", true);
                   #  # ]
     509                 :             :         }
     510                 :             : 
     511         [ +  - ]:           2 :         vout.push_back(std::move(out));
     512                 :             : 
     513         [ -  + ]:           2 :         if (have_undo) {
     514                 :           0 :             amt_total_out += txout.nValue;
     515                 :             :         }
     516                 :           2 :     }
     517   [ +  -  +  - ]:           4 :     entry.pushKV("vout", std::move(vout));
     518                 :             : 
     519         [ -  + ]:           2 :     if (have_undo) {
     520                 :           0 :         const CAmount fee = amt_total_in - amt_total_out;
     521         [ #  # ]:           0 :         CHECK_NONFATAL(MoneyRange(fee));
     522   [ #  #  #  #  :           0 :         entry.pushKV("fee", ValueFromAmount(fee));
                   #  # ]
     523                 :             :     }
     524                 :             : 
     525         [ -  + ]:           4 :     if (!block_hash.IsNull()) {
     526   [ #  #  #  #  :           0 :         entry.pushKV("blockhash", block_hash.GetHex());
             #  #  #  # ]
     527                 :             :     }
     528                 :             : 
     529         [ -  + ]:           2 :     if (include_hex) {
     530   [ #  #  #  #  :           0 :         entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
             #  #  #  # ]
     531                 :             :     }
     532                 :           2 : }
        

Generated by: LCOV version 2.0-1