LCOV - code coverage report
Current view: top level - src - core_read.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 80.8 % 120 97
Test Date: 2024-08-28 04:44:32 Functions: 90.0 % 10 9
Branches: 53.5 % 200 107

             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 <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 <util/result.h>
      14                 :             : #include <util/strencodings.h>
      15                 :             : 
      16                 :             : #include <algorithm>
      17                 :             : #include <string>
      18                 :             : 
      19                 :             : using util::SplitString;
      20                 :             : 
      21                 :             : namespace {
      22                 :             : class OpCodeParser
      23                 :             : {
      24                 :             : private:
      25                 :             :     std::map<std::string, opcodetype> mapOpNames;
      26                 :             : 
      27                 :             : public:
      28                 :          12 :     OpCodeParser()
      29                 :          12 :     {
      30         [ +  + ]:        2244 :         for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
      31                 :             :             // Allow OP_RESERVED to get into mapOpNames
      32         [ +  + ]:        2232 :             if (op < OP_NOP && op != OP_RESERVED) {
      33                 :        1152 :                 continue;
      34                 :             :             }
      35                 :             : 
      36         [ +  - ]:        1080 :             std::string strName = GetOpName(static_cast<opcodetype>(op));
      37         [ -  + ]:        1080 :             if (strName == "OP_UNKNOWN") {
      38                 :           0 :                 continue;
      39                 :             :             }
      40         [ +  - ]:        1080 :             mapOpNames[strName] = static_cast<opcodetype>(op);
      41                 :             :             // Convenience: OP_ADD and just ADD are both recognized:
      42   [ +  -  +  - ]:        1080 :             if (strName.compare(0, 3, "OP_") == 0) { // strName starts with "OP_"
      43   [ +  -  +  - ]:        1080 :                 mapOpNames[strName.substr(3)] = static_cast<opcodetype>(op);
      44                 :             :             }
      45                 :        1080 :         }
      46                 :          12 :     }
      47                 :        3881 :     opcodetype Parse(const std::string& s) const
      48                 :             :     {
      49                 :        3881 :         auto it = mapOpNames.find(s);
      50   [ +  +  +  - ]:        3881 :         if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
      51                 :        3879 :         return it->second;
      52                 :             :     }
      53                 :             : };
      54                 :             : 
      55                 :        3881 : opcodetype ParseOpCode(const std::string& s)
      56                 :             : {
      57   [ +  +  +  -  :        3881 :     static const OpCodeParser ocp;
                   +  - ]
      58                 :        3881 :     return ocp.Parse(s);
      59                 :             : }
      60                 :             : 
      61                 :             : } // namespace
      62                 :             : 
      63                 :        2745 : CScript ParseScript(const std::string& s)
      64                 :             : {
      65                 :        2745 :     CScript result;
      66                 :             : 
      67         [ +  - ]:        2745 :     std::vector<std::string> words = SplitString(s, " \t\n");
      68                 :             : 
      69         [ +  + ]:       14877 :     for (const std::string& w : words) {
      70         [ +  + ]:       12141 :         if (w.empty()) {
      71                 :             :             // Empty string, ignore. (SplitString doesn't combine multiple separators)
      72   [ +  -  +  + ]:       11928 :         } else if (std::all_of(w.begin(), w.end(), ::IsDigit) ||
      73   [ +  +  +  -  :        7419 :                    (w.front() == '-' && w.size() > 1 && std::all_of(w.begin() + 1, w.end(), ::IsDigit)))
             +  -  +  - ]
      74                 :             :         {
      75                 :             :             // Number
      76                 :        4592 :             const auto num{ToIntegral<int64_t>(w)};
      77                 :             : 
      78                 :             :             // limit the range of numbers ParseScript accepts in decimal
      79                 :             :             // since numbers outside -0xFFFFFFFF...0xFFFFFFFF are illegal in scripts
      80   [ +  +  +  +  :        4592 :             if (!num.has_value() || num > int64_t{0xffffffff} || num < -1 * int64_t{0xffffffff}) {
                   +  + ]
      81                 :           7 :                 throw std::runtime_error("script parse error: decimal numeric value only allowed in the "
      82         [ +  - ]:           7 :                                          "range -0xFFFFFFFF...0xFFFFFFFF");
      83                 :             :             }
      84                 :             : 
      85         [ +  - ]:       12132 :             result << num.value();
      86   [ +  -  +  +  :       19090 :         } else if (w.substr(0, 2) == "0x" && w.size() > 2 && IsHex(std::string(w.begin() + 2, w.end()))) {
          +  -  +  -  +  
          -  +  -  +  +  
             -  -  -  - ]
      87                 :             :             // Raw hex data, inserted NOT pushed onto stack:
      88   [ +  -  +  - ]:        4418 :             std::vector<unsigned char> raw = ParseHex(std::string(w.begin() + 2, w.end()));
      89                 :        2209 :             result.insert(result.end(), raw.begin(), raw.end());
      90   [ +  -  +  +  :        7336 :         } else if (w.size() >= 2 && w.front() == '\'' && w.back() == '\'') {
                   -  + ]
      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         [ +  - ]:        1246 :             std::vector<unsigned char> value(w.begin() + 1, w.end() - 1);
      94                 :        1246 :             result << value;
      95                 :        1246 :         } else {
      96                 :             :             // opcode, e.g. OP_ADD or ADD:
      97   [ +  +  +  - ]:        3881 :             result << ParseOpCode(w);
      98                 :             :         }
      99                 :             :     }
     100                 :             : 
     101                 :        2736 :     return result;
     102                 :        2745 : }
     103                 :             : 
     104                 :             : // Check that all of the input and output scripts of a transaction contains valid opcodes
     105                 :          23 : static bool CheckTxScriptsSanity(const CMutableTransaction& tx)
     106                 :             : {
     107                 :             :     // Check input scripts for non-coinbase txs
     108         [ +  - ]:          46 :     if (!CTransaction(tx).IsCoinBase()) {
     109         [ +  + ]:         239 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     110   [ +  -  +  +  :         216 :             if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
                   +  - ]
     111                 :             :                 return false;
     112                 :             :             }
     113                 :             :         }
     114                 :             :     }
     115                 :             :     // Check output scripts
     116         [ +  + ]:          51 :     for (unsigned int i = 0; i < tx.vout.size(); i++) {
     117   [ +  +  -  +  :          29 :         if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
                   -  - ]
     118                 :             :             return false;
     119                 :             :         }
     120                 :             :     }
     121                 :             : 
     122                 :             :     return true;
     123                 :             : }
     124                 :             : 
     125                 :          25 : static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>& tx_data, bool try_no_witness, bool try_witness)
     126                 :             : {
     127                 :             :     // General strategy:
     128                 :             :     // - Decode both with extended serialization (which interprets the 0x0001 tag as a marker for
     129                 :             :     //   the presence of witnesses) and with legacy serialization (which interprets the tag as a
     130                 :             :     //   0-input 1-output incomplete transaction).
     131                 :             :     //   - Restricted by try_no_witness (which disables legacy if false) and try_witness (which
     132                 :             :     //     disables extended if false).
     133                 :             :     //   - Ignore serializations that do not fully consume the hex string.
     134                 :             :     // - If neither succeeds, fail.
     135                 :             :     // - If only one succeeds, return that one.
     136                 :             :     // - If both decode attempts succeed:
     137                 :             :     //   - If only one passes the CheckTxScriptsSanity check, return that one.
     138                 :             :     //   - If neither or both pass CheckTxScriptsSanity, return the extended one.
     139                 :             : 
     140         [ +  - ]:          25 :     CMutableTransaction tx_extended, tx_legacy;
     141                 :          25 :     bool ok_extended = false, ok_legacy = false;
     142                 :             : 
     143                 :             :     // Try decoding with extended serialization support, and remember if the result successfully
     144                 :             :     // consumes the entire input.
     145         [ +  + ]:          25 :     if (try_witness) {
     146         [ +  - ]:          21 :         DataStream ssData(tx_data);
     147                 :          21 :         try {
     148         [ +  + ]:          21 :             ssData >> TX_WITH_WITNESS(tx_extended);
     149         [ +  - ]:          17 :             if (ssData.empty()) ok_extended = true;
     150         [ -  + ]:           4 :         } catch (const std::exception&) {
     151                 :             :             // Fall through.
     152                 :           4 :         }
     153                 :           0 :     }
     154                 :             : 
     155                 :             :     // Optimization: if extended decoding succeeded and the result passes CheckTxScriptsSanity,
     156                 :             :     // don't bother decoding the other way.
     157   [ +  +  +  -  :          21 :     if (ok_extended && CheckTxScriptsSanity(tx_extended)) {
                   -  + ]
     158                 :          17 :         tx = std::move(tx_extended);
     159                 :          17 :         return true;
     160                 :             :     }
     161                 :             : 
     162                 :             :     // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
     163         [ +  + ]:           8 :     if (try_no_witness) {
     164         [ +  - ]:           7 :         DataStream ssData(tx_data);
     165                 :           7 :         try {
     166         [ +  + ]:           7 :             ssData >> TX_NO_WITNESS(tx_legacy);
     167         [ +  - ]:           6 :             if (ssData.empty()) ok_legacy = true;
     168         [ -  + ]:           1 :         } catch (const std::exception&) {
     169                 :             :             // Fall through.
     170                 :           1 :         }
     171                 :           0 :     }
     172                 :             : 
     173                 :             :     // If legacy decoding succeeded and passes CheckTxScriptsSanity, that's our answer, as we know
     174                 :             :     // at this point that extended decoding either failed or doesn't pass the sanity check.
     175   [ +  +  +  -  :           7 :     if (ok_legacy && CheckTxScriptsSanity(tx_legacy)) {
                   +  + ]
     176                 :           5 :         tx = std::move(tx_legacy);
     177                 :           5 :         return true;
     178                 :             :     }
     179                 :             : 
     180                 :             :     // If extended decoding succeeded, and neither decoding passes sanity, return the extended one.
     181         [ -  + ]:           3 :     if (ok_extended) {
     182                 :           0 :         tx = std::move(tx_extended);
     183                 :           0 :         return true;
     184                 :             :     }
     185                 :             : 
     186                 :             :     // If legacy decoding succeeded and extended didn't, return the legacy one.
     187         [ +  + ]:           3 :     if (ok_legacy) {
     188                 :           1 :         tx = std::move(tx_legacy);
     189                 :           1 :         return true;
     190                 :             :     }
     191                 :             : 
     192                 :             :     // If none succeeded, we failed.
     193                 :             :     return false;
     194                 :          50 : }
     195                 :             : 
     196                 :          27 : bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness, bool try_witness)
     197                 :             : {
     198         [ +  + ]:          27 :     if (!IsHex(hex_tx)) {
     199                 :             :         return false;
     200                 :             :     }
     201                 :             : 
     202                 :          25 :     std::vector<unsigned char> txData(ParseHex(hex_tx));
     203         [ +  - ]:          25 :     return DecodeTx(tx, txData, try_no_witness, try_witness);
     204                 :          25 : }
     205                 :             : 
     206                 :           1 : bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
     207                 :             : {
     208         [ +  - ]:           1 :     if (!IsHex(hex_header)) return false;
     209                 :             : 
     210                 :           1 :     const std::vector<unsigned char> header_data{ParseHex(hex_header)};
     211         [ +  - ]:           1 :     DataStream ser_header{header_data};
     212                 :           1 :     try {
     213         [ -  + ]:           2 :         ser_header >> header;
     214         [ -  + ]:           1 :     } catch (const std::exception&) {
     215                 :           1 :         return false;
     216                 :           1 :     }
     217                 :             :     return true;
     218                 :           1 : }
     219                 :             : 
     220                 :          10 : bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
     221                 :             : {
     222         [ +  - ]:          10 :     if (!IsHex(strHexBlk))
     223                 :             :         return false;
     224                 :             : 
     225                 :          10 :     std::vector<unsigned char> blockData(ParseHex(strHexBlk));
     226         [ +  - ]:          10 :     DataStream ssBlock(blockData);
     227                 :          10 :     try {
     228         [ +  - ]:          20 :         ssBlock >> TX_WITH_WITNESS(block);
     229                 :             :     }
     230         [ -  - ]:           0 :     catch (const std::exception&) {
     231                 :           0 :         return false;
     232                 :           0 :     }
     233                 :             : 
     234                 :             :     return true;
     235                 :          10 : }
     236                 :             : 
     237                 :           0 : util::Result<int> SighashFromStr(const std::string& sighash)
     238                 :             : {
     239                 :           0 :     static const std::map<std::string, int> map_sighash_values = {
     240         [ #  # ]:           0 :         {std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
     241                 :           0 :         {std::string("ALL"), int(SIGHASH_ALL)},
     242                 :           0 :         {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
     243                 :           0 :         {std::string("NONE"), int(SIGHASH_NONE)},
     244                 :           0 :         {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
     245                 :           0 :         {std::string("SINGLE"), int(SIGHASH_SINGLE)},
     246                 :           0 :         {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
     247   [ #  #  #  #  :           0 :     };
             #  #  #  # ]
     248                 :           0 :     const auto& it = map_sighash_values.find(sighash);
     249         [ #  # ]:           0 :     if (it != map_sighash_values.end()) {
     250                 :           0 :         return it->second;
     251                 :             :     } else {
     252   [ #  #  #  # ]:           0 :         return util::Error{Untranslated("'" + sighash + "' is not a valid sighash parameter.")};
     253                 :             :     }
     254   [ #  #  #  #  :           0 : }
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
        

Generated by: LCOV version 2.0-1