tests: Add missing locks to tests
[bitcoinplatinum.git] / src / test / txvalidationcache_tests.cpp
blobce3060a5f31f4cbb2ddd127123b4e78af3d6e91a
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 "consensus/validation.h"
6 #include "key.h"
7 #include "validation.h"
8 #include "miner.h"
9 #include "pubkey.h"
10 #include "txmempool.h"
11 #include "random.h"
12 #include "script/standard.h"
13 #include "script/sign.h"
14 #include "test/test_bitcoin.h"
15 #include "utiltime.h"
16 #include "core_io.h"
17 #include "keystore.h"
18 #include "policy/policy.h"
20 #include <boost/test/unit_test.hpp>
22 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks);
24 BOOST_AUTO_TEST_SUITE(tx_validationcache_tests)
26 static bool
27 ToMemPool(CMutableTransaction& tx)
29 LOCK(cs_main);
31 CValidationState state;
32 return AcceptToMemoryPool(mempool, state, MakeTransactionRef(tx), nullptr /* pfMissingInputs */,
33 nullptr /* plTxnReplaced */, true /* bypass_limits */, 0 /* nAbsurdFee */);
36 BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
38 // Make sure skipping validation of transctions that were
39 // validated going into the memory pool does not allow
40 // double-spends in blocks to pass validation when they should not.
42 CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
44 // Create a double-spend of mature coinbase txn:
45 std::vector<CMutableTransaction> spends;
46 spends.resize(2);
47 for (int i = 0; i < 2; i++)
49 spends[i].nVersion = 1;
50 spends[i].vin.resize(1);
51 spends[i].vin[0].prevout.hash = coinbaseTxns[0].GetHash();
52 spends[i].vin[0].prevout.n = 0;
53 spends[i].vout.resize(1);
54 spends[i].vout[0].nValue = 11*CENT;
55 spends[i].vout[0].scriptPubKey = scriptPubKey;
57 // Sign:
58 std::vector<unsigned char> vchSig;
59 uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
60 BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
61 vchSig.push_back((unsigned char)SIGHASH_ALL);
62 spends[i].vin[0].scriptSig << vchSig;
65 CBlock block;
67 // Test 1: block with both of those transactions should be rejected.
68 block = CreateAndProcessBlock(spends, scriptPubKey);
69 LOCK(cs_main);
70 BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
72 // Test 2: ... and should be rejected if spend1 is in the memory pool
73 BOOST_CHECK(ToMemPool(spends[0]));
74 block = CreateAndProcessBlock(spends, scriptPubKey);
75 BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
76 mempool.clear();
78 // Test 3: ... and should be rejected if spend2 is in the memory pool
79 BOOST_CHECK(ToMemPool(spends[1]));
80 block = CreateAndProcessBlock(spends, scriptPubKey);
81 BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.GetHash());
82 mempool.clear();
84 // Final sanity test: first spend in mempool, second in block, that's OK:
85 std::vector<CMutableTransaction> oneSpend;
86 oneSpend.push_back(spends[0]);
87 BOOST_CHECK(ToMemPool(spends[1]));
88 block = CreateAndProcessBlock(oneSpend, scriptPubKey);
89 BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
90 // spends[1] should have been removed from the mempool when the
91 // block with spends[0] is accepted:
92 BOOST_CHECK_EQUAL(mempool.size(), 0);
95 // Run CheckInputs (using pcoinsTip) on the given transaction, for all script
96 // flags. Test that CheckInputs passes for all flags that don't overlap with
97 // the failing_flags argument, but otherwise fails.
98 // CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY (and future NOP codes that may
99 // get reassigned) have an interaction with DISCOURAGE_UPGRADABLE_NOPS: if
100 // the script flags used contain DISCOURAGE_UPGRADABLE_NOPS but don't contain
101 // CHECKLOCKTIMEVERIFY (or CHECKSEQUENCEVERIFY), but the script does contain
102 // OP_CHECKLOCKTIMEVERIFY (or OP_CHECKSEQUENCEVERIFY), then script execution
103 // should fail.
104 // Capture this interaction with the upgraded_nop argument: set it when evaluating
105 // any script flag that is implemented as an upgraded NOP code.
106 void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_flags, bool add_to_cache, bool upgraded_nop)
108 PrecomputedTransactionData txdata(tx);
109 // If we add many more flags, this loop can get too expensive, but we can
110 // rewrite in the future to randomly pick a set of flags to evaluate.
111 for (uint32_t test_flags=0; test_flags < (1U << 16); test_flags += 1) {
112 CValidationState state;
113 // Filter out incompatible flag choices
114 if ((test_flags & SCRIPT_VERIFY_CLEANSTACK)) {
115 // CLEANSTACK requires P2SH and WITNESS, see VerifyScript() in
116 // script/interpreter.cpp
117 test_flags |= SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS;
119 if ((test_flags & SCRIPT_VERIFY_WITNESS)) {
120 // WITNESS requires P2SH
121 test_flags |= SCRIPT_VERIFY_P2SH;
123 bool ret = CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, nullptr);
124 // CheckInputs should succeed iff test_flags doesn't intersect with
125 // failing_flags
126 bool expected_return_value = !(test_flags & failing_flags);
127 if (expected_return_value && upgraded_nop) {
128 // If the script flag being tested corresponds to an upgraded NOP,
129 // then script execution should fail if DISCOURAGE_UPGRADABLE_NOPS
130 // is set.
131 expected_return_value = !(test_flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS);
133 BOOST_CHECK_EQUAL(ret, expected_return_value);
135 // Test the caching
136 if (ret && add_to_cache) {
137 // Check that we get a cache hit if the tx was valid
138 std::vector<CScriptCheck> scriptchecks;
139 BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
140 BOOST_CHECK(scriptchecks.empty());
141 } else {
142 // Check that we get script executions to check, if the transaction
143 // was invalid, or we didn't add to cache.
144 std::vector<CScriptCheck> scriptchecks;
145 BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
146 BOOST_CHECK_EQUAL(scriptchecks.size(), tx.vin.size());
151 BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
153 // Test that passing CheckInputs with one set of script flags doesn't imply
154 // that we would pass again with a different set of flags.
156 LOCK(cs_main);
157 InitScriptExecutionCache();
160 CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
161 CScript p2sh_scriptPubKey = GetScriptForDestination(CScriptID(p2pk_scriptPubKey));
162 CScript p2pkh_scriptPubKey = GetScriptForDestination(coinbaseKey.GetPubKey().GetID());
163 CScript p2wpkh_scriptPubKey = GetScriptForWitness(p2pkh_scriptPubKey);
165 CBasicKeyStore keystore;
166 keystore.AddKey(coinbaseKey);
167 keystore.AddCScript(p2pk_scriptPubKey);
169 // flags to test: SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, SCRIPT_VERIFY_CHECKSEQUENCE_VERIFY, SCRIPT_VERIFY_NULLDUMMY, uncompressed pubkey thing
171 // Create 2 outputs that match the three scripts above, spending the first
172 // coinbase tx.
173 CMutableTransaction spend_tx;
175 spend_tx.nVersion = 1;
176 spend_tx.vin.resize(1);
177 spend_tx.vin[0].prevout.hash = coinbaseTxns[0].GetHash();
178 spend_tx.vin[0].prevout.n = 0;
179 spend_tx.vout.resize(4);
180 spend_tx.vout[0].nValue = 11*CENT;
181 spend_tx.vout[0].scriptPubKey = p2sh_scriptPubKey;
182 spend_tx.vout[1].nValue = 11*CENT;
183 spend_tx.vout[1].scriptPubKey = p2wpkh_scriptPubKey;
184 spend_tx.vout[2].nValue = 11*CENT;
185 spend_tx.vout[2].scriptPubKey = CScript() << OP_CHECKLOCKTIMEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
186 spend_tx.vout[3].nValue = 11*CENT;
187 spend_tx.vout[3].scriptPubKey = CScript() << OP_CHECKSEQUENCEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
189 // Sign, with a non-DER signature
191 std::vector<unsigned char> vchSig;
192 uint256 hash = SignatureHash(p2pk_scriptPubKey, spend_tx, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
193 BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
194 vchSig.push_back((unsigned char) 0); // padding byte makes this non-DER
195 vchSig.push_back((unsigned char)SIGHASH_ALL);
196 spend_tx.vin[0].scriptSig << vchSig;
199 LOCK(cs_main);
201 // Test that invalidity under a set of flags doesn't preclude validity
202 // under other (eg consensus) flags.
203 // spend_tx is invalid according to DERSIG
205 CValidationState state;
206 PrecomputedTransactionData ptd_spend_tx(spend_tx);
208 BOOST_CHECK(!CheckInputs(spend_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
210 // If we call again asking for scriptchecks (as happens in
211 // ConnectBlock), we should add a script check object for this -- we're
212 // not caching invalidity (if that changes, delete this test case).
213 std::vector<CScriptCheck> scriptchecks;
214 BOOST_CHECK(CheckInputs(spend_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, &scriptchecks));
215 BOOST_CHECK_EQUAL(scriptchecks.size(), 1);
217 // Test that CheckInputs returns true iff DERSIG-enforcing flags are
218 // not present. Don't add these checks to the cache, so that we can
219 // test later that block validation works fine in the absence of cached
220 // successes.
221 ValidateCheckInputsForAllFlags(spend_tx, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false, false);
223 // And if we produce a block with this tx, it should be valid (DERSIG not
224 // enabled yet), even though there's no cache entry.
225 CBlock block;
227 block = CreateAndProcessBlock({spend_tx}, p2pk_scriptPubKey);
228 BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
229 BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
232 // Test P2SH: construct a transaction that is valid without P2SH, and
233 // then test validity with P2SH.
235 CMutableTransaction invalid_under_p2sh_tx;
236 invalid_under_p2sh_tx.nVersion = 1;
237 invalid_under_p2sh_tx.vin.resize(1);
238 invalid_under_p2sh_tx.vin[0].prevout.hash = spend_tx.GetHash();
239 invalid_under_p2sh_tx.vin[0].prevout.n = 0;
240 invalid_under_p2sh_tx.vout.resize(1);
241 invalid_under_p2sh_tx.vout[0].nValue = 11*CENT;
242 invalid_under_p2sh_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
243 std::vector<unsigned char> vchSig2(p2pk_scriptPubKey.begin(), p2pk_scriptPubKey.end());
244 invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2;
246 ValidateCheckInputsForAllFlags(invalid_under_p2sh_tx, SCRIPT_VERIFY_P2SH, true, false);
249 // Test CHECKLOCKTIMEVERIFY
251 CMutableTransaction invalid_with_cltv_tx;
252 invalid_with_cltv_tx.nVersion = 1;
253 invalid_with_cltv_tx.nLockTime = 100;
254 invalid_with_cltv_tx.vin.resize(1);
255 invalid_with_cltv_tx.vin[0].prevout.hash = spend_tx.GetHash();
256 invalid_with_cltv_tx.vin[0].prevout.n = 2;
257 invalid_with_cltv_tx.vin[0].nSequence = 0;
258 invalid_with_cltv_tx.vout.resize(1);
259 invalid_with_cltv_tx.vout[0].nValue = 11*CENT;
260 invalid_with_cltv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
262 // Sign
263 std::vector<unsigned char> vchSig;
264 uint256 hash = SignatureHash(spend_tx.vout[2].scriptPubKey, invalid_with_cltv_tx, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
265 BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
266 vchSig.push_back((unsigned char)SIGHASH_ALL);
267 invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
269 ValidateCheckInputsForAllFlags(invalid_with_cltv_tx, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true);
271 // Make it valid, and check again
272 invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
273 CValidationState state;
274 PrecomputedTransactionData txdata(invalid_with_cltv_tx);
275 BOOST_CHECK(CheckInputs(invalid_with_cltv_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
278 // TEST CHECKSEQUENCEVERIFY
280 CMutableTransaction invalid_with_csv_tx;
281 invalid_with_csv_tx.nVersion = 2;
282 invalid_with_csv_tx.vin.resize(1);
283 invalid_with_csv_tx.vin[0].prevout.hash = spend_tx.GetHash();
284 invalid_with_csv_tx.vin[0].prevout.n = 3;
285 invalid_with_csv_tx.vin[0].nSequence = 100;
286 invalid_with_csv_tx.vout.resize(1);
287 invalid_with_csv_tx.vout[0].nValue = 11*CENT;
288 invalid_with_csv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
290 // Sign
291 std::vector<unsigned char> vchSig;
292 uint256 hash = SignatureHash(spend_tx.vout[3].scriptPubKey, invalid_with_csv_tx, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
293 BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
294 vchSig.push_back((unsigned char)SIGHASH_ALL);
295 invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
297 ValidateCheckInputsForAllFlags(invalid_with_csv_tx, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true);
299 // Make it valid, and check again
300 invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
301 CValidationState state;
302 PrecomputedTransactionData txdata(invalid_with_csv_tx);
303 BOOST_CHECK(CheckInputs(invalid_with_csv_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
306 // TODO: add tests for remaining script flags
308 // Test that passing CheckInputs with a valid witness doesn't imply success
309 // for the same tx with a different witness.
311 CMutableTransaction valid_with_witness_tx;
312 valid_with_witness_tx.nVersion = 1;
313 valid_with_witness_tx.vin.resize(1);
314 valid_with_witness_tx.vin[0].prevout.hash = spend_tx.GetHash();
315 valid_with_witness_tx.vin[0].prevout.n = 1;
316 valid_with_witness_tx.vout.resize(1);
317 valid_with_witness_tx.vout[0].nValue = 11*CENT;
318 valid_with_witness_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
320 // Sign
321 SignatureData sigdata;
322 ProduceSignature(MutableTransactionSignatureCreator(&keystore, &valid_with_witness_tx, 0, 11*CENT, SIGHASH_ALL), spend_tx.vout[1].scriptPubKey, sigdata);
323 UpdateTransaction(valid_with_witness_tx, 0, sigdata);
325 // This should be valid under all script flags.
326 ValidateCheckInputsForAllFlags(valid_with_witness_tx, 0, true, false);
328 // Remove the witness, and check that it is now invalid.
329 valid_with_witness_tx.vin[0].scriptWitness.SetNull();
330 ValidateCheckInputsForAllFlags(valid_with_witness_tx, SCRIPT_VERIFY_WITNESS, true, false);
334 // Test a transaction with multiple inputs.
335 CMutableTransaction tx;
337 tx.nVersion = 1;
338 tx.vin.resize(2);
339 tx.vin[0].prevout.hash = spend_tx.GetHash();
340 tx.vin[0].prevout.n = 0;
341 tx.vin[1].prevout.hash = spend_tx.GetHash();
342 tx.vin[1].prevout.n = 1;
343 tx.vout.resize(1);
344 tx.vout[0].nValue = 22*CENT;
345 tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
347 // Sign
348 for (int i=0; i<2; ++i) {
349 SignatureData sigdata;
350 ProduceSignature(MutableTransactionSignatureCreator(&keystore, &tx, i, 11*CENT, SIGHASH_ALL), spend_tx.vout[i].scriptPubKey, sigdata);
351 UpdateTransaction(tx, i, sigdata);
354 // This should be valid under all script flags
355 ValidateCheckInputsForAllFlags(tx, 0, true, false);
357 // Check that if the second input is invalid, but the first input is
358 // valid, the transaction is not cached.
359 // Invalidate vin[1]
360 tx.vin[1].scriptWitness.SetNull();
362 CValidationState state;
363 PrecomputedTransactionData txdata(tx);
364 // This transaction is now invalid under segwit, because of the second input.
365 BOOST_CHECK(!CheckInputs(tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
367 std::vector<CScriptCheck> scriptchecks;
368 // Make sure this transaction was not cached (ie because the first
369 // input was valid)
370 BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks));
371 // Should get 2 script checks back -- caching is on a whole-transaction basis.
372 BOOST_CHECK_EQUAL(scriptchecks.size(), 2);
376 BOOST_AUTO_TEST_SUITE_END()