PIVX Core  5.6.99
P2P Digital Currency
script.cpp
Go to the documentation of this file.
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 const char* GetOpName(opcodetype opcode)
15 {
16  switch (opcode)
17  {
18  // push value
19  case OP_0 : return "0";
20  case OP_PUSHDATA1 : return "OP_PUSHDATA1";
21  case OP_PUSHDATA2 : return "OP_PUSHDATA2";
22  case OP_PUSHDATA4 : return "OP_PUSHDATA4";
23  case OP_1NEGATE : return "-1";
24  case OP_RESERVED : return "OP_RESERVED";
25  case OP_1 : return "1";
26  case OP_2 : return "2";
27  case OP_3 : return "3";
28  case OP_4 : return "4";
29  case OP_5 : return "5";
30  case OP_6 : return "6";
31  case OP_7 : return "7";
32  case OP_8 : return "8";
33  case OP_9 : return "9";
34  case OP_10 : return "10";
35  case OP_11 : return "11";
36  case OP_12 : return "12";
37  case OP_13 : return "13";
38  case OP_14 : return "14";
39  case OP_15 : return "15";
40  case OP_16 : return "16";
41 
42  // control
43  case OP_NOP : return "OP_NOP";
44  case OP_VER : return "OP_VER";
45  case OP_IF : return "OP_IF";
46  case OP_NOTIF : return "OP_NOTIF";
47  case OP_VERIF : return "OP_VERIF";
48  case OP_VERNOTIF : return "OP_VERNOTIF";
49  case OP_ELSE : return "OP_ELSE";
50  case OP_ENDIF : return "OP_ENDIF";
51  case OP_VERIFY : return "OP_VERIFY";
52  case OP_RETURN : return "OP_RETURN";
53 
54  // stack ops
55  case OP_TOALTSTACK : return "OP_TOALTSTACK";
56  case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
57  case OP_2DROP : return "OP_2DROP";
58  case OP_2DUP : return "OP_2DUP";
59  case OP_3DUP : return "OP_3DUP";
60  case OP_2OVER : return "OP_2OVER";
61  case OP_2ROT : return "OP_2ROT";
62  case OP_2SWAP : return "OP_2SWAP";
63  case OP_IFDUP : return "OP_IFDUP";
64  case OP_DEPTH : return "OP_DEPTH";
65  case OP_DROP : return "OP_DROP";
66  case OP_DUP : return "OP_DUP";
67  case OP_NIP : return "OP_NIP";
68  case OP_OVER : return "OP_OVER";
69  case OP_PICK : return "OP_PICK";
70  case OP_ROLL : return "OP_ROLL";
71  case OP_ROT : return "OP_ROT";
72  case OP_SWAP : return "OP_SWAP";
73  case OP_TUCK : return "OP_TUCK";
74 
75  // splice ops
76  case OP_CAT : return "OP_CAT";
77  case OP_SUBSTR : return "OP_SUBSTR";
78  case OP_LEFT : return "OP_LEFT";
79  case OP_RIGHT : return "OP_RIGHT";
80  case OP_SIZE : return "OP_SIZE";
81 
82  // bit logic
83  case OP_INVERT : return "OP_INVERT";
84  case OP_AND : return "OP_AND";
85  case OP_OR : return "OP_OR";
86  case OP_XOR : return "OP_XOR";
87  case OP_EQUAL : return "OP_EQUAL";
88  case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
89  case OP_RESERVED1 : return "OP_RESERVED1";
90  case OP_RESERVED2 : return "OP_RESERVED2";
91 
92  // numeric
93  case OP_1ADD : return "OP_1ADD";
94  case OP_1SUB : return "OP_1SUB";
95  case OP_2MUL : return "OP_2MUL";
96  case OP_2DIV : return "OP_2DIV";
97  case OP_NEGATE : return "OP_NEGATE";
98  case OP_ABS : return "OP_ABS";
99  case OP_NOT : return "OP_NOT";
100  case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
101  case OP_ADD : return "OP_ADD";
102  case OP_SUB : return "OP_SUB";
103  case OP_MUL : return "OP_MUL";
104  case OP_DIV : return "OP_DIV";
105  case OP_MOD : return "OP_MOD";
106  case OP_LSHIFT : return "OP_LSHIFT";
107  case OP_RSHIFT : return "OP_RSHIFT";
108  case OP_BOOLAND : return "OP_BOOLAND";
109  case OP_BOOLOR : return "OP_BOOLOR";
110  case OP_NUMEQUAL : return "OP_NUMEQUAL";
111  case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
112  case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
113  case OP_LESSTHAN : return "OP_LESSTHAN";
114  case OP_GREATERTHAN : return "OP_GREATERTHAN";
115  case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
116  case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
117  case OP_MIN : return "OP_MIN";
118  case OP_MAX : return "OP_MAX";
119  case OP_WITHIN : return "OP_WITHIN";
120 
121  // crypto
122  case OP_RIPEMD160 : return "OP_RIPEMD160";
123  case OP_SHA1 : return "OP_SHA1";
124  case OP_SHA256 : return "OP_SHA256";
125  case OP_HASH160 : return "OP_HASH160";
126  case OP_HASH256 : return "OP_HASH256";
127  case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
128  case OP_CHECKSIG : return "OP_CHECKSIG";
129  case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
130  case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
131  case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
132 
133  // expansion
134  case OP_NOP1 : return "OP_NOP1"; // OP_NOP1
135  case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY"; // OP_NOP2
136  case OP_NOP3 : return "OP_NOP3"; // OP_NOP3
137  case OP_NOP4 : return "OP_NOP4"; // OP_NOP4
138  case OP_NOP5 : return "OP_NOP5"; // OP_NOP5
139  case OP_NOP6 : return "OP_NOP6"; // OP_NOP6
140  case OP_NOP7 : return "OP_NOP7"; // OP_NOP7
141  case OP_NOP8 : return "OP_NOP8"; // OP_NOP8
142  case OP_NOP9 : return "OP_NOP9"; // OP_NOP9
143  case OP_NOP10 : return "OP_NOP10"; // OP_NOP10
144 
145  // zerocoin
146  case OP_ZEROCOINMINT : return "OP_ZEROCOINMINT";
147  case OP_ZEROCOINSPEND : return "OP_ZEROCOINSPEND";
148  case OP_ZEROCOINPUBLICSPEND : return "OP_ZEROCOINPUBLICSPEND";
149 
150  // cold staking
151  case OP_CHECKCOLDSTAKEVERIFY_LOF : return "OP_CHECKCOLDSTAKEVERIFY_LOF";
152  case OP_CHECKCOLDSTAKEVERIFY : return "OP_CHECKCOLDSTAKEVERIFY";
153 
154  // exchange address
155  case OP_EXCHANGEADDR : return "OP_EXCHANGEADDR";
156 
157  case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
158 
159  default:
160  return "OP_UNKNOWN";
161  }
162 }
163 
164 unsigned int CScript::GetSigOpCount(bool fAccurate) const
165 {
166  unsigned int n = 0;
167  const_iterator pc = begin();
168  opcodetype lastOpcode = OP_INVALIDOPCODE;
169  while (pc < end())
170  {
171  opcodetype opcode;
172  if (!GetOp(pc, opcode))
173  break;
174  if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
175  n++;
176  else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
177  {
178  if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
179  n += DecodeOP_N(lastOpcode);
180  else
181  n += MAX_PUBKEYS_PER_MULTISIG;
182  }
183  lastOpcode = opcode;
184  }
185  return n;
186 }
187 
188 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
189 {
190  if (!IsPayToScriptHash())
191  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  const_iterator pc = scriptSig.begin();
197  std::vector<unsigned char> data;
198  while (pc < scriptSig.end())
199  {
200  opcodetype opcode;
201  if (!scriptSig.GetOp(pc, opcode, data))
202  return 0;
203  if (opcode > OP_16)
204  return 0;
205  }
206 
208  CScript subscript(data.begin(), data.end());
209  return subscript.GetSigOpCount(true);
210 }
211 
213 {
214  // Extra-fast test for pay-to-pubkey-hash CScripts:
215  return (this->size() == 25 &&
216  (*this)[0] == OP_DUP &&
217  (*this)[1] == OP_HASH160 &&
218  (*this)[2] == 0x14 &&
219  (*this)[23] == OP_EQUALVERIFY &&
220  (*this)[24] == OP_CHECKSIG);
221 }
222 
224 {
225  // Extra-fast test for pay-to-script-hash CScripts:
226  return (this->size() == 23 &&
227  (*this)[0] == OP_HASH160 &&
228  (*this)[1] == 0x14 &&
229  (*this)[22] == OP_EQUAL);
230 }
231 
232 // P2CS script: either with or without last output free
234 {
235  return (this->size() == 51 &&
236  (*this)[0] == OP_DUP &&
237  (*this)[1] == OP_HASH160 &&
238  (*this)[2] == OP_ROT &&
239  (*this)[3] == OP_IF &&
240  ((*this)[4] == OP_CHECKCOLDSTAKEVERIFY || (*this)[4] == OP_CHECKCOLDSTAKEVERIFY_LOF) &&
241  (*this)[5] == 0x14 &&
242  (*this)[26] == OP_ELSE &&
243  (*this)[27] == 0x14 &&
244  (*this)[48] == OP_ENDIF &&
245  (*this)[49] == OP_EQUALVERIFY &&
246  (*this)[50] == OP_CHECKSIG);
247 }
248 
250 {
251  return IsPayToColdStaking() && (*this)[4] == OP_CHECKCOLDSTAKEVERIFY_LOF;
252 }
253 
255 {
256  return (this->size() == 26 &&
257  (*this)[0] == OP_EXCHANGEADDR &&
258  (*this)[1] == OP_DUP &&
259  (*this)[2] == OP_HASH160 &&
260  (*this)[3] == 0x14 &&
261  (*this)[24] == OP_EQUALVERIFY &&
262  (*this)[25] == OP_CHECKSIG);
263 }
264 
265 bool CScript::StartsWithOpcode(const opcodetype opcode) const
266 {
267  return (!this->empty() && (*this)[0] == opcode);
268 }
269 
271 {
273 }
274 
276 {
278 }
279 
281 {
283 }
284 
286 {
287  while (pc < end())
288  {
289  opcodetype opcode;
290  if (!GetOp(pc, opcode))
291  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  if (opcode > OP_16)
297  return false;
298  }
299  return true;
300 }
301 
303 {
304  return this->IsPushOnly(begin());
305 }
306 
308 {
309  return memusage::DynamicUsage(*static_cast<const CScriptBase*>(this));
310 }
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
bool IsPayToScriptHash() const
Definition: script.cpp:223
bool IsPayToColdStakingLOF() const
Definition: script.cpp:249
bool StartsWithOpcode(const opcodetype opcode) const
Definition: script.cpp:265
size_t DynamicMemoryUsage() const
Definition: script.cpp:307
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:568
bool IsPushOnly() const
Definition: script.cpp:302
bool IsZerocoinSpend() const
Definition: script.cpp:275
bool IsZerocoinPublicSpend() const
Definition: script.cpp:280
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:488
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:164
bool IsZerocoinMint() const
Definition: script.cpp:270
bool IsPayToPublicKeyHash() const
Definition: script.cpp:212
bool IsPayToColdStaking() const
Definition: script.cpp:233
bool IsPayToExchangeAddress() const
Definition: script.cpp:254
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
bool empty() const
Definition: prevector.h:281
size_type size() const
Definition: prevector.h:277
value_type * data()
Definition: prevector.h:526
iterator begin()
Definition: prevector.h:285
iterator end()
Definition: prevector.h:287
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:14
opcodetype
Script opcodes.
Definition: script.h:50
@ OP_NUMNOTEQUAL
Definition: script.h:149
@ OP_RESERVED1
Definition: script.h:124
@ OP_2
Definition: script.h:61
@ OP_SHA256
Definition: script.h:162
@ OP_PUSHDATA4
Definition: script.h:56
@ OP_NOP5
Definition: script.h:177
@ OP_RIGHT
Definition: script.h:114
@ OP_BOOLAND
Definition: script.h:145
@ OP_CHECKMULTISIG
Definition: script.h:168
@ OP_NEGATE
Definition: script.h:132
@ OP_IF
Definition: script.h:80
@ OP_13
Definition: script.h:72
@ OP_ROT
Definition: script.h:106
@ OP_SWAP
Definition: script.h:107
@ OP_1NEGATE
Definition: script.h:57
@ OP_VERNOTIF
Definition: script.h:83
@ OP_CHECKSIG
Definition: script.h:166
@ OP_CHECKLOCKTIMEVERIFY
Definition: script.h:174
@ OP_LESSTHAN
Definition: script.h:150
@ OP_16
Definition: script.h:75
@ OP_LEFT
Definition: script.h:113
@ OP_14
Definition: script.h:73
@ OP_NOP10
Definition: script.h:182
@ OP_2DIV
Definition: script.h:131
@ OP_INVALIDOPCODE
Definition: script.h:196
@ OP_NOT
Definition: script.h:134
@ OP_EQUAL
Definition: script.h:122
@ OP_NUMEQUAL
Definition: script.h:147
@ OP_MOD
Definition: script.h:141
@ OP_NOTIF
Definition: script.h:81
@ OP_4
Definition: script.h:63
@ OP_10
Definition: script.h:69
@ OP_SIZE
Definition: script.h:115
@ OP_3DUP
Definition: script.h:94
@ OP_ZEROCOINSPEND
Definition: script.h:186
@ OP_ENDIF
Definition: script.h:85
@ OP_NOP1
Definition: script.h:172
@ OP_DUP
Definition: script.h:101
@ OP_GREATERTHAN
Definition: script.h:151
@ OP_NOP
Definition: script.h:78
@ OP_CHECKCOLDSTAKEVERIFY
Definition: script.h:191
@ OP_VERIF
Definition: script.h:82
@ OP_TOALTSTACK
Definition: script.h:90
@ OP_CODESEPARATOR
Definition: script.h:165
@ OP_RIPEMD160
Definition: script.h:160
@ OP_MIN
Definition: script.h:154
@ OP_ZEROCOINMINT
Definition: script.h:185
@ OP_HASH256
Definition: script.h:164
@ OP_MAX
Definition: script.h:155
@ OP_1SUB
Definition: script.h:129
@ OP_FROMALTSTACK
Definition: script.h:91
@ OP_SUB
Definition: script.h:138
@ OP_NUMEQUALVERIFY
Definition: script.h:148
@ OP_OVER
Definition: script.h:103
@ OP_NOP8
Definition: script.h:180
@ OP_DIV
Definition: script.h:140
@ OP_HASH160
Definition: script.h:163
@ OP_2DUP
Definition: script.h:93
@ OP_NIP
Definition: script.h:102
@ OP_2MUL
Definition: script.h:130
@ OP_NOP4
Definition: script.h:176
@ OP_NOP3
Definition: script.h:175
@ OP_1
Definition: script.h:59
@ OP_LESSTHANOREQUAL
Definition: script.h:152
@ OP_2DROP
Definition: script.h:92
@ OP_DEPTH
Definition: script.h:99
@ OP_NOP9
Definition: script.h:181
@ OP_VER
Definition: script.h:79
@ OP_VERIFY
Definition: script.h:86
@ OP_RESERVED2
Definition: script.h:125
@ OP_12
Definition: script.h:71
@ OP_ADD
Definition: script.h:137
@ OP_CHECKCOLDSTAKEVERIFY_LOF
Definition: script.h:190
@ OP_CHECKMULTISIGVERIFY
Definition: script.h:169
@ OP_NOP7
Definition: script.h:179
@ OP_8
Definition: script.h:67
@ OP_BOOLOR
Definition: script.h:146
@ OP_XOR
Definition: script.h:121
@ OP_DROP
Definition: script.h:100
@ OP_MUL
Definition: script.h:139
@ OP_WITHIN
Definition: script.h:157
@ OP_ELSE
Definition: script.h:84
@ OP_15
Definition: script.h:74
@ OP_CHECKSIGVERIFY
Definition: script.h:167
@ OP_PUSHDATA1
Definition: script.h:54
@ OP_TUCK
Definition: script.h:108
@ OP_2OVER
Definition: script.h:95
@ OP_0NOTEQUAL
Definition: script.h:135
@ OP_9
Definition: script.h:68
@ OP_3
Definition: script.h:62
@ OP_11
Definition: script.h:70
@ OP_SHA1
Definition: script.h:161
@ OP_SUBSTR
Definition: script.h:112
@ OP_GREATERTHANOREQUAL
Definition: script.h:153
@ OP_RSHIFT
Definition: script.h:143
@ OP_2SWAP
Definition: script.h:97
@ OP_PUSHDATA2
Definition: script.h:55
@ OP_2ROT
Definition: script.h:96
@ OP_6
Definition: script.h:65
@ OP_INVERT
Definition: script.h:118
@ OP_0
Definition: script.h:52
@ OP_ABS
Definition: script.h:133
@ OP_LSHIFT
Definition: script.h:142
@ OP_RETURN
Definition: script.h:87
@ OP_EXCHANGEADDR
Definition: script.h:194
@ OP_IFDUP
Definition: script.h:98
@ OP_PICK
Definition: script.h:104
@ OP_AND
Definition: script.h:119
@ OP_EQUALVERIFY
Definition: script.h:123
@ OP_CAT
Definition: script.h:111
@ OP_RESERVED
Definition: script.h:58
@ OP_1ADD
Definition: script.h:128
@ OP_7
Definition: script.h:66
@ OP_ZEROCOINPUBLICSPEND
Definition: script.h:187
@ OP_OR
Definition: script.h:120
@ OP_ROLL
Definition: script.h:105
@ OP_NOP6
Definition: script.h:178
@ OP_5
Definition: script.h:64