LCOV - code coverage report
Current view: top level - src/script - script.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 178 201 88.6 %
Date: 2025-02-23 09:33:43 Functions: 15 15 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 The Bitcoin developers
       3             : // Copyright (c) 2017-2021 The PIVX Core developers
       4             : // Distributed under the MIT software license, see the accompanying
       5             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6             : 
       7             : #include "script.h"
       8             : #include "tinyformat.h"
       9             : #include "utilstrencodings.h"
      10             : 
      11             : #include <atomic>
      12             : 
      13             : 
      14        7205 : const char* GetOpName(opcodetype opcode)
      15             : {
      16        7205 :     switch (opcode)
      17             :     {
      18             :     // push value
      19             :     case OP_0                      : return "0";
      20           0 :     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
      21           0 :     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
      22           0 :     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
      23           0 :     case OP_1NEGATE                : return "-1";
      24           6 :     case OP_RESERVED               : return "OP_RESERVED";
      25           1 :     case OP_1                      : return "1";
      26           1 :     case OP_2                      : return "2";
      27           1 :     case OP_3                      : return "3";
      28           0 :     case OP_4                      : return "4";
      29           0 :     case OP_5                      : return "5";
      30           0 :     case OP_6                      : return "6";
      31           0 :     case OP_7                      : return "7";
      32           0 :     case OP_8                      : return "8";
      33           0 :     case OP_9                      : return "9";
      34           0 :     case OP_10                     : return "10";
      35           0 :     case OP_11                     : return "11";
      36           0 :     case OP_12                     : return "12";
      37           0 :     case OP_13                     : return "13";
      38           0 :     case OP_14                     : return "14";
      39           0 :     case OP_15                     : return "15";
      40           0 :     case OP_16                     : return "16";
      41             : 
      42             :     // control
      43           6 :     case OP_NOP                    : return "OP_NOP";
      44           6 :     case OP_VER                    : return "OP_VER";
      45          59 :     case OP_IF                     : return "OP_IF";
      46           6 :     case OP_NOTIF                  : return "OP_NOTIF";
      47           6 :     case OP_VERIF                  : return "OP_VERIF";
      48           6 :     case OP_VERNOTIF               : return "OP_VERNOTIF";
      49          59 :     case OP_ELSE                   : return "OP_ELSE";
      50          59 :     case OP_ENDIF                  : return "OP_ENDIF";
      51           6 :     case OP_VERIFY                 : return "OP_VERIFY";
      52          11 :     case OP_RETURN                 : return "OP_RETURN";
      53             : 
      54             :     // stack ops
      55           6 :     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
      56           6 :     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
      57           6 :     case OP_2DROP                  : return "OP_2DROP";
      58           6 :     case OP_2DUP                   : return "OP_2DUP";
      59           6 :     case OP_3DUP                   : return "OP_3DUP";
      60           6 :     case OP_2OVER                  : return "OP_2OVER";
      61           6 :     case OP_2ROT                   : return "OP_2ROT";
      62           6 :     case OP_2SWAP                  : return "OP_2SWAP";
      63           6 :     case OP_IFDUP                  : return "OP_IFDUP";
      64           6 :     case OP_DEPTH                  : return "OP_DEPTH";
      65           8 :     case OP_DROP                   : return "OP_DROP";
      66        1552 :     case OP_DUP                    : return "OP_DUP";
      67           6 :     case OP_NIP                    : return "OP_NIP";
      68           6 :     case OP_OVER                   : return "OP_OVER";
      69           6 :     case OP_PICK                   : return "OP_PICK";
      70           6 :     case OP_ROLL                   : return "OP_ROLL";
      71          58 :     case OP_ROT                    : return "OP_ROT";
      72           6 :     case OP_SWAP                   : return "OP_SWAP";
      73           6 :     case OP_TUCK                   : return "OP_TUCK";
      74             : 
      75             :     // splice ops
      76           6 :     case OP_CAT                    : return "OP_CAT";
      77           6 :     case OP_SUBSTR                 : return "OP_SUBSTR";
      78           6 :     case OP_LEFT                   : return "OP_LEFT";
      79           6 :     case OP_RIGHT                  : return "OP_RIGHT";
      80           6 :     case OP_SIZE                   : return "OP_SIZE";
      81             : 
      82             :     // bit logic
      83           6 :     case OP_INVERT                 : return "OP_INVERT";
      84           6 :     case OP_AND                    : return "OP_AND";
      85           6 :     case OP_OR                     : return "OP_OR";
      86           6 :     case OP_XOR                    : return "OP_XOR";
      87          34 :     case OP_EQUAL                  : return "OP_EQUAL";
      88        1550 :     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
      89           6 :     case OP_RESERVED1              : return "OP_RESERVED1";
      90           6 :     case OP_RESERVED2              : return "OP_RESERVED2";
      91             : 
      92             :     // numeric
      93           6 :     case OP_1ADD                   : return "OP_1ADD";
      94           6 :     case OP_1SUB                   : return "OP_1SUB";
      95           6 :     case OP_2MUL                   : return "OP_2MUL";
      96           6 :     case OP_2DIV                   : return "OP_2DIV";
      97           6 :     case OP_NEGATE                 : return "OP_NEGATE";
      98           6 :     case OP_ABS                    : return "OP_ABS";
      99          30 :     case OP_NOT                    : return "OP_NOT";
     100           6 :     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
     101           6 :     case OP_ADD                    : return "OP_ADD";
     102           6 :     case OP_SUB                    : return "OP_SUB";
     103           6 :     case OP_MUL                    : return "OP_MUL";
     104           6 :     case OP_DIV                    : return "OP_DIV";
     105           6 :     case OP_MOD                    : return "OP_MOD";
     106           6 :     case OP_LSHIFT                 : return "OP_LSHIFT";
     107           6 :     case OP_RSHIFT                 : return "OP_RSHIFT";
     108           6 :     case OP_BOOLAND                : return "OP_BOOLAND";
     109           6 :     case OP_BOOLOR                 : return "OP_BOOLOR";
     110           6 :     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
     111           6 :     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
     112           6 :     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
     113           6 :     case OP_LESSTHAN               : return "OP_LESSTHAN";
     114           6 :     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
     115           6 :     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
     116           6 :     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
     117           6 :     case OP_MIN                    : return "OP_MIN";
     118           6 :     case OP_MAX                    : return "OP_MAX";
     119           6 :     case OP_WITHIN                 : return "OP_WITHIN";
     120             : 
     121             :     // crypto
     122           6 :     case OP_RIPEMD160              : return "OP_RIPEMD160";
     123           6 :     case OP_SHA1                   : return "OP_SHA1";
     124           6 :     case OP_SHA256                 : return "OP_SHA256";
     125        1578 :     case OP_HASH160                : return "OP_HASH160";
     126           6 :     case OP_HASH256                : return "OP_HASH256";
     127           6 :     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
     128        1595 :     case OP_CHECKSIG               : return "OP_CHECKSIG";
     129           7 :     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
     130          31 :     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
     131           6 :     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
     132             : 
     133             :     // expansion
     134           6 :     case OP_NOP1                   : return "OP_NOP1";                  // OP_NOP1
     135          11 :     case OP_CHECKLOCKTIMEVERIFY    : return "OP_CHECKLOCKTIMEVERIFY";   // OP_NOP2
     136           6 :     case OP_NOP3                   : return "OP_NOP3";                  // OP_NOP3
     137           6 :     case OP_NOP4                   : return "OP_NOP4";                  // OP_NOP4
     138           6 :     case OP_NOP5                   : return "OP_NOP5";                  // OP_NOP5
     139           6 :     case OP_NOP6                   : return "OP_NOP6";                  // OP_NOP6
     140           6 :     case OP_NOP7                   : return "OP_NOP7";                  // OP_NOP7
     141          10 :     case OP_NOP8                   : return "OP_NOP8";                  // OP_NOP8
     142           6 :     case OP_NOP9                   : return "OP_NOP9";                  // OP_NOP9
     143           6 :     case OP_NOP10                  : return "OP_NOP10";                 // OP_NOP10
     144             : 
     145             :     // zerocoin
     146           6 :     case OP_ZEROCOINMINT           : return "OP_ZEROCOINMINT";
     147           6 :     case OP_ZEROCOINSPEND          : return "OP_ZEROCOINSPEND";
     148           0 :     case OP_ZEROCOINPUBLICSPEND    : return "OP_ZEROCOINPUBLICSPEND";
     149             : 
     150             :     // cold staking
     151          52 :     case OP_CHECKCOLDSTAKEVERIFY_LOF   : return "OP_CHECKCOLDSTAKEVERIFY_LOF";
     152           0 :     case OP_CHECKCOLDSTAKEVERIFY       : return "OP_CHECKCOLDSTAKEVERIFY";
     153             : 
     154             :     // exchange address
     155           0 :     case OP_EXCHANGEADDR           : return "OP_EXCHANGEADDR";
     156             : 
     157           0 :     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
     158             : 
     159          42 :     default:
     160          42 :         return "OP_UNKNOWN";
     161             :     }
     162             : }
     163             : 
     164     6190705 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
     165             : {
     166     6190705 :     unsigned int n = 0;
     167     6190705 :     const_iterator pc = begin();
     168     6190705 :     opcodetype lastOpcode = OP_INVALIDOPCODE;
     169    58749932 :     while (pc < end())
     170             :     {
     171    23184291 :         opcodetype opcode;
     172    23184291 :         if (!GetOp(pc, opcode))
     173             :             break;
     174    23184191 :         if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
     175     4153115 :             n++;
     176    19031146 :         else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
     177             :         {
     178       17058 :             if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
     179          54 :                 n += DecodeOP_N(lastOpcode);
     180             :             else
     181       17004 :                 n += MAX_PUBKEYS_PER_MULTISIG;
     182             :         }
     183    23184191 :         lastOpcode = opcode;
     184             :     }
     185     6190705 :     return n;
     186             : }
     187             : 
     188       62403 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
     189             : {
     190       62403 :     if (!IsPayToScriptHash())
     191           0 :         return GetSigOpCount(true);
     192             : 
     193             :     // This is a pay-to-script-hash scriptPubKey;
     194             :     // get the last item that the scriptSig
     195             :     // pushes onto the stack:
     196       62403 :     const_iterator pc = scriptSig.begin();
     197      124806 :     std::vector<unsigned char> data;
     198      374570 :     while (pc < scriptSig.end())
     199             :     {
     200      124882 :         opcodetype opcode;
     201      124882 :         if (!scriptSig.GetOp(pc, opcode, data))
     202           0 :             return 0;
     203      124882 :         if (opcode > OP_16)
     204             :             return 0;
     205             :     }
     206             : 
     207             :     /// ... and return its opcount:
     208      124806 :     CScript subscript(data.begin(), data.end());
     209       62403 :     return subscript.GetSigOpCount(true);
     210             : }
     211             : 
     212        3853 : bool CScript::IsPayToPublicKeyHash() const
     213             : {
     214             :     // Extra-fast test for pay-to-pubkey-hash CScripts:
     215        3853 :     return (this->size() == 25 &&
     216        7300 :             (*this)[0] == OP_DUP &&
     217        7300 :             (*this)[1] == OP_HASH160 &&
     218        7300 :             (*this)[2] == 0x14 &&
     219       11153 :             (*this)[23] == OP_EQUALVERIFY &&
     220        7300 :             (*this)[24] == OP_CHECKSIG);
     221             : }
     222             : 
     223    26452425 : bool CScript::IsPayToScriptHash() const
     224             : {
     225             :     // Extra-fast test for pay-to-script-hash CScripts:
     226    26452425 :     return (this->size() == 23 &&
     227      676536 :             (*this)[0] == OP_HASH160 &&
     228    27128985 :             (*this)[1] == 0x14 &&
     229      676520 :             (*this)[22] == OP_EQUAL);
     230             : }
     231             : 
     232             : // P2CS script: either with or without last output free
     233     2647401 : bool CScript::IsPayToColdStaking() const
     234             : {
     235     2647401 :     return (this->size() == 51 &&
     236        7678 :             (*this)[0] == OP_DUP &&
     237        7674 :             (*this)[1] == OP_HASH160 &&
     238        7674 :             (*this)[2] == OP_ROT &&
     239        7674 :             (*this)[3] == OP_IF &&
     240        7674 :             ((*this)[4] == OP_CHECKCOLDSTAKEVERIFY || (*this)[4] == OP_CHECKCOLDSTAKEVERIFY_LOF) &&
     241        7674 :             (*this)[5] == 0x14 &&
     242        7674 :             (*this)[26] == OP_ELSE &&
     243        7674 :             (*this)[27] == 0x14 &&
     244        7674 :             (*this)[48] == OP_ENDIF &&
     245     2655077 :             (*this)[49] == OP_EQUALVERIFY &&
     246        7674 :             (*this)[50] == OP_CHECKSIG);
     247             : }
     248             : 
     249       10496 : bool CScript::IsPayToColdStakingLOF() const
     250             : {
     251       10505 :     return IsPayToColdStaking() && (*this)[4] == OP_CHECKCOLDSTAKEVERIFY_LOF;
     252             : }
     253             : 
     254     2424338 : bool CScript::IsPayToExchangeAddress() const
     255             : {
     256     2424338 :     return (this->size() == 26 &&
     257        1014 :             (*this)[0] == OP_EXCHANGEADDR &&
     258        1014 :             (*this)[1] == OP_DUP &&
     259        1014 :             (*this)[2] == OP_HASH160 &&
     260        1014 :             (*this)[3] == 0x14 &&
     261     2425346 :             (*this)[24] == OP_EQUALVERIFY &&
     262        1014 :             (*this)[25] == OP_CHECKSIG);
     263             : }
     264             : 
     265    36872696 : bool CScript::StartsWithOpcode(const opcodetype opcode) const
     266             : {
     267    85651317 :     return (!this->empty() && (*this)[0] == opcode);
     268             : }
     269             : 
     270    10244714 : bool CScript::IsZerocoinMint() const
     271             : {
     272    10244714 :     return StartsWithOpcode(OP_ZEROCOINMINT);
     273             : }
     274             : 
     275     2269555 : bool CScript::IsZerocoinSpend() const
     276             : {
     277     2269555 :     return StartsWithOpcode(OP_ZEROCOINSPEND);
     278             : }
     279             : 
     280    24358407 : bool CScript::IsZerocoinPublicSpend() const
     281             : {
     282    24358407 :     return StartsWithOpcode(OP_ZEROCOINPUBLICSPEND);
     283             : }
     284             : 
     285      491785 : bool CScript::IsPushOnly(const_iterator pc) const
     286             : {
     287     2789600 :     while (pc < end())
     288             :     {
     289      903027 :         opcodetype opcode;
     290      903027 :         if (!GetOp(pc, opcode))
     291          11 :             return false;
     292             :         // Note that IsPushOnly() *does* consider OP_RESERVED to be a
     293             :         // push-type opcode, however execution of OP_RESERVED fails, so
     294             :         // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
     295             :         // the P2SH special validation code being executed.
     296      903026 :         if (opcode > OP_16)
     297             :             return false;
     298             :     }
     299             :     return true;
     300             : }
     301             : 
     302      477509 : bool CScript::IsPushOnly() const
     303             : {
     304      955018 :     return this->IsPushOnly(begin());
     305             : }
     306             : 
     307    23782620 : size_t CScript::DynamicMemoryUsage() const
     308             : {
     309    23782620 :     return memusage::DynamicUsage(*static_cast<const CScriptBase*>(this));
     310             : }

Generated by: LCOV version 1.14