LCOV - code coverage report
Current view: top level - src - core_read.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 157 175 89.7 %
Date: 2021-06-29 14:35:33 Functions: 9 10 90.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 76 98 77.6 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2020 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 <primitives/block.h>
#       8                 :            : #include <primitives/transaction.h>
#       9                 :            : #include <script/script.h>
#      10                 :            : #include <script/sign.h>
#      11                 :            : #include <serialize.h>
#      12                 :            : #include <streams.h>
#      13                 :            : #include <univalue.h>
#      14                 :            : #include <util/strencodings.h>
#      15                 :            : #include <version.h>
#      16                 :            : 
#      17                 :            : #include <boost/algorithm/string/classification.hpp>
#      18                 :            : #include <boost/algorithm/string/split.hpp>
#      19                 :            : 
#      20                 :            : #include <algorithm>
#      21                 :            : #include <string>
#      22                 :            : 
#      23                 :            : namespace {
#      24                 :            : 
#      25                 :            : opcodetype ParseOpCode(const std::string& s)
#      26                 :       7728 : {
#      27                 :       7728 :     static std::map<std::string, opcodetype> mapOpNames;
#      28                 :            : 
#      29         [ +  + ]:       7728 :     if (mapOpNames.empty())
#      30                 :          2 :     {
#      31         [ +  + ]:        374 :         for (unsigned int op = 0; op <= MAX_OPCODE; op++)
#      32                 :        372 :         {
#      33                 :            :             // Allow OP_RESERVED to get into mapOpNames
#      34 [ +  + ][ +  + ]:        372 :             if (op < OP_NOP && op != OP_RESERVED)
#      35                 :        192 :                 continue;
#      36                 :            : 
#      37                 :        180 :             std::string strName = GetOpName(static_cast<opcodetype>(op));
#      38         [ -  + ]:        180 :             if (strName == "OP_UNKNOWN")
#      39                 :          0 :                 continue;
#      40                 :        180 :             mapOpNames[strName] = static_cast<opcodetype>(op);
#      41                 :            :             // Convenience: OP_ADD and just ADD are both recognized:
#      42         [ +  - ]:        180 :             if (strName.compare(0, 3, "OP_") == 0) {  // strName starts with "OP_"
#      43                 :        180 :                 mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
#      44                 :        180 :             }
#      45                 :        180 :         }
#      46                 :          2 :     }
#      47                 :            : 
#      48                 :       7728 :     auto it = mapOpNames.find(s);
#      49         [ -  + ]:       7728 :     if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
#      50                 :       7728 :     return it->second;
#      51                 :       7728 : }
#      52                 :            : 
#      53                 :            : } // namespace
#      54                 :            : 
#      55                 :            : CScript ParseScript(const std::string& s)
#      56                 :       5380 : {
#      57                 :       5380 :     CScript result;
#      58                 :            : 
#      59                 :       5380 :     std::vector<std::string> words;
#      60                 :       5380 :     boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
#      61                 :            : 
#      62         [ +  + ]:      29402 :     for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
#      63                 :      24022 :     {
#      64         [ +  + ]:      24022 :         if (w->empty())
#      65                 :        324 :         {
#      66                 :            :             // Empty string, ignore. (boost::split given '' will return one word)
#      67                 :        324 :         }
#      68 [ +  + ][ +  + ]:      23698 :         else if (std::all_of(w->begin(), w->end(), ::IsDigit) ||
#      69 [ +  + ][ +  - ]:      23698 :             (w->front() == '-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(), ::IsDigit)))
#                 [ +  - ]
#      70                 :       9080 :         {
#      71                 :            :             // Number
#      72                 :       9080 :             int64_t n = atoi64(*w);
#      73                 :            : 
#      74                 :            :             //limit the range of numbers ParseScript accepts in decimal
#      75                 :            :             //since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
#      76 [ -  + ][ -  + ]:       9080 :             if (n > int64_t{0xffffffff} || n < -1 * int64_t{0xffffffff}) {
#      77                 :          0 :                 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
#      78                 :          0 :                                          "range -0xFFFFFFFF...0xFFFFFFFF");
#      79                 :          0 :             }
#      80                 :            : 
#      81                 :       9080 :             result << n;
#      82                 :       9080 :         }
#      83 [ +  + ][ +  + ]:      14618 :         else if (w->substr(0,2) == "0x" && w->size() > 2 && IsHex(std::string(w->begin()+2, w->end())))
#         [ +  - ][ +  - ]
#      84                 :       4402 :         {
#      85                 :            :             // Raw hex data, inserted NOT pushed onto stack:
#      86                 :       4402 :             std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
#      87                 :       4402 :             result.insert(result.end(), raw.begin(), raw.end());
#      88                 :       4402 :         }
#      89 [ +  - ][ +  + ]:      10216 :         else if (w->size() >= 2 && w->front() == '\'' && w->back() == '\'')
#                 [ +  - ]
#      90                 :       2488 :         {
#      91                 :            :             // Single-quoted string, pushed as data. NOTE: this is poor-man's
#      92                 :            :             // parsing, spaces/tabs/newlines in single-quoted strings won't work.
#      93                 :       2488 :             std::vector<unsigned char> value(w->begin()+1, w->end()-1);
#      94                 :       2488 :             result << value;
#      95                 :       2488 :         }
#      96                 :       7728 :         else
#      97                 :       7728 :         {
#      98                 :            :             // opcode, e.g. OP_ADD or ADD:
#      99                 :       7728 :             result << ParseOpCode(*w);
#     100                 :       7728 :         }
#     101                 :      24022 :     }
#     102                 :            : 
#     103                 :       5380 :     return result;
#     104                 :       5380 : }
#     105                 :            : 
#     106                 :            : // Check that all of the input and output scripts of a transaction contains valid opcodes
#     107                 :            : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
#     108                 :      14963 : {
#     109                 :            :     // Check input scripts for non-coinbase txs
#     110         [ +  + ]:      14963 :     if (!CTransaction(tx).IsCoinBase()) {
#     111         [ +  + ]:      74724 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
#     112 [ +  + ][ -  + ]:      59767 :             if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
#     113                 :          1 :                 return false;
#     114                 :          1 :             }
#     115                 :      59767 :         }
#     116                 :      14958 :     }
#     117                 :            :     // Check output scripts
#     118         [ +  + ]:     176669 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
#     119 [ +  + ][ -  + ]:     161717 :         if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
#     120                 :         10 :             return false;
#     121                 :         10 :         }
#     122                 :     161717 :     }
#     123                 :            : 
#     124                 :      14962 :     return true;
#     125                 :      14962 : }
#     126                 :            : 
#     127                 :            : static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
#     128                 :      14976 : {
#     129                 :            :     // General strategy:
#     130                 :            :     // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
#     131                 :            :     //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
#     132                 :            :     //   0-input 1-output incomplete transaction).
#     133                 :            :     //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
#     134                 :            :     //     disables extended if false).
#     135                 :            :     //   - Ignore serializations that do not fully consume the hex string.
#     136                 :            :     // - If neither succeeds, fail.
#     137                 :            :     // - If only one succeeds, return that one.
#     138                 :            :     // - If both decode attempts succeed:
#     139                 :            :     //   - If only one passes the CheckTxScriptsSanity check, return that one.
#     140                 :            :     //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
#     141                 :            : 
#     142                 :      14976 :     CMutableTransaction tx_extended, tx_legacy;
#     143                 :      14976 :     bool ok_extended = false, ok_legacy = false;
#     144                 :            : 
#     145                 :            :     // Try decoding with extended serialization support, and remember if the result successfully
#     146                 :            :     // consumes the entire input.
#     147         [ +  + ]:      14976 :     if (try_witness) {
#     148                 :      14968 :         CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
#     149                 :      14968 :         try {
#     150                 :      14968 :             ssData >> tx_extended;
#     151         [ +  + ]:      14968 :             if (ssData.empty()) ok_extended = true;
#     152                 :      14968 :         } catch (const std::exception&) {
#     153                 :            :             // Fall through.
#     154                 :        261 :         }
#     155                 :      14968 :     }
#     156                 :            : 
#     157                 :            :     // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
#     158                 :            :     // don't bother decoding the other way.
#     159 [ +  + ][ +  + ]:      14976 :     if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
#     160                 :      14694 :         tx = std::move(tx_extended);
#     161                 :      14694 :         return true;
#     162                 :      14694 :     }
#     163                 :            : 
#     164                 :            :     // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
#     165         [ +  + ]:        282 :     if (try_no_witness) {
#     166                 :        268 :         CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
#     167                 :        268 :         try {
#     168                 :        268 :             ssData >> tx_legacy;
#     169         [ +  + ]:        268 :             if (ssData.empty()) ok_legacy = true;
#     170                 :        268 :         } catch (const std::exception&) {
#     171                 :            :             // Fall through.
#     172                 :          4 :         }
#     173                 :        268 :     }
#     174                 :            : 
#     175                 :            :     // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
#     176                 :            :     // at this point that extended decoding either failed or doesn't pass the sanity check.
#     177 [ +  + ][ +  - ]:        282 :     if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
#     178                 :        258 :         tx = std::move(tx_legacy);
#     179                 :        258 :         return true;
#     180                 :        258 :     }
#     181                 :            : 
#     182                 :            :     // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
#     183         [ +  + ]:         24 :     if (ok_extended) {
#     184                 :         11 :         tx = std::move(tx_extended);
#     185                 :         11 :         return true;
#     186                 :         11 :     }
#     187                 :            : 
#     188                 :            :     // If legacy decoding succeeded and extended didn't, return the legacy one.
#     189         [ -  + ]:         13 :     if (ok_legacy) {
#     190                 :          0 :         tx = std::move(tx_legacy);
#     191                 :          0 :         return true;
#     192                 :          0 :     }
#     193                 :            : 
#     194                 :            :     // If none succeeded, we failed.
#     195                 :         13 :     return false;
#     196                 :         13 : }
#     197                 :            : 
#     198                 :            : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
#     199                 :      14981 : {
#     200         [ +  + ]:      14981 :     if (!IsHex(hex_tx)) {
#     201                 :          5 :         return false;
#     202                 :          5 :     }
#     203                 :            : 
#     204                 :      14976 :     std::vector<unsigned char> txData(ParseHex(hex_tx));
#     205                 :      14976 :     return DecodeTx(tx, txData, try_no_witness, try_witness);
#     206                 :      14976 : }
#     207                 :            : 
#     208                 :            : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
#     209                 :         13 : {
#     210         [ +  + ]:         13 :     if (!IsHex(hex_header)) return false;
#     211                 :            : 
#     212                 :         12 :     const std::vector<unsigned char> header_data{ParseHex(hex_header)};
#     213                 :         12 :     CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
#     214                 :         12 :     try {
#     215                 :         12 :         ser_header >> header;
#     216                 :         12 :     } catch (const std::exception&) {
#     217                 :          1 :         return false;
#     218                 :          1 :     }
#     219                 :         11 :     return true;
#     220                 :         11 : }
#     221                 :            : 
#     222                 :            : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
#     223                 :       4222 : {
#     224         [ -  + ]:       4222 :     if (!IsHex(strHexBlk))
#     225                 :          0 :         return false;
#     226                 :            : 
#     227                 :       4222 :     std::vector<unsigned char> blockData(ParseHex(strHexBlk));
#     228                 :       4222 :     CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
#     229                 :       4222 :     try {
#     230                 :       4222 :         ssBlock >> block;
#     231                 :       4222 :     }
#     232                 :       4222 :     catch (const std::exception&) {
#     233                 :          3 :         return false;
#     234                 :          3 :     }
#     235                 :            : 
#     236                 :       4219 :     return true;
#     237                 :       4219 : }
#     238                 :            : 
#     239                 :            : bool ParseHashStr(const std::string& strHex, uint256& result)
#     240                 :         84 : {
#     241 [ +  + ][ -  + ]:         84 :     if ((strHex.size() != 64) || !IsHex(strHex))
#     242                 :          3 :         return false;
#     243                 :            : 
#     244                 :         81 :     result.SetHex(strHex);
#     245                 :         81 :     return true;
#     246                 :         81 : }
#     247                 :            : 
#     248                 :            : std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
#     249                 :          0 : {
#     250                 :          0 :     std::string strHex;
#     251         [ #  # ]:          0 :     if (v.isStr())
#     252                 :          0 :         strHex = v.getValStr();
#     253         [ #  # ]:          0 :     if (!IsHex(strHex))
#     254                 :          0 :         throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
#     255                 :          0 :     return ParseHex(strHex);
#     256                 :          0 : }
#     257                 :            : 
#     258                 :            : int ParseSighashString(const UniValue& sighash)
#     259                 :       1591 : {
#     260                 :       1591 :     int hash_type = SIGHASH_ALL;
#     261         [ +  + ]:       1591 :     if (!sighash.isNull()) {
#     262                 :        317 :         static std::map<std::string, int> map_sighash_values = {
#     263                 :        317 :             {std::string("ALL"), int(SIGHASH_ALL)},
#     264                 :        317 :             {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
#     265                 :        317 :             {std::string("NONE"), int(SIGHASH_NONE)},
#     266                 :        317 :             {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
#     267                 :        317 :             {std::string("SINGLE"), int(SIGHASH_SINGLE)},
#     268                 :        317 :             {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
#     269                 :        317 :         };
#     270                 :        317 :         std::string strHashType = sighash.get_str();
#     271                 :        317 :         const auto& it = map_sighash_values.find(strHashType);
#     272         [ +  - ]:        317 :         if (it != map_sighash_values.end()) {
#     273                 :        317 :             hash_type = it->second;
#     274                 :        317 :         } else {
#     275                 :          0 :             throw std::runtime_error(strHashType + " is not a valid sighash parameter.");
#     276                 :          0 :         }
#     277                 :       1591 :     }
#     278                 :       1591 :     return hash_type;
#     279                 :       1591 : }

Generated by: LCOV version 1.14