Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / script_P2SH_tests.cpp
blob58aa32c96913e6b180275f01408768b2c1f9ded1
1 // Copyright (c) 2012-2016 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.
5 #include "consensus/tx_verify.h"
6 #include "core_io.h"
7 #include "key.h"
8 #include "keystore.h"
9 #include "validation.h"
10 #include "policy/policy.h"
11 #include "script/script.h"
12 #include "script/script_error.h"
13 #include "script/sign.h"
14 #include "script/ismine.h"
15 #include "test/test_bitcoin.h"
17 #include <vector>
19 #include <boost/test/unit_test.hpp>
21 // Helpers:
22 static std::vector<unsigned char>
23 Serialize(const CScript& s)
25 std::vector<unsigned char> sSerialized(s.begin(), s.end());
26 return sSerialized;
29 static bool
30 Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict, ScriptError& err)
32 // Create dummy to/from transactions:
33 CMutableTransaction txFrom;
34 txFrom.vout.resize(1);
35 txFrom.vout[0].scriptPubKey = scriptPubKey;
37 CMutableTransaction txTo;
38 txTo.vin.resize(1);
39 txTo.vout.resize(1);
40 txTo.vin[0].prevout.n = 0;
41 txTo.vin[0].prevout.hash = txFrom.GetHash();
42 txTo.vin[0].scriptSig = scriptSig;
43 txTo.vout[0].nValue = 1;
45 return VerifyScript(scriptSig, scriptPubKey, nullptr, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), &err);
49 BOOST_FIXTURE_TEST_SUITE(script_P2SH_tests, BasicTestingSetup)
51 BOOST_AUTO_TEST_CASE(sign)
53 LOCK(cs_main);
54 // Pay-to-script-hash looks like this:
55 // scriptSig: <sig> <sig...> <serialized_script>
56 // scriptPubKey: HASH160 <hash> EQUAL
58 // Test SignSignature() (and therefore the version of Solver() that signs transactions)
59 CBasicKeyStore keystore;
60 CKey key[4];
61 for (int i = 0; i < 4; i++)
63 key[i].MakeNewKey(true);
64 keystore.AddKey(key[i]);
67 // 8 Scripts: checking all combinations of
68 // different keys, straight/P2SH, pubkey/pubkeyhash
69 CScript standardScripts[4];
70 standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
71 standardScripts[1] = GetScriptForDestination(key[1].GetPubKey().GetID());
72 standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
73 standardScripts[3] = GetScriptForDestination(key[2].GetPubKey().GetID());
74 CScript evalScripts[4];
75 for (int i = 0; i < 4; i++)
77 keystore.AddCScript(standardScripts[i]);
78 evalScripts[i] = GetScriptForDestination(CScriptID(standardScripts[i]));
81 CMutableTransaction txFrom; // Funding transaction:
82 std::string reason;
83 txFrom.vout.resize(8);
84 for (int i = 0; i < 4; i++)
86 txFrom.vout[i].scriptPubKey = evalScripts[i];
87 txFrom.vout[i].nValue = COIN;
88 txFrom.vout[i+4].scriptPubKey = standardScripts[i];
89 txFrom.vout[i+4].nValue = COIN;
91 BOOST_CHECK(IsStandardTx(txFrom, reason));
93 CMutableTransaction txTo[8]; // Spending transactions
94 for (int i = 0; i < 8; i++)
96 txTo[i].vin.resize(1);
97 txTo[i].vout.resize(1);
98 txTo[i].vin[0].prevout.n = i;
99 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
100 txTo[i].vout[0].nValue = 1;
101 BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
103 for (int i = 0; i < 8; i++)
105 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i));
107 // All of the above should be OK, and the txTos have valid signatures
108 // Check to make sure signature verification fails if we use the wrong ScriptSig:
109 for (int i = 0; i < 8; i++) {
110 PrecomputedTransactionData txdata(txTo[i]);
111 for (int j = 0; j < 8; j++)
113 CScript sigSave = txTo[i].vin[0].scriptSig;
114 txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
115 bool sigOK = CScriptCheck(txFrom.vout[txTo[i].vin[0].prevout.n], txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)();
116 if (i == j)
117 BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
118 else
119 BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
120 txTo[i].vin[0].scriptSig = sigSave;
125 BOOST_AUTO_TEST_CASE(norecurse)
127 ScriptError err;
128 // Make sure only the outer pay-to-script-hash does the
129 // extra-validation thing:
130 CScript invalidAsScript;
131 invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
133 CScript p2sh = GetScriptForDestination(CScriptID(invalidAsScript));
135 CScript scriptSig;
136 scriptSig << Serialize(invalidAsScript);
138 // Should not verify, because it will try to execute OP_INVALIDOPCODE
139 BOOST_CHECK(!Verify(scriptSig, p2sh, true, err));
140 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_BAD_OPCODE, ScriptErrorString(err));
142 // Try to recur, and verification should succeed because
143 // the inner HASH160 <> EQUAL should only check the hash:
144 CScript p2sh2 = GetScriptForDestination(CScriptID(p2sh));
145 CScript scriptSig2;
146 scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
148 BOOST_CHECK(Verify(scriptSig2, p2sh2, true, err));
149 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
152 BOOST_AUTO_TEST_CASE(set)
154 LOCK(cs_main);
155 // Test the CScript::Set* methods
156 CBasicKeyStore keystore;
157 CKey key[4];
158 std::vector<CPubKey> keys;
159 for (int i = 0; i < 4; i++)
161 key[i].MakeNewKey(true);
162 keystore.AddKey(key[i]);
163 keys.push_back(key[i].GetPubKey());
166 CScript inner[4];
167 inner[0] = GetScriptForDestination(key[0].GetPubKey().GetID());
168 inner[1] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
169 inner[2] = GetScriptForMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
170 inner[3] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
172 CScript outer[4];
173 for (int i = 0; i < 4; i++)
175 outer[i] = GetScriptForDestination(CScriptID(inner[i]));
176 keystore.AddCScript(inner[i]);
179 CMutableTransaction txFrom; // Funding transaction:
180 std::string reason;
181 txFrom.vout.resize(4);
182 for (int i = 0; i < 4; i++)
184 txFrom.vout[i].scriptPubKey = outer[i];
185 txFrom.vout[i].nValue = CENT;
187 BOOST_CHECK(IsStandardTx(txFrom, reason));
189 CMutableTransaction txTo[4]; // Spending transactions
190 for (int i = 0; i < 4; i++)
192 txTo[i].vin.resize(1);
193 txTo[i].vout.resize(1);
194 txTo[i].vin[0].prevout.n = i;
195 txTo[i].vin[0].prevout.hash = txFrom.GetHash();
196 txTo[i].vout[0].nValue = 1*CENT;
197 txTo[i].vout[0].scriptPubKey = inner[i];
198 BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
200 for (int i = 0; i < 4; i++)
202 BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i));
203 BOOST_CHECK_MESSAGE(IsStandardTx(txTo[i], reason), strprintf("txTo[%d].IsStandard", i));
207 BOOST_AUTO_TEST_CASE(is)
209 // Test CScript::IsPayToScriptHash()
210 uint160 dummy;
211 CScript p2sh;
212 p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL;
213 BOOST_CHECK(p2sh.IsPayToScriptHash());
215 // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
216 static const unsigned char direct[] = { OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
217 BOOST_CHECK(CScript(direct, direct+sizeof(direct)).IsPayToScriptHash());
218 static const unsigned char pushdata1[] = { OP_HASH160, OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
219 BOOST_CHECK(!CScript(pushdata1, pushdata1+sizeof(pushdata1)).IsPayToScriptHash());
220 static const unsigned char pushdata2[] = { OP_HASH160, OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
221 BOOST_CHECK(!CScript(pushdata2, pushdata2+sizeof(pushdata2)).IsPayToScriptHash());
222 static const unsigned char pushdata4[] = { OP_HASH160, OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
223 BOOST_CHECK(!CScript(pushdata4, pushdata4+sizeof(pushdata4)).IsPayToScriptHash());
225 CScript not_p2sh;
226 BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
228 not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL;
229 BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
231 not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL;
232 BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
234 not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG;
235 BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
238 BOOST_AUTO_TEST_CASE(switchover)
240 // Test switch over code
241 CScript notValid;
242 ScriptError err;
243 notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
244 CScript scriptSig;
245 scriptSig << Serialize(notValid);
247 CScript fund = GetScriptForDestination(CScriptID(notValid));
250 // Validation should succeed under old rules (hash is correct):
251 BOOST_CHECK(Verify(scriptSig, fund, false, err));
252 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
253 // Fail under new:
254 BOOST_CHECK(!Verify(scriptSig, fund, true, err));
255 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EQUALVERIFY, ScriptErrorString(err));
258 BOOST_AUTO_TEST_CASE(AreInputsStandard)
260 LOCK(cs_main);
261 CCoinsView coinsDummy;
262 CCoinsViewCache coins(&coinsDummy);
263 CBasicKeyStore keystore;
264 CKey key[6];
265 std::vector<CPubKey> keys;
266 for (int i = 0; i < 6; i++)
268 key[i].MakeNewKey(true);
269 keystore.AddKey(key[i]);
271 for (int i = 0; i < 3; i++)
272 keys.push_back(key[i].GetPubKey());
274 CMutableTransaction txFrom;
275 txFrom.vout.resize(7);
277 // First three are standard:
278 CScript pay1 = GetScriptForDestination(key[0].GetPubKey().GetID());
279 keystore.AddCScript(pay1);
280 CScript pay1of3 = GetScriptForMultisig(1, keys);
282 txFrom.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(pay1)); // P2SH (OP_CHECKSIG)
283 txFrom.vout[0].nValue = 1000;
284 txFrom.vout[1].scriptPubKey = pay1; // ordinary OP_CHECKSIG
285 txFrom.vout[1].nValue = 2000;
286 txFrom.vout[2].scriptPubKey = pay1of3; // ordinary OP_CHECKMULTISIG
287 txFrom.vout[2].nValue = 3000;
289 // vout[3] is complicated 1-of-3 AND 2-of-3
290 // ... that is OK if wrapped in P2SH:
291 CScript oneAndTwo;
292 oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey());
293 oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY;
294 oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey());
295 oneAndTwo << OP_3 << OP_CHECKMULTISIG;
296 keystore.AddCScript(oneAndTwo);
297 txFrom.vout[3].scriptPubKey = GetScriptForDestination(CScriptID(oneAndTwo));
298 txFrom.vout[3].nValue = 4000;
300 // vout[4] is max sigops:
301 CScript fifteenSigops; fifteenSigops << OP_1;
302 for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++)
303 fifteenSigops << ToByteVector(key[i%3].GetPubKey());
304 fifteenSigops << OP_15 << OP_CHECKMULTISIG;
305 keystore.AddCScript(fifteenSigops);
306 txFrom.vout[4].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops));
307 txFrom.vout[4].nValue = 5000;
309 // vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS
310 CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG;
311 keystore.AddCScript(sixteenSigops);
312 txFrom.vout[5].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops));
313 txFrom.vout[5].nValue = 5000;
314 CScript twentySigops; twentySigops << OP_CHECKMULTISIG;
315 keystore.AddCScript(twentySigops);
316 txFrom.vout[6].scriptPubKey = GetScriptForDestination(CScriptID(twentySigops));
317 txFrom.vout[6].nValue = 6000;
319 AddCoins(coins, txFrom, 0);
321 CMutableTransaction txTo;
322 txTo.vout.resize(1);
323 txTo.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
325 txTo.vin.resize(5);
326 for (int i = 0; i < 5; i++)
328 txTo.vin[i].prevout.n = i;
329 txTo.vin[i].prevout.hash = txFrom.GetHash();
331 BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0, SIGHASH_ALL));
332 BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1, SIGHASH_ALL));
333 BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2, SIGHASH_ALL));
334 // SignSignature doesn't know how to sign these. We're
335 // not testing validating signatures, so just create
336 // dummy signatures that DO include the correct P2SH scripts:
337 txTo.vin[3].scriptSig << OP_11 << OP_11 << std::vector<unsigned char>(oneAndTwo.begin(), oneAndTwo.end());
338 txTo.vin[4].scriptSig << std::vector<unsigned char>(fifteenSigops.begin(), fifteenSigops.end());
340 BOOST_CHECK(::AreInputsStandard(txTo, coins));
341 // 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4]
342 BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txTo, coins), 22U);
344 CMutableTransaction txToNonStd1;
345 txToNonStd1.vout.resize(1);
346 txToNonStd1.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
347 txToNonStd1.vout[0].nValue = 1000;
348 txToNonStd1.vin.resize(1);
349 txToNonStd1.vin[0].prevout.n = 5;
350 txToNonStd1.vin[0].prevout.hash = txFrom.GetHash();
351 txToNonStd1.vin[0].scriptSig << std::vector<unsigned char>(sixteenSigops.begin(), sixteenSigops.end());
353 BOOST_CHECK(!::AreInputsStandard(txToNonStd1, coins));
354 BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd1, coins), 16U);
356 CMutableTransaction txToNonStd2;
357 txToNonStd2.vout.resize(1);
358 txToNonStd2.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
359 txToNonStd2.vout[0].nValue = 1000;
360 txToNonStd2.vin.resize(1);
361 txToNonStd2.vin[0].prevout.n = 6;
362 txToNonStd2.vin[0].prevout.hash = txFrom.GetHash();
363 txToNonStd2.vin[0].scriptSig << std::vector<unsigned char>(twentySigops.begin(), twentySigops.end());
365 BOOST_CHECK(!::AreInputsStandard(txToNonStd2, coins));
366 BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd2, coins), 20U);
369 BOOST_AUTO_TEST_SUITE_END()