forked from bitcoinfibre/bitcoinfibre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockencodings_tests.cpp
More file actions
672 lines (542 loc) · 27.8 KB
/
blockencodings_tests.cpp
File metadata and controls
672 lines (542 loc) · 27.8 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Copyright (c) 2011-2017 The Bitcoin Core developers
// Unlike the rest of Bitcoin Core, this file is
// distributed under the Affero General Public License (AGPL v3)
#include <blockencodings.h>
#include <consensus/merkle.h>
#include <chainparams.h>
#include <fec.h>
#include <random.h>
#include <test/test_bitcoin.h>
#include "test/data/block413567.hex.h"
#include <boost/test/unit_test.hpp>
#include <random>
std::vector<std::pair<uint256, CTransactionRef>> extra_txn;
struct RegtestingSetup : public TestingSetup {
RegtestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {}
};
BOOST_FIXTURE_TEST_SUITE(blockencodings_tests, RegtestingSetup)
static CBlock BuildBlockTestCase() {
CBlock block;
CMutableTransaction tx;
tx.vin.resize(1);
tx.vin[0].scriptSig.resize(10);
tx.vout.resize(1);
tx.vout[0].nValue = 42;
block.vtx.resize(3);
block.vtx[0] = MakeTransactionRef(tx);
block.nVersion = 42;
block.hashPrevBlock = InsecureRand256();
block.nBits = 0x207fffff;
tx.vin[0].prevout.hash = InsecureRand256();
tx.vin[0].prevout.n = 0;
block.vtx[1] = MakeTransactionRef(tx);
tx.vin.resize(FEC_CHUNK_SIZE / 38);
for (size_t i = 0; i < tx.vin.size(); i++) {
tx.vin[i].prevout.hash = InsecureRand256();
tx.vin[i].prevout.n = 0;
}
size_t tx2Size = GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
assert(tx2Size > FEC_CHUNK_SIZE && tx2Size < 2*FEC_CHUNK_SIZE);
block.vtx[2] = MakeTransactionRef(tx);
bool mutated;
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
assert(!mutated);
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus())) ++block.nNonce;
return block;
}
// Number of shared use_counts we expect for a tx we haven't touched
// == 2 (mempool + our copy from the GetSharedTx call)
#define SHARED_TX_OFFSET 2
BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
{
CTxMemPool pool;
TestMemPoolEntryHelper entry;
CBlock block(BuildBlockTestCase());
pool.addUnchecked(block.vtx[2]->GetHash(), entry.FromTx(*block.vtx[2]));
LOCK(pool.cs);
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
// Do a simple ShortTxIDs RT
{
CBlockHeaderAndShortTxIDs shortIDs(block, true);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
BOOST_CHECK( partialBlock.IsTxAvailable(0));
BOOST_CHECK(!partialBlock.IsTxAvailable(1));
BOOST_CHECK( partialBlock.IsTxAvailable(2));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
size_t poolSize = pool.size();
pool.removeRecursive(*block.vtx[2]);
BOOST_CHECK_EQUAL(pool.size(), poolSize - 1);
CBlock block2;
{
PartiallyDownloadedBlock tmp = partialBlock;
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_INVALID); // No transactions
partialBlock = tmp;
}
// Wrong transaction
{
PartiallyDownloadedBlock tmp = partialBlock;
partialBlock.FillBlock(block2, {block.vtx[2]}); // Current implementation doesn't check txn here, but don't require that
partialBlock = tmp;
}
bool mutated;
BOOST_CHECK(block.hashMerkleRoot != BlockMerkleRoot(block2, &mutated));
CBlock block3;
BOOST_CHECK(partialBlock.FillBlock(block3, {block.vtx[1]}) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block3.GetHash().ToString());
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block3, &mutated).ToString());
BOOST_CHECK(!mutated);
}
}
class TestHeaderAndShortIDs {
// Utility to encode custom CBlockHeaderAndShortTxIDs
public:
CBlockHeader header;
uint64_t nonce;
std::vector<uint64_t> shorttxids;
std::vector<PrefilledTransaction> prefilledtxn;
explicit TestHeaderAndShortIDs(const CBlockHeaderAndShortTxIDs& orig) {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << orig;
stream >> *this;
}
explicit TestHeaderAndShortIDs(const CBlock& block) :
TestHeaderAndShortIDs(CBlockHeaderAndShortTxIDs(block, true)) {}
uint64_t GetShortID(const uint256& txhash) const {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << *this;
CBlockHeaderAndShortTxIDs base;
stream >> base;
return base.GetShortID(txhash);
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(header);
READWRITE(nonce);
size_t shorttxids_size = shorttxids.size();
READWRITE(VARINT(shorttxids_size));
shorttxids.resize(shorttxids_size);
for (size_t i = 0; i < shorttxids.size(); i++) {
uint32_t lsb = shorttxids[i] & 0xffffffff;
uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
READWRITE(lsb);
READWRITE(msb);
shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
}
READWRITE(prefilledtxn);
}
};
BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
{
CTxMemPool pool;
TestMemPoolEntryHelper entry;
CBlock block(BuildBlockTestCase());
pool.addUnchecked(block.vtx[2]->GetHash(), entry.FromTx(*block.vtx[2]));
LOCK(pool.cs);
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
uint256 txhash;
// Test with pre-forwarding tx 1, but not coinbase
{
TestHeaderAndShortIDs shortIDs(block);
shortIDs.prefilledtxn.resize(1);
shortIDs.prefilledtxn[0] = {1, block.vtx[1]};
shortIDs.shorttxids.resize(2);
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[0]->GetHash());
shortIDs.shorttxids[1] = shortIDs.GetShortID(block.vtx[2]->GetHash());
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
BOOST_CHECK(!partialBlock.IsTxAvailable(0));
BOOST_CHECK( partialBlock.IsTxAvailable(1));
BOOST_CHECK( partialBlock.IsTxAvailable(2));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
CBlock block2;
{
PartiallyDownloadedBlock tmp = partialBlock;
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_INVALID); // No transactions
partialBlock = tmp;
}
// Wrong transaction
{
PartiallyDownloadedBlock tmp = partialBlock;
partialBlock.FillBlock(block2, {block.vtx[1]}); // Current implementation doesn't check txn here, but don't require that
partialBlock = tmp;
}
bool mutated;
BOOST_CHECK(block.hashMerkleRoot != BlockMerkleRoot(block2, &mutated));
CBlock block3;
PartiallyDownloadedBlock partialBlockCopy = partialBlock;
BOOST_CHECK(partialBlock.FillBlock(block3, {block.vtx[0]}) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block3.GetHash().ToString());
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block3, &mutated).ToString());
BOOST_CHECK(!mutated);
txhash = block.vtx[2]->GetHash();
block.vtx.clear();
block2.vtx.clear();
block3.vtx.clear();
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1); // + 1 because of partialBlockCopy.
}
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
}
BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
{
CTxMemPool pool;
TestMemPoolEntryHelper entry;
CBlock block(BuildBlockTestCase());
pool.addUnchecked(block.vtx[1]->GetHash(), entry.FromTx(*block.vtx[1]));
LOCK(pool.cs);
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
uint256 txhash;
// Test with pre-forwarding coinbase + tx 2 with tx 1 in mempool
{
TestHeaderAndShortIDs shortIDs(block);
shortIDs.prefilledtxn.resize(2);
shortIDs.prefilledtxn[0] = {0, block.vtx[0]};
shortIDs.prefilledtxn[1] = {1, block.vtx[2]}; // id == 1 as it is 1 after index 1
shortIDs.shorttxids.resize(1);
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[1]->GetHash());
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
BOOST_CHECK( partialBlock.IsTxAvailable(0));
BOOST_CHECK( partialBlock.IsTxAvailable(1));
BOOST_CHECK( partialBlock.IsTxAvailable(2));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
CBlock block2;
PartiallyDownloadedBlock partialBlockCopy = partialBlock;
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
bool mutated;
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block2, &mutated).ToString());
BOOST_CHECK(!mutated);
txhash = block.vtx[1]->GetHash();
block.vtx.clear();
block2.vtx.clear();
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1); // + 1 because of partialBlockCopy.
}
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
}
BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
{
CTxMemPool pool;
CMutableTransaction coinbase;
coinbase.vin.resize(1);
coinbase.vin[0].scriptSig.resize(10);
coinbase.vout.resize(1);
coinbase.vout[0].nValue = 42;
CBlock block;
block.vtx.resize(1);
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
block.nVersion = 42;
block.hashPrevBlock = InsecureRand256();
block.nBits = 0x207fffff;
bool mutated;
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
assert(!mutated);
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus())) ++block.nNonce;
// Test simple header round-trip with only coinbase
{
CBlockHeaderAndShortTxIDs shortIDs(block, false);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
BOOST_CHECK(partialBlock.IsTxAvailable(0));
CBlock block2;
std::vector<CTransactionRef> vtx_missing;
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block2, &mutated).ToString());
BOOST_CHECK(!mutated);
}
// Do a FEC-coded-block RT
{
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
CBlockHeaderAndLengthShortTxIDs headerAndIDs(block);
stream << headerAndIDs;
ChunkCodedBlock fecBlock(block, headerAndIDs);
BOOST_CHECK_EQUAL(fecBlock.GetCodedBlock().size(), 0);
CBlockHeaderAndLengthShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedChunkBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
size_t firstChunkProcessed;
while (!partialBlock.IsIterativeFillDone())
BOOST_CHECK(partialBlock.DoIterativeFill(firstChunkProcessed) == READ_STATUS_OK);
BOOST_CHECK(partialBlock.FinalizeBlock() == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), partialBlock.GetBlock()->GetHash().ToString());
bool mutated;
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(*partialBlock.GetBlock(), &mutated).ToString());
BOOST_CHECK(!mutated);
}
}
BOOST_AUTO_TEST_CASE(SimpleBlockFECRoundTripTest)
{
CTxMemPool pool;
TestMemPoolEntryHelper entry;
CBlock block(BuildBlockTestCase());
pool.addUnchecked(block.vtx[2]->GetHash(), entry.FromTx(*block.vtx[2]));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
// Do a FEC-coded-block RT
{
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
CBlockHeaderAndLengthShortTxIDs headerAndIDs(block);
stream << headerAndIDs;
ChunkCodedBlock fecBlock(block, headerAndIDs);
BOOST_CHECK_EQUAL(fecBlock.GetCodedBlock().size(), 2 * FEC_CHUNK_SIZE);
CBlockHeaderAndLengthShortTxIDs shortIDs2;
stream >> shortIDs2;
PartiallyDownloadedChunkBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
size_t firstChunkProcessed;
while (!partialBlock.IsIterativeFillDone())
BOOST_CHECK(partialBlock.DoIterativeFill(firstChunkProcessed) == READ_STATUS_OK);
// Check the partial block picked txn in order: tx2, *
BOOST_CHECK( partialBlock.IsChunkAvailable(0));
BOOST_CHECK(!partialBlock.IsChunkAvailable(1));
BOOST_CHECK(!memcmp(partialBlock.GetChunk(0), &fecBlock.GetCodedBlock()[0], FEC_CHUNK_SIZE));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
memcpy(partialBlock.GetChunk(1), &fecBlock.GetCodedBlock()[FEC_CHUNK_SIZE], FEC_CHUNK_SIZE);
partialBlock.MarkChunkAvailable(1);
BOOST_CHECK(partialBlock.FinalizeBlock() == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), partialBlock.GetBlock()->GetHash().ToString());
bool mutated;
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(*partialBlock.GetBlock(), &mutated).ToString());
BOOST_CHECK(!mutated);
}
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
}
#define DIV_CEIL(a, b) (((a) + (b) - 1) / (b))
BOOST_AUTO_TEST_CASE(FECedBlockFECRoundTripTest)
{
CTxMemPool pool;
TestMemPoolEntryHelper entry;
CBlock block(BuildBlockTestCase());
pool.addUnchecked(block.vtx[2]->GetHash(), entry.FromTx(*block.vtx[2]));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
// Do a FEC-coded-block RT
{
size_t header_size, block_size;
std::pair<std::unique_ptr<FECChunkType[]>, std::vector<uint32_t>> header_fec_chunks(std::piecewise_construct, std::forward_as_tuple(new FECChunkType[5]), std::forward_as_tuple(5));
std::pair<std::unique_ptr<FECChunkType[]>, std::vector<uint32_t>> block_fec_chunks(std::piecewise_construct, std::forward_as_tuple(new FECChunkType[5]), std::forward_as_tuple(5));
{
CBlockHeaderAndLengthShortTxIDs headerAndIDs(block);
ChunkCodedBlock fecBlock(block, headerAndIDs);
std::vector<unsigned char> header_data;
VectorOutputStream stream(&header_data, SER_NETWORK, PROTOCOL_VERSION);
stream << headerAndIDs;
BOOST_CHECK(BuildFECChunks(header_data, header_fec_chunks));
header_size = header_data.size();
BOOST_CHECK_EQUAL(fecBlock.GetCodedBlock().size(), 2 * FEC_CHUNK_SIZE);
BOOST_CHECK(BuildFECChunks(fecBlock.GetCodedBlock(), block_fec_chunks));
block_size = fecBlock.GetCodedBlock().size();
}
CBlockHeaderAndLengthShortTxIDs shortIDs2;
{
FECDecoder header_decoder(header_size);
// Two header chunks shoud be more than sufficient
BOOST_CHECK(header_decoder.ProvideChunk(&header_fec_chunks.first[0], header_fec_chunks.second[0]));
BOOST_CHECK(header_decoder.ProvideChunk(&header_fec_chunks.first[1], header_fec_chunks.second[1]));
BOOST_CHECK(header_decoder.DecodeReady());
std::vector<unsigned char> header_data(DIV_CEIL(header_size, FEC_CHUNK_SIZE) * FEC_CHUNK_SIZE);
for (size_t i = 0; i < DIV_CEIL(header_size, FEC_CHUNK_SIZE); i++)
memcpy(&header_data[i * FEC_CHUNK_SIZE], header_decoder.GetDataPtr(i), FEC_CHUNK_SIZE);
VectorInputStream stream(&header_data, SER_NETWORK, PROTOCOL_VERSION);
stream >> shortIDs2;
}
PartiallyDownloadedChunkBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
size_t firstChunkProcessed;
while (!partialBlock.IsIterativeFillDone())
BOOST_CHECK(partialBlock.DoIterativeFill(firstChunkProcessed) == READ_STATUS_OK);
// Check the partial block picked txn in order: tx2, *
BOOST_CHECK( partialBlock.IsChunkAvailable(0));
BOOST_CHECK(!partialBlock.IsChunkAvailable(1));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
std::vector<unsigned char> block_data(block_size);
{
FECDecoder block_decoder(block_size);
// Mempool gave us one chunk
BOOST_CHECK(block_decoder.ProvideChunk(partialBlock.GetChunk(0), 0));
// This (obviously) should not be sufficient
BOOST_CHECK(!block_decoder.DecodeReady());
BOOST_CHECK(block_decoder.ProvideChunk(&block_fec_chunks.first[2], block_fec_chunks.second[2]));
// This might be sufficient
if (!block_decoder.DecodeReady()) {
// This might not be sufficient...check that adding the same chunk does nothing if its not
BOOST_CHECK(block_decoder.ProvideChunk(&block_fec_chunks.first[2], block_fec_chunks.second[2]));
BOOST_CHECK(!block_decoder.DecodeReady());
// Adding another chunk might be sufficient
BOOST_CHECK(block_decoder.ProvideChunk(&block_fec_chunks.first[3], block_fec_chunks.second[3]));
if (!block_decoder.DecodeReady())
BOOST_CHECK(block_decoder.ProvideChunk(&block_fec_chunks.first[0], block_fec_chunks.second[0]));
// But three FEC chunks should definitely be enough
BOOST_CHECK(block_decoder.DecodeReady());
}
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE); i++)
memcpy(&block_data[i * FEC_CHUNK_SIZE], block_decoder.GetDataPtr(i), FEC_CHUNK_SIZE);
}
BOOST_CHECK(!memcmp(partialBlock.GetChunk(0), &block_data[0], FEC_CHUNK_SIZE));
memcpy(partialBlock.GetChunk(1), &block_data[FEC_CHUNK_SIZE], FEC_CHUNK_SIZE);
partialBlock.MarkChunkAvailable(1);
BOOST_CHECK(partialBlock.FinalizeBlock() == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), partialBlock.GetBlock()->GetHash().ToString());
bool mutated;
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(*partialBlock.GetBlock(), &mutated).ToString());
BOOST_CHECK(!mutated);
}
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
}
static void TestBlockWithMempool(const CBlock& block, CTxMemPool& pool) {
// Do a FEC-coded-block RT
size_t header_size, block_size;
std::vector<std::pair<size_t, std::vector<unsigned char> > > header_chunks;
std::vector<std::pair<size_t, std::vector<unsigned char> > > block_chunks;
{
CBlockHeaderAndLengthShortTxIDs headerAndIDs(block);
ChunkCodedBlock fecBlock(block, headerAndIDs);
std::vector<unsigned char> header_data;
VectorOutputStream stream(&header_data, SER_NETWORK, PROTOCOL_VERSION);
stream << headerAndIDs;
header_size = header_data.size();
size_t header_fec_chunk_count = DIV_CEIL(header_size, FEC_CHUNK_SIZE) + 10;
std::pair<std::unique_ptr<FECChunkType[]>, std::vector<uint32_t>> header_fec_chunks(std::piecewise_construct, std::forward_as_tuple(new FECChunkType[header_fec_chunk_count]), std::forward_as_tuple(header_fec_chunk_count));
BOOST_CHECK(BuildFECChunks(header_data, header_fec_chunks));
for (size_t i = 0; i < DIV_CEIL(header_size, FEC_CHUNK_SIZE); i++) {
std::vector<unsigned char>::iterator endit = header_data.begin() + std::min(header_size, (i+1) * FEC_CHUNK_SIZE);
header_chunks.push_back(std::make_pair(i,
std::vector<unsigned char>(header_data.begin() + i * FEC_CHUNK_SIZE, endit)));
header_chunks.back().second.resize(FEC_CHUNK_SIZE);
}
for (size_t i = 0; i < DIV_CEIL(header_size, FEC_CHUNK_SIZE) + 10; i++)
header_chunks.push_back(std::make_pair(header_fec_chunks.second[i],
std::vector<unsigned char>(((unsigned char*)&header_fec_chunks.first[i]), ((unsigned char*)&header_fec_chunks.first[i]) + FEC_CHUNK_SIZE)));
block_size = fecBlock.GetCodedBlock().size();
BOOST_CHECK(block_size % FEC_CHUNK_SIZE == 0);
size_t block_fec_chunk_count = DIV_CEIL(block_size, FEC_CHUNK_SIZE) + 10;
std::pair<std::unique_ptr<FECChunkType[]>, std::vector<uint32_t>> block_fec_chunks(std::piecewise_construct, std::forward_as_tuple(new FECChunkType[block_fec_chunk_count]), std::forward_as_tuple(block_fec_chunk_count));
BOOST_CHECK(BuildFECChunks(fecBlock.GetCodedBlock(), block_fec_chunks));
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE); i++)
block_chunks.push_back(std::make_pair(i,
std::vector<unsigned char>(fecBlock.GetCodedBlock().begin() + i * FEC_CHUNK_SIZE, fecBlock.GetCodedBlock().begin() + (i+1) * FEC_CHUNK_SIZE)));
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE) + 10; i++)
block_chunks.push_back(std::make_pair(block_fec_chunks.second[i],
std::vector<unsigned char>(((unsigned char*)&block_fec_chunks.first[i]), ((unsigned char*)&block_fec_chunks.first[i]) + FEC_CHUNK_SIZE)));
}
CBlockHeaderAndLengthShortTxIDs shortIDs;
{
size_t header_chunk_count = DIV_CEIL(header_size, FEC_CHUNK_SIZE);
FECDecoder header_decoder(header_size);
// Pass in random chunks until we have enough
std::random_shuffle(header_chunks.begin(), header_chunks.end());
for (size_t i = 0; i < header_chunks.size() - 5 && !header_decoder.DecodeReady(); i++)
BOOST_CHECK(header_decoder.ProvideChunk(&header_chunks[i].second[0], header_chunks[i].first));
BOOST_CHECK(header_decoder.DecodeReady());
std::vector<unsigned char> header_data(header_chunk_count * FEC_CHUNK_SIZE);
for (size_t i = 0; i < header_chunk_count; i++)
memcpy(&header_data[i * FEC_CHUNK_SIZE], header_decoder.GetDataPtr(i), FEC_CHUNK_SIZE);
VectorInputStream stream(&header_data, SER_NETWORK, PROTOCOL_VERSION);
stream >> shortIDs;
}
PartiallyDownloadedChunkBlock partialBlock(&pool);
BOOST_CHECK(partialBlock.InitData(shortIDs, extra_txn) == READ_STATUS_OK);
size_t firstChunkProcessed;
while (!partialBlock.IsIterativeFillDone())
BOOST_CHECK(partialBlock.DoIterativeFill(firstChunkProcessed) == READ_STATUS_OK);
if (!partialBlock.IsBlockAvailable()) {
FECDecoder block_decoder(block_size);
std::vector<std::pair<size_t, std::vector<unsigned char> > > block_chunks_sorted(block_chunks);
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE); i++) {
if (partialBlock.IsChunkAvailable(i)) {
BOOST_CHECK(block_decoder.ProvideChunk(partialBlock.GetChunk(i), i));
BOOST_CHECK(!memcmp(partialBlock.GetChunk(i), &block_chunks_sorted[i].second[0], FEC_CHUNK_SIZE));
}
}
// Pass in random chunks until we have enough
std::random_shuffle(block_chunks.begin(), block_chunks.end());
for (size_t i = 0; i < block_chunks.size() - 5 && !block_decoder.DecodeReady(); i++)
BOOST_CHECK(block_decoder.ProvideChunk(&block_chunks[i].second[0], block_chunks[i].first));
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE); i++) {
if (!partialBlock.IsChunkAvailable(i)) {
memcpy(partialBlock.GetChunk(i), block_decoder.GetDataPtr(i), FEC_CHUNK_SIZE);
partialBlock.MarkChunkAvailable(i);
}
}
for (size_t i = 0; i < DIV_CEIL(block_size, FEC_CHUNK_SIZE); i++) {
BOOST_CHECK(partialBlock.IsChunkAvailable(i));
BOOST_CHECK(!memcmp(partialBlock.GetChunk(i), &block_chunks_sorted[i].second[0], FEC_CHUNK_SIZE));
}
}
BOOST_CHECK(partialBlock.FinalizeBlock() == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), partialBlock.GetBlock()->GetHash().ToString());
bool mutated;
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(*partialBlock.GetBlock(), &mutated).ToString());
BOOST_CHECK(!mutated);
}
BOOST_AUTO_TEST_CASE(RealFECedBlockRoundTripTest)
{
CBlock block;
CDataStream stream((const char*)blockencodings_tests::block413567,
(const char*)&blockencodings_tests::block413567[sizeof(blockencodings_tests::block413567)],
SER_NETWORK, PROTOCOL_VERSION);
stream >> block;
bool mutated;
assert(block.hashMerkleRoot == BlockMerkleRoot(block, &mutated));
assert(!mutated);
assert(block.GetHash() == uint256S("0000000000000000025aff8be8a55df8f89c77296db6198f272d6577325d4069"));
std::mt19937_64 g(0xdeadbeef);
for (size_t i = 0; i < 10; i++) {
std::vector<CTransactionRef> vtx2(block.vtx.begin() + 1, block.vtx.end());
std::shuffle(vtx2.begin(), vtx2.end(), g);
size_t k = 0;
for (size_t j = 1220; k < vtx2.size(); j /= 5) { // 1220 txn, 244 txn, 48 txn, 10 txn a few times
j = std::max(j, (size_t)10);
CTxMemPool pool;
TestMemPoolEntryHelper entry;
for (const size_t k_init = k; k - k_init < j && k < vtx2.size(); k++)
pool.addUnchecked(vtx2[k]->GetHash(), entry.FromTx(*vtx2[k]));
TestBlockWithMempool(block, pool);
}
}
// Test with an empty mempool
CTxMemPool pool;
TestBlockWithMempool(block, pool);
// Test with all txn (except coinbase) available in mempool
TestMemPoolEntryHelper entry;
for (size_t i = 1; i < block.vtx.size(); i++)
pool.addUnchecked(block.vtx[i]->GetHash(), entry.FromTx(*block.vtx[i]));
TestBlockWithMempool(block, pool);
}
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
BlockTransactionsRequest req1;
req1.blockhash = InsecureRand256();
req1.indexes.resize(4);
req1.indexes[0] = 0;
req1.indexes[1] = 1;
req1.indexes[2] = 3;
req1.indexes[3] = 4;
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << req1;
BlockTransactionsRequest req2;
stream >> req2;
BOOST_CHECK_EQUAL(req1.blockhash.ToString(), req2.blockhash.ToString());
BOOST_CHECK_EQUAL(req1.indexes.size(), req2.indexes.size());
BOOST_CHECK_EQUAL(req1.indexes[0], req2.indexes[0]);
BOOST_CHECK_EQUAL(req1.indexes[1], req2.indexes[1]);
BOOST_CHECK_EQUAL(req1.indexes[2], req2.indexes[2]);
BOOST_CHECK_EQUAL(req1.indexes[3], req2.indexes[3]);
}
BOOST_AUTO_TEST_SUITE_END()