Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / transaction_tests.cpp
blobcb6ab7cdbec27ed23111d8ae43a810d3d38b1ea0
1 // Copyright (c) 2011-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 "data/tx_invalid.json.h"
6 #include "data/tx_valid.json.h"
7 #include "test/test_bitcoin.h"
9 #include "clientversion.h"
10 #include "checkqueue.h"
11 #include "consensus/tx_verify.h"
12 #include "consensus/validation.h"
13 #include "core_io.h"
14 #include "key.h"
15 #include "keystore.h"
16 #include "validation.h"
17 #include "policy/policy.h"
18 #include "script/script.h"
19 #include "script/sign.h"
20 #include "script/script_error.h"
21 #include "script/standard.h"
22 #include "utilstrencodings.h"
24 #include <map>
25 #include <string>
27 #include <boost/algorithm/string/classification.hpp>
28 #include <boost/algorithm/string/split.hpp>
29 #include <boost/test/unit_test.hpp>
31 #include <univalue.h>
33 typedef std::vector<unsigned char> valtype;
35 // In script_tests.cpp
36 extern UniValue read_json(const std::string& jsondata);
38 static std::map<std::string, unsigned int> mapFlagNames = {
39 {std::string("NONE"), (unsigned int)SCRIPT_VERIFY_NONE},
40 {std::string("P2SH"), (unsigned int)SCRIPT_VERIFY_P2SH},
41 {std::string("STRICTENC"), (unsigned int)SCRIPT_VERIFY_STRICTENC},
42 {std::string("DERSIG"), (unsigned int)SCRIPT_VERIFY_DERSIG},
43 {std::string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S},
44 {std::string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY},
45 {std::string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA},
46 {std::string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY},
47 {std::string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS},
48 {std::string("CLEANSTACK"), (unsigned int)SCRIPT_VERIFY_CLEANSTACK},
49 {std::string("MINIMALIF"), (unsigned int)SCRIPT_VERIFY_MINIMALIF},
50 {std::string("NULLFAIL"), (unsigned int)SCRIPT_VERIFY_NULLFAIL},
51 {std::string("CHECKLOCKTIMEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY},
52 {std::string("CHECKSEQUENCEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY},
53 {std::string("WITNESS"), (unsigned int)SCRIPT_VERIFY_WITNESS},
54 {std::string("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM},
55 {std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE},
58 unsigned int ParseScriptFlags(std::string strFlags)
60 if (strFlags.empty()) {
61 return 0;
63 unsigned int flags = 0;
64 std::vector<std::string> words;
65 boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(","));
67 for (std::string word : words)
69 if (!mapFlagNames.count(word))
70 BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
71 flags |= mapFlagNames[word];
74 return flags;
77 std::string FormatScriptFlags(unsigned int flags)
79 if (flags == 0) {
80 return "";
82 std::string ret;
83 std::map<std::string, unsigned int>::const_iterator it = mapFlagNames.begin();
84 while (it != mapFlagNames.end()) {
85 if (flags & it->second) {
86 ret += it->first + ",";
88 it++;
90 return ret.substr(0, ret.size() - 1);
93 BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
95 BOOST_AUTO_TEST_CASE(tx_valid)
97 // Read tests from test/data/tx_valid.json
98 // Format is an array of arrays
99 // Inner arrays are either [ "comment" ]
100 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
101 // ... where all scripts are stringified scripts.
103 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
104 UniValue tests = read_json(std::string(json_tests::tx_valid, json_tests::tx_valid + sizeof(json_tests::tx_valid)));
106 ScriptError err;
107 for (unsigned int idx = 0; idx < tests.size(); idx++) {
108 UniValue test = tests[idx];
109 std::string strTest = test.write();
110 if (test[0].isArray())
112 if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
114 BOOST_ERROR("Bad test: " << strTest);
115 continue;
118 std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
119 std::map<COutPoint, int64_t> mapprevOutValues;
120 UniValue inputs = test[0].get_array();
121 bool fValid = true;
122 for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
123 const UniValue& input = inputs[inpIdx];
124 if (!input.isArray())
126 fValid = false;
127 break;
129 UniValue vinput = input.get_array();
130 if (vinput.size() < 3 || vinput.size() > 4)
132 fValid = false;
133 break;
135 COutPoint outpoint(uint256S(vinput[0].get_str()), vinput[1].get_int());
136 mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
137 if (vinput.size() >= 4)
139 mapprevOutValues[outpoint] = vinput[3].get_int64();
142 if (!fValid)
144 BOOST_ERROR("Bad test: " << strTest);
145 continue;
148 std::string transaction = test[1].get_str();
149 CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION);
150 CTransaction tx(deserialize, stream);
152 CValidationState state;
153 BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
154 BOOST_CHECK(state.IsValid());
156 PrecomputedTransactionData txdata(tx);
157 for (unsigned int i = 0; i < tx.vin.size(); i++)
159 if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
161 BOOST_ERROR("Bad test: " << strTest);
162 break;
165 CAmount amount = 0;
166 if (mapprevOutValues.count(tx.vin[i].prevout)) {
167 amount = mapprevOutValues[tx.vin[i].prevout];
169 unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
170 const CScriptWitness *witness = &tx.vin[i].scriptWitness;
171 BOOST_CHECK_MESSAGE(VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout],
172 witness, verify_flags, TransactionSignatureChecker(&tx, i, amount, txdata), &err),
173 strTest);
174 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
180 BOOST_AUTO_TEST_CASE(tx_invalid)
182 // Read tests from test/data/tx_invalid.json
183 // Format is an array of arrays
184 // Inner arrays are either [ "comment" ]
185 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
186 // ... where all scripts are stringified scripts.
188 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
189 UniValue tests = read_json(std::string(json_tests::tx_invalid, json_tests::tx_invalid + sizeof(json_tests::tx_invalid)));
191 // Initialize to SCRIPT_ERR_OK. The tests expect err to be changed to a
192 // value other than SCRIPT_ERR_OK.
193 ScriptError err = SCRIPT_ERR_OK;
194 for (unsigned int idx = 0; idx < tests.size(); idx++) {
195 UniValue test = tests[idx];
196 std::string strTest = test.write();
197 if (test[0].isArray())
199 if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
201 BOOST_ERROR("Bad test: " << strTest);
202 continue;
205 std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
206 std::map<COutPoint, int64_t> mapprevOutValues;
207 UniValue inputs = test[0].get_array();
208 bool fValid = true;
209 for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
210 const UniValue& input = inputs[inpIdx];
211 if (!input.isArray())
213 fValid = false;
214 break;
216 UniValue vinput = input.get_array();
217 if (vinput.size() < 3 || vinput.size() > 4)
219 fValid = false;
220 break;
222 COutPoint outpoint(uint256S(vinput[0].get_str()), vinput[1].get_int());
223 mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
224 if (vinput.size() >= 4)
226 mapprevOutValues[outpoint] = vinput[3].get_int64();
229 if (!fValid)
231 BOOST_ERROR("Bad test: " << strTest);
232 continue;
235 std::string transaction = test[1].get_str();
236 CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION );
237 CTransaction tx(deserialize, stream);
239 CValidationState state;
240 fValid = CheckTransaction(tx, state) && state.IsValid();
242 PrecomputedTransactionData txdata(tx);
243 for (unsigned int i = 0; i < tx.vin.size() && fValid; i++)
245 if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
247 BOOST_ERROR("Bad test: " << strTest);
248 break;
251 unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
252 CAmount amount = 0;
253 if (mapprevOutValues.count(tx.vin[i].prevout)) {
254 amount = mapprevOutValues[tx.vin[i].prevout];
256 const CScriptWitness *witness = &tx.vin[i].scriptWitness;
257 fValid = VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout],
258 witness, verify_flags, TransactionSignatureChecker(&tx, i, amount, txdata), &err);
260 BOOST_CHECK_MESSAGE(!fValid, strTest);
261 BOOST_CHECK_MESSAGE(err != SCRIPT_ERR_OK, ScriptErrorString(err));
266 BOOST_AUTO_TEST_CASE(basic_transaction_tests)
268 // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
269 unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
270 std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
271 CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
272 CMutableTransaction tx;
273 stream >> tx;
274 CValidationState state;
275 BOOST_CHECK_MESSAGE(CheckTransaction(tx, state) && state.IsValid(), "Simple deserialized transaction should be valid.");
277 // Check that duplicate txins fail
278 tx.vin.push_back(tx.vin[0]);
279 BOOST_CHECK_MESSAGE(!CheckTransaction(tx, state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
283 // Helper: create two dummy transactions, each with
284 // two outputs. The first has 11 and 50 CENT outputs
285 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
286 // paid to a TX_PUBKEYHASH.
288 static std::vector<CMutableTransaction>
289 SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet)
291 std::vector<CMutableTransaction> dummyTransactions;
292 dummyTransactions.resize(2);
294 // Add some keys to the keystore:
295 CKey key[4];
296 for (int i = 0; i < 4; i++)
298 key[i].MakeNewKey(i % 2);
299 keystoreRet.AddKey(key[i]);
302 // Create some dummy input transactions
303 dummyTransactions[0].vout.resize(2);
304 dummyTransactions[0].vout[0].nValue = 11*CENT;
305 dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
306 dummyTransactions[0].vout[1].nValue = 50*CENT;
307 dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
308 AddCoins(coinsRet, dummyTransactions[0], 0);
310 dummyTransactions[1].vout.resize(2);
311 dummyTransactions[1].vout[0].nValue = 21*CENT;
312 dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(key[2].GetPubKey().GetID());
313 dummyTransactions[1].vout[1].nValue = 22*CENT;
314 dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(key[3].GetPubKey().GetID());
315 AddCoins(coinsRet, dummyTransactions[1], 0);
317 return dummyTransactions;
320 BOOST_AUTO_TEST_CASE(test_Get)
322 CBasicKeyStore keystore;
323 CCoinsView coinsDummy;
324 CCoinsViewCache coins(&coinsDummy);
325 std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
327 CMutableTransaction t1;
328 t1.vin.resize(3);
329 t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
330 t1.vin[0].prevout.n = 1;
331 t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
332 t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
333 t1.vin[1].prevout.n = 0;
334 t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
335 t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
336 t1.vin[2].prevout.n = 1;
337 t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
338 t1.vout.resize(2);
339 t1.vout[0].nValue = 90*CENT;
340 t1.vout[0].scriptPubKey << OP_1;
342 BOOST_CHECK(AreInputsStandard(t1, coins));
343 BOOST_CHECK_EQUAL(coins.GetValueIn(t1), (50+21+22)*CENT);
346 void CreateCreditAndSpend(const CKeyStore& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)
348 CMutableTransaction outputm;
349 outputm.nVersion = 1;
350 outputm.vin.resize(1);
351 outputm.vin[0].prevout.SetNull();
352 outputm.vin[0].scriptSig = CScript();
353 outputm.vout.resize(1);
354 outputm.vout[0].nValue = 1;
355 outputm.vout[0].scriptPubKey = outscript;
356 CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
357 ssout << outputm;
358 ssout >> output;
359 assert(output->vin.size() == 1);
360 assert(output->vin[0] == outputm.vin[0]);
361 assert(output->vout.size() == 1);
362 assert(output->vout[0] == outputm.vout[0]);
364 CMutableTransaction inputm;
365 inputm.nVersion = 1;
366 inputm.vin.resize(1);
367 inputm.vin[0].prevout.hash = output->GetHash();
368 inputm.vin[0].prevout.n = 0;
369 inputm.vout.resize(1);
370 inputm.vout[0].nValue = 1;
371 inputm.vout[0].scriptPubKey = CScript();
372 bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL);
373 assert(ret == success);
374 CDataStream ssin(SER_NETWORK, PROTOCOL_VERSION);
375 ssin << inputm;
376 ssin >> input;
377 assert(input.vin.size() == 1);
378 assert(input.vin[0] == inputm.vin[0]);
379 assert(input.vout.size() == 1);
380 assert(input.vout[0] == inputm.vout[0]);
381 assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);
384 void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, int flags, bool success)
386 ScriptError error;
387 CTransaction inputi(input);
388 bool ret = VerifyScript(inputi.vin[0].scriptSig, output->vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output->vout[0].nValue), &error);
389 assert(ret == success);
392 static CScript PushAll(const std::vector<valtype>& values)
394 CScript result;
395 for (const valtype& v : values) {
396 if (v.size() == 0) {
397 result << OP_0;
398 } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
399 result << CScript::EncodeOP_N(v[0]);
400 } else {
401 result << v;
404 return result;
407 void ReplaceRedeemScript(CScript& script, const CScript& redeemScript)
409 std::vector<valtype> stack;
410 EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SIGVERSION_BASE);
411 assert(stack.size() > 0);
412 stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());
413 script = PushAll(stack);
416 BOOST_AUTO_TEST_CASE(test_big_witness_transaction) {
417 CMutableTransaction mtx;
418 mtx.nVersion = 1;
420 CKey key;
421 key.MakeNewKey(true); // Need to use compressed keys in segwit or the signing will fail
422 CBasicKeyStore keystore;
423 keystore.AddKeyPubKey(key, key.GetPubKey());
424 CKeyID hash = key.GetPubKey().GetID();
425 CScript scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(hash.begin(), hash.end());
427 std::vector<int> sigHashes;
428 sigHashes.push_back(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
429 sigHashes.push_back(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);
430 sigHashes.push_back(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
431 sigHashes.push_back(SIGHASH_NONE);
432 sigHashes.push_back(SIGHASH_SINGLE);
433 sigHashes.push_back(SIGHASH_ALL);
435 // create a big transaction of 4500 inputs signed by the same key
436 for(uint32_t ij = 0; ij < 4500; ij++) {
437 uint32_t i = mtx.vin.size();
438 uint256 prevId;
439 prevId.SetHex("0000000000000000000000000000000000000000000000000000000000000100");
440 COutPoint outpoint(prevId, i);
442 mtx.vin.resize(mtx.vin.size() + 1);
443 mtx.vin[i].prevout = outpoint;
444 mtx.vin[i].scriptSig = CScript();
446 mtx.vout.resize(mtx.vout.size() + 1);
447 mtx.vout[i].nValue = 1000;
448 mtx.vout[i].scriptPubKey = CScript() << OP_1;
451 // sign all inputs
452 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
453 bool hashSigned = SignSignature(keystore, scriptPubKey, mtx, i, 1000, sigHashes.at(i % sigHashes.size()));
454 assert(hashSigned);
457 CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
458 auto vstream = WithOrVersion(&ssout, 0);
459 vstream << mtx;
460 CTransaction tx(deserialize, vstream);
462 // check all inputs concurrently, with the cache
463 PrecomputedTransactionData txdata(tx);
464 boost::thread_group threadGroup;
465 CCheckQueue<CScriptCheck> scriptcheckqueue(128);
466 CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
468 for (int i=0; i<20; i++)
469 threadGroup.create_thread(boost::bind(&CCheckQueue<CScriptCheck>::Thread, boost::ref(scriptcheckqueue)));
471 std::vector<Coin> coins;
472 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
473 Coin coin;
474 coin.nHeight = 1;
475 coin.fCoinBase = false;
476 coin.out.nValue = 1000;
477 coin.out.scriptPubKey = scriptPubKey;
478 coins.emplace_back(std::move(coin));
481 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
482 std::vector<CScriptCheck> vChecks;
483 CScriptCheck check(coins[tx.vin[i].prevout.n].out, tx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false, &txdata);
484 vChecks.push_back(CScriptCheck());
485 check.swap(vChecks.back());
486 control.Add(vChecks);
489 bool controlCheck = control.Wait();
490 assert(controlCheck);
492 threadGroup.interrupt_all();
493 threadGroup.join_all();
496 BOOST_AUTO_TEST_CASE(test_witness)
498 CBasicKeyStore keystore, keystore2;
499 CKey key1, key2, key3, key1L, key2L;
500 CPubKey pubkey1, pubkey2, pubkey3, pubkey1L, pubkey2L;
501 key1.MakeNewKey(true);
502 key2.MakeNewKey(true);
503 key3.MakeNewKey(true);
504 key1L.MakeNewKey(false);
505 key2L.MakeNewKey(false);
506 pubkey1 = key1.GetPubKey();
507 pubkey2 = key2.GetPubKey();
508 pubkey3 = key3.GetPubKey();
509 pubkey1L = key1L.GetPubKey();
510 pubkey2L = key2L.GetPubKey();
511 keystore.AddKeyPubKey(key1, pubkey1);
512 keystore.AddKeyPubKey(key2, pubkey2);
513 keystore.AddKeyPubKey(key1L, pubkey1L);
514 keystore.AddKeyPubKey(key2L, pubkey2L);
515 CScript scriptPubkey1, scriptPubkey2, scriptPubkey1L, scriptPubkey2L, scriptMulti;
516 scriptPubkey1 << ToByteVector(pubkey1) << OP_CHECKSIG;
517 scriptPubkey2 << ToByteVector(pubkey2) << OP_CHECKSIG;
518 scriptPubkey1L << ToByteVector(pubkey1L) << OP_CHECKSIG;
519 scriptPubkey2L << ToByteVector(pubkey2L) << OP_CHECKSIG;
520 std::vector<CPubKey> oneandthree;
521 oneandthree.push_back(pubkey1);
522 oneandthree.push_back(pubkey3);
523 scriptMulti = GetScriptForMultisig(2, oneandthree);
524 keystore.AddCScript(scriptPubkey1);
525 keystore.AddCScript(scriptPubkey2);
526 keystore.AddCScript(scriptPubkey1L);
527 keystore.AddCScript(scriptPubkey2L);
528 keystore.AddCScript(scriptMulti);
529 keystore.AddCScript(GetScriptForWitness(scriptPubkey1));
530 keystore.AddCScript(GetScriptForWitness(scriptPubkey2));
531 keystore.AddCScript(GetScriptForWitness(scriptPubkey1L));
532 keystore.AddCScript(GetScriptForWitness(scriptPubkey2L));
533 keystore.AddCScript(GetScriptForWitness(scriptMulti));
534 keystore2.AddCScript(scriptMulti);
535 keystore2.AddCScript(GetScriptForWitness(scriptMulti));
536 keystore2.AddKeyPubKey(key3, pubkey3);
538 CTransactionRef output1, output2;
539 CMutableTransaction input1, input2;
540 SignatureData sigdata;
542 // Normal pay-to-compressed-pubkey.
543 CreateCreditAndSpend(keystore, scriptPubkey1, output1, input1);
544 CreateCreditAndSpend(keystore, scriptPubkey2, output2, input2);
545 CheckWithFlag(output1, input1, 0, true);
546 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
547 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
548 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
549 CheckWithFlag(output1, input2, 0, false);
550 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
551 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
552 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
554 // P2SH pay-to-compressed-pubkey.
555 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey1)), output1, input1);
556 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey2)), output2, input2);
557 ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1);
558 CheckWithFlag(output1, input1, 0, true);
559 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
560 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
561 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
562 CheckWithFlag(output1, input2, 0, true);
563 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
564 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
565 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
567 // Witness pay-to-compressed-pubkey (v0).
568 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey1), output1, input1);
569 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey2), output2, input2);
570 CheckWithFlag(output1, input1, 0, true);
571 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
572 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
573 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
574 CheckWithFlag(output1, input2, 0, true);
575 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
576 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
577 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
579 // P2SH witness pay-to-compressed-pubkey (v0).
580 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1))), output1, input1);
581 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2))), output2, input2);
582 ReplaceRedeemScript(input2.vin[0].scriptSig, GetScriptForWitness(scriptPubkey1));
583 CheckWithFlag(output1, input1, 0, true);
584 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
585 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
586 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
587 CheckWithFlag(output1, input2, 0, true);
588 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
589 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
590 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
592 // Normal pay-to-uncompressed-pubkey.
593 CreateCreditAndSpend(keystore, scriptPubkey1L, output1, input1);
594 CreateCreditAndSpend(keystore, scriptPubkey2L, output2, input2);
595 CheckWithFlag(output1, input1, 0, true);
596 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
597 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
598 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
599 CheckWithFlag(output1, input2, 0, false);
600 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
601 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
602 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
604 // P2SH pay-to-uncompressed-pubkey.
605 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey1L)), output1, input1);
606 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey2L)), output2, input2);
607 ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1L);
608 CheckWithFlag(output1, input1, 0, true);
609 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
610 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
611 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
612 CheckWithFlag(output1, input2, 0, true);
613 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
614 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
615 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
617 // Signing disabled for witness pay-to-uncompressed-pubkey (v1).
618 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey1L), output1, input1, false);
619 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey2L), output2, input2, false);
621 // Signing disabled for P2SH witness pay-to-uncompressed-pubkey (v1).
622 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1L))), output1, input1, false);
623 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2L))), output2, input2, false);
625 // Normal 2-of-2 multisig
626 CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false);
627 CheckWithFlag(output1, input1, 0, false);
628 CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false);
629 CheckWithFlag(output2, input2, 0, false);
630 BOOST_CHECK(*output1 == *output2);
631 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
632 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
634 // P2SH 2-of-2 multisig
635 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptMulti)), output1, input1, false);
636 CheckWithFlag(output1, input1, 0, true);
637 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false);
638 CreateCreditAndSpend(keystore2, GetScriptForDestination(CScriptID(scriptMulti)), output2, input2, false);
639 CheckWithFlag(output2, input2, 0, true);
640 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false);
641 BOOST_CHECK(*output1 == *output2);
642 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
643 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
644 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
646 // Witness 2-of-2 multisig
647 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptMulti), output1, input1, false);
648 CheckWithFlag(output1, input1, 0, true);
649 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
650 CreateCreditAndSpend(keystore2, GetScriptForWitness(scriptMulti), output2, input2, false);
651 CheckWithFlag(output2, input2, 0, true);
652 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
653 BOOST_CHECK(*output1 == *output2);
654 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
655 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
656 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
658 // P2SH witness 2-of-2 multisig
659 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti))), output1, input1, false);
660 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
661 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
662 CreateCreditAndSpend(keystore2, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti))), output2, input2, false);
663 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, true);
664 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
665 BOOST_CHECK(*output1 == *output2);
666 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
667 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
668 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
671 BOOST_AUTO_TEST_CASE(test_IsStandard)
673 LOCK(cs_main);
674 CBasicKeyStore keystore;
675 CCoinsView coinsDummy;
676 CCoinsViewCache coins(&coinsDummy);
677 std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
679 CMutableTransaction t;
680 t.vin.resize(1);
681 t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
682 t.vin[0].prevout.n = 1;
683 t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
684 t.vout.resize(1);
685 t.vout[0].nValue = 90*CENT;
686 CKey key;
687 key.MakeNewKey(true);
688 t.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
690 std::string reason;
691 BOOST_CHECK(IsStandardTx(t, reason));
693 // Check dust with default relay fee:
694 CAmount nDustThreshold = 182 * dustRelayFee.GetFeePerK()/1000;
695 BOOST_CHECK_EQUAL(nDustThreshold, 546);
696 // dust:
697 t.vout[0].nValue = nDustThreshold - 1;
698 BOOST_CHECK(!IsStandardTx(t, reason));
699 // not dust:
700 t.vout[0].nValue = nDustThreshold;
701 BOOST_CHECK(IsStandardTx(t, reason));
703 // Check dust with odd relay fee to verify rounding:
704 // nDustThreshold = 182 * 3702 / 1000
705 dustRelayFee = CFeeRate(3702);
706 // dust:
707 t.vout[0].nValue = 673 - 1;
708 BOOST_CHECK(!IsStandardTx(t, reason));
709 // not dust:
710 t.vout[0].nValue = 673;
711 BOOST_CHECK(IsStandardTx(t, reason));
712 dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
714 t.vout[0].scriptPubKey = CScript() << OP_1;
715 BOOST_CHECK(!IsStandardTx(t, reason));
717 // MAX_OP_RETURN_RELAY-byte TX_NULL_DATA (standard)
718 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
719 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY, t.vout[0].scriptPubKey.size());
720 BOOST_CHECK(IsStandardTx(t, reason));
722 // MAX_OP_RETURN_RELAY+1-byte TX_NULL_DATA (non-standard)
723 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
724 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size());
725 BOOST_CHECK(!IsStandardTx(t, reason));
727 // Data payload can be encoded in any way...
728 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
729 BOOST_CHECK(IsStandardTx(t, reason));
730 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("00") << ParseHex("01");
731 BOOST_CHECK(IsStandardTx(t, reason));
732 // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
733 t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 << ParseHex("01") << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16;
734 BOOST_CHECK(IsStandardTx(t, reason));
735 t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << ParseHex("01") << 2 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
736 BOOST_CHECK(IsStandardTx(t, reason));
738 // ...so long as it only contains PUSHDATA's
739 t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RETURN;
740 BOOST_CHECK(!IsStandardTx(t, reason));
742 // TX_NULL_DATA w/o PUSHDATA
743 t.vout.resize(1);
744 t.vout[0].scriptPubKey = CScript() << OP_RETURN;
745 BOOST_CHECK(IsStandardTx(t, reason));
747 // Only one TX_NULL_DATA permitted in all cases
748 t.vout.resize(2);
749 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
750 t.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
751 BOOST_CHECK(!IsStandardTx(t, reason));
753 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
754 t.vout[1].scriptPubKey = CScript() << OP_RETURN;
755 BOOST_CHECK(!IsStandardTx(t, reason));
757 t.vout[0].scriptPubKey = CScript() << OP_RETURN;
758 t.vout[1].scriptPubKey = CScript() << OP_RETURN;
759 BOOST_CHECK(!IsStandardTx(t, reason));
762 BOOST_AUTO_TEST_SUITE_END()