LCOV - code coverage report
Current view: top level - src - core_write.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 163 184 88.6 %
Date: 2023-10-05 15:40:34 Functions: 9 9 100.0 %
Branches: 278 535 52.0 %

           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                 :     599420 : UniValue ValueFromAmount(const CAmount amount)
      27                 :            : {
      28                 :            :     static_assert(COIN > 1);
      29                 :     599420 :     int64_t quotient = amount / COIN;
      30                 :     599420 :     int64_t remainder = amount % COIN;
      31         [ +  + ]:     599420 :     if (amount < 0) {
      32                 :      36970 :         quotient = -quotient;
      33                 :      36970 :         remainder = -remainder;
      34                 :      36970 :     }
      35         [ +  - ]:     599420 :     return UniValue(UniValue::VNUM,
      36                 :     599420 :             strprintf("%s%d.%08d", amount < 0 ? "-" : "", quotient, remainder));
      37                 :          0 : }
      38                 :            : 
      39                 :        458 : std::string FormatScript(const CScript& script)
      40                 :            : {
      41                 :        458 :     std::string ret;
      42         [ +  - ]:        458 :     CScript::const_iterator it = script.begin();
      43                 :            :     opcodetype op;
      44   [ +  +  +  -  :     217969 :     while (it != script.end()) {
                   +  + ]
      45                 :     955027 :         CScript::const_iterator it2 = it;
      46                 :     955027 :         std::vector<unsigned char> vch;
      47   [ +  +  +  + ]:     955027 :         if (script.GetOp(it, op, vch)) {
      48         [ +  + ]:     368259 :             if (op == OP_0) {
      49         [ +  - ]:      31343 :                 ret += "0 ";
      50                 :      31343 :                 continue;
      51   [ +  +  +  + ]:     336916 :             } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) {
      52   [ +  +  +  - ]:     615164 :                 ret += strprintf("%i ", op - OP_1NEGATE - 1);
      53                 :      28478 :                 continue;
      54   [ +  +  +  + ]:     308438 :             } else if (op >= OP_NOP && op <= OP_NOP10) {
      55         [ +  - ]:     271563 :                 std::string str(GetOpName(op));
      56   [ +  -  -  +  :     271563 :                 if (str.substr(0, 3) == std::string("OP_")) {
                   +  - ]
      57   [ +  -  +  -  :     271563 :                     ret += str.substr(3, std::string::npos) + " ";
                   +  - ]
      58                 :     271563 :                     continue;
      59                 :            :                 }
      60         [ +  - ]:     271563 :             }
      61         [ +  + ]:      36875 :             if (vch.size() > 0) {
      62   [ +  -  +  -  :      28388 :                 ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
          +  -  +  -  +  
                -  +  - ]
      63   [ +  -  +  -  :      14194 :                                                HexStr(std::vector<uint8_t>(it - vch.size(), it)));
             +  -  +  - ]
      64                 :      14194 :             } else {
      65   [ +  -  +  -  :      22681 :                 ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
          +  -  +  -  +  
                      - ]
      66                 :            :             }
      67                 :      36875 :             continue;
      68                 :            :         }
      69   [ +  -  +  -  :         82 :         ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
          +  -  +  -  -  
                +  -  + ]
      70                 :         82 :         break;
      71      [ -  +  + ]:     368341 :     }
      72   [ +  +  +  + ]:    1172162 :     return ret.substr(0, ret.empty() ? ret.npos : ret.size() - 1);
      73                 :    1625158 : }
      74                 :        173 : 
      75   [ +  -  #  # ]:       1211 : const std::map<unsigned char, std::string> mapSigHashTypes = {
      76   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_ALL), std::string("ALL")},
      77   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_ALL|SIGHASH_ANYONECANPAY), std::string("ALL|ANYONECANPAY")},
      78   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_NONE), std::string("NONE")},
      79   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_NONE|SIGHASH_ANYONECANPAY), std::string("NONE|ANYONECANPAY")},
      80   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_SINGLE), std::string("SINGLE")},
      81   [ +  -  +  - ]:        173 :     {static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
      82                 :            : };
      83                 :            : 
      84                 :         65 : std::string SighashToStr(unsigned char sighash_type)
      85                 :            : {
      86                 :         65 :     const auto& it = mapSigHashTypes.find(sighash_type);
      87   [ +  +  -  + ]:         65 :     if (it == mapSigHashTypes.end()) return "";
      88                 :          3 :     return it->second;
      89                 :         65 : }
      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                 :     659843 : std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode)
      99                 :            : {
     100                 :     659843 :     std::string str;
     101                 :            :     opcodetype opcode;
     102                 :     659843 :     std::vector<unsigned char> vch;
     103         [ +  - ]:     659843 :     CScript::const_iterator pc = script.begin();
     104   [ +  -  +  -  :    7515498 :     while (pc < script.end()) {
                   +  + ]
     105         [ +  + ]:    7242759 :         if (!str.empty()) {
     106         [ +  - ]:    6677740 :             str += " ";
     107                 :    6677740 :         }
     108   [ +  -  +  + ]:    7242759 :         if (!script.GetOp(pc, opcode, vch)) {
     109         [ +  - ]:     387104 :             str += "[error]";
     110                 :     387104 :             return str;
     111                 :            :         }
     112   [ +  -  +  + ]:    6855655 :         if (0 <= opcode && opcode <= OP_PUSHDATA4) {
     113         [ +  + ]:    1997870 :             if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
     114   [ +  -  +  -  :    1567027 :                 str += strprintf("%d", CScriptNum(vch, false).getint());
             +  -  +  - ]
     115                 :    1567027 :             } else {
     116                 :            :                 // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
     117   [ +  +  +  -  :     430843 :                 if (fAttemptSighashDecode && !script.IsUnspendable()) {
                   +  + ]
     118                 :      40826 :                     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   [ +  -  +  + ]:      40826 :                     if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC, nullptr)) {
     124                 :       5638 :                         const unsigned char chSigHashType = vch.back();
     125         [ +  - ]:       5638 :                         const auto it = mapSigHashTypes.find(chSigHashType);
     126         [ +  - ]:       5638 :                         if (it != mapSigHashTypes.end()) {
     127   [ +  -  +  - ]:       5638 :                             strSigHashDecode = "[" + it->second + "]";
     128                 :       5638 :                             vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode.
     129                 :       5638 :                         }
     130                 :       5638 :                     }
     131   [ +  -  +  -  :      40826 :                     str += HexStr(vch) + strSigHashDecode;
             +  -  +  - ]
     132                 :      40826 :                 } else {
     133   [ +  -  +  -  :     390017 :                     str += HexStr(vch);
                   +  - ]
     134                 :            :                 }
     135                 :            :             }
     136                 :    1997870 :         } else {
     137   [ +  -  +  - ]:    4857785 :             str += GetOpName(opcode);
     138                 :            :         }
     139                 :            :     }
     140                 :     272739 :     return str;
     141         [ +  - ]:     659843 : }
     142                 :            : 
     143                 :       2008 : std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
     144                 :            : {
     145                 :       2008 :     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
     146         [ +  - ]:       2008 :     ssTx << tx;
     147   [ +  -  +  - ]:       2008 :     return HexStr(ssTx);
     148                 :       2008 : }
     149                 :            : 
     150                 :     600346 : void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool include_address, const SigningProvider* provider)
     151                 :            : {
     152                 :     600346 :     CTxDestination address;
     153                 :            : 
     154   [ +  -  +  -  :     600346 :     out.pushKV("asm", ScriptToAsmStr(script));
             +  -  +  - ]
     155         [ +  + ]:     600346 :     if (include_address) {
     156   [ +  -  +  +  :     600038 :         out.pushKV("desc", InferDescriptor(script, provider ? *provider : DUMMY_SIGNING_PROVIDER)->ToString());
          +  -  +  -  +  
                -  +  - ]
     157                 :     600038 :     }
     158         [ +  + ]:     600346 :     if (include_hex) {
     159   [ +  -  +  -  :     599645 :         out.pushKV("hex", HexStr(script));
          +  -  +  -  +  
                      - ]
     160                 :     599645 :     }
     161                 :            : 
     162                 :     600346 :     std::vector<std::vector<unsigned char>> solns;
     163         [ +  - ]:     600346 :     const TxoutType type{Solver(script, solns)};
     164                 :            : 
     165   [ +  +  +  -  :     600346 :     if (include_address && ExtractDestination(script, address) && type != TxoutType::PUBKEY) {
             +  +  -  + ]
     166   [ +  -  +  -  :     126960 :         out.pushKV("address", EncodeDestination(address));
             +  -  -  + ]
     167                 :     126960 :     }
     168   [ +  -  +  -  :     600346 :     out.pushKV("type", GetTxnOutputType(type));
             +  -  +  - ]
     169                 :     600346 : }
     170                 :            : 
     171                 :       1356 : void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
     172                 :            : {
     173                 :       1356 :     CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
     174                 :            : 
     175   [ +  -  +  -  :       1356 :     entry.pushKV("txid", tx.GetHash().GetHex());
          +  -  +  -  +  
                      - ]
     176   [ +  -  +  -  :       1356 :     entry.pushKV("hash", tx.GetWitnessHash().GetHex());
          +  -  +  -  +  
                      - ]
     177                 :            :     // Transaction version is actually unsigned in consensus checks, just signed in memory,
     178                 :            :     // so cast to unsigned before giving it to the user.
     179   [ +  -  +  -  :       1356 :     entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion)));
                   +  - ]
     180   [ +  -  +  -  :       1356 :     entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
             +  -  +  - ]
     181   [ +  -  +  -  :       1356 :     entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
             +  -  +  - ]
     182   [ +  -  +  -  :       1356 :     entry.pushKV("weight", GetTransactionWeight(tx));
             +  -  +  - ]
     183   [ +  -  +  -  :       1356 :     entry.pushKV("locktime", (int64_t)tx.nLockTime);
                   +  - ]
     184                 :            : 
     185         [ +  - ]:       1356 :     UniValue vin{UniValue::VARR};
     186                 :            : 
     187                 :            :     // If available, use Undo data to calculate the fee. Note that txundo == nullptr
     188                 :            :     // for coinbase transactions and for transactions where undo data is unavailable.
     189                 :       1356 :     const bool have_undo = txundo != nullptr;
     190                 :       1356 :     CAmount amt_total_in = 0;
     191                 :       1356 :     CAmount amt_total_out = 0;
     192                 :            : 
     193         [ +  + ]:      60406 :     for (unsigned int i = 0; i < tx.vin.size(); i++) {
     194                 :      59050 :         const CTxIn& txin = tx.vin[i];
     195         [ +  - ]:      59050 :         UniValue in(UniValue::VOBJ);
     196   [ +  -  +  + ]:      59050 :         if (tx.IsCoinBase()) {
     197   [ +  -  +  -  :         11 :             in.pushKV("coinbase", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     198                 :         11 :         } else {
     199   [ +  -  +  -  :      59039 :             in.pushKV("txid", txin.prevout.hash.GetHex());
             +  -  +  - ]
     200   [ +  -  +  -  :      59039 :             in.pushKV("vout", (int64_t)txin.prevout.n);
                   +  - ]
     201         [ +  - ]:      59039 :             UniValue o(UniValue::VOBJ);
     202   [ +  -  +  -  :      59039 :             o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
             +  -  +  - ]
     203   [ +  -  +  -  :      59039 :             o.pushKV("hex", HexStr(txin.scriptSig));
          +  -  +  -  +  
                      - ]
     204   [ +  -  +  -  :      59039 :             in.pushKV("scriptSig", o);
                   +  - ]
     205                 :      59039 :         }
     206   [ +  -  +  + ]:      59050 :         if (!tx.vin[i].scriptWitness.IsNull()) {
     207         [ +  - ]:       3266 :             UniValue txinwitness(UniValue::VARR);
     208         [ +  + ]:      33624 :             for (const auto& item : tx.vin[i].scriptWitness.stack) {
     209   [ +  -  +  -  :      30358 :                 txinwitness.push_back(HexStr(item));
             +  -  +  - ]
     210                 :            :             }
     211   [ +  -  +  -  :       3266 :             in.pushKV("txinwitness", txinwitness);
                   +  - ]
     212                 :       3266 :         }
     213         [ +  - ]:      59050 :         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", o_script_pub_key);
                   #  # ]
     228   [ #  #  #  #  :          0 :                 in.pushKV("prevout", p);
                   #  # ]
     229                 :          0 :             }
     230                 :          0 :         }
     231   [ +  -  +  -  :      59050 :         in.pushKV("sequence", (int64_t)txin.nSequence);
                   +  - ]
     232   [ +  -  +  - ]:      59050 :         vin.push_back(in);
     233                 :      59050 :     }
     234   [ +  -  +  -  :       1356 :     entry.pushKV("vin", vin);
                   +  - ]
     235                 :            : 
     236         [ +  - ]:       1356 :     UniValue vout(UniValue::VARR);
     237         [ +  + ]:     600653 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     238                 :     599297 :         const CTxOut& txout = tx.vout[i];
     239                 :            : 
     240         [ +  - ]:     599297 :         UniValue out(UniValue::VOBJ);
     241                 :            : 
     242   [ +  -  +  -  :     599297 :         out.pushKV("value", ValueFromAmount(txout.nValue));
                   +  - ]
     243   [ +  -  +  -  :     599297 :         out.pushKV("n", (int64_t)i);
                   +  - ]
     244                 :            : 
     245         [ +  - ]:     599297 :         UniValue o(UniValue::VOBJ);
     246         [ +  - ]:     599297 :         ScriptToUniv(txout.scriptPubKey, /*out=*/o, /*include_hex=*/true, /*include_address=*/true);
     247   [ +  -  +  -  :     599297 :         out.pushKV("scriptPubKey", o);
                   +  - ]
     248   [ +  -  -  + ]:     599297 :         vout.push_back(out);
     249                 :            : 
     250         [ +  - ]:     599297 :         if (have_undo) {
     251                 :          0 :             amt_total_out += txout.nValue;
     252                 :          0 :         }
     253                 :     599297 :     }
     254   [ +  -  +  -  :       1356 :     entry.pushKV("vout", vout);
                   +  - ]
     255                 :            : 
     256         [ -  + ]:       1356 :     if (have_undo) {
     257                 :          0 :         const CAmount fee = amt_total_in - amt_total_out;
     258   [ #  #  #  # ]:          0 :         CHECK_NONFATAL(MoneyRange(fee));
     259   [ #  #  #  #  :          0 :         entry.pushKV("fee", ValueFromAmount(fee));
                   #  # ]
     260                 :          0 :     }
     261                 :            : 
     262   [ +  -  +  + ]:       1356 :     if (!block_hash.IsNull()) {
     263   [ +  -  +  -  :        591 :         entry.pushKV("blockhash", block_hash.GetHex());
             +  -  +  - ]
     264                 :        591 :     }
     265                 :            : 
     266         [ +  + ]:       1356 :     if (include_hex) {
     267   [ +  -  +  -  :       1182 :         entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
             +  -  -  + ]
     268                 :       1182 :     }
     269                 :       1356 : }

Generated by: LCOV version 1.14