LCOV - code coverage report
Current view: top level - src - core_write.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 89.3 % 159 142
Test Date: 2024-11-04 04:15:01 Functions: 100.0 % 7 7
Branches: 54.5 % 378 206

             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                 :      848865 : UniValue ValueFromAmount(const CAmount amount)
      27                 :             : {
      28                 :      848865 :     static_assert(COIN > 1);
      29                 :      848865 :     int64_t quotient = amount / COIN;
      30                 :      848865 :     int64_t remainder = amount % COIN;
      31         [ +  + ]:      848865 :     if (amount < 0) {
      32                 :       54826 :         quotient = -quotient;
      33                 :       54826 :         remainder = -remainder;
      34                 :             :     }
      35                 :       54826 :     return UniValue(UniValue::VNUM,
      36                 :      848865 :             strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
      37                 :             : }
      38                 :             : 
      39                 :         966 : std::string FormatScript(const CScript& script)
      40                 :             : {
      41         [ +  + ]:         966 :     std::string ret;
      42         [ +  + ]:        1932 :     CScript::const_iterator it = script.begin();
      43                 :             :     opcodetype op;
      44         [ +  + ]:     1012743 :     while (it != script.end()) {
      45                 :     1012037 :         CScript::const_iterator it2 = it;
      46                 :     1012037 :         std::vector<unsigned char> vch;
      47   [ +  -  +  + ]:     1012037 :         if (script.GetOp(it, op, vch)) {
      48         [ +  + ]:     1011777 :             if (op == OP_0) {
      49         [ +  - ]:      101843 :                 ret += "0 ";
      50                 :      101843 :                 continue;
      51   [ +  +  +  + ]:      909934 :             } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
      52         [ +  - ]:       57788 :                 ret += strprintf("%i ", op - OP_1NEGATE - 1);
      53                 :       28894 :                 continue;
      54         [ +  + ]:      881040 :             } else if (op >= OP_NOP && op <= OP_NOP10) {
      55         [ +  - ]:      695171 :                 std::string str(GetOpName(op));
      56   [ +  -  +  -  :      695171 :                 if (str.substr(0, 3) == std::string("OP_")) {
                   +  - ]
      57   [ +  -  +  - ]:     2085513 :                     ret += str.substr(3, std::string::npos) + " ";
      58                 :      695171 :                     continue;
      59                 :             :                 }
      60                 :      695171 :             }
      61         [ +  + ]:      185869 :             if (vch.size() > 0) {
      62   [ +  -  +  -  :      404511 :                 ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
             +  -  +  - ]
      63   [ +  -  +  - ]:      404511 :                                                HexStr(std::vector<uint8_t>(it - vch.size(), it)));
      64                 :             :             } else {
      65   [ +  -  +  -  :      102064 :                 ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
                   +  - ]
      66                 :             :             }
      67                 :      185869 :             continue;
      68                 :      185869 :         }
      69   [ +  -  +  -  :         520 :         ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
                   +  - ]
      70                 :         260 :         break;
      71                 :     1012037 :     }
      72   [ +  +  +  - ]:         966 :     return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
      73                 :         966 : }
      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                 :         173 : std::string SighashToStr(unsigned char sighash_type)
      85                 :             : {
      86                 :         173 :     const auto& it = mapSigHashTypes.find(sighash_type);
      87         [ +  + ]:         173 :     if (it == mapSigHashTypes.end()) return "";
      88                 :           8 :     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                 :      931929 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
      99                 :             : {
     100         [ +  + ]:      931929 :     std::string str;
     101                 :      931929 :     opcodetype opcode;
     102                 :      931929 :     std::vector<unsigned char> vch;
     103         [ +  + ]:     1863858 :     CScript::const_iterator pc = script.begin();
     104         [ +  + ]:    10002334 :     while (pc < script.end()) {
     105         [ +  + ]:     9567180 :         if (!str.empty()) {
     106         [ +  - ]:     8813735 :             str += " ";
     107                 :             :         }
     108   [ +  -  +  + ]:     9567180 :         if (!script.GetOp(pc, opcode, vch)) {
     109         [ +  - ]:      931929 :             str += "[error]";
     110                 :             :             return str;
     111                 :             :         }
     112         [ +  + ]:     9070405 :         if (0 <= opcode && opcode <= OP_PUSHDATA4) {
     113         [ +  + ]:     3583546 :             if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
     114   [ +  -  +  - ]:     8781297 :                 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   [ +  +  +  + ]:      656447 :                 if (fAttemptSighashDecode && !script.IsUnspendable()) {
     118         [ +  - ]:       64125 :                     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   [ +  -  +  + ]:       64125 :                     if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
     124                 :        3972 :                         const unsigned char chSigHashType = vch.back();
     125                 :        3972 :                         const auto it = mapSigHashTypes.find(chSigHashType);
     126         [ +  - ]:        3972 :                         if (it != mapSigHashTypes.end()) {
     127         [ +  - ]:        7944 :                             strSigHashDecode = "[" + it->second + "]";
     128                 :        3972 :                             vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
     129                 :             :                         }
     130                 :             :                     }
     131   [ +  -  +  - ]:      128250 :                     str += HexStr(vch) + strSigHashDecode;
     132                 :       64125 :                 } else {
     133         [ +  - ]:     1184644 :                     str += HexStr(vch);
     134                 :             :                 }
     135                 :             :             }
     136                 :             :         } else {
     137         [ +  - ]:    10973718 :             str += GetOpName(opcode);
     138                 :             :         }
     139                 :             :     }
     140                 :             :     return str;
     141                 :      931929 : }
     142                 :             : 
     143                 :        2709 : std::string EncodeHexTx(const CTransaction& tx)
     144                 :             : {
     145                 :        2709 :     DataStream ssTx;
     146         [ +  - ]:        2709 :     ssTx << TX_WITH_WITNESS(tx);
     147         [ +  - ]:        2709 :     return HexStr(ssTx);
     148                 :        2709 : }
     149                 :             : 
     150                 :      850833 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
     151                 :             : {
     152                 :      850833 :     CTxDestination address;
     153                 :             : 
     154   [ +  -  +  -  :     1701666 :     out.pushKV("asm", ScriptToAsmStr(script));
             +  -  +  - ]
     155         [ +  + ]:      850833 :     if (include_address) {
     156   [ +  +  +  -  :     1700278 :         out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
          +  -  +  -  +  
                -  +  - ]
     157                 :             :     }
     158         [ +  + ]:      850833 :     if (include_hex) {
     159   [ +  +  +  -  :     2548038 :         out.pushKV("hex", HexStr(script));
          +  -  +  -  +  
                      - ]
     160                 :             :     }
     161                 :             : 
     162                 :      850833 :     std::vector<std::vector<unsigned char>> solns;
     163         [ +  - ]:      850833 :     const TxoutType type{Solver(script, solns)};
     164                 :             : 
     165   [ +  +  +  -  :      850833 :     if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
             +  +  +  - ]
     166   [ +  -  +  -  :      363470 :         out.pushKV("address", EncodeDestination(address));
             +  -  +  - ]
     167                 :             :     }
     168   [ +  -  +  -  :     1701666 :     out.pushKV("type", GetTxnOutputType(type));
             +  -  +  - ]
     169                 :      850833 : }
     170                 :             : 
     171                 :        2137 : void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, const CTxUndo* txundo, TxVerbosity verbosity)
     172                 :             : {
     173                 :        2137 :     CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
     174                 :             : 
     175   [ +  -  +  -  :        4274 :     entry.pushKV("txid", tx.GetHash().GetHex());
                   +  - ]
     176   [ +  -  +  -  :        4274 :     entry.pushKV("hash", tx.GetWitnessHash().GetHex());
                   +  - ]
     177   [ +  -  +  - ]:        4274 :     entry.pushKV("version", tx.version);
     178   [ +  -  +  - ]:        4274 :     entry.pushKV("size", tx.GetTotalSize());
     179   [ +  -  +  - ]:        4274 :     entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
     180   [ +  -  +  - ]:        4274 :     entry.pushKV("weight", GetTransactionWeight(tx));
     181   [ +  -  +  - ]:        4274 :     entry.pushKV("locktime", (int64_t)tx.nLockTime);
     182                 :             : 
     183                 :        2137 :     UniValue vin{UniValue::VARR};
     184                 :             : 
     185                 :             :     // If available, use Undo data to calculate the fee. Note that txundo == nullptr
     186                 :             :     // for coinbase transactions and for transactions where undo data is unavailable.
     187                 :        2137 :     const bool have_undo = txundo != nullptr;
     188                 :        2137 :     CAmount amt_total_in = 0;
     189                 :        2137 :     CAmount amt_total_out = 0;
     190                 :             : 
     191         [ +  + ]:       82281 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     192                 :       80144 :         const CTxIn& txin = tx.vin[i];
     193                 :       80144 :         UniValue in(UniValue::VOBJ);
     194         [ +  + ]:       80144 :         if (tx.IsCoinBase()) {
     195   [ +  +  +  -  :          81 :             in.pushKV("coinbase", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     196                 :             :         } else {
     197   [ +  -  +  -  :      160234 :             in.pushKV("txid", txin.prevout.hash.GetHex());
             +  -  +  - ]
     198   [ +  -  +  -  :      160234 :             in.pushKV("vout", (int64_t)txin.prevout.n);
                   +  - ]
     199                 :       80117 :             UniValue o(UniValue::VOBJ);
     200   [ +  -  +  -  :      160234 :             o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
             +  -  +  - ]
     201   [ +  +  +  -  :      240351 :             o.pushKV("hex", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     202   [ +  -  +  - ]:      160234 :             in.pushKV("scriptSig", std::move(o));
     203                 :       80117 :         }
     204         [ +  + ]:       80144 :         if (!tx.vin[i].scriptWitness.IsNull()) {
     205                 :        7190 :             UniValue txinwitness(UniValue::VARR);
     206         [ +  + ]:      428630 :             for (const auto& item : tx.vin[i].scriptWitness.stack) {
     207   [ +  -  +  -  :      421440 :                 txinwitness.push_back(HexStr(item));
                   +  - ]
     208                 :             :             }
     209   [ +  -  +  - ]:       14380 :             in.pushKV("txinwitness", std::move(txinwitness));
     210                 :        7190 :         }
     211         [ -  + ]:       80144 :         if (have_undo) {
     212         [ #  # ]:           0 :             const Coin& prev_coin = txundo->vprevout[i];
     213                 :           0 :             const CTxOut& prev_txout = prev_coin.out;
     214                 :             : 
     215                 :           0 :             amt_total_in += prev_txout.nValue;
     216                 :             : 
     217         [ #  # ]:           0 :             if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
     218                 :           0 :                 UniValue o_script_pub_key(UniValue::VOBJ);
     219         [ #  # ]:           0 :                 ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);
     220                 :             : 
     221                 :           0 :                 UniValue p(UniValue::VOBJ);
     222   [ #  #  #  #  :           0 :                 p.pushKV("generated", bool(prev_coin.fCoinBase));
                   #  # ]
     223   [ #  #  #  #  :           0 :                 p.pushKV("height", uint64_t(prev_coin.nHeight));
                   #  # ]
     224   [ #  #  #  #  :           0 :                 p.pushKV("value", ValueFromAmount(prev_txout.nValue));
                   #  # ]
     225   [ #  #  #  # ]:           0 :                 p.pushKV("scriptPubKey", std::move(o_script_pub_key));
     226   [ #  #  #  # ]:           0 :                 in.pushKV("prevout", std::move(p));
     227                 :           0 :             }
     228                 :             :         }
     229   [ +  -  +  -  :      160288 :         in.pushKV("sequence", (int64_t)txin.nSequence);
                   +  - ]
     230         [ +  - ]:       80144 :         vin.push_back(std::move(in));
     231                 :       80144 :     }
     232   [ +  -  +  - ]:        4274 :     entry.pushKV("vin", std::move(vin));
     233                 :             : 
     234                 :        2137 :     UniValue vout(UniValue::VARR);
     235         [ +  + ]:      850538 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     236                 :      848401 :         const CTxOut& txout = tx.vout[i];
     237                 :             : 
     238                 :      848401 :         UniValue out(UniValue::VOBJ);
     239                 :             : 
     240   [ +  -  +  -  :     1696802 :         out.pushKV("value", ValueFromAmount(txout.nValue));
                   +  - ]
     241   [ +  -  +  -  :     1696802 :         out.pushKV("n", (int64_t)i);
                   +  - ]
     242                 :             : 
     243                 :      848401 :         UniValue o(UniValue::VOBJ);
     244         [ +  - ]:      848401 :         ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
     245   [ +  -  +  - ]:     1696802 :         out.pushKV("scriptPubKey", std::move(o));
     246         [ +  - ]:      848401 :         vout.push_back(std::move(out));
     247                 :             : 
     248         [ -  + ]:      848401 :         if (have_undo) {
     249                 :           0 :             amt_total_out += txout.nValue;
     250                 :             :         }
     251                 :      848401 :     }
     252   [ +  -  +  - ]:        4274 :     entry.pushKV("vout", std::move(vout));
     253                 :             : 
     254         [ -  + ]:        2137 :     if (have_undo) {
     255                 :           0 :         const CAmount fee = amt_total_in - amt_total_out;
     256         [ #  # ]:           0 :         CHECK_NONFATAL(MoneyRange(fee));
     257   [ #  #  #  #  :           0 :         entry.pushKV("fee", ValueFromAmount(fee));
                   #  # ]
     258                 :             :     }
     259                 :             : 
     260         [ +  + ]:        2137 :     if (!block_hash.IsNull()) {
     261   [ +  -  +  -  :        1628 :         entry.pushKV("blockhash", block_hash.GetHex());
             +  -  +  - ]
     262                 :             :     }
     263                 :             : 
     264         [ +  + ]:        2137 :     if (include_hex) {
     265   [ +  -  +  -  :        3256 :         entry.pushKV("hex", EncodeHexTx(tx)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
             +  -  +  - ]
     266                 :             :     }
     267                 :        2137 : }
        

Generated by: LCOV version 2.0-1