5
5
#include < consensus/validation.h>
6
6
#include < key.h>
7
7
#include < random.h>
8
+ #include < script/sigcache.h>
8
9
#include < script/sign.h>
9
10
#include < script/signingprovider.h>
10
11
#include < test/util/setup_common.h>
@@ -22,6 +23,7 @@ struct Dersig100Setup : public TestChain100Setup {
22
23
bool CheckInputScripts (const CTransaction& tx, TxValidationState& state,
23
24
const CCoinsViewCache& inputs, unsigned int flags, bool cacheSigStore,
24
25
bool cacheFullScriptStore, PrecomputedTransactionData& txdata,
26
+ ValidationCache& validation_cache,
25
27
std::vector<CScriptCheck>* pvChecks) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
26
28
27
29
BOOST_AUTO_TEST_SUITE (txvalidationcache_tests)
@@ -118,7 +120,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup)
118
120
// should fail.
119
121
// Capture this interaction with the upgraded_nop argument: set it when evaluating
120
122
// any script flag that is implemented as an upgraded NOP code.
121
- static void ValidateCheckInputsForAllFlags (const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
123
+ static void ValidateCheckInputsForAllFlags (const CTransaction &tx, uint32_t failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip, ValidationCache& validation_cache ) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
122
124
{
123
125
PrecomputedTransactionData txdata;
124
126
@@ -140,7 +142,7 @@ static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t fail
140
142
// WITNESS requires P2SH
141
143
test_flags |= SCRIPT_VERIFY_P2SH;
142
144
}
143
- bool ret = CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, nullptr );
145
+ bool ret = CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, validation_cache, nullptr );
144
146
// CheckInputScripts should succeed iff test_flags doesn't intersect with
145
147
// failing_flags
146
148
bool expected_return_value = !(test_flags & failing_flags);
@@ -150,13 +152,13 @@ static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t fail
150
152
if (ret && add_to_cache) {
151
153
// Check that we get a cache hit if the tx was valid
152
154
std::vector<CScriptCheck> scriptchecks;
153
- BOOST_CHECK (CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, &scriptchecks));
155
+ BOOST_CHECK (CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, validation_cache, &scriptchecks));
154
156
BOOST_CHECK (scriptchecks.empty ());
155
157
} else {
156
158
// Check that we get script executions to check, if the transaction
157
159
// was invalid, or we didn't add to cache.
158
160
std::vector<CScriptCheck> scriptchecks;
159
- BOOST_CHECK (CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, &scriptchecks));
161
+ BOOST_CHECK (CheckInputScripts (tx, state, &active_coins_tip, test_flags, true , add_to_cache, txdata, validation_cache, &scriptchecks));
160
162
BOOST_CHECK_EQUAL (scriptchecks.size (), tx.vin .size ());
161
163
}
162
164
}
@@ -214,20 +216,20 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
214
216
TxValidationState state;
215
217
PrecomputedTransactionData ptd_spend_tx;
216
218
217
- BOOST_CHECK (!CheckInputScripts (CTransaction (spend_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true , true , ptd_spend_tx, nullptr ));
219
+ BOOST_CHECK (!CheckInputScripts (CTransaction (spend_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true , true , ptd_spend_tx, m_node. chainman -> m_validation_cache , nullptr ));
218
220
219
221
// If we call again asking for scriptchecks (as happens in
220
222
// ConnectBlock), we should add a script check object for this -- we're
221
223
// not caching invalidity (if that changes, delete this test case).
222
224
std::vector<CScriptCheck> scriptchecks;
223
- BOOST_CHECK (CheckInputScripts (CTransaction (spend_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true , true , ptd_spend_tx, &scriptchecks));
225
+ BOOST_CHECK (CheckInputScripts (CTransaction (spend_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true , true , ptd_spend_tx, m_node. chainman -> m_validation_cache , &scriptchecks));
224
226
BOOST_CHECK_EQUAL (scriptchecks.size (), 1U );
225
227
226
228
// Test that CheckInputScripts returns true iff DERSIG-enforcing flags are
227
229
// not present. Don't add these checks to the cache, so that we can
228
230
// test later that block validation works fine in the absence of cached
229
231
// successes.
230
- ValidateCheckInputsForAllFlags (CTransaction (spend_tx), SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false , m_node.chainman ->ActiveChainstate ().CoinsTip ());
232
+ ValidateCheckInputsForAllFlags (CTransaction (spend_tx), SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
231
233
}
232
234
233
235
// And if we produce a block with this tx, it should be valid (DERSIG not
@@ -253,7 +255,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
253
255
std::vector<unsigned char > vchSig2 (p2pk_scriptPubKey.begin (), p2pk_scriptPubKey.end ());
254
256
invalid_under_p2sh_tx.vin [0 ].scriptSig << vchSig2;
255
257
256
- ValidateCheckInputsForAllFlags (CTransaction (invalid_under_p2sh_tx), SCRIPT_VERIFY_P2SH, true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
258
+ ValidateCheckInputsForAllFlags (CTransaction (invalid_under_p2sh_tx), SCRIPT_VERIFY_P2SH, true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
257
259
}
258
260
259
261
// Test CHECKLOCKTIMEVERIFY
@@ -276,13 +278,13 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
276
278
vchSig.push_back ((unsigned char )SIGHASH_ALL);
277
279
invalid_with_cltv_tx.vin [0 ].scriptSig = CScript () << vchSig << 101 ;
278
280
279
- ValidateCheckInputsForAllFlags (CTransaction (invalid_with_cltv_tx), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
281
+ ValidateCheckInputsForAllFlags (CTransaction (invalid_with_cltv_tx), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
280
282
281
283
// Make it valid, and check again
282
284
invalid_with_cltv_tx.vin [0 ].scriptSig = CScript () << vchSig << 100 ;
283
285
TxValidationState state;
284
286
PrecomputedTransactionData txdata;
285
- BOOST_CHECK (CheckInputScripts (CTransaction (invalid_with_cltv_tx), state, m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true , true , txdata, nullptr ));
287
+ BOOST_CHECK (CheckInputScripts (CTransaction (invalid_with_cltv_tx), state, m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true , true , txdata, m_node. chainman -> m_validation_cache , nullptr ));
286
288
}
287
289
288
290
// TEST CHECKSEQUENCEVERIFY
@@ -304,13 +306,13 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
304
306
vchSig.push_back ((unsigned char )SIGHASH_ALL);
305
307
invalid_with_csv_tx.vin [0 ].scriptSig = CScript () << vchSig << 101 ;
306
308
307
- ValidateCheckInputsForAllFlags (CTransaction (invalid_with_csv_tx), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
309
+ ValidateCheckInputsForAllFlags (CTransaction (invalid_with_csv_tx), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
308
310
309
311
// Make it valid, and check again
310
312
invalid_with_csv_tx.vin [0 ].scriptSig = CScript () << vchSig << 100 ;
311
313
TxValidationState state;
312
314
PrecomputedTransactionData txdata;
313
- BOOST_CHECK (CheckInputScripts (CTransaction (invalid_with_csv_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true , true , txdata, nullptr ));
315
+ BOOST_CHECK (CheckInputScripts (CTransaction (invalid_with_csv_tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true , true , txdata, m_node. chainman -> m_validation_cache , nullptr ));
314
316
}
315
317
316
318
// TODO: add tests for remaining script flags
@@ -333,11 +335,11 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
333
335
UpdateInput (valid_with_witness_tx.vin [0 ], sigdata);
334
336
335
337
// This should be valid under all script flags.
336
- ValidateCheckInputsForAllFlags (CTransaction (valid_with_witness_tx), 0 , true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
338
+ ValidateCheckInputsForAllFlags (CTransaction (valid_with_witness_tx), 0 , true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
337
339
338
340
// Remove the witness, and check that it is now invalid.
339
341
valid_with_witness_tx.vin [0 ].scriptWitness .SetNull ();
340
- ValidateCheckInputsForAllFlags (CTransaction (valid_with_witness_tx), SCRIPT_VERIFY_WITNESS, true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
342
+ ValidateCheckInputsForAllFlags (CTransaction (valid_with_witness_tx), SCRIPT_VERIFY_WITNESS, true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
341
343
}
342
344
343
345
{
@@ -362,7 +364,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
362
364
}
363
365
364
366
// This should be valid under all script flags
365
- ValidateCheckInputsForAllFlags (CTransaction (tx), 0 , true , m_node.chainman ->ActiveChainstate ().CoinsTip ());
367
+ ValidateCheckInputsForAllFlags (CTransaction (tx), 0 , true , m_node.chainman ->ActiveChainstate ().CoinsTip (), m_node. chainman -> m_validation_cache );
366
368
367
369
// Check that if the second input is invalid, but the first input is
368
370
// valid, the transaction is not cached.
@@ -372,12 +374,12 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
372
374
TxValidationState state;
373
375
PrecomputedTransactionData txdata;
374
376
// This transaction is now invalid under segwit, because of the second input.
375
- BOOST_CHECK (!CheckInputScripts (CTransaction (tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true , true , txdata, nullptr ));
377
+ BOOST_CHECK (!CheckInputScripts (CTransaction (tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true , true , txdata, m_node. chainman -> m_validation_cache , nullptr ));
376
378
377
379
std::vector<CScriptCheck> scriptchecks;
378
380
// Make sure this transaction was not cached (ie because the first
379
381
// input was valid)
380
- BOOST_CHECK (CheckInputScripts (CTransaction (tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true , true , txdata, &scriptchecks));
382
+ BOOST_CHECK (CheckInputScripts (CTransaction (tx), state, &m_node.chainman ->ActiveChainstate ().CoinsTip (), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true , true , txdata, m_node. chainman -> m_validation_cache , &scriptchecks));
381
383
// Should get 2 script checks back -- caching is on a whole-transaction basis.
382
384
BOOST_CHECK_EQUAL (scriptchecks.size (), 2U );
383
385
}
0 commit comments