forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxvalidationcache_tests.cpp
322 lines (313 loc) · 14.8 KB
/
txvalidationcache_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2011-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#include <consensus/validation.h>
#include <key.h>
#include <validation.h>
#include <txmempool.h>
#include <script/standard.h>
#include <script/sign.h>
#include <script/signingprovider.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
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);
BOOST_AUTO_TEST_SUITE(tx_validationcache_tests)
// @todo: Re-enable tests ref commit acf994bed3b461e72b8c942b762465b5900f639a
BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
{
// uint256 masternodesID = testMasternodeKeys.begin()->first; // TODO: (temp) !!!
// // Make sure skipping validation of transactions that were
// // validated going into the memory pool does not allow
// // double-spends in blocks to pass validation when they should not.
//
// CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
//
// // Create a double-spend of mature coinbase txn:
// std::vector<CMutableTransaction> spends;
// spends.resize(2);
// for (int i = 0; i < 2; i++)
// {
// spends[i].nVersion = 1;
// spends[i].vin.resize(1);
// spends[i].vin[0].prevout.hash = m_coinbase_txns[0]->GetHash();
// spends[i].vin[0].prevout.n = 0;
// spends[i].vout.resize(1);
// spends[i].vout[0].nValue = 11*CENT;
// spends[i].vout[0].scriptPubKey = scriptPubKey;
//
// // Sign:
// std::vector<unsigned char> vchSig;
// uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);
// BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
// vchSig.push_back((unsigned char)SIGHASH_ALL);
// spends[i].vin[0].scriptSig << vchSig;
// }
//
// CBlock block;
//
// // Test 1: block with both of those transactions should be rejected.
// block = CreateAndProcessBlock(spends, scriptPubKey, masternodesID);
// {
// LOCK(cs_main);
// BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
// }
//
// // Test 2: ... and should be rejected if spend1 is in the memory pool
// BOOST_CHECK(ToMemPool(spends[0]));
// block = CreateAndProcessBlock(spends, scriptPubKey, masternodesID);
// {
// LOCK(cs_main);
// BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
// }
// mempool.clear();
//
// // Test 3: ... and should be rejected if spend2 is in the memory pool
// BOOST_CHECK(ToMemPool(spends[1]));
// block = CreateAndProcessBlock(spends, scriptPubKey, masternodesID);
// {
// LOCK(cs_main);
// BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
// }
// mempool.clear();
//
// // Final sanity test: first spend in mempool, second in block, that's OK:
// std::vector<CMutableTransaction> oneSpend;
// oneSpend.push_back(spends[0]);
// BOOST_CHECK(ToMemPool(spends[1]));
// block = CreateAndProcessBlock(oneSpend, scriptPubKey, masternodesID);
// {
// LOCK(cs_main);
// BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() == block.GetHash());
// }
// // spends[1] should have been removed from the mempool when the
// // block with spends[0] is accepted:
// BOOST_CHECK_EQUAL(mempool.size(), 0U);
}
// @todo: Re-enable tests ref commit acf994bed3b461e72b8c942b762465b5900f639a
BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
{
// uint256 masternodesID = testMasternodeKeys.begin()->first; // TODO: (temp) !!!
// // Test that passing CheckInputs with one set of script flags doesn't imply
// // that we would pass again with a different set of flags.
// {
// LOCK(cs_main);
// InitScriptExecutionCache();
// }
//
// CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
// CScript p2sh_scriptPubKey = GetScriptForDestination(ScriptHash(p2pk_scriptPubKey));
// CScript p2pkh_scriptPubKey = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
// CScript p2wpkh_scriptPubKey = GetScriptForWitness(p2pkh_scriptPubKey);
//
// FillableSigningProvider keystore;
// BOOST_CHECK(keystore.AddKey(coinbaseKey));
// BOOST_CHECK(keystore.AddCScript(p2pk_scriptPubKey));
//
// // flags to test: SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, SCRIPT_VERIFY_CHECKSEQUENCE_VERIFY, SCRIPT_VERIFY_NULLDUMMY, uncompressed pubkey thing
//
// // Create 2 outputs that match the three scripts above, spending the first
// // coinbase tx.
// CMutableTransaction spend_tx;
//
// spend_tx.nVersion = 1;
// spend_tx.vin.resize(1);
// spend_tx.vin[0].prevout.hash = m_coinbase_txns[0]->GetHash();
// spend_tx.vin[0].prevout.n = 0;
// spend_tx.vout.resize(4);
// spend_tx.vout[0].nValue = 11*CENT;
// spend_tx.vout[0].scriptPubKey = p2sh_scriptPubKey;
// spend_tx.vout[1].nValue = 11*CENT;
// spend_tx.vout[1].scriptPubKey = p2wpkh_scriptPubKey;
// spend_tx.vout[2].nValue = 11*CENT;
// spend_tx.vout[2].scriptPubKey = CScript() << OP_CHECKLOCKTIMEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
// spend_tx.vout[3].nValue = 11*CENT;
// spend_tx.vout[3].scriptPubKey = CScript() << OP_CHECKSEQUENCEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
//
// // Sign, with a non-DER signature
// {
// std::vector<unsigned char> vchSig;
// uint256 hash = SignatureHash(p2pk_scriptPubKey, spend_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
// BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
// vchSig.push_back((unsigned char) 0); // padding byte makes this non-DER
// vchSig.push_back((unsigned char)SIGHASH_ALL);
// spend_tx.vin[0].scriptSig << vchSig;
// }
//
// // Test that invalidity under a set of flags doesn't preclude validity
// // under other (eg consensus) flags.
// // spend_tx is invalid according to DERSIG
// {
// LOCK(cs_main);
//
// CValidationState state;
// PrecomputedTransactionData ptd_spend_tx(spend_tx);
//
// BOOST_CHECK(!CheckInputs(CTransaction(spend_tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
//
// // If we call again asking for scriptchecks (as happens in
// // ConnectBlock), we should add a script check object for this -- we're
// // not caching invalidity (if that changes, delete this test case).
// std::vector<CScriptCheck> scriptchecks;
// BOOST_CHECK(CheckInputs(CTransaction(spend_tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, &scriptchecks));
// BOOST_CHECK_EQUAL(scriptchecks.size(), 1U);
//
// // Test that CheckInputs returns true iff DERSIG-enforcing flags are
// // not present. Don't add these checks to the cache, so that we can
// // test later that block validation works fine in the absence of cached
// // successes.
// ValidateCheckInputsForAllFlags(CTransaction(spend_tx), SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false);
// }
//
// // And if we produce a block with this tx, it should be valid (DERSIG not
// // enabled yet), even though there's no cache entry.
// CBlock block;
//
// block = CreateAndProcessBlock({spend_tx}, p2pk_scriptPubKey, masternodesID);
// LOCK(cs_main);
// BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() == block.GetHash());
// BOOST_CHECK(::ChainstateActive().CoinsTip().GetBestBlock() == block.GetHash());
//
// // Test P2SH: construct a transaction that is valid without P2SH, and
// // then test validity with P2SH.
// {
// CMutableTransaction invalid_under_p2sh_tx;
// invalid_under_p2sh_tx.nVersion = 1;
// invalid_under_p2sh_tx.vin.resize(1);
// invalid_under_p2sh_tx.vin[0].prevout.hash = spend_tx.GetHash();
// invalid_under_p2sh_tx.vin[0].prevout.n = 0;
// invalid_under_p2sh_tx.vout.resize(1);
// invalid_under_p2sh_tx.vout[0].nValue = 11*CENT;
// invalid_under_p2sh_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
// std::vector<unsigned char> vchSig2(p2pk_scriptPubKey.begin(), p2pk_scriptPubKey.end());
// invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2;
//
// ValidateCheckInputsForAllFlags(CTransaction(invalid_under_p2sh_tx), SCRIPT_VERIFY_P2SH, true);
// }
//
// // Test CHECKLOCKTIMEVERIFY
// {
// CMutableTransaction invalid_with_cltv_tx;
// invalid_with_cltv_tx.nVersion = 1;
// invalid_with_cltv_tx.nLockTime = 100;
// invalid_with_cltv_tx.vin.resize(1);
// invalid_with_cltv_tx.vin[0].prevout.hash = spend_tx.GetHash();
// invalid_with_cltv_tx.vin[0].prevout.n = 2;
// invalid_with_cltv_tx.vin[0].nSequence = 0;
// invalid_with_cltv_tx.vout.resize(1);
// invalid_with_cltv_tx.vout[0].nValue = 11*CENT;
// invalid_with_cltv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
//
// // Sign
// std::vector<unsigned char> vchSig;
// uint256 hash = SignatureHash(spend_tx.vout[2].scriptPubKey, invalid_with_cltv_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
// BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
// vchSig.push_back((unsigned char)SIGHASH_ALL);
// invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
//
// ValidateCheckInputsForAllFlags(CTransaction(invalid_with_cltv_tx), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true);
//
// // Make it valid, and check again
// invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
// CValidationState state;
// PrecomputedTransactionData txdata(invalid_with_cltv_tx);
// BOOST_CHECK(CheckInputs(CTransaction(invalid_with_cltv_tx), state, ::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
// }
//
// // TEST CHECKSEQUENCEVERIFY
// {
// CMutableTransaction invalid_with_csv_tx;
// invalid_with_csv_tx.nVersion = 2;
// invalid_with_csv_tx.vin.resize(1);
// invalid_with_csv_tx.vin[0].prevout.hash = spend_tx.GetHash();
// invalid_with_csv_tx.vin[0].prevout.n = 3;
// invalid_with_csv_tx.vin[0].nSequence = 100;
// invalid_with_csv_tx.vout.resize(1);
// invalid_with_csv_tx.vout[0].nValue = 11*CENT;
// invalid_with_csv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
//
// // Sign
// std::vector<unsigned char> vchSig;
// uint256 hash = SignatureHash(spend_tx.vout[3].scriptPubKey, invalid_with_csv_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
// BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
// vchSig.push_back((unsigned char)SIGHASH_ALL);
// invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
//
// ValidateCheckInputsForAllFlags(CTransaction(invalid_with_csv_tx), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true);
//
// // Make it valid, and check again
// invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
// CValidationState state;
// PrecomputedTransactionData txdata(invalid_with_csv_tx);
// BOOST_CHECK(CheckInputs(CTransaction(invalid_with_csv_tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
// }
//
// // TODO: add tests for remaining script flags
//
// // Test that passing CheckInputs with a valid witness doesn't imply success
// // for the same tx with a different witness.
// {
// CMutableTransaction valid_with_witness_tx;
// valid_with_witness_tx.nVersion = 1;
// valid_with_witness_tx.vin.resize(1);
// valid_with_witness_tx.vin[0].prevout.hash = spend_tx.GetHash();
// valid_with_witness_tx.vin[0].prevout.n = 1;
// valid_with_witness_tx.vout.resize(1);
// valid_with_witness_tx.vout[0].nValue = 11*CENT;
// valid_with_witness_tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
//
// // Sign
// SignatureData sigdata;
// BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(&valid_with_witness_tx, 0, 11*CENT, SIGHASH_ALL), spend_tx.vout[1].scriptPubKey, sigdata));
// UpdateInput(valid_with_witness_tx.vin[0], sigdata);
//
// // This should be valid under all script flags.
// ValidateCheckInputsForAllFlags(CTransaction(valid_with_witness_tx), 0, true);
//
// // Remove the witness, and check that it is now invalid.
// valid_with_witness_tx.vin[0].scriptWitness.SetNull();
// ValidateCheckInputsForAllFlags(CTransaction(valid_with_witness_tx), SCRIPT_VERIFY_WITNESS, true);
// }
//
// {
// // Test a transaction with multiple inputs.
// CMutableTransaction tx;
//
// tx.nVersion = 1;
// tx.vin.resize(2);
// tx.vin[0].prevout.hash = spend_tx.GetHash();
// tx.vin[0].prevout.n = 0;
// tx.vin[1].prevout.hash = spend_tx.GetHash();
// tx.vin[1].prevout.n = 1;
// tx.vout.resize(1);
// tx.vout[0].nValue = 22*CENT;
// tx.vout[0].scriptPubKey = p2pk_scriptPubKey;
//
// // Sign
// for (int i=0; i<2; ++i) {
// SignatureData sigdata;
// BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(&tx, i, 11*CENT, SIGHASH_ALL), spend_tx.vout[i].scriptPubKey, sigdata));
// UpdateInput(tx.vin[i], sigdata);
// }
//
// // This should be valid under all script flags
// ValidateCheckInputsForAllFlags(CTransaction(tx), 0, true);
//
// // Check that if the second input is invalid, but the first input is
// // valid, the transaction is not cached.
// // Invalidate vin[1]
// tx.vin[1].scriptWitness.SetNull();
//
// CValidationState state;
// PrecomputedTransactionData txdata(tx);
// // This transaction is now invalid under segwit, because of the second input.
// BOOST_CHECK(!CheckInputs(CTransaction(tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
//
// std::vector<CScriptCheck> scriptchecks;
// // Make sure this transaction was not cached (ie because the first
// // input was valid)
// BOOST_CHECK(CheckInputs(CTransaction(tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks));
// // Should get 2 script checks back -- caching is on a whole-transaction basis.
// BOOST_CHECK_EQUAL(scriptchecks.size(), 2U);
// }
}
BOOST_AUTO_TEST_SUITE_END()