LCOV - code coverage report
Current view: top level - src - core_io.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 75.9 % 282 214
Test Date: 2026-02-04 04:43:42 Functions: 82.4 % 17 14
Branches: 44.1 % 658 290

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

Generated by: LCOV version 2.0-1