forked from drivechain-project/mainchain-old
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bmm_tests.cpp
402 lines (292 loc) · 11.2 KB
/
bmm_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (c) 2017-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
#include <consensus/validation.h>
#include <keystore.h>
#include <random.h>
#include <script/sign.h>
#include <sidechain.h>
#include <uint256.h>
#include <utilstrencodings.h>
#include <validation.h>
#include <test/test_skydoge.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(bmm_tests, TestChain100Setup)
BOOST_AUTO_TEST_CASE(bmm_commit)
{
// Create a BMM h* request transaction
// Create critical data
CScript bytes;
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0);
CCriticalData criticalData;
criticalData.vBytes = std::vector<unsigned char>(bytes.begin(), bytes.end());
criticalData.hashCritical = GetRandHash();
// Create transaction with critical data
CMutableTransaction mtx;
mtx.nVersion = 3;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].prevout.hash = GetRandHash();
mtx.vin[0].prevout.n = 0;
mtx.vout[0].scriptPubKey = CScript() << OP_0;
mtx.vout[0].nValue = 50 * CENT;
// Set locktime to the block we would like critical data to be commited in
mtx.nLockTime = 102;
// Add critical data
mtx.criticalData = criticalData;
// Create dummy coinbase
CMutableTransaction coinbase;
coinbase.nVersion = 1;
coinbase.vin.resize(1);
coinbase.vout.resize(1);
coinbase.vin[0].prevout.SetNull();
coinbase.vin[0].scriptSig = CScript() << 102;
// Add dummy coinbase & critical data tx to block
CBlock block;
block.vtx.push_back(MakeTransactionRef(std::move(coinbase)));
block.vtx.push_back(MakeTransactionRef(std::move(mtx)));
// Generate commit
GenerateCriticalHashCommitments(block);
BOOST_REQUIRE(block.vtx[0]->vout.size() == 2);
// Check that the commit has been generated
uint256 hashCritical;
std::vector<unsigned char> vBytes;
BOOST_CHECK(block.vtx[0]->vout[1].scriptPubKey.IsCriticalHashCommit(hashCritical, vBytes));
BOOST_CHECK(hashCritical == criticalData.hashCritical);
}
BOOST_AUTO_TEST_CASE(bmm_commit_format)
{
// Test the IsBMMCommitment function with many different BMM requests
CCriticalData bmm;
// Null
BOOST_CHECK(!bmm.IsBMMRequest());
// Null bytes
bmm.hashCritical = GetRandHash();
BOOST_CHECK(!bmm.IsBMMRequest());
// Null h*
bmm.hashCritical.SetNull();
bmm.vBytes = std::vector<unsigned char> {0x00};
BOOST_CHECK(!bmm.IsBMMRequest());
// With valid h*, invalid bytes
bmm.hashCritical = GetRandHash();
BOOST_CHECK(!bmm.IsBMMRequest());
// Null h*, valid bytes
bmm.hashCritical.SetNull();
CScript bytes;
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0); // nSidechain
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bmm.vBytes = ToByteVector(bytes);
BOOST_CHECK(!bmm.IsBMMRequest());
// Valid
bmm.hashCritical = GetRandHash();
BOOST_CHECK(bmm.IsBMMRequest());
// Valid nSidechain 0 - 255 and check decoded return by reference)
for (unsigned int i = 0; i < 256; i++) {
bytes.clear();
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(i); // nSidechain
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bmm.vBytes = ToByteVector(bytes);
uint8_t nSidechain;
std::string strPrevBlock = "";
BOOST_CHECK(bmm.IsBMMRequest(nSidechain, strPrevBlock));
BOOST_CHECK(nSidechain == i);
BOOST_CHECK(strPrevBlock == "fdfdfdfd");
}
// Invalid bytes - too few
bytes.clear();
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0); // nSidechain
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bytes.push_back(0xFD);
bmm.vBytes = ToByteVector(bytes);
BOOST_CHECK(!bmm.IsBMMRequest());
// Invalid prev bytes - too many
bytes.clear();
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(255); // nSidechain
bytes << ParseHex(HexStr(std::string("The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")));
bmm.vBytes = ToByteVector(bytes);
BOOST_CHECK(!bmm.IsBMMRequest());
}
BOOST_AUTO_TEST_CASE(bmm_prevbytes_mempool)
{
// Create a BMM h* request transaction
// Create critical data
CScript bytes;
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0);
CBlock block = CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
// Get the prevBlock hash
std::string strPrevHash = chainActive.Tip()->GetBlockHash().ToString();
strPrevHash = strPrevHash.substr(strPrevHash.size() - 8, strPrevHash.size() - 1);
std::vector<unsigned char> vPrevBytes = ParseHex(strPrevHash);
bytes.resize(8);
memcpy(&bytes[4], vPrevBytes.data(), vPrevBytes.size());
CCriticalData criticalData;
criticalData.vBytes = ToByteVector(bytes);
criticalData.hashCritical = GetRandHash();
// Create transaction with critical data
CMutableTransaction mtx;
mtx.nVersion = 3;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[0].GetHash();
mtx.vin[0].prevout.n = 0;
mtx.vout[0].scriptPubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
mtx.vout[0].nValue = 50 * CENT;
// Set locktime to the block we would like critical data to be commited in
mtx.nLockTime = 101;
// Add critical data
mtx.criticalData = criticalData;
// Check that this is a valid BMM request first of all
BOOST_CHECK(criticalData.IsBMMRequest());
// Setup a keystore to hold the coinbase key
CBasicKeyStore tempKeystore;
tempKeystore.AddKey(coinbaseKey);
const CKeyStore& keystoreConst = tempKeystore;
const CTransaction& txToSign = mtx;
TransactionSignatureCreator creator(&keystoreConst, &txToSign, 0, coinbaseTxns[0].vout[0].nValue);
SignatureData sigdata;
bool sigCreated = ProduceSignature(creator, coinbaseTxns[0].vout[0].scriptPubKey, sigdata);
BOOST_CHECK(sigCreated);
mtx.vin[0].scriptSig = sigdata.scriptSig;
CValidationState state;
// Check that valid prevBytes are accepted to the mempool
{
LOCK(cs_main);
BOOST_CHECK(AcceptToMemoryPool(mempool, state, MakeTransactionRef(mtx),
nullptr, nullptr, false, 0));
}
BOOST_CHECK(state.IsValid());
// Remove the transaction we just added to the mempool before the next test
mempool.removeRecursive(CTransaction(mtx));
// Check that invalid (wrong block) prevBytes are rejected
// Mine another block
block = CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
// Update input
mtx.vin.clear();
mtx.vin.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[1].GetHash();
mtx.vin[0].prevout.n = 0;
// Update locktime
mtx.nLockTime = 102;
const CTransaction& txToSign2 = mtx;
TransactionSignatureCreator creator2(&keystoreConst, &txToSign2, 0, coinbaseTxns[1].vout[0].nValue);
SignatureData sigdata2;
BOOST_CHECK(ProduceSignature(creator2, coinbaseTxns[1].vout[0].scriptPubKey, sigdata2));
mtx.vin[0].scriptSig = sigdata2.scriptSig;
CValidationState state2;
// We haven't updated the prevBytes so they are outdated and invalid now,
// confirm that this will be rejected from the memory pool.
{
LOCK(cs_main);
BOOST_CHECK(!AcceptToMemoryPool(mempool, state2, MakeTransactionRef(mtx),
nullptr, nullptr, false, 0));
}
BOOST_CHECK(!state2.IsValid());
// Remove the transaction we just added to the mempool before the next test
mempool.removeRecursive(CTransaction(mtx));
// Check that invalid (too many) prevBytes are rejected
// Mine another block
block = CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
bytes.clear();
bytes.resize(3);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0);
bytes << ParseHex(HexStr(std::string("trueno")));
criticalData.vBytes = ToByteVector(bytes);
// Update input
mtx.vin.clear();
mtx.vin.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[2].GetHash();
mtx.vin[0].prevout.n = 0;
// Update locktime
mtx.nLockTime = 103;
const CTransaction& txToSign3 = mtx;
TransactionSignatureCreator creator3(&keystoreConst, &txToSign3, 0, coinbaseTxns[2].vout[0].nValue);
SignatureData sigdata3;
BOOST_CHECK(ProduceSignature(creator3, coinbaseTxns[2].vout[0].scriptPubKey, sigdata3));
mtx.vin[0].scriptSig = sigdata3.scriptSig;
CValidationState state3;
// Check that a BMM request with too many prevBytes is rejected
{
LOCK(cs_main);
BOOST_CHECK(!AcceptToMemoryPool(mempool, state3, MakeTransactionRef(mtx),
nullptr, nullptr, false, 0));
}
BOOST_CHECK(!state3.IsValid());
// Remove the transaction we just added to the mempool before the next test
mempool.removeRecursive(CTransaction(mtx));
// Check that invalid (null) prevBytes are rejected
// Mine another block
block = CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash());
BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
bytes.clear();
bytes.resize(4);
bytes[0] = 0x00;
bytes[1] = 0xbf;
bytes[2] = 0x00;
bytes[3] = uint8_t(0);
criticalData.vBytes = std::vector<unsigned char>(bytes.begin(), bytes.end());
// Update input
mtx.vin.clear();
mtx.vin.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[3].GetHash();
mtx.vin[0].prevout.n = 0;
// Update locktime
mtx.nLockTime = 104;
const CTransaction& txToSign4 = mtx;
TransactionSignatureCreator creator4(&keystoreConst, &txToSign4, 0, coinbaseTxns[3].vout[0].nValue);
SignatureData sigdata4;
BOOST_CHECK(ProduceSignature(creator4, coinbaseTxns[3].vout[0].scriptPubKey, sigdata4));
mtx.vin[0].scriptSig = sigdata4.scriptSig;
CValidationState state4;
// Check that valid prevBytes are accepted to the mempool
{
LOCK(cs_main);
BOOST_CHECK(!AcceptToMemoryPool(mempool, state4, MakeTransactionRef(mtx),
nullptr, nullptr, false, 0));
}
BOOST_CHECK(!state4.IsValid());
// Remove the transaction we just added to the mempool before the next test
mempool.removeRecursive(CTransaction(mtx));
}
BOOST_AUTO_TEST_SUITE_END()