LCOV - code coverage report
Current view: top level - src - core_io.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.2 % 278 273
Test Date: 2026-04-04 05:08:43 Functions: 100.0 % 17 17
Branches: 60.4 % 642 388

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

Generated by: LCOV version 2.0-1