Merge #10502: scripted-diff: Remove BOOST_FOREACH, Q_FOREACH and PAIRTYPE
[bitcoinplatinum.git] / src / test / transaction_tests.cpp
blob778d2fd7426579f127a1c8faedbabd4ebac4d982
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>
30 #include <boost/foreach.hpp>
32 #include <univalue.h>
34 typedef std::vector<unsigned char> valtype;
36 // In script_tests.cpp
37 extern UniValue read_json(const std::string& jsondata);
39 static std::map<std::string, unsigned int> mapFlagNames = {
40 {std::string("NONE"), (unsigned int)SCRIPT_VERIFY_NONE},
41 {std::string("P2SH"), (unsigned int)SCRIPT_VERIFY_P2SH},
42 {std::string("STRICTENC"), (unsigned int)SCRIPT_VERIFY_STRICTENC},
43 {std::string("DERSIG"), (unsigned int)SCRIPT_VERIFY_DERSIG},
44 {std::string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S},
45 {std::string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY},
46 {std::string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA},
47 {std::string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY},
48 {std::string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS},
49 {std::string("CLEANSTACK"), (unsigned int)SCRIPT_VERIFY_CLEANSTACK},
50 {std::string("MINIMALIF"), (unsigned int)SCRIPT_VERIFY_MINIMALIF},
51 {std::string("NULLFAIL"), (unsigned int)SCRIPT_VERIFY_NULLFAIL},
52 {std::string("CHECKLOCKTIMEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY},
53 {std::string("CHECKSEQUENCEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY},
54 {std::string("WITNESS"), (unsigned int)SCRIPT_VERIFY_WITNESS},
55 {std::string("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM},
56 {std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE},
59 unsigned int ParseScriptFlags(std::string strFlags)
61 if (strFlags.empty()) {
62 return 0;
64 unsigned int flags = 0;
65 std::vector<std::string> words;
66 boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(","));
68 for (std::string word : words)
70 if (!mapFlagNames.count(word))
71 BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
72 flags |= mapFlagNames[word];
75 return flags;
78 std::string FormatScriptFlags(unsigned int flags)
80 if (flags == 0) {
81 return "";
83 std::string ret;
84 std::map<std::string, unsigned int>::const_iterator it = mapFlagNames.begin();
85 while (it != mapFlagNames.end()) {
86 if (flags & it->second) {
87 ret += it->first + ",";
89 it++;
91 return ret.substr(0, ret.size() - 1);
94 BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
96 BOOST_AUTO_TEST_CASE(tx_valid)
98 // Read tests from test/data/tx_valid.json
99 // Format is an array of arrays
100 // Inner arrays are either [ "comment" ]
101 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
102 // ... where all scripts are stringified scripts.
104 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
105 UniValue tests = read_json(std::string(json_tests::tx_valid, json_tests::tx_valid + sizeof(json_tests::tx_valid)));
107 ScriptError err;
108 for (unsigned int idx = 0; idx < tests.size(); idx++) {
109 UniValue test = tests[idx];
110 std::string strTest = test.write();
111 if (test[0].isArray())
113 if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
115 BOOST_ERROR("Bad test: " << strTest);
116 continue;
119 std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
120 std::map<COutPoint, int64_t> mapprevOutValues;
121 UniValue inputs = test[0].get_array();
122 bool fValid = true;
123 for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
124 const UniValue& input = inputs[inpIdx];
125 if (!input.isArray())
127 fValid = false;
128 break;
130 UniValue vinput = input.get_array();
131 if (vinput.size() < 3 || vinput.size() > 4)
133 fValid = false;
134 break;
136 COutPoint outpoint(uint256S(vinput[0].get_str()), vinput[1].get_int());
137 mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
138 if (vinput.size() >= 4)
140 mapprevOutValues[outpoint] = vinput[3].get_int64();
143 if (!fValid)
145 BOOST_ERROR("Bad test: " << strTest);
146 continue;
149 std::string transaction = test[1].get_str();
150 CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION);
151 CTransaction tx(deserialize, stream);
153 CValidationState state;
154 BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
155 BOOST_CHECK(state.IsValid());
157 PrecomputedTransactionData txdata(tx);
158 for (unsigned int i = 0; i < tx.vin.size(); i++)
160 if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
162 BOOST_ERROR("Bad test: " << strTest);
163 break;
166 CAmount amount = 0;
167 if (mapprevOutValues.count(tx.vin[i].prevout)) {
168 amount = mapprevOutValues[tx.vin[i].prevout];
170 unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
171 const CScriptWitness *witness = &tx.vin[i].scriptWitness;
172 BOOST_CHECK_MESSAGE(VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout],
173 witness, verify_flags, TransactionSignatureChecker(&tx, i, amount, txdata), &err),
174 strTest);
175 BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
181 BOOST_AUTO_TEST_CASE(tx_invalid)
183 // Read tests from test/data/tx_invalid.json
184 // Format is an array of arrays
185 // Inner arrays are either [ "comment" ]
186 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
187 // ... where all scripts are stringified scripts.
189 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
190 UniValue tests = read_json(std::string(json_tests::tx_invalid, json_tests::tx_invalid + sizeof(json_tests::tx_invalid)));
192 // Initialize to SCRIPT_ERR_OK. The tests expect err to be changed to a
193 // value other than SCRIPT_ERR_OK.
194 ScriptError err = SCRIPT_ERR_OK;
195 for (unsigned int idx = 0; idx < tests.size(); idx++) {
196 UniValue test = tests[idx];
197 std::string strTest = test.write();
198 if (test[0].isArray())
200 if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
202 BOOST_ERROR("Bad test: " << strTest);
203 continue;
206 std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
207 std::map<COutPoint, int64_t> mapprevOutValues;
208 UniValue inputs = test[0].get_array();
209 bool fValid = true;
210 for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
211 const UniValue& input = inputs[inpIdx];
212 if (!input.isArray())
214 fValid = false;
215 break;
217 UniValue vinput = input.get_array();
218 if (vinput.size() < 3 || vinput.size() > 4)
220 fValid = false;
221 break;
223 COutPoint outpoint(uint256S(vinput[0].get_str()), vinput[1].get_int());
224 mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
225 if (vinput.size() >= 4)
227 mapprevOutValues[outpoint] = vinput[3].get_int64();
230 if (!fValid)
232 BOOST_ERROR("Bad test: " << strTest);
233 continue;
236 std::string transaction = test[1].get_str();
237 CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION );
238 CTransaction tx(deserialize, stream);
240 CValidationState state;
241 fValid = CheckTransaction(tx, state) && state.IsValid();
243 PrecomputedTransactionData txdata(tx);
244 for (unsigned int i = 0; i < tx.vin.size() && fValid; i++)
246 if (!mapprevOutScriptPubKeys.count(tx.vin[i].prevout))
248 BOOST_ERROR("Bad test: " << strTest);
249 break;
252 unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
253 CAmount amount = 0;
254 if (mapprevOutValues.count(tx.vin[i].prevout)) {
255 amount = mapprevOutValues[tx.vin[i].prevout];
257 const CScriptWitness *witness = &tx.vin[i].scriptWitness;
258 fValid = VerifyScript(tx.vin[i].scriptSig, mapprevOutScriptPubKeys[tx.vin[i].prevout],
259 witness, verify_flags, TransactionSignatureChecker(&tx, i, amount, txdata), &err);
261 BOOST_CHECK_MESSAGE(!fValid, strTest);
262 BOOST_CHECK_MESSAGE(err != SCRIPT_ERR_OK, ScriptErrorString(err));
267 BOOST_AUTO_TEST_CASE(basic_transaction_tests)
269 // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
270 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};
271 std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
272 CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
273 CMutableTransaction tx;
274 stream >> tx;
275 CValidationState state;
276 BOOST_CHECK_MESSAGE(CheckTransaction(tx, state) && state.IsValid(), "Simple deserialized transaction should be valid.");
278 // Check that duplicate txins fail
279 tx.vin.push_back(tx.vin[0]);
280 BOOST_CHECK_MESSAGE(!CheckTransaction(tx, state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
284 // Helper: create two dummy transactions, each with
285 // two outputs. The first has 11 and 50 CENT outputs
286 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
287 // paid to a TX_PUBKEYHASH.
289 static std::vector<CMutableTransaction>
290 SetupDummyInputs(CBasicKeyStore& keystoreRet, CCoinsViewCache& coinsRet)
292 std::vector<CMutableTransaction> dummyTransactions;
293 dummyTransactions.resize(2);
295 // Add some keys to the keystore:
296 CKey key[4];
297 for (int i = 0; i < 4; i++)
299 key[i].MakeNewKey(i % 2);
300 keystoreRet.AddKey(key[i]);
303 // Create some dummy input transactions
304 dummyTransactions[0].vout.resize(2);
305 dummyTransactions[0].vout[0].nValue = 11*CENT;
306 dummyTransactions[0].vout[0].scriptPubKey << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
307 dummyTransactions[0].vout[1].nValue = 50*CENT;
308 dummyTransactions[0].vout[1].scriptPubKey << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
309 AddCoins(coinsRet, dummyTransactions[0], 0);
311 dummyTransactions[1].vout.resize(2);
312 dummyTransactions[1].vout[0].nValue = 21*CENT;
313 dummyTransactions[1].vout[0].scriptPubKey = GetScriptForDestination(key[2].GetPubKey().GetID());
314 dummyTransactions[1].vout[1].nValue = 22*CENT;
315 dummyTransactions[1].vout[1].scriptPubKey = GetScriptForDestination(key[3].GetPubKey().GetID());
316 AddCoins(coinsRet, dummyTransactions[1], 0);
318 return dummyTransactions;
321 BOOST_AUTO_TEST_CASE(test_Get)
323 CBasicKeyStore keystore;
324 CCoinsView coinsDummy;
325 CCoinsViewCache coins(&coinsDummy);
326 std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
328 CMutableTransaction t1;
329 t1.vin.resize(3);
330 t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
331 t1.vin[0].prevout.n = 1;
332 t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
333 t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
334 t1.vin[1].prevout.n = 0;
335 t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
336 t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
337 t1.vin[2].prevout.n = 1;
338 t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
339 t1.vout.resize(2);
340 t1.vout[0].nValue = 90*CENT;
341 t1.vout[0].scriptPubKey << OP_1;
343 BOOST_CHECK(AreInputsStandard(t1, coins));
344 BOOST_CHECK_EQUAL(coins.GetValueIn(t1), (50+21+22)*CENT);
347 void CreateCreditAndSpend(const CKeyStore& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)
349 CMutableTransaction outputm;
350 outputm.nVersion = 1;
351 outputm.vin.resize(1);
352 outputm.vin[0].prevout.SetNull();
353 outputm.vin[0].scriptSig = CScript();
354 outputm.vout.resize(1);
355 outputm.vout[0].nValue = 1;
356 outputm.vout[0].scriptPubKey = outscript;
357 CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
358 ssout << outputm;
359 ssout >> output;
360 assert(output->vin.size() == 1);
361 assert(output->vin[0] == outputm.vin[0]);
362 assert(output->vout.size() == 1);
363 assert(output->vout[0] == outputm.vout[0]);
365 CMutableTransaction inputm;
366 inputm.nVersion = 1;
367 inputm.vin.resize(1);
368 inputm.vin[0].prevout.hash = output->GetHash();
369 inputm.vin[0].prevout.n = 0;
370 inputm.vout.resize(1);
371 inputm.vout[0].nValue = 1;
372 inputm.vout[0].scriptPubKey = CScript();
373 bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL);
374 assert(ret == success);
375 CDataStream ssin(SER_NETWORK, PROTOCOL_VERSION);
376 ssin << inputm;
377 ssin >> input;
378 assert(input.vin.size() == 1);
379 assert(input.vin[0] == inputm.vin[0]);
380 assert(input.vout.size() == 1);
381 assert(input.vout[0] == inputm.vout[0]);
382 assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);
385 void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, int flags, bool success)
387 ScriptError error;
388 CTransaction inputi(input);
389 bool ret = VerifyScript(inputi.vin[0].scriptSig, output->vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output->vout[0].nValue), &error);
390 assert(ret == success);
393 static CScript PushAll(const std::vector<valtype>& values)
395 CScript result;
396 for (const valtype& v : values) {
397 if (v.size() == 0) {
398 result << OP_0;
399 } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
400 result << CScript::EncodeOP_N(v[0]);
401 } else {
402 result << v;
405 return result;
408 void ReplaceRedeemScript(CScript& script, const CScript& redeemScript)
410 std::vector<valtype> stack;
411 EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SIGVERSION_BASE);
412 assert(stack.size() > 0);
413 stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());
414 script = PushAll(stack);
417 BOOST_AUTO_TEST_CASE(test_big_witness_transaction) {
418 CMutableTransaction mtx;
419 mtx.nVersion = 1;
421 CKey key;
422 key.MakeNewKey(true); // Need to use compressed keys in segwit or the signing will fail
423 CBasicKeyStore keystore;
424 keystore.AddKeyPubKey(key, key.GetPubKey());
425 CKeyID hash = key.GetPubKey().GetID();
426 CScript scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(hash.begin(), hash.end());
428 std::vector<int> sigHashes;
429 sigHashes.push_back(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
430 sigHashes.push_back(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);
431 sigHashes.push_back(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
432 sigHashes.push_back(SIGHASH_NONE);
433 sigHashes.push_back(SIGHASH_SINGLE);
434 sigHashes.push_back(SIGHASH_ALL);
436 // create a big transaction of 4500 inputs signed by the same key
437 for(uint32_t ij = 0; ij < 4500; ij++) {
438 uint32_t i = mtx.vin.size();
439 uint256 prevId;
440 prevId.SetHex("0000000000000000000000000000000000000000000000000000000000000100");
441 COutPoint outpoint(prevId, i);
443 mtx.vin.resize(mtx.vin.size() + 1);
444 mtx.vin[i].prevout = outpoint;
445 mtx.vin[i].scriptSig = CScript();
447 mtx.vout.resize(mtx.vout.size() + 1);
448 mtx.vout[i].nValue = 1000;
449 mtx.vout[i].scriptPubKey = CScript() << OP_1;
452 // sign all inputs
453 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
454 bool hashSigned = SignSignature(keystore, scriptPubKey, mtx, i, 1000, sigHashes.at(i % sigHashes.size()));
455 assert(hashSigned);
458 CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
459 auto vstream = WithOrVersion(&ssout, 0);
460 vstream << mtx;
461 CTransaction tx(deserialize, vstream);
463 // check all inputs concurrently, with the cache
464 PrecomputedTransactionData txdata(tx);
465 boost::thread_group threadGroup;
466 CCheckQueue<CScriptCheck> scriptcheckqueue(128);
467 CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
469 for (int i=0; i<20; i++)
470 threadGroup.create_thread(boost::bind(&CCheckQueue<CScriptCheck>::Thread, boost::ref(scriptcheckqueue)));
472 std::vector<Coin> coins;
473 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
474 Coin coin;
475 coin.nHeight = 1;
476 coin.fCoinBase = false;
477 coin.out.nValue = 1000;
478 coin.out.scriptPubKey = scriptPubKey;
479 coins.emplace_back(std::move(coin));
482 for(uint32_t i = 0; i < mtx.vin.size(); i++) {
483 std::vector<CScriptCheck> vChecks;
484 const CTxOut& output = coins[tx.vin[i].prevout.n].out;
485 CScriptCheck check(output.scriptPubKey, output.nValue, tx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false, &txdata);
486 vChecks.push_back(CScriptCheck());
487 check.swap(vChecks.back());
488 control.Add(vChecks);
491 bool controlCheck = control.Wait();
492 assert(controlCheck);
494 threadGroup.interrupt_all();
495 threadGroup.join_all();
498 BOOST_AUTO_TEST_CASE(test_witness)
500 CBasicKeyStore keystore, keystore2;
501 CKey key1, key2, key3, key1L, key2L;
502 CPubKey pubkey1, pubkey2, pubkey3, pubkey1L, pubkey2L;
503 key1.MakeNewKey(true);
504 key2.MakeNewKey(true);
505 key3.MakeNewKey(true);
506 key1L.MakeNewKey(false);
507 key2L.MakeNewKey(false);
508 pubkey1 = key1.GetPubKey();
509 pubkey2 = key2.GetPubKey();
510 pubkey3 = key3.GetPubKey();
511 pubkey1L = key1L.GetPubKey();
512 pubkey2L = key2L.GetPubKey();
513 keystore.AddKeyPubKey(key1, pubkey1);
514 keystore.AddKeyPubKey(key2, pubkey2);
515 keystore.AddKeyPubKey(key1L, pubkey1L);
516 keystore.AddKeyPubKey(key2L, pubkey2L);
517 CScript scriptPubkey1, scriptPubkey2, scriptPubkey1L, scriptPubkey2L, scriptMulti;
518 scriptPubkey1 << ToByteVector(pubkey1) << OP_CHECKSIG;
519 scriptPubkey2 << ToByteVector(pubkey2) << OP_CHECKSIG;
520 scriptPubkey1L << ToByteVector(pubkey1L) << OP_CHECKSIG;
521 scriptPubkey2L << ToByteVector(pubkey2L) << OP_CHECKSIG;
522 std::vector<CPubKey> oneandthree;
523 oneandthree.push_back(pubkey1);
524 oneandthree.push_back(pubkey3);
525 scriptMulti = GetScriptForMultisig(2, oneandthree);
526 keystore.AddCScript(scriptPubkey1);
527 keystore.AddCScript(scriptPubkey2);
528 keystore.AddCScript(scriptPubkey1L);
529 keystore.AddCScript(scriptPubkey2L);
530 keystore.AddCScript(scriptMulti);
531 keystore.AddCScript(GetScriptForWitness(scriptPubkey1));
532 keystore.AddCScript(GetScriptForWitness(scriptPubkey2));
533 keystore.AddCScript(GetScriptForWitness(scriptPubkey1L));
534 keystore.AddCScript(GetScriptForWitness(scriptPubkey2L));
535 keystore.AddCScript(GetScriptForWitness(scriptMulti));
536 keystore2.AddCScript(scriptMulti);
537 keystore2.AddCScript(GetScriptForWitness(scriptMulti));
538 keystore2.AddKeyPubKey(key3, pubkey3);
540 CTransactionRef output1, output2;
541 CMutableTransaction input1, input2;
542 SignatureData sigdata;
544 // Normal pay-to-compressed-pubkey.
545 CreateCreditAndSpend(keystore, scriptPubkey1, output1, input1);
546 CreateCreditAndSpend(keystore, scriptPubkey2, output2, input2);
547 CheckWithFlag(output1, input1, 0, true);
548 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
549 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
550 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
551 CheckWithFlag(output1, input2, 0, false);
552 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
553 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
554 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
556 // P2SH pay-to-compressed-pubkey.
557 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey1)), output1, input1);
558 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey2)), output2, input2);
559 ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1);
560 CheckWithFlag(output1, input1, 0, true);
561 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
562 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
563 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
564 CheckWithFlag(output1, input2, 0, true);
565 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
566 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
567 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
569 // Witness pay-to-compressed-pubkey (v0).
570 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey1), output1, input1);
571 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey2), output2, input2);
572 CheckWithFlag(output1, input1, 0, true);
573 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
574 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
575 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
576 CheckWithFlag(output1, input2, 0, true);
577 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
578 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
579 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
581 // P2SH witness pay-to-compressed-pubkey (v0).
582 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1))), output1, input1);
583 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2))), output2, input2);
584 ReplaceRedeemScript(input2.vin[0].scriptSig, GetScriptForWitness(scriptPubkey1));
585 CheckWithFlag(output1, input1, 0, true);
586 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
587 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
588 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
589 CheckWithFlag(output1, input2, 0, true);
590 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
591 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
592 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
594 // Normal pay-to-uncompressed-pubkey.
595 CreateCreditAndSpend(keystore, scriptPubkey1L, output1, input1);
596 CreateCreditAndSpend(keystore, scriptPubkey2L, output2, input2);
597 CheckWithFlag(output1, input1, 0, true);
598 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
599 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
600 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
601 CheckWithFlag(output1, input2, 0, false);
602 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
603 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
604 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
606 // P2SH pay-to-uncompressed-pubkey.
607 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey1L)), output1, input1);
608 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptPubkey2L)), output2, input2);
609 ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1L);
610 CheckWithFlag(output1, input1, 0, true);
611 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
612 CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
613 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
614 CheckWithFlag(output1, input2, 0, true);
615 CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
616 CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
617 CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
619 // Signing disabled for witness pay-to-uncompressed-pubkey (v1).
620 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey1L), output1, input1, false);
621 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptPubkey2L), output2, input2, false);
623 // Signing disabled for P2SH witness pay-to-uncompressed-pubkey (v1).
624 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1L))), output1, input1, false);
625 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2L))), output2, input2, false);
627 // Normal 2-of-2 multisig
628 CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false);
629 CheckWithFlag(output1, input1, 0, false);
630 CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false);
631 CheckWithFlag(output2, input2, 0, false);
632 BOOST_CHECK(*output1 == *output2);
633 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
634 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
636 // P2SH 2-of-2 multisig
637 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(scriptMulti)), output1, input1, false);
638 CheckWithFlag(output1, input1, 0, true);
639 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false);
640 CreateCreditAndSpend(keystore2, GetScriptForDestination(CScriptID(scriptMulti)), output2, input2, false);
641 CheckWithFlag(output2, input2, 0, true);
642 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false);
643 BOOST_CHECK(*output1 == *output2);
644 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
645 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
646 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
648 // Witness 2-of-2 multisig
649 CreateCreditAndSpend(keystore, GetScriptForWitness(scriptMulti), output1, input1, false);
650 CheckWithFlag(output1, input1, 0, true);
651 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
652 CreateCreditAndSpend(keystore2, GetScriptForWitness(scriptMulti), output2, input2, false);
653 CheckWithFlag(output2, input2, 0, true);
654 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
655 BOOST_CHECK(*output1 == *output2);
656 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
657 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
658 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
660 // P2SH witness 2-of-2 multisig
661 CreateCreditAndSpend(keystore, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti))), output1, input1, false);
662 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
663 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
664 CreateCreditAndSpend(keystore2, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti))), output2, input2, false);
665 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, true);
666 CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
667 BOOST_CHECK(*output1 == *output2);
668 UpdateTransaction(input1, 0, CombineSignatures(output1->vout[0].scriptPubKey, MutableTransactionSignatureChecker(&input1, 0, output1->vout[0].nValue), DataFromTransaction(input1, 0), DataFromTransaction(input2, 0)));
669 CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
670 CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
673 BOOST_AUTO_TEST_CASE(test_IsStandard)
675 LOCK(cs_main);
676 CBasicKeyStore keystore;
677 CCoinsView coinsDummy;
678 CCoinsViewCache coins(&coinsDummy);
679 std::vector<CMutableTransaction> dummyTransactions = SetupDummyInputs(keystore, coins);
681 CMutableTransaction t;
682 t.vin.resize(1);
683 t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
684 t.vin[0].prevout.n = 1;
685 t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
686 t.vout.resize(1);
687 t.vout[0].nValue = 90*CENT;
688 CKey key;
689 key.MakeNewKey(true);
690 t.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
692 std::string reason;
693 BOOST_CHECK(IsStandardTx(t, reason));
695 // Check dust with default relay fee:
696 CAmount nDustThreshold = 182 * dustRelayFee.GetFeePerK()/1000 * 3;
697 BOOST_CHECK_EQUAL(nDustThreshold, 546);
698 // dust:
699 t.vout[0].nValue = nDustThreshold - 1;
700 BOOST_CHECK(!IsStandardTx(t, reason));
701 // not dust:
702 t.vout[0].nValue = nDustThreshold;
703 BOOST_CHECK(IsStandardTx(t, reason));
705 // Check dust with odd relay fee to verify rounding:
706 // nDustThreshold = 182 * 1234 / 1000 * 3
707 dustRelayFee = CFeeRate(1234);
708 // dust:
709 t.vout[0].nValue = 672 - 1;
710 BOOST_CHECK(!IsStandardTx(t, reason));
711 // not dust:
712 t.vout[0].nValue = 672;
713 BOOST_CHECK(IsStandardTx(t, reason));
714 dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
716 t.vout[0].scriptPubKey = CScript() << OP_1;
717 BOOST_CHECK(!IsStandardTx(t, reason));
719 // MAX_OP_RETURN_RELAY-byte TX_NULL_DATA (standard)
720 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
721 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY, t.vout[0].scriptPubKey.size());
722 BOOST_CHECK(IsStandardTx(t, reason));
724 // MAX_OP_RETURN_RELAY+1-byte TX_NULL_DATA (non-standard)
725 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
726 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size());
727 BOOST_CHECK(!IsStandardTx(t, reason));
729 // Data payload can be encoded in any way...
730 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
731 BOOST_CHECK(IsStandardTx(t, reason));
732 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("00") << ParseHex("01");
733 BOOST_CHECK(IsStandardTx(t, reason));
734 // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
735 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;
736 BOOST_CHECK(IsStandardTx(t, reason));
737 t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << ParseHex("01") << 2 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
738 BOOST_CHECK(IsStandardTx(t, reason));
740 // ...so long as it only contains PUSHDATA's
741 t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RETURN;
742 BOOST_CHECK(!IsStandardTx(t, reason));
744 // TX_NULL_DATA w/o PUSHDATA
745 t.vout.resize(1);
746 t.vout[0].scriptPubKey = CScript() << OP_RETURN;
747 BOOST_CHECK(IsStandardTx(t, reason));
749 // Only one TX_NULL_DATA permitted in all cases
750 t.vout.resize(2);
751 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
752 t.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
753 BOOST_CHECK(!IsStandardTx(t, reason));
755 t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
756 t.vout[1].scriptPubKey = CScript() << OP_RETURN;
757 BOOST_CHECK(!IsStandardTx(t, reason));
759 t.vout[0].scriptPubKey = CScript() << OP_RETURN;
760 t.vout[1].scriptPubKey = CScript() << OP_RETURN;
761 BOOST_CHECK(!IsStandardTx(t, reason));
764 BOOST_AUTO_TEST_SUITE_END()