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"
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"
27 #include <boost/algorithm/string/classification.hpp>
28 #include <boost/algorithm/string/split.hpp>
29 #include <boost/assign/list_of.hpp>
30 #include <boost/test/unit_test.hpp>
31 #include <boost/assign/list_of.hpp>
32 #include <boost/foreach.hpp>
36 typedef std::vector
<unsigned char> valtype
;
38 // In script_tests.cpp
39 extern UniValue
read_json(const std::string
& jsondata
);
41 static std::map
<std::string
, unsigned int> mapFlagNames
= boost::assign::map_list_of
42 (std::string("NONE"), (unsigned int)SCRIPT_VERIFY_NONE
)
43 (std::string("P2SH"), (unsigned int)SCRIPT_VERIFY_P2SH
)
44 (std::string("STRICTENC"), (unsigned int)SCRIPT_VERIFY_STRICTENC
)
45 (std::string("DERSIG"), (unsigned int)SCRIPT_VERIFY_DERSIG
)
46 (std::string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S
)
47 (std::string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY
)
48 (std::string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA
)
49 (std::string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY
)
50 (std::string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
)
51 (std::string("CLEANSTACK"), (unsigned int)SCRIPT_VERIFY_CLEANSTACK
)
52 (std::string("MINIMALIF"), (unsigned int)SCRIPT_VERIFY_MINIMALIF
)
53 (std::string("NULLFAIL"), (unsigned int)SCRIPT_VERIFY_NULLFAIL
)
54 (std::string("CHECKLOCKTIMEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
)
55 (std::string("CHECKSEQUENCEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
)
56 (std::string("WITNESS"), (unsigned int)SCRIPT_VERIFY_WITNESS
)
57 (std::string("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM
)
58 (std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
);
60 unsigned int ParseScriptFlags(std::string strFlags
)
62 if (strFlags
.empty()) {
65 unsigned int flags
= 0;
66 std::vector
<std::string
> words
;
67 boost::algorithm::split(words
, strFlags
, boost::algorithm::is_any_of(","));
69 BOOST_FOREACH(std::string word
, words
)
71 if (!mapFlagNames
.count(word
))
72 BOOST_ERROR("Bad test: unknown verification flag '" << word
<< "'");
73 flags
|= mapFlagNames
[word
];
79 std::string
FormatScriptFlags(unsigned int flags
)
85 std::map
<std::string
, unsigned int>::const_iterator it
= mapFlagNames
.begin();
86 while (it
!= mapFlagNames
.end()) {
87 if (flags
& it
->second
) {
88 ret
+= it
->first
+ ",";
92 return ret
.substr(0, ret
.size() - 1);
95 BOOST_FIXTURE_TEST_SUITE(transaction_tests
, BasicTestingSetup
)
97 BOOST_AUTO_TEST_CASE(tx_valid
)
99 // Read tests from test/data/tx_valid.json
100 // Format is an array of arrays
101 // Inner arrays are either [ "comment" ]
102 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
103 // ... where all scripts are stringified scripts.
105 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
106 UniValue tests
= read_json(std::string(json_tests::tx_valid
, json_tests::tx_valid
+ sizeof(json_tests::tx_valid
)));
109 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
110 UniValue test
= tests
[idx
];
111 std::string strTest
= test
.write();
112 if (test
[0].isArray())
114 if (test
.size() != 3 || !test
[1].isStr() || !test
[2].isStr())
116 BOOST_ERROR("Bad test: " << strTest
);
120 std::map
<COutPoint
, CScript
> mapprevOutScriptPubKeys
;
121 std::map
<COutPoint
, int64_t> mapprevOutValues
;
122 UniValue inputs
= test
[0].get_array();
124 for (unsigned int inpIdx
= 0; inpIdx
< inputs
.size(); inpIdx
++) {
125 const UniValue
& input
= inputs
[inpIdx
];
126 if (!input
.isArray())
131 UniValue vinput
= input
.get_array();
132 if (vinput
.size() < 3 || vinput
.size() > 4)
137 COutPoint
outpoint(uint256S(vinput
[0].get_str()), vinput
[1].get_int());
138 mapprevOutScriptPubKeys
[outpoint
] = ParseScript(vinput
[2].get_str());
139 if (vinput
.size() >= 4)
141 mapprevOutValues
[outpoint
] = vinput
[3].get_int64();
146 BOOST_ERROR("Bad test: " << strTest
);
150 std::string transaction
= test
[1].get_str();
151 CDataStream
stream(ParseHex(transaction
), SER_NETWORK
, PROTOCOL_VERSION
);
152 CTransaction
tx(deserialize
, stream
);
154 CValidationState state
;
155 BOOST_CHECK_MESSAGE(CheckTransaction(tx
, state
), strTest
);
156 BOOST_CHECK(state
.IsValid());
158 PrecomputedTransactionData
txdata(tx
);
159 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
161 if (!mapprevOutScriptPubKeys
.count(tx
.vin
[i
].prevout
))
163 BOOST_ERROR("Bad test: " << strTest
);
168 if (mapprevOutValues
.count(tx
.vin
[i
].prevout
)) {
169 amount
= mapprevOutValues
[tx
.vin
[i
].prevout
];
171 unsigned int verify_flags
= ParseScriptFlags(test
[2].get_str());
172 const CScriptWitness
*witness
= &tx
.vin
[i
].scriptWitness
;
173 BOOST_CHECK_MESSAGE(VerifyScript(tx
.vin
[i
].scriptSig
, mapprevOutScriptPubKeys
[tx
.vin
[i
].prevout
],
174 witness
, verify_flags
, TransactionSignatureChecker(&tx
, i
, amount
, txdata
), &err
),
176 BOOST_CHECK_MESSAGE(err
== SCRIPT_ERR_OK
, ScriptErrorString(err
));
182 BOOST_AUTO_TEST_CASE(tx_invalid
)
184 // Read tests from test/data/tx_invalid.json
185 // Format is an array of arrays
186 // Inner arrays are either [ "comment" ]
187 // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, verifyFlags
188 // ... where all scripts are stringified scripts.
190 // verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
191 UniValue tests
= read_json(std::string(json_tests::tx_invalid
, json_tests::tx_invalid
+ sizeof(json_tests::tx_invalid
)));
193 // Initialize to SCRIPT_ERR_OK. The tests expect err to be changed to a
194 // value other than SCRIPT_ERR_OK.
195 ScriptError err
= SCRIPT_ERR_OK
;
196 for (unsigned int idx
= 0; idx
< tests
.size(); idx
++) {
197 UniValue test
= tests
[idx
];
198 std::string strTest
= test
.write();
199 if (test
[0].isArray())
201 if (test
.size() != 3 || !test
[1].isStr() || !test
[2].isStr())
203 BOOST_ERROR("Bad test: " << strTest
);
207 std::map
<COutPoint
, CScript
> mapprevOutScriptPubKeys
;
208 std::map
<COutPoint
, int64_t> mapprevOutValues
;
209 UniValue inputs
= test
[0].get_array();
211 for (unsigned int inpIdx
= 0; inpIdx
< inputs
.size(); inpIdx
++) {
212 const UniValue
& input
= inputs
[inpIdx
];
213 if (!input
.isArray())
218 UniValue vinput
= input
.get_array();
219 if (vinput
.size() < 3 || vinput
.size() > 4)
224 COutPoint
outpoint(uint256S(vinput
[0].get_str()), vinput
[1].get_int());
225 mapprevOutScriptPubKeys
[outpoint
] = ParseScript(vinput
[2].get_str());
226 if (vinput
.size() >= 4)
228 mapprevOutValues
[outpoint
] = vinput
[3].get_int64();
233 BOOST_ERROR("Bad test: " << strTest
);
237 std::string transaction
= test
[1].get_str();
238 CDataStream
stream(ParseHex(transaction
), SER_NETWORK
, PROTOCOL_VERSION
);
239 CTransaction
tx(deserialize
, stream
);
241 CValidationState state
;
242 fValid
= CheckTransaction(tx
, state
) && state
.IsValid();
244 PrecomputedTransactionData
txdata(tx
);
245 for (unsigned int i
= 0; i
< tx
.vin
.size() && fValid
; i
++)
247 if (!mapprevOutScriptPubKeys
.count(tx
.vin
[i
].prevout
))
249 BOOST_ERROR("Bad test: " << strTest
);
253 unsigned int verify_flags
= ParseScriptFlags(test
[2].get_str());
255 if (mapprevOutValues
.count(tx
.vin
[i
].prevout
)) {
256 amount
= mapprevOutValues
[tx
.vin
[i
].prevout
];
258 const CScriptWitness
*witness
= &tx
.vin
[i
].scriptWitness
;
259 fValid
= VerifyScript(tx
.vin
[i
].scriptSig
, mapprevOutScriptPubKeys
[tx
.vin
[i
].prevout
],
260 witness
, verify_flags
, TransactionSignatureChecker(&tx
, i
, amount
, txdata
), &err
);
262 BOOST_CHECK_MESSAGE(!fValid
, strTest
);
263 BOOST_CHECK_MESSAGE(err
!= SCRIPT_ERR_OK
, ScriptErrorString(err
));
268 BOOST_AUTO_TEST_CASE(basic_transaction_tests
)
270 // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
271 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};
272 std::vector
<unsigned char> vch(ch
, ch
+ sizeof(ch
) -1);
273 CDataStream
stream(vch
, SER_DISK
, CLIENT_VERSION
);
274 CMutableTransaction tx
;
276 CValidationState state
;
277 BOOST_CHECK_MESSAGE(CheckTransaction(tx
, state
) && state
.IsValid(), "Simple deserialized transaction should be valid.");
279 // Check that duplicate txins fail
280 tx
.vin
.push_back(tx
.vin
[0]);
281 BOOST_CHECK_MESSAGE(!CheckTransaction(tx
, state
) || !state
.IsValid(), "Transaction with duplicate txins should be invalid.");
285 // Helper: create two dummy transactions, each with
286 // two outputs. The first has 11 and 50 CENT outputs
287 // paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
288 // paid to a TX_PUBKEYHASH.
290 static std::vector
<CMutableTransaction
>
291 SetupDummyInputs(CBasicKeyStore
& keystoreRet
, CCoinsViewCache
& coinsRet
)
293 std::vector
<CMutableTransaction
> dummyTransactions
;
294 dummyTransactions
.resize(2);
296 // Add some keys to the keystore:
298 for (int i
= 0; i
< 4; i
++)
300 key
[i
].MakeNewKey(i
% 2);
301 keystoreRet
.AddKey(key
[i
]);
304 // Create some dummy input transactions
305 dummyTransactions
[0].vout
.resize(2);
306 dummyTransactions
[0].vout
[0].nValue
= 11*CENT
;
307 dummyTransactions
[0].vout
[0].scriptPubKey
<< ToByteVector(key
[0].GetPubKey()) << OP_CHECKSIG
;
308 dummyTransactions
[0].vout
[1].nValue
= 50*CENT
;
309 dummyTransactions
[0].vout
[1].scriptPubKey
<< ToByteVector(key
[1].GetPubKey()) << OP_CHECKSIG
;
310 coinsRet
.ModifyCoins(dummyTransactions
[0].GetHash())->FromTx(dummyTransactions
[0], 0);
312 dummyTransactions
[1].vout
.resize(2);
313 dummyTransactions
[1].vout
[0].nValue
= 21*CENT
;
314 dummyTransactions
[1].vout
[0].scriptPubKey
= GetScriptForDestination(key
[2].GetPubKey().GetID());
315 dummyTransactions
[1].vout
[1].nValue
= 22*CENT
;
316 dummyTransactions
[1].vout
[1].scriptPubKey
= GetScriptForDestination(key
[3].GetPubKey().GetID());
317 coinsRet
.ModifyCoins(dummyTransactions
[1].GetHash())->FromTx(dummyTransactions
[1], 0);
319 return dummyTransactions
;
322 BOOST_AUTO_TEST_CASE(test_Get
)
324 CBasicKeyStore keystore
;
325 CCoinsView coinsDummy
;
326 CCoinsViewCache
coins(&coinsDummy
);
327 std::vector
<CMutableTransaction
> dummyTransactions
= SetupDummyInputs(keystore
, coins
);
329 CMutableTransaction t1
;
331 t1
.vin
[0].prevout
.hash
= dummyTransactions
[0].GetHash();
332 t1
.vin
[0].prevout
.n
= 1;
333 t1
.vin
[0].scriptSig
<< std::vector
<unsigned char>(65, 0);
334 t1
.vin
[1].prevout
.hash
= dummyTransactions
[1].GetHash();
335 t1
.vin
[1].prevout
.n
= 0;
336 t1
.vin
[1].scriptSig
<< std::vector
<unsigned char>(65, 0) << std::vector
<unsigned char>(33, 4);
337 t1
.vin
[2].prevout
.hash
= dummyTransactions
[1].GetHash();
338 t1
.vin
[2].prevout
.n
= 1;
339 t1
.vin
[2].scriptSig
<< std::vector
<unsigned char>(65, 0) << std::vector
<unsigned char>(33, 4);
341 t1
.vout
[0].nValue
= 90*CENT
;
342 t1
.vout
[0].scriptPubKey
<< OP_1
;
344 BOOST_CHECK(AreInputsStandard(t1
, coins
));
345 BOOST_CHECK_EQUAL(coins
.GetValueIn(t1
), (50+21+22)*CENT
);
348 void CreateCreditAndSpend(const CKeyStore
& keystore
, const CScript
& outscript
, CTransactionRef
& output
, CMutableTransaction
& input
, bool success
= true)
350 CMutableTransaction outputm
;
351 outputm
.nVersion
= 1;
352 outputm
.vin
.resize(1);
353 outputm
.vin
[0].prevout
.SetNull();
354 outputm
.vin
[0].scriptSig
= CScript();
355 outputm
.vout
.resize(1);
356 outputm
.vout
[0].nValue
= 1;
357 outputm
.vout
[0].scriptPubKey
= outscript
;
358 CDataStream
ssout(SER_NETWORK
, PROTOCOL_VERSION
);
361 assert(output
->vin
.size() == 1);
362 assert(output
->vin
[0] == outputm
.vin
[0]);
363 assert(output
->vout
.size() == 1);
364 assert(output
->vout
[0] == outputm
.vout
[0]);
366 CMutableTransaction inputm
;
368 inputm
.vin
.resize(1);
369 inputm
.vin
[0].prevout
.hash
= output
->GetHash();
370 inputm
.vin
[0].prevout
.n
= 0;
371 inputm
.vout
.resize(1);
372 inputm
.vout
[0].nValue
= 1;
373 inputm
.vout
[0].scriptPubKey
= CScript();
374 bool ret
= SignSignature(keystore
, *output
, inputm
, 0, SIGHASH_ALL
);
375 assert(ret
== success
);
376 CDataStream
ssin(SER_NETWORK
, PROTOCOL_VERSION
);
379 assert(input
.vin
.size() == 1);
380 assert(input
.vin
[0] == inputm
.vin
[0]);
381 assert(input
.vout
.size() == 1);
382 assert(input
.vout
[0] == inputm
.vout
[0]);
383 assert(input
.vin
[0].scriptWitness
.stack
== inputm
.vin
[0].scriptWitness
.stack
);
386 void CheckWithFlag(const CTransactionRef
& output
, const CMutableTransaction
& input
, int flags
, bool success
)
389 CTransaction
inputi(input
);
390 bool ret
= VerifyScript(inputi
.vin
[0].scriptSig
, output
->vout
[0].scriptPubKey
, &inputi
.vin
[0].scriptWitness
, flags
, TransactionSignatureChecker(&inputi
, 0, output
->vout
[0].nValue
), &error
);
391 assert(ret
== success
);
394 static CScript
PushAll(const std::vector
<valtype
>& values
)
397 BOOST_FOREACH(const valtype
& v
, values
) {
400 } else if (v
.size() == 1 && v
[0] >= 1 && v
[0] <= 16) {
401 result
<< CScript::EncodeOP_N(v
[0]);
409 void ReplaceRedeemScript(CScript
& script
, const CScript
& redeemScript
)
411 std::vector
<valtype
> stack
;
412 EvalScript(stack
, script
, SCRIPT_VERIFY_STRICTENC
, BaseSignatureChecker(), SIGVERSION_BASE
);
413 assert(stack
.size() > 0);
414 stack
.back() = std::vector
<unsigned char>(redeemScript
.begin(), redeemScript
.end());
415 script
= PushAll(stack
);
418 BOOST_AUTO_TEST_CASE(test_big_witness_transaction
) {
419 CMutableTransaction mtx
;
423 key
.MakeNewKey(true); // Need to use compressed keys in segwit or the signing will fail
424 CBasicKeyStore keystore
;
425 keystore
.AddKeyPubKey(key
, key
.GetPubKey());
426 CKeyID hash
= key
.GetPubKey().GetID();
427 CScript scriptPubKey
= CScript() << OP_0
<< std::vector
<unsigned char>(hash
.begin(), hash
.end());
429 std::vector
<int> sigHashes
;
430 sigHashes
.push_back(SIGHASH_NONE
| SIGHASH_ANYONECANPAY
);
431 sigHashes
.push_back(SIGHASH_SINGLE
| SIGHASH_ANYONECANPAY
);
432 sigHashes
.push_back(SIGHASH_ALL
| SIGHASH_ANYONECANPAY
);
433 sigHashes
.push_back(SIGHASH_NONE
);
434 sigHashes
.push_back(SIGHASH_SINGLE
);
435 sigHashes
.push_back(SIGHASH_ALL
);
437 // create a big transaction of 4500 inputs signed by the same key
438 for(uint32_t ij
= 0; ij
< 4500; ij
++) {
439 uint32_t i
= mtx
.vin
.size();
441 prevId
.SetHex("0000000000000000000000000000000000000000000000000000000000000100");
442 COutPoint
outpoint(prevId
, i
);
444 mtx
.vin
.resize(mtx
.vin
.size() + 1);
445 mtx
.vin
[i
].prevout
= outpoint
;
446 mtx
.vin
[i
].scriptSig
= CScript();
448 mtx
.vout
.resize(mtx
.vout
.size() + 1);
449 mtx
.vout
[i
].nValue
= 1000;
450 mtx
.vout
[i
].scriptPubKey
= CScript() << OP_1
;
454 for(uint32_t i
= 0; i
< mtx
.vin
.size(); i
++) {
455 bool hashSigned
= SignSignature(keystore
, scriptPubKey
, mtx
, i
, 1000, sigHashes
.at(i
% sigHashes
.size()));
459 CDataStream
ssout(SER_NETWORK
, PROTOCOL_VERSION
);
460 auto vstream
= WithOrVersion(&ssout
, 0);
462 CTransaction
tx(deserialize
, vstream
);
464 // check all inputs concurrently, with the cache
465 PrecomputedTransactionData
txdata(tx
);
466 boost::thread_group threadGroup
;
467 CCheckQueue
<CScriptCheck
> scriptcheckqueue(128);
468 CCheckQueueControl
<CScriptCheck
> control(&scriptcheckqueue
);
470 for (int i
=0; i
<20; i
++)
471 threadGroup
.create_thread(boost::bind(&CCheckQueue
<CScriptCheck
>::Thread
, boost::ref(scriptcheckqueue
)));
474 coins
.fCoinBase
= false;
475 for(uint32_t i
= 0; i
< mtx
.vin
.size(); i
++) {
478 txout
.scriptPubKey
= scriptPubKey
;
479 coins
.vout
.push_back(txout
);
482 for(uint32_t i
= 0; i
< mtx
.vin
.size(); i
++) {
483 std::vector
<CScriptCheck
> vChecks
;
484 CScriptCheck
check(coins
, tx
, i
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, false, &txdata
);
485 vChecks
.push_back(CScriptCheck());
486 check
.swap(vChecks
.back());
487 control
.Add(vChecks
);
490 bool controlCheck
= control
.Wait();
491 assert(controlCheck
);
493 threadGroup
.interrupt_all();
494 threadGroup
.join_all();
497 BOOST_AUTO_TEST_CASE(test_witness
)
499 CBasicKeyStore keystore
, keystore2
;
500 CKey key1
, key2
, key3
, key1L
, key2L
;
501 CPubKey pubkey1
, pubkey2
, pubkey3
, pubkey1L
, pubkey2L
;
502 key1
.MakeNewKey(true);
503 key2
.MakeNewKey(true);
504 key3
.MakeNewKey(true);
505 key1L
.MakeNewKey(false);
506 key2L
.MakeNewKey(false);
507 pubkey1
= key1
.GetPubKey();
508 pubkey2
= key2
.GetPubKey();
509 pubkey3
= key3
.GetPubKey();
510 pubkey1L
= key1L
.GetPubKey();
511 pubkey2L
= key2L
.GetPubKey();
512 keystore
.AddKeyPubKey(key1
, pubkey1
);
513 keystore
.AddKeyPubKey(key2
, pubkey2
);
514 keystore
.AddKeyPubKey(key1L
, pubkey1L
);
515 keystore
.AddKeyPubKey(key2L
, pubkey2L
);
516 CScript scriptPubkey1
, scriptPubkey2
, scriptPubkey1L
, scriptPubkey2L
, scriptMulti
;
517 scriptPubkey1
<< ToByteVector(pubkey1
) << OP_CHECKSIG
;
518 scriptPubkey2
<< ToByteVector(pubkey2
) << OP_CHECKSIG
;
519 scriptPubkey1L
<< ToByteVector(pubkey1L
) << OP_CHECKSIG
;
520 scriptPubkey2L
<< ToByteVector(pubkey2L
) << OP_CHECKSIG
;
521 std::vector
<CPubKey
> oneandthree
;
522 oneandthree
.push_back(pubkey1
);
523 oneandthree
.push_back(pubkey3
);
524 scriptMulti
= GetScriptForMultisig(2, oneandthree
);
525 keystore
.AddCScript(scriptPubkey1
);
526 keystore
.AddCScript(scriptPubkey2
);
527 keystore
.AddCScript(scriptPubkey1L
);
528 keystore
.AddCScript(scriptPubkey2L
);
529 keystore
.AddCScript(scriptMulti
);
530 keystore
.AddCScript(GetScriptForWitness(scriptPubkey1
));
531 keystore
.AddCScript(GetScriptForWitness(scriptPubkey2
));
532 keystore
.AddCScript(GetScriptForWitness(scriptPubkey1L
));
533 keystore
.AddCScript(GetScriptForWitness(scriptPubkey2L
));
534 keystore
.AddCScript(GetScriptForWitness(scriptMulti
));
535 keystore2
.AddCScript(scriptMulti
);
536 keystore2
.AddCScript(GetScriptForWitness(scriptMulti
));
537 keystore2
.AddKeyPubKey(key3
, pubkey3
);
539 CTransactionRef output1
, output2
;
540 CMutableTransaction input1
, input2
;
541 SignatureData sigdata
;
543 // Normal pay-to-compressed-pubkey.
544 CreateCreditAndSpend(keystore
, scriptPubkey1
, output1
, input1
);
545 CreateCreditAndSpend(keystore
, scriptPubkey2
, output2
, input2
);
546 CheckWithFlag(output1
, input1
, 0, true);
547 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
548 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
549 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
550 CheckWithFlag(output1
, input2
, 0, false);
551 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, false);
552 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
553 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
555 // P2SH pay-to-compressed-pubkey.
556 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(scriptPubkey1
)), output1
, input1
);
557 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(scriptPubkey2
)), output2
, input2
);
558 ReplaceRedeemScript(input2
.vin
[0].scriptSig
, scriptPubkey1
);
559 CheckWithFlag(output1
, input1
, 0, true);
560 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
561 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
562 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
563 CheckWithFlag(output1
, input2
, 0, true);
564 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, false);
565 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
566 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
568 // Witness pay-to-compressed-pubkey (v0).
569 CreateCreditAndSpend(keystore
, GetScriptForWitness(scriptPubkey1
), output1
, input1
);
570 CreateCreditAndSpend(keystore
, GetScriptForWitness(scriptPubkey2
), output2
, input2
);
571 CheckWithFlag(output1
, input1
, 0, true);
572 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
573 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
574 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
575 CheckWithFlag(output1
, input2
, 0, true);
576 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, true);
577 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
578 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
580 // P2SH witness pay-to-compressed-pubkey (v0).
581 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1
))), output1
, input1
);
582 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2
))), output2
, input2
);
583 ReplaceRedeemScript(input2
.vin
[0].scriptSig
, GetScriptForWitness(scriptPubkey1
));
584 CheckWithFlag(output1
, input1
, 0, true);
585 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
586 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
587 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
588 CheckWithFlag(output1
, input2
, 0, true);
589 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, true);
590 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
591 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
593 // Normal pay-to-uncompressed-pubkey.
594 CreateCreditAndSpend(keystore
, scriptPubkey1L
, output1
, input1
);
595 CreateCreditAndSpend(keystore
, scriptPubkey2L
, output2
, input2
);
596 CheckWithFlag(output1
, input1
, 0, true);
597 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
598 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
599 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
600 CheckWithFlag(output1
, input2
, 0, false);
601 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, false);
602 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
603 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
605 // P2SH pay-to-uncompressed-pubkey.
606 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(scriptPubkey1L
)), output1
, input1
);
607 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(scriptPubkey2L
)), output2
, input2
);
608 ReplaceRedeemScript(input2
.vin
[0].scriptSig
, scriptPubkey1L
);
609 CheckWithFlag(output1
, input1
, 0, true);
610 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
611 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, true);
612 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
613 CheckWithFlag(output1
, input2
, 0, true);
614 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_P2SH
, false);
615 CheckWithFlag(output1
, input2
, SCRIPT_VERIFY_WITNESS
| SCRIPT_VERIFY_P2SH
, false);
616 CheckWithFlag(output1
, input2
, STANDARD_SCRIPT_VERIFY_FLAGS
, false);
618 // Signing disabled for witness pay-to-uncompressed-pubkey (v1).
619 CreateCreditAndSpend(keystore
, GetScriptForWitness(scriptPubkey1L
), output1
, input1
, false);
620 CreateCreditAndSpend(keystore
, GetScriptForWitness(scriptPubkey2L
), output2
, input2
, false);
622 // Signing disabled for P2SH witness pay-to-uncompressed-pubkey (v1).
623 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey1L
))), output1
, input1
, false);
624 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptPubkey2L
))), output2
, input2
, false);
626 // Normal 2-of-2 multisig
627 CreateCreditAndSpend(keystore
, scriptMulti
, output1
, input1
, false);
628 CheckWithFlag(output1
, input1
, 0, false);
629 CreateCreditAndSpend(keystore2
, scriptMulti
, output2
, input2
, false);
630 CheckWithFlag(output2
, input2
, 0, false);
631 BOOST_CHECK(*output1
== *output2
);
632 UpdateTransaction(input1
, 0, CombineSignatures(output1
->vout
[0].scriptPubKey
, MutableTransactionSignatureChecker(&input1
, 0, output1
->vout
[0].nValue
), DataFromTransaction(input1
, 0), DataFromTransaction(input2
, 0)));
633 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
635 // P2SH 2-of-2 multisig
636 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(scriptMulti
)), output1
, input1
, false);
637 CheckWithFlag(output1
, input1
, 0, true);
638 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, false);
639 CreateCreditAndSpend(keystore2
, GetScriptForDestination(CScriptID(scriptMulti
)), output2
, input2
, false);
640 CheckWithFlag(output2
, input2
, 0, true);
641 CheckWithFlag(output2
, input2
, SCRIPT_VERIFY_P2SH
, false);
642 BOOST_CHECK(*output1
== *output2
);
643 UpdateTransaction(input1
, 0, CombineSignatures(output1
->vout
[0].scriptPubKey
, MutableTransactionSignatureChecker(&input1
, 0, output1
->vout
[0].nValue
), DataFromTransaction(input1
, 0), DataFromTransaction(input2
, 0)));
644 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
645 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
647 // Witness 2-of-2 multisig
648 CreateCreditAndSpend(keystore
, GetScriptForWitness(scriptMulti
), output1
, input1
, false);
649 CheckWithFlag(output1
, input1
, 0, true);
650 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, false);
651 CreateCreditAndSpend(keystore2
, GetScriptForWitness(scriptMulti
), output2
, input2
, false);
652 CheckWithFlag(output2
, input2
, 0, true);
653 CheckWithFlag(output2
, input2
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, false);
654 BOOST_CHECK(*output1
== *output2
);
655 UpdateTransaction(input1
, 0, CombineSignatures(output1
->vout
[0].scriptPubKey
, MutableTransactionSignatureChecker(&input1
, 0, output1
->vout
[0].nValue
), DataFromTransaction(input1
, 0), DataFromTransaction(input2
, 0)));
656 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, true);
657 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
659 // P2SH witness 2-of-2 multisig
660 CreateCreditAndSpend(keystore
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti
))), output1
, input1
, false);
661 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
, true);
662 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, false);
663 CreateCreditAndSpend(keystore2
, GetScriptForDestination(CScriptID(GetScriptForWitness(scriptMulti
))), output2
, input2
, false);
664 CheckWithFlag(output2
, input2
, SCRIPT_VERIFY_P2SH
, true);
665 CheckWithFlag(output2
, input2
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, false);
666 BOOST_CHECK(*output1
== *output2
);
667 UpdateTransaction(input1
, 0, CombineSignatures(output1
->vout
[0].scriptPubKey
, MutableTransactionSignatureChecker(&input1
, 0, output1
->vout
[0].nValue
), DataFromTransaction(input1
, 0), DataFromTransaction(input2
, 0)));
668 CheckWithFlag(output1
, input1
, SCRIPT_VERIFY_P2SH
| SCRIPT_VERIFY_WITNESS
, true);
669 CheckWithFlag(output1
, input1
, STANDARD_SCRIPT_VERIFY_FLAGS
, true);
672 BOOST_AUTO_TEST_CASE(test_IsStandard
)
675 CBasicKeyStore keystore
;
676 CCoinsView coinsDummy
;
677 CCoinsViewCache
coins(&coinsDummy
);
678 std::vector
<CMutableTransaction
> dummyTransactions
= SetupDummyInputs(keystore
, coins
);
680 CMutableTransaction t
;
682 t
.vin
[0].prevout
.hash
= dummyTransactions
[0].GetHash();
683 t
.vin
[0].prevout
.n
= 1;
684 t
.vin
[0].scriptSig
<< std::vector
<unsigned char>(65, 0);
686 t
.vout
[0].nValue
= 90*CENT
;
688 key
.MakeNewKey(true);
689 t
.vout
[0].scriptPubKey
= GetScriptForDestination(key
.GetPubKey().GetID());
692 BOOST_CHECK(IsStandardTx(t
, reason
));
694 // Check dust with default relay fee:
695 CAmount nDustThreshold
= 182 * dustRelayFee
.GetFeePerK()/1000 * 3;
696 BOOST_CHECK_EQUAL(nDustThreshold
, 546);
698 t
.vout
[0].nValue
= nDustThreshold
- 1;
699 BOOST_CHECK(!IsStandardTx(t
, reason
));
701 t
.vout
[0].nValue
= nDustThreshold
;
702 BOOST_CHECK(IsStandardTx(t
, reason
));
704 // Check dust with odd relay fee to verify rounding:
705 // nDustThreshold = 182 * 1234 / 1000 * 3
706 dustRelayFee
= CFeeRate(1234);
708 t
.vout
[0].nValue
= 672 - 1;
709 BOOST_CHECK(!IsStandardTx(t
, reason
));
711 t
.vout
[0].nValue
= 672;
712 BOOST_CHECK(IsStandardTx(t
, reason
));
713 dustRelayFee
= CFeeRate(DUST_RELAY_TX_FEE
);
715 t
.vout
[0].scriptPubKey
= CScript() << OP_1
;
716 BOOST_CHECK(!IsStandardTx(t
, reason
));
718 // MAX_OP_RETURN_RELAY-byte TX_NULL_DATA (standard)
719 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
720 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY
, t
.vout
[0].scriptPubKey
.size());
721 BOOST_CHECK(IsStandardTx(t
, reason
));
723 // MAX_OP_RETURN_RELAY+1-byte TX_NULL_DATA (non-standard)
724 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
725 BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY
+ 1, t
.vout
[0].scriptPubKey
.size());
726 BOOST_CHECK(!IsStandardTx(t
, reason
));
728 // Data payload can be encoded in any way...
729 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("");
730 BOOST_CHECK(IsStandardTx(t
, reason
));
731 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("00") << ParseHex("01");
732 BOOST_CHECK(IsStandardTx(t
, reason
));
733 // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
734 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;
735 BOOST_CHECK(IsStandardTx(t
, reason
));
736 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< 0 << ParseHex("01") << 2 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
737 BOOST_CHECK(IsStandardTx(t
, reason
));
739 // ...so long as it only contains PUSHDATA's
740 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< OP_RETURN
;
741 BOOST_CHECK(!IsStandardTx(t
, reason
));
743 // TX_NULL_DATA w/o PUSHDATA
745 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
;
746 BOOST_CHECK(IsStandardTx(t
, reason
));
748 // Only one TX_NULL_DATA permitted in all cases
750 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
751 t
.vout
[1].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
752 BOOST_CHECK(!IsStandardTx(t
, reason
));
754 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
<< ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
755 t
.vout
[1].scriptPubKey
= CScript() << OP_RETURN
;
756 BOOST_CHECK(!IsStandardTx(t
, reason
));
758 t
.vout
[0].scriptPubKey
= CScript() << OP_RETURN
;
759 t
.vout
[1].scriptPubKey
= CScript() << OP_RETURN
;
760 BOOST_CHECK(!IsStandardTx(t
, reason
));
763 BOOST_AUTO_TEST_SUITE_END()