LCOV - code coverage report
Current view: top level - src/script - script.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 194 209 92.8 %
Date: 2022-04-21 14:51:19 Functions: 161 166 97.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: 82 104 78.8 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2021 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #ifndef BITCOIN_SCRIPT_SCRIPT_H
#       7                 :            : #define BITCOIN_SCRIPT_SCRIPT_H
#       8                 :            : 
#       9                 :            : #include <attributes.h>
#      10                 :            : #include <crypto/common.h>
#      11                 :            : #include <prevector.h>
#      12                 :            : #include <serialize.h>
#      13                 :            : 
#      14                 :            : #include <assert.h>
#      15                 :            : #include <climits>
#      16                 :            : #include <limits>
#      17                 :            : #include <stdexcept>
#      18                 :            : #include <stdint.h>
#      19                 :            : #include <string.h>
#      20                 :            : #include <string>
#      21                 :            : #include <vector>
#      22                 :            : 
#      23                 :            : // Maximum number of bytes pushable to the stack
#      24                 :            : static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
#      25                 :            : 
#      26                 :            : // Maximum number of non-push operations per script
#      27                 :            : static const int MAX_OPS_PER_SCRIPT = 201;
#      28                 :            : 
#      29                 :            : // Maximum number of public keys per multisig
#      30                 :            : static const int MAX_PUBKEYS_PER_MULTISIG = 20;
#      31                 :            : 
#      32                 :            : /** The limit of keys in OP_CHECKSIGADD-based scripts. It is due to the stack limit in BIP342. */
#      33                 :            : static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A = 999;
#      34                 :            : 
#      35                 :            : // Maximum script length in bytes
#      36                 :            : static const int MAX_SCRIPT_SIZE = 10000;
#      37                 :            : 
#      38                 :            : // Maximum number of values on script interpreter stack
#      39                 :            : static const int MAX_STACK_SIZE = 1000;
#      40                 :            : 
#      41                 :            : // Threshold for nLockTime: below this value it is interpreted as block number,
#      42                 :            : // otherwise as UNIX timestamp.
#      43                 :            : static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov  5 00:53:20 1985 UTC
#      44                 :            : 
#      45                 :            : // Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a
#      46                 :            : // transaction with this lock time will never be valid unless lock time
#      47                 :            : // checking is disabled (by setting all input sequence numbers to
#      48                 :            : // SEQUENCE_FINAL).
#      49                 :            : static const uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;
#      50                 :            : 
#      51                 :            : // Tag for input annex. If there are at least two witness elements for a transaction input,
#      52                 :            : // and the first byte of the last element is 0x50, this last element is called annex, and
#      53                 :            : // has meanings independent of the script
#      54                 :            : static constexpr unsigned int ANNEX_TAG = 0x50;
#      55                 :            : 
#      56                 :            : // Validation weight per passing signature (Tapscript only, see BIP 342).
#      57                 :            : static constexpr int64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED{50};
#      58                 :            : 
#      59                 :            : // How much weight budget is added to the witness size (Tapscript only, see BIP 342).
#      60                 :            : static constexpr int64_t VALIDATION_WEIGHT_OFFSET{50};
#      61                 :            : 
#      62                 :            : template <typename T>
#      63                 :            : std::vector<unsigned char> ToByteVector(const T& in)
#      64                 :    3968352 : {
#      65                 :    3968352 :     return std::vector<unsigned char>(in.begin(), in.end());
#      66                 :    3968352 : }
#      67                 :            : 
#      68                 :            : /** Script opcodes */
#      69                 :            : enum opcodetype
#      70                 :            : {
#      71                 :            :     // push value
#      72                 :            :     OP_0 = 0x00,
#      73                 :            :     OP_FALSE = OP_0,
#      74                 :            :     OP_PUSHDATA1 = 0x4c,
#      75                 :            :     OP_PUSHDATA2 = 0x4d,
#      76                 :            :     OP_PUSHDATA4 = 0x4e,
#      77                 :            :     OP_1NEGATE = 0x4f,
#      78                 :            :     OP_RESERVED = 0x50,
#      79                 :            :     OP_1 = 0x51,
#      80                 :            :     OP_TRUE=OP_1,
#      81                 :            :     OP_2 = 0x52,
#      82                 :            :     OP_3 = 0x53,
#      83                 :            :     OP_4 = 0x54,
#      84                 :            :     OP_5 = 0x55,
#      85                 :            :     OP_6 = 0x56,
#      86                 :            :     OP_7 = 0x57,
#      87                 :            :     OP_8 = 0x58,
#      88                 :            :     OP_9 = 0x59,
#      89                 :            :     OP_10 = 0x5a,
#      90                 :            :     OP_11 = 0x5b,
#      91                 :            :     OP_12 = 0x5c,
#      92                 :            :     OP_13 = 0x5d,
#      93                 :            :     OP_14 = 0x5e,
#      94                 :            :     OP_15 = 0x5f,
#      95                 :            :     OP_16 = 0x60,
#      96                 :            : 
#      97                 :            :     // control
#      98                 :            :     OP_NOP = 0x61,
#      99                 :            :     OP_VER = 0x62,
#     100                 :            :     OP_IF = 0x63,
#     101                 :            :     OP_NOTIF = 0x64,
#     102                 :            :     OP_VERIF = 0x65,
#     103                 :            :     OP_VERNOTIF = 0x66,
#     104                 :            :     OP_ELSE = 0x67,
#     105                 :            :     OP_ENDIF = 0x68,
#     106                 :            :     OP_VERIFY = 0x69,
#     107                 :            :     OP_RETURN = 0x6a,
#     108                 :            : 
#     109                 :            :     // stack ops
#     110                 :            :     OP_TOALTSTACK = 0x6b,
#     111                 :            :     OP_FROMALTSTACK = 0x6c,
#     112                 :            :     OP_2DROP = 0x6d,
#     113                 :            :     OP_2DUP = 0x6e,
#     114                 :            :     OP_3DUP = 0x6f,
#     115                 :            :     OP_2OVER = 0x70,
#     116                 :            :     OP_2ROT = 0x71,
#     117                 :            :     OP_2SWAP = 0x72,
#     118                 :            :     OP_IFDUP = 0x73,
#     119                 :            :     OP_DEPTH = 0x74,
#     120                 :            :     OP_DROP = 0x75,
#     121                 :            :     OP_DUP = 0x76,
#     122                 :            :     OP_NIP = 0x77,
#     123                 :            :     OP_OVER = 0x78,
#     124                 :            :     OP_PICK = 0x79,
#     125                 :            :     OP_ROLL = 0x7a,
#     126                 :            :     OP_ROT = 0x7b,
#     127                 :            :     OP_SWAP = 0x7c,
#     128                 :            :     OP_TUCK = 0x7d,
#     129                 :            : 
#     130                 :            :     // splice ops
#     131                 :            :     OP_CAT = 0x7e,
#     132                 :            :     OP_SUBSTR = 0x7f,
#     133                 :            :     OP_LEFT = 0x80,
#     134                 :            :     OP_RIGHT = 0x81,
#     135                 :            :     OP_SIZE = 0x82,
#     136                 :            : 
#     137                 :            :     // bit logic
#     138                 :            :     OP_INVERT = 0x83,
#     139                 :            :     OP_AND = 0x84,
#     140                 :            :     OP_OR = 0x85,
#     141                 :            :     OP_XOR = 0x86,
#     142                 :            :     OP_EQUAL = 0x87,
#     143                 :            :     OP_EQUALVERIFY = 0x88,
#     144                 :            :     OP_RESERVED1 = 0x89,
#     145                 :            :     OP_RESERVED2 = 0x8a,
#     146                 :            : 
#     147                 :            :     // numeric
#     148                 :            :     OP_1ADD = 0x8b,
#     149                 :            :     OP_1SUB = 0x8c,
#     150                 :            :     OP_2MUL = 0x8d,
#     151                 :            :     OP_2DIV = 0x8e,
#     152                 :            :     OP_NEGATE = 0x8f,
#     153                 :            :     OP_ABS = 0x90,
#     154                 :            :     OP_NOT = 0x91,
#     155                 :            :     OP_0NOTEQUAL = 0x92,
#     156                 :            : 
#     157                 :            :     OP_ADD = 0x93,
#     158                 :            :     OP_SUB = 0x94,
#     159                 :            :     OP_MUL = 0x95,
#     160                 :            :     OP_DIV = 0x96,
#     161                 :            :     OP_MOD = 0x97,
#     162                 :            :     OP_LSHIFT = 0x98,
#     163                 :            :     OP_RSHIFT = 0x99,
#     164                 :            : 
#     165                 :            :     OP_BOOLAND = 0x9a,
#     166                 :            :     OP_BOOLOR = 0x9b,
#     167                 :            :     OP_NUMEQUAL = 0x9c,
#     168                 :            :     OP_NUMEQUALVERIFY = 0x9d,
#     169                 :            :     OP_NUMNOTEQUAL = 0x9e,
#     170                 :            :     OP_LESSTHAN = 0x9f,
#     171                 :            :     OP_GREATERTHAN = 0xa0,
#     172                 :            :     OP_LESSTHANOREQUAL = 0xa1,
#     173                 :            :     OP_GREATERTHANOREQUAL = 0xa2,
#     174                 :            :     OP_MIN = 0xa3,
#     175                 :            :     OP_MAX = 0xa4,
#     176                 :            : 
#     177                 :            :     OP_WITHIN = 0xa5,
#     178                 :            : 
#     179                 :            :     // crypto
#     180                 :            :     OP_RIPEMD160 = 0xa6,
#     181                 :            :     OP_SHA1 = 0xa7,
#     182                 :            :     OP_SHA256 = 0xa8,
#     183                 :            :     OP_HASH160 = 0xa9,
#     184                 :            :     OP_HASH256 = 0xaa,
#     185                 :            :     OP_CODESEPARATOR = 0xab,
#     186                 :            :     OP_CHECKSIG = 0xac,
#     187                 :            :     OP_CHECKSIGVERIFY = 0xad,
#     188                 :            :     OP_CHECKMULTISIG = 0xae,
#     189                 :            :     OP_CHECKMULTISIGVERIFY = 0xaf,
#     190                 :            : 
#     191                 :            :     // expansion
#     192                 :            :     OP_NOP1 = 0xb0,
#     193                 :            :     OP_CHECKLOCKTIMEVERIFY = 0xb1,
#     194                 :            :     OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,
#     195                 :            :     OP_CHECKSEQUENCEVERIFY = 0xb2,
#     196                 :            :     OP_NOP3 = OP_CHECKSEQUENCEVERIFY,
#     197                 :            :     OP_NOP4 = 0xb3,
#     198                 :            :     OP_NOP5 = 0xb4,
#     199                 :            :     OP_NOP6 = 0xb5,
#     200                 :            :     OP_NOP7 = 0xb6,
#     201                 :            :     OP_NOP8 = 0xb7,
#     202                 :            :     OP_NOP9 = 0xb8,
#     203                 :            :     OP_NOP10 = 0xb9,
#     204                 :            : 
#     205                 :            :     // Opcode added by BIP 342 (Tapscript)
#     206                 :            :     OP_CHECKSIGADD = 0xba,
#     207                 :            : 
#     208                 :            :     OP_INVALIDOPCODE = 0xff,
#     209                 :            : };
#     210                 :            : 
#     211                 :            : // Maximum value that an opcode can be
#     212                 :            : static const unsigned int MAX_OPCODE = OP_NOP10;
#     213                 :            : 
#     214                 :            : std::string GetOpName(opcodetype opcode);
#     215                 :            : 
#     216                 :            : class scriptnum_error : public std::runtime_error
#     217                 :            : {
#     218                 :            : public:
#     219                 :       1409 :     explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
#     220                 :            : };
#     221                 :            : 
#     222                 :            : class CScriptNum
#     223                 :            : {
#     224                 :            : /**
#     225                 :            :  * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
#     226                 :            :  * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
#     227                 :            :  * but results may overflow (and are valid as long as they are not used in a subsequent
#     228                 :            :  * numeric operation). CScriptNum enforces those semantics by storing results as
#     229                 :            :  * an int64 and allowing out-of-range values to be returned as a vector of bytes but
#     230                 :            :  * throwing an exception if arithmetic is done or the result is interpreted as an integer.
#     231                 :            :  */
#     232                 :            : public:
#     233                 :            : 
#     234                 :            :     explicit CScriptNum(const int64_t& n)
#     235                 :     517998 :     {
#     236                 :     517998 :         m_value = n;
#     237                 :     517998 :     }
#     238                 :            : 
#     239                 :            :     static const size_t nDefaultMaxNumSize = 4;
#     240                 :            : 
#     241                 :            :     explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
#     242                 :            :                         const size_t nMaxNumSize = nDefaultMaxNumSize)
#     243                 :    3569092 :     {
#     244         [ +  + ]:    3569092 :         if (vch.size() > nMaxNumSize) {
#     245                 :        405 :             throw scriptnum_error("script number overflow");
#     246                 :        405 :         }
#     247 [ +  + ][ +  + ]:    3568687 :         if (fRequireMinimal && vch.size() > 0) {
#     248                 :            :             // Check that the number is encoded with the minimum possible
#     249                 :            :             // number of bytes.
#     250                 :            :             //
#     251                 :            :             // If the most-significant-byte - excluding the sign bit - is zero
#     252                 :            :             // then we're not minimal. Note how this test also rejects the
#     253                 :            :             // negative-zero encoding, 0x80.
#     254         [ +  + ]:      64791 :             if ((vch.back() & 0x7f) == 0) {
#     255                 :            :                 // One exception: if there's more than one byte and the most
#     256                 :            :                 // significant bit of the second-most-significant-byte is set
#     257                 :            :                 // it would conflict with the sign bit. An example of this case
#     258                 :            :                 // is +-255, which encode to 0xff00 and 0xff80 respectively.
#     259                 :            :                 // (big-endian).
#     260 [ +  + ][ +  + ]:       1982 :                 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
#     261                 :       1004 :                     throw scriptnum_error("non-minimally encoded script number");
#     262                 :       1004 :                 }
#     263                 :       1982 :             }
#     264                 :      64791 :         }
#     265                 :    3567683 :         m_value = set_vch(vch);
#     266                 :    3567683 :     }
#     267                 :            : 
#     268                 :      15787 :     inline bool operator==(const int64_t& rhs) const    { return m_value == rhs; }
#     269                 :      26101 :     inline bool operator!=(const int64_t& rhs) const    { return m_value != rhs; }
#     270                 :      12074 :     inline bool operator<=(const int64_t& rhs) const    { return m_value <= rhs; }
#     271                 :      59964 :     inline bool operator< (const int64_t& rhs) const    { return m_value <  rhs; }
#     272                 :      13105 :     inline bool operator>=(const int64_t& rhs) const    { return m_value >= rhs; }
#     273                 :      34663 :     inline bool operator> (const int64_t& rhs) const    { return m_value >  rhs; }
#     274                 :            : 
#     275                 :      10171 :     inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
#     276                 :       7544 :     inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
#     277                 :       6458 :     inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
#     278                 :       6844 :     inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
#     279                 :       5896 :     inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
#     280                 :       6142 :     inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
#     281                 :            : 
#     282                 :      74457 :     inline CScriptNum operator+(   const int64_t& rhs)    const { return CScriptNum(m_value + rhs);}
#     283                 :      11486 :     inline CScriptNum operator-(   const int64_t& rhs)    const { return CScriptNum(m_value - rhs);}
#     284                 :       4206 :     inline CScriptNum operator+(   const CScriptNum& rhs) const { return operator+(rhs.m_value);   }
#     285                 :       5870 :     inline CScriptNum operator-(   const CScriptNum& rhs) const { return operator-(rhs.m_value);   }
#     286                 :            : 
#     287                 :        400 :     inline CScriptNum& operator+=( const CScriptNum& rhs)       { return operator+=(rhs.m_value);  }
#     288                 :        192 :     inline CScriptNum& operator-=( const CScriptNum& rhs)       { return operator-=(rhs.m_value);  }
#     289                 :            : 
#     290                 :      24480 :     inline CScriptNum operator&(   const int64_t& rhs)    const { return CScriptNum(m_value & rhs);}
#     291                 :          0 :     inline CScriptNum operator&(   const CScriptNum& rhs) const { return operator&(rhs.m_value);   }
#     292                 :            : 
#     293                 :          0 :     inline CScriptNum& operator&=( const CScriptNum& rhs)       { return operator&=(rhs.m_value);  }
#     294                 :            : 
#     295                 :            :     inline CScriptNum operator-()                         const
#     296                 :       3016 :     {
#     297                 :       3016 :         assert(m_value != std::numeric_limits<int64_t>::min());
#     298                 :          0 :         return CScriptNum(-m_value);
#     299                 :       3016 :     }
#     300                 :            : 
#     301                 :            :     inline CScriptNum& operator=( const int64_t& rhs)
#     302                 :       7001 :     {
#     303                 :       7001 :         m_value = rhs;
#     304                 :       7001 :         return *this;
#     305                 :       7001 :     }
#     306                 :            : 
#     307                 :            :     inline CScriptNum& operator+=( const int64_t& rhs)
#     308                 :        400 :     {
#     309                 :        400 :         assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
#     310                 :        400 :                            (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
#     311                 :          0 :         m_value += rhs;
#     312                 :        400 :         return *this;
#     313                 :        400 :     }
#     314                 :            : 
#     315                 :            :     inline CScriptNum& operator-=( const int64_t& rhs)
#     316                 :        192 :     {
#     317                 :        192 :         assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
#     318                 :        192 :                            (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
#     319                 :          0 :         m_value -= rhs;
#     320                 :        192 :         return *this;
#     321                 :        192 :     }
#     322                 :            : 
#     323                 :            :     inline CScriptNum& operator&=( const int64_t& rhs)
#     324                 :          0 :     {
#     325                 :          0 :         m_value &= rhs;
#     326                 :          0 :         return *this;
#     327                 :          0 :     }
#     328                 :            : 
#     329                 :            :     int getint() const
#     330                 :    3487821 :     {
#     331         [ +  + ]:    3487821 :         if (m_value > std::numeric_limits<int>::max())
#     332                 :       2450 :             return std::numeric_limits<int>::max();
#     333         [ +  + ]:    3485371 :         else if (m_value < std::numeric_limits<int>::min())
#     334                 :       1748 :             return std::numeric_limits<int>::min();
#     335                 :    3483623 :         return m_value;
#     336                 :    3487821 :     }
#     337                 :            : 
#     338                 :        226 :     int64_t GetInt64() const { return m_value; }
#     339                 :            : 
#     340                 :            :     std::vector<unsigned char> getvch() const
#     341                 :     471112 :     {
#     342                 :     471112 :         return serialize(m_value);
#     343                 :     471112 :     }
#     344                 :            : 
#     345                 :            :     static std::vector<unsigned char> serialize(const int64_t& value)
#     346                 :     732878 :     {
#     347         [ +  + ]:     732878 :         if(value == 0)
#     348                 :      48309 :             return std::vector<unsigned char>();
#     349                 :            : 
#     350                 :     684569 :         std::vector<unsigned char> result;
#     351                 :     684569 :         const bool neg = value < 0;
#     352         [ +  + ]:     684569 :         uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 : static_cast<uint64_t>(value);
#     353                 :            : 
#     354         [ +  + ]:    1612503 :         while(absvalue)
#     355                 :     927934 :         {
#     356                 :     927934 :             result.push_back(absvalue & 0xff);
#     357                 :     927934 :             absvalue >>= 8;
#     358                 :     927934 :         }
#     359                 :            : 
#     360                 :            : //    - If the most significant byte is >= 0x80 and the value is positive, push a
#     361                 :            : //    new zero-byte to make the significant byte < 0x80 again.
#     362                 :            : 
#     363                 :            : //    - If the most significant byte is >= 0x80 and the value is negative, push a
#     364                 :            : //    new 0x80 byte that will be popped off when converting to an integral.
#     365                 :            : 
#     366                 :            : //    - If the most significant byte is < 0x80 and the value is negative, add
#     367                 :            : //    0x80 to it, since it will be subtracted and interpreted as a negative when
#     368                 :            : //    converting to an integral.
#     369                 :            : 
#     370         [ +  + ]:     684569 :         if (result.back() & 0x80)
#     371         [ +  + ]:      97950 :             result.push_back(neg ? 0x80 : 0);
#     372         [ +  + ]:     586619 :         else if (neg)
#     373                 :       8676 :             result.back() |= 0x80;
#     374                 :            : 
#     375                 :     684569 :         return result;
#     376                 :     732878 :     }
#     377                 :            : 
#     378                 :            : private:
#     379                 :            :     static int64_t set_vch(const std::vector<unsigned char>& vch)
#     380                 :    3567683 :     {
#     381         [ +  + ]:    3567683 :       if (vch.empty())
#     382                 :      88837 :           return 0;
#     383                 :            : 
#     384                 :    3478846 :       int64_t result = 0;
#     385         [ +  + ]:    6991094 :       for (size_t i = 0; i != vch.size(); ++i)
#     386                 :    3512248 :           result |= static_cast<int64_t>(vch[i]) << 8*i;
#     387                 :            : 
#     388                 :            :       // If the input vector's most significant byte is 0x80, remove it from
#     389                 :            :       // the result's msb and return a negative.
#     390         [ +  + ]:    3478846 :       if (vch.back() & 0x80)
#     391                 :       3341 :           return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
#     392                 :            : 
#     393                 :    3475505 :       return result;
#     394                 :    3478846 :     }
#     395                 :            : 
#     396                 :            :     int64_t m_value;
#     397                 :            : };
#     398                 :            : 
#     399                 :            : /**
#     400                 :            :  * We use a prevector for the script to reduce the considerable memory overhead
#     401                 :            :  *  of vectors in cases where they normally contain a small number of small elements.
#     402                 :            :  * Tests in October 2015 showed use of this reduced dbcache memory usage by 23%
#     403                 :            :  *  and made an initial sync 13% faster.
#     404                 :            :  */
#     405                 :            : typedef prevector<28, unsigned char> CScriptBase;
#     406                 :            : 
#     407                 :            : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);
#     408                 :            : 
#     409                 :            : /** Serialized script, used inside transaction inputs and outputs */
#     410                 :            : class CScript : public CScriptBase
#     411                 :            : {
#     412                 :            : protected:
#     413                 :            :     CScript& push_int64(int64_t n)
#     414                 :     332303 :     {
#     415 [ +  + ][ +  + ]:     332303 :         if (n == -1 || (n >= 1 && n <= 16))
#                 [ +  + ]
#     416                 :      64780 :         {
#     417                 :      64780 :             push_back(n + (OP_1 - 1));
#     418                 :      64780 :         }
#     419         [ +  + ]:     267523 :         else if (n == 0)
#     420                 :       5751 :         {
#     421                 :       5751 :             push_back(OP_0);
#     422                 :       5751 :         }
#     423                 :     261772 :         else
#     424                 :     261772 :         {
#     425                 :     261772 :             *this << CScriptNum::serialize(n);
#     426                 :     261772 :         }
#     427                 :     332303 :         return *this;
#     428                 :     332303 :     }
#     429                 :            : public:
#     430                 :  128569812 :     CScript() { }
#     431                 :    1444324 :     CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
#     432                 :     203098 :     CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
#     433                 :       1160 :     CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
#     434                 :            : 
#     435                 :   52681780 :     SERIALIZE_METHODS(CScript, obj) { READWRITEAS(CScriptBase, obj); }
#     436                 :            : 
#     437                 :          0 :     explicit CScript(int64_t b) { operator<<(b); }
#     438                 :     100063 :     explicit CScript(opcodetype b)     { operator<<(b); }
#     439                 :          0 :     explicit CScript(const CScriptNum& b) { operator<<(b); }
#     440                 :            :     // delete non-existent constructor to defend against future introduction
#     441                 :            :     // e.g. via prevector
#     442                 :            :     explicit CScript(const std::vector<unsigned char>& b) = delete;
#     443                 :            : 
#     444                 :            :     /** Delete non-existent operator to defend against future introduction */
#     445                 :            :     CScript& operator<<(const CScript& b) = delete;
#     446                 :            : 
#     447                 :     332303 :     CScript& operator<<(int64_t b) LIFETIMEBOUND { return push_int64(b); }
#     448                 :            : 
#     449                 :            :     CScript& operator<<(opcodetype opcode) LIFETIMEBOUND
#     450                 :   13930550 :     {
#     451 [ +  + ][ +  + ]:   13930552 :         if (opcode < 0 || opcode > 0xff)
#     452                 :          0 :             throw std::runtime_error("CScript::operator<<(): invalid opcode");
#     453                 :   13930550 :         insert(end(), (unsigned char)opcode);
#     454                 :   13930550 :         return *this;
#     455                 :   13930550 :     }
#     456                 :            : 
#     457                 :            :     CScript& operator<<(const CScriptNum& b) LIFETIMEBOUND
#     458                 :      15170 :     {
#     459                 :      15170 :         *this << b.getvch();
#     460                 :      15170 :         return *this;
#     461                 :      15170 :     }
#     462                 :            : 
#     463                 :            :     CScript& operator<<(const std::vector<unsigned char>& b) LIFETIMEBOUND
#     464                 :    6431993 :     {
#     465         [ +  + ]:    6431993 :         if (b.size() < OP_PUSHDATA1)
#     466                 :    6430583 :         {
#     467                 :    6430583 :             insert(end(), (unsigned char)b.size());
#     468                 :    6430583 :         }
#     469         [ +  + ]:       1410 :         else if (b.size() <= 0xff)
#     470                 :        652 :         {
#     471                 :        652 :             insert(end(), OP_PUSHDATA1);
#     472                 :        652 :             insert(end(), (unsigned char)b.size());
#     473                 :        652 :         }
#     474         [ +  + ]:        758 :         else if (b.size() <= 0xffff)
#     475                 :        706 :         {
#     476                 :        706 :             insert(end(), OP_PUSHDATA2);
#     477                 :        706 :             uint8_t _data[2];
#     478                 :        706 :             WriteLE16(_data, b.size());
#     479                 :        706 :             insert(end(), _data, _data + sizeof(_data));
#     480                 :        706 :         }
#     481                 :         52 :         else
#     482                 :         52 :         {
#     483                 :         52 :             insert(end(), OP_PUSHDATA4);
#     484                 :         52 :             uint8_t _data[4];
#     485                 :         52 :             WriteLE32(_data, b.size());
#     486                 :         52 :             insert(end(), _data, _data + sizeof(_data));
#     487                 :         52 :         }
#     488                 :    6431993 :         insert(end(), b.begin(), b.end());
#     489                 :    6431993 :         return *this;
#     490                 :    6431993 :     }
#     491                 :            : 
#     492                 :            :     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
#     493                 :   30898515 :     {
#     494                 :   30898515 :         return GetScriptOp(pc, end(), opcodeRet, &vchRet);
#     495                 :   30898515 :     }
#     496                 :            : 
#     497                 :            :     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
#     498                 :  332855589 :     {
#     499                 :  332855589 :         return GetScriptOp(pc, end(), opcodeRet, nullptr);
#     500                 :  332855589 :     }
#     501                 :            : 
#     502                 :            :     /** Encode/decode small integers: */
#     503                 :            :     static int DecodeOP_N(opcodetype opcode)
#     504                 :    3359534 :     {
#     505         [ +  + ]:    3359534 :         if (opcode == OP_0)
#     506                 :    3027967 :             return 0;
#     507                 :     331567 :         assert(opcode >= OP_1 && opcode <= OP_16);
#     508                 :          0 :         return (int)opcode - (int)(OP_1 - 1);
#     509                 :    3359534 :     }
#     510                 :            :     static opcodetype EncodeOP_N(int n)
#     511                 :         50 :     {
#     512                 :         50 :         assert(n >= 0 && n <= 16);
#     513         [ -  + ]:         50 :         if (n == 0)
#     514                 :          0 :             return OP_0;
#     515                 :         50 :         return (opcodetype)(OP_1+n-1);
#     516                 :         50 :     }
#     517                 :            : 
#     518                 :            :     /**
#     519                 :            :      * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
#     520                 :            :      * as 20 sigops. With pay-to-script-hash, that changed:
#     521                 :            :      * CHECKMULTISIGs serialized in scriptSigs are
#     522                 :            :      * counted more accurately, assuming they are of the form
#     523                 :            :      *  ... OP_N CHECKMULTISIG ...
#     524                 :            :      */
#     525                 :            :     unsigned int GetSigOpCount(bool fAccurate) const;
#     526                 :            : 
#     527                 :            :     /**
#     528                 :            :      * Accurately count sigOps, including sigOps in
#     529                 :            :      * pay-to-script-hash transactions:
#     530                 :            :      */
#     531                 :            :     unsigned int GetSigOpCount(const CScript& scriptSig) const;
#     532                 :            : 
#     533                 :            :     bool IsPayToScriptHash() const;
#     534                 :            :     bool IsPayToWitnessScriptHash() const;
#     535                 :            :     bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;
#     536                 :            : 
#     537                 :            :     /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
#     538                 :            :     bool IsPushOnly(const_iterator pc) const;
#     539                 :            :     bool IsPushOnly() const;
#     540                 :            : 
#     541                 :            :     /** Check if the script contains valid OP_CODES */
#     542                 :            :     bool HasValidOps() const;
#     543                 :            : 
#     544                 :            :     /**
#     545                 :            :      * Returns whether the script is guaranteed to fail at execution,
#     546                 :            :      * regardless of the initial stack. This allows outputs to be pruned
#     547                 :            :      * instantly when entering the UTXO set.
#     548                 :            :      */
#     549                 :            :     bool IsUnspendable() const
#     550                 :   25172754 :     {
#     551 [ +  + ][ +  + ]:   25172754 :         return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
#                 [ +  + ]
#     552                 :   25172754 :     }
#     553                 :            : 
#     554                 :            :     void clear()
#     555                 :  116715865 :     {
#     556                 :            :         // The default prevector::clear() does not release memory
#     557                 :  116715865 :         CScriptBase::clear();
#     558                 :  116715865 :         shrink_to_fit();
#     559                 :  116715865 :     }
#     560                 :            : };
#     561                 :            : 
#     562                 :            : struct CScriptWitness
#     563                 :            : {
#     564                 :            :     // Note that this encodes the data elements being pushed, rather than
#     565                 :            :     // encoding them as a CScript that pushes them.
#     566                 :            :     std::vector<std::vector<unsigned char> > stack;
#     567                 :            : 
#     568                 :            :     // Some compilers complain without a default constructor
#     569                 :    8291702 :     CScriptWitness() { }
#     570                 :            : 
#     571                 :    3721272 :     bool IsNull() const { return stack.empty(); }
#     572                 :            : 
#     573                 :        940 :     void SetNull() { stack.clear(); stack.shrink_to_fit(); }
#     574                 :            : 
#     575                 :            :     std::string ToString() const;
#     576                 :            : };
#     577                 :            : 
#     578                 :            : /** Test for OP_SUCCESSx opcodes as defined by BIP342. */
#     579                 :            : bool IsOpSuccess(const opcodetype& opcode);
#     580                 :            : 
#     581                 :            : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode);
#     582                 :            : 
#     583                 :            : /** Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<. */
#     584                 :            : template<typename... Ts>
#     585                 :            : CScript BuildScript(Ts&&... inputs)
#     586                 :       2210 : {
#     587                 :       2210 :     CScript ret;
#     588                 :       2210 :     int cnt{0};
#     589                 :            : 
#     590                 :       5458 :     ([&ret, &cnt] (Ts&& input) {
#     591                 :       5458 :         cnt++;
#     592                 :       5458 :         if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>, CScript>) {
#     593                 :            :             // If it is a CScript, extend ret with it. Move or copy the first element instead.
#     594 [ -  + ][ -  + ]:       3758 :             if (cnt == 0) {
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#                 [ -  + ]
#     595                 :          0 :                 ret = std::forward<Ts>(input);
#     596                 :       1700 :             } else {
#     597                 :       1700 :                 ret.insert(ret.end(), input.begin(), input.end());
#     598                 :       1700 :             }
#     599                 :       3758 :         } else {
#     600                 :            :             // Otherwise invoke CScript::operator<<.
#     601                 :       3758 :             ret << input;
#     602                 :       3758 :         }
#     603                 :       5458 :     } (std::forward<Ts>(inputs)), ...);
#     604                 :            : 
#     605                 :       2210 :     return ret;
#     606                 :       2210 : }
#     607                 :            : 
#     608                 :            : #endif // BITCOIN_SCRIPT_SCRIPT_H

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a