LCOV - code coverage report
Current view: top level - src - core_write.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 89.4 % 160 143
Test Date: 2025-07-28 04:06:55 Functions: 100.0 % 7 7
Branches: 54.4 % 384 209

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <core_io.h>
       6                 :             : 
       7                 :             : #include <common/system.h>
       8                 :             : #include <consensus/amount.h>
       9                 :             : #include <consensus/consensus.h>
      10                 :             : #include <consensus/validation.h>
      11                 :             : #include <key_io.h>
      12                 :             : #include <script/descriptor.h>
      13                 :             : #include <script/script.h>
      14                 :             : #include <script/solver.h>
      15                 :             : #include <serialize.h>
      16                 :             : #include <streams.h>
      17                 :             : #include <undo.h>
      18                 :             : #include <univalue.h>
      19                 :             : #include <util/check.h>
      20                 :             : #include <util/strencodings.h>
      21                 :             : 
      22                 :             : #include <map>
      23                 :             : #include <string>
      24                 :             : #include <vector>
      25                 :             : 
      26                 :     1438772 : UniValue ValueFromAmount(const CAmount amount)
      27                 :             : {
      28                 :     1438772 :     static_assert(COIN > 1);
      29                 :     1438772 :     int64_t quotient = amount / COIN;
      30                 :     1438772 :     int64_t remainder = amount % COIN;
      31         [ +  + ]:     1438772 :     if (amount < 0) {
      32                 :       96422 :         quotient = -quotient;
      33                 :       96422 :         remainder = -remainder;
      34                 :             :     }
      35                 :       96422 :     return UniValue(UniValue::VNUM,
      36                 :     1438772 :             strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
      37                 :             : }
      38                 :             : 
      39                 :        2584 : std::string FormatScript(const CScript& script)
      40                 :             : {
      41         [ +  + ]:        2584 :     std::string ret;
      42         [ +  + ]:        5168 :     CScript::const_iterator it = script.begin();
      43                 :             :     opcodetype op;
      44         [ +  + ]:     1841181 :     while (it != script.end()) {
      45                 :     1838938 :         CScript::const_iterator it2 = it;
      46                 :     1838938 :         std::vector<unsigned char> vch;
      47   [ +  -  +  + ]:     1838938 :         if (script.GetOp(it, op, vch)) {
      48         [ +  + ]:     1838597 :             if (op == OP_0) {
      49         [ +  - ]:      115176 :                 ret += "0 ";
      50                 :      115176 :                 continue;
      51   [ +  +  +  + ]:     1723421 :             } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
      52         [ +  - ]:      122030 :                 ret += strprintf("%i ", op - OP_1NEGATE - 1);
      53                 :       61015 :                 continue;
      54         [ +  + ]:     1662406 :             } else if (op >= OP_NOP && op <= OP_NOP10) {
      55         [ +  - ]:     1207132 :                 std::string str(GetOpName(op));
      56   [ +  -  +  -  :     1207132 :                 if (str.substr(0, 3) == std::string("OP_")) {
                   +  - ]
      57   [ +  -  +  - ]:     3621396 :                     ret += str.substr(3, std::string::npos) + " ";
      58                 :     1207132 :                     continue;
      59                 :             :                 }
      60                 :     1207132 :             }
      61         [ +  + ]:      455274 :             if (vch.size() > 0) {
      62   [ +  -  +  -  :      812856 :                 ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
             +  -  +  - ]
      63   [ +  -  +  - ]:      812856 :                                                HexStr(std::vector<uint8_t>(it - vch.size(), it)));
      64                 :             :             } else {
      65   [ +  -  +  -  :      368644 :                 ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
                   +  - ]
      66                 :             :             }
      67                 :      455274 :             continue;
      68                 :      455274 :         }
      69   [ +  -  +  -  :         682 :         ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
                   +  - ]
      70                 :         341 :         break;
      71                 :     1838938 :     }
      72   [ +  +  +  - ]:        2584 :     return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
      73                 :        2584 : }
      74                 :             : 
      75                 :             : const std::map<unsigned char, std::string> mapSigHashTypes = {
      76                 :             :     {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
      77                 :             :     {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
      78                 :             :     {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
      79                 :             :     {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
      80                 :             :     {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
      81                 :             :     {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
      82                 :             : };
      83                 :             : 
      84                 :         457 : std::string SighashToStr(unsigned char sighash_type)
      85                 :             : {
      86                 :         457 :     const auto& it = mapSigHashTypes.find(sighash_type);
      87         [ +  + ]:         457 :     if (it == mapSigHashTypes.end()) return "";
      88                 :          18 :     return it->second;
      89                 :             : }
      90                 :             : 
      91                 :             : /**
      92                 :             :  * Create the assembly string representation of a CScript object.
      93                 :             :  * @param[in] script    CScript object to convert into the asm string representation.
      94                 :             :  * @param[in] fAttemptSighashDecode    Whether to attempt to decode sighash types on data within the script that matches the format
      95                 :             :  *                                     of a signature. Only pass true for scripts you believe could contain signatures. For example,
      96                 :             :  *                                     pass false, or omit the this argument (defaults to false), for scriptPubKeys.
      97                 :             :  */
      98                 :     1590646 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
      99                 :             : {
     100         [ +  + ]:     1590646 :     std::string str;
     101                 :     1590646 :     opcodetype opcode;
     102                 :     1590646 :     std::vector<unsigned char> vch;
     103         [ +  + ]:     3181292 :     CScript::const_iterator pc = script.begin();
     104         [ +  + ]:    18816220 :     while (pc < script.end()) {
     105         [ +  + ]:    18016726 :         if (!str.empty()) {
     106         [ +  - ]:    16737517 :             str += " ";
     107                 :             :         }
     108   [ +  -  +  + ]:    18016726 :         if (!script.GetOp(pc, opcode, vch)) {
     109         [ +  - ]:     1590646 :             str += "[error]";
     110                 :             :             return str;
     111                 :             :         }
     112         [ +  + ]:    17225574 :         if (0 <= opcode && opcode <= OP_PUSHDATA4) {
     113         [ +  + ]:     5884211 :             if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
     114   [ +  -  +  - ]:    14033127 :                 str += strprintf("%d", CScriptNum(vch, false).getint());
     115                 :             :             } else {
     116                 :             :                 // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
     117   [ +  +  +  + ]:     1206502 :                 if (fAttemptSighashDecode && !script.IsUnspendable()) {
     118         [ +  - ]:      102668 :                     std::string strSigHashDecode;
     119                 :             :                     // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig.
     120                 :             :                     // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to
     121                 :             :                     // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the
     122                 :             :                     // checks in CheckSignatureEncoding.
     123   [ +  -  +  + ]:      102668 :                     if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
     124                 :        4756 :                         const unsigned char chSigHashType = vch.back();
     125                 :        4756 :                         const auto it = mapSigHashTypes.find(chSigHashType);
     126         [ +  - ]:        4756 :                         if (it != mapSigHashTypes.end()) {
     127         [ +  - ]:        9512 :                             strSigHashDecode = "[" + it->second + "]";
     128                 :        4756 :                             vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
     129                 :             :                         }
     130                 :             :                     }
     131   [ +  -  +  - ]:      205336 :                     str += HexStr(vch) + strSigHashDecode;
     132                 :      102668 :                 } else {
     133         [ +  - ]:     2207668 :                     str += HexStr(vch);
     134                 :             :                 }
     135                 :             :             }
     136                 :             :         } else {
     137         [ +  - ]:    22682726 :             str += GetOpName(opcode);
     138                 :             :         }
     139                 :             :     }
     140                 :             :     return str;
     141                 :     1590646 : }
     142                 :             : 
     143                 :        4238 : std::string EncodeHexTx(const CTransaction& tx)
     144                 :             : {
     145                 :        4238 :     DataStream ssTx;
     146         [ +  - ]:        4238 :     ssTx << TX_WITH_WITNESS(tx);
     147         [ +  - ]:        4238 :     return HexStr(ssTx);
     148                 :        4238 : }
     149                 :             : 
     150                 :     1441922 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
     151                 :             : {
     152                 :     1441922 :     CTxDestination address;
     153                 :             : 
     154   [ +  -  +  -  :     2883844 :     out.pushKV("asm", ScriptToAsmStr(script));
             +  -  +  - ]
     155         [ +  + ]:     1441922 :     if (include_address) {
     156   [ +  +  +  -  :     2881994 :         out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
          +  -  +  -  +  
                -  +  - ]
     157                 :             :     }
     158         [ +  + ]:     1441922 :     if (include_hex) {
     159   [ +  +  +  -  :     4317789 :         out.pushKV("hex", HexStr(script));
          +  -  +  -  +  
                      - ]
     160                 :             :     }
     161                 :             : 
     162                 :     1441922 :     std::vector<std::vector<unsigned char>> solns;
     163         [ +  - ]:     1441922 :     const TxoutType type{Solver(script, solns)};
     164                 :             : 
     165   [ +  +  +  -  :     1441922 :     if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
             +  +  +  - ]
     166   [ +  -  +  -  :      694208 :         out.pushKV("address", EncodeDestination(address));
             +  -  +  - ]
     167                 :             :     }
     168   [ +  -  +  -  :     2883844 :     out.pushKV("type", GetTxnOutputType(type));
             +  -  +  - ]
     169                 :     1441922 : }
     170                 :             : 
     171                 :        3437 : void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, const CTxUndo* txundo, TxVerbosity verbosity)
     172                 :             : {
     173                 :        3437 :     CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
     174                 :             : 
     175   [ +  -  +  -  :        6874 :     entry.pushKV("txid", tx.GetHash().GetHex());
                   +  - ]
     176   [ +  -  +  -  :        6874 :     entry.pushKV("hash", tx.GetWitnessHash().GetHex());
                   +  - ]
     177   [ +  -  +  - ]:        6874 :     entry.pushKV("version", tx.version);
     178   [ +  -  +  - ]:        6874 :     entry.pushKV("size", tx.GetTotalSize());
     179   [ +  -  +  - ]:        6874 :     entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
     180   [ +  -  +  - ]:        6874 :     entry.pushKV("weight", GetTransactionWeight(tx));
     181   [ +  -  +  - ]:        6874 :     entry.pushKV("locktime", (int64_t)tx.nLockTime);
     182                 :             : 
     183                 :        3437 :     UniValue vin{UniValue::VARR};
     184         [ +  - ]:        3437 :     vin.reserve(tx.vin.size());
     185                 :             : 
     186                 :             :     // If available, use Undo data to calculate the fee. Note that txundo == nullptr
     187                 :             :     // for coinbase transactions and for transactions where undo data is unavailable.
     188                 :             :     const bool have_undo = txundo != nullptr;
     189                 :             :     CAmount amt_total_in = 0;
     190                 :      149583 :     CAmount amt_total_out = 0;
     191                 :             : 
     192         [ +  + ]:      149583 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     193                 :      146146 :         const CTxIn& txin = tx.vin[i];
     194                 :      146146 :         UniValue in(UniValue::VOBJ);
     195         [ +  + ]:      146146 :         if (tx.IsCoinBase()) {
     196   [ +  +  +  -  :          87 :             in.pushKV("coinbase", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     197                 :             :         } else {
     198   [ +  -  +  -  :      292234 :             in.pushKV("txid", txin.prevout.hash.GetHex());
             +  -  +  - ]
     199   [ +  -  +  -  :      292234 :             in.pushKV("vout", (int64_t)txin.prevout.n);
                   +  - ]
     200                 :      146117 :             UniValue o(UniValue::VOBJ);
     201   [ +  -  +  -  :      292234 :             o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
             +  -  +  - ]
     202   [ +  +  +  -  :      438351 :             o.pushKV("hex", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     203   [ +  -  +  - ]:      292234 :             in.pushKV("scriptSig", std::move(o));
     204                 :      146117 :         }
     205         [ +  + ]:      146146 :         if (!tx.vin[i].scriptWitness.IsNull()) {
     206                 :       10937 :             UniValue txinwitness(UniValue::VARR);
     207         [ +  - ]:       10937 :             txinwitness.reserve(tx.vin[i].scriptWitness.stack.size());
     208         [ +  + ]:      497212 :             for (const auto& item : tx.vin[i].scriptWitness.stack) {
     209   [ +  -  +  -  :      486275 :                 txinwitness.push_back(HexStr(item));
                   +  - ]
     210                 :             :             }
     211   [ +  -  +  - ]:       21874 :             in.pushKV("txinwitness", std::move(txinwitness));
     212                 :       10937 :         }
     213         [ -  + ]:      146146 :         if (have_undo) {
     214         [ #  # ]:           0 :             const Coin& prev_coin = txundo->vprevout[i];
     215                 :           0 :             const CTxOut& prev_txout = prev_coin.out;
     216                 :             : 
     217                 :           0 :             amt_total_in += prev_txout.nValue;
     218                 :             : 
     219         [ #  # ]:           0 :             if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
     220                 :           0 :                 UniValue o_script_pub_key(UniValue::VOBJ);
     221         [ #  # ]:           0 :                 ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
     222                 :             : 
     223                 :           0 :                 UniValue p(UniValue::VOBJ);
     224   [ #  #  #  #  :           0 :                 p.pushKV("generated", bool(prev_coin.fCoinBase));
                   #  # ]
     225   [ #  #  #  #  :           0 :                 p.pushKV("height", uint64_t(prev_coin.nHeight));
                   #  # ]
     226   [ #  #  #  #  :           0 :                 p.pushKV("value", ValueFromAmount(prev_txout.nValue));
                   #  # ]
     227   [ #  #  #  # ]:           0 :                 p.pushKV("scriptPubKey", std::move(o_script_pub_key));
     228   [ #  #  #  # ]:           0 :                 in.pushKV("prevout", std::move(p));
     229                 :           0 :             }
     230                 :             :         }
     231   [ +  -  +  -  :      292292 :         in.pushKV("sequence", (int64_t)txin.nSequence);
                   +  - ]
     232         [ +  - ]:      146146 :         vin.push_back(std::move(in));
     233                 :      146146 :     }
     234   [ +  -  +  - ]:        6874 :     entry.pushKV("vin", std::move(vin));
     235                 :             : 
     236                 :        3437 :     UniValue vout(UniValue::VARR);
     237         [ +  - ]:        3437 :     vout.reserve(tx.vout.size());
     238         [ +  + ]:     1440529 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     239                 :     1437092 :         const CTxOut& txout = tx.vout[i];
     240                 :             : 
     241                 :     1437092 :         UniValue out(UniValue::VOBJ);
     242                 :             : 
     243   [ +  -  +  -  :     2874184 :         out.pushKV("value", ValueFromAmount(txout.nValue));
                   +  - ]
     244   [ +  -  +  -  :     2874184 :         out.pushKV("n", (int64_t)i);
                   +  - ]
     245                 :             : 
     246                 :     1437092 :         UniValue o(UniValue::VOBJ);
     247         [ +  - ]:     1437092 :         ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
     248   [ +  -  +  - ]:     2874184 :         out.pushKV("scriptPubKey", std::move(o));
     249         [ +  - ]:     1437092 :         vout.push_back(std::move(out));
     250                 :             : 
     251         [ -  + ]:     1437092 :         if (have_undo) {
     252                 :           0 :             amt_total_out += txout.nValue;
     253                 :             :         }
     254                 :     1437092 :     }
     255   [ +  -  +  - ]:        6874 :     entry.pushKV("vout", std::move(vout));
     256                 :             : 
     257         [ -  + ]:        3437 :     if (have_undo) {
     258                 :           0 :         const CAmount fee = amt_total_in - amt_total_out;
     259         [ #  # ]:           0 :         CHECK_NONFATAL(MoneyRange(fee));
     260   [ #  #  #  #  :           0 :         entry.pushKV("fee", ValueFromAmount(fee));
                   #  # ]
     261                 :             :     }
     262                 :             : 
     263         [ +  + ]:        3437 :     if (!block_hash.IsNull()) {
     264   [ +  -  +  -  :        2586 :         entry.pushKV("blockhash", block_hash.GetHex());
             +  -  +  - ]
     265                 :             :     }
     266                 :             : 
     267         [ +  + ]:        3437 :     if (include_hex) {
     268   [ +  -  +  -  :        5176 :         entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
             +  -  +  - ]
     269                 :             :     }
     270                 :        3437 : }
        

Generated by: LCOV version 2.0-1