forked from UlordChain/UlordChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaimtrie_tests.cpp
More file actions
1009 lines (744 loc) · 32.7 KB
/
claimtrie_tests.cpp
File metadata and controls
1009 lines (744 loc) · 32.7 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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015 The LBRY Foundation
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://opensource.org/licenses/mit-license.php
#include "main.h"
#include "consensus/validation.h"
#include "consensus/merkle.h"
#include "primitives/transaction.h"
#include "miner.h"
#include "txmempool.h"
#include "claimtrie.h"
#include "nameclaim.h"
#include "arith_uint256.h"
#include "coins.h"
#include "streams.h"
#include "chainparams.h"
#include "policy/policy.h"
#include "pow.h"
#include <boost/test/unit_test.hpp>
#include <iostream>
#include "test/test_ulord.h"
#include <string>
using namespace std;
CScript scriptPubKey = CScript() << OP_TRUE;
BOOST_FIXTURE_TEST_SUITE(claimtrie_tests, RegTestingSetup)
CMutableTransaction BuildTransaction(const uint256& prevhash)
{
CMutableTransaction tx;
tx.nVersion = 1;
tx.nLockTime = 0;
tx.vin.resize(1);
tx.vout.resize(1);
tx.vin[0].prevout.hash = prevhash;
tx.vin[0].prevout.n = 0;
tx.vin[0].scriptSig = CScript();
tx.vin[0].nSequence = std::numeric_limits<unsigned int>::max();
tx.vout[0].scriptPubKey = CScript();
tx.vout[0].nValue = 0;
return tx;
}
CMutableTransaction BuildTransaction(const CMutableTransaction& prev, uint32_t prevout=0, unsigned int numOutputs=1)
{
CMutableTransaction tx;
tx.nVersion = 1;
tx.nLockTime = 0;
tx.vin.resize(1);
tx.vout.resize(numOutputs);
tx.vin[0].prevout.hash = prev.GetHash();
tx.vin[0].prevout.n = prevout;
tx.vin[0].scriptSig = CScript();
tx.vin[0].nSequence = std::numeric_limits<unsigned int>::max();
CAmount valuePerOutput = prev.vout[prevout].nValue;
unsigned int numOutputsCopy = numOutputs;
while ((numOutputsCopy = numOutputsCopy >> 1) > 0)
{
valuePerOutput = valuePerOutput >> 1;
}
for (unsigned int i = 0; i < numOutputs; ++i)
{
tx.vout[i].scriptPubKey = CScript();
tx.vout[i].nValue = valuePerOutput;
}
return tx;
}
CMutableTransaction BuildTransaction(const CTransaction& prev, uint32_t prevout=0, unsigned int numOutputs=1)
{
return BuildTransaction(CMutableTransaction(prev), prevout, numOutputs);
}
void AddToMempool(CMutableTransaction& tx)
{
//CCoinsView dummy;
//CCoinsViewCache view(&dummy);
//CAmount inChainInputValue;
//double dPriority = view.GetPriority(tx, chainActive.Height(), inChainInputValue);
//unsigned int nSigOps = GetLegacySigOpCount(tx);
//nSigOps += GetP2SHSigOpCount(tx, view);
//LockPoints lp;
//BOOST_CHECK(CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp));
//mempool.addUnchecked(tx.GetHash(), CTxMemPoolEntry(tx, 0, GetTime(), 111.1, chainActive.Height(), mempool.HasNoInputsOf(tx), 10000000000, false, nSigOps, lp));
CValidationState state;
BOOST_CHECK(AcceptToMemoryPool(mempool, state, tx, false, NULL));
//TestMemPoolEntryHelper entry;
//entry.nFee = 11;
//entry.dPriority = 111.0;
//entry.nHeight = 11;
//mempool.addUnchecked(tx.GetHash(), entry.Fee(10000).Time(GetTime()).FromTx(tx));
}
bool CreateBlock(CBlockTemplate* pblocktemplate)
{
static int unique_block_counter = 0;
const CChainParams& chainparams = Params(CBaseChainParams::REGTEST);
CBlock* pblock = &pblocktemplate->block;
pblock->nVersion = 1;
pblock->nTime = chainActive.Tip()->GetBlockTime()+Params().GetConsensus().nPowTargetSpacing;
CMutableTransaction txCoinbase(pblock->vtx[0]);
txCoinbase.vin[0].scriptSig = CScript() << CScriptNum(unique_block_counter++) << CScriptNum(chainActive.Height());
txCoinbase.vout[0].nValue = GetBlockSubsidy(chainActive.Height() + 1, chainparams.GetConsensus());
pblock->vtx[0] = CTransaction(txCoinbase);
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
pblock->nBits=GetNextWorkRequired(chainActive.Tip(),pblock,chainparams.GetConsensus());
for (arith_uint256 i = 0; ; ++i)
{
pblock->nNonce = ArithToUint256(i);
if (CheckProofOfWork(pblock->GetHash(), pblock->nBits, chainparams.GetConsensus()))
{
break;
}
}
CValidationState state;
bool success = (ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL) && state.IsValid() && pblock->GetHash() == chainActive.Tip()->GetBlockHash());
pblock->hashPrevBlock = pblock->GetHash();
return success;
}
bool RemoveBlock(uint256& blockhash)
{
CValidationState state;
if (mapBlockIndex.count(blockhash) == 0)
return false;
CBlockIndex* pblockindex = mapBlockIndex[blockhash];
InvalidateBlock(state, Params().GetConsensus(), pblockindex);
if (state.IsValid())
{
ActivateBestChain(state, Params());
}
mempool.clear();
return state.IsValid();
}
bool CreateCoinbases(unsigned int num_coinbases, std::vector<CTransaction>& coinbases)
{
CBlockTemplate *pblocktemplate;
coinbases.clear();
BOOST_CHECK(pblocktemplate = CreateNewBlock(Params(), scriptPubKey));
BOOST_CHECK(pblocktemplate->block.vtx.size() == 1);
pblocktemplate->block.hashPrevBlock = chainActive.Tip()->GetBlockHash();
for (unsigned int i = 0; i < 100 + num_coinbases; ++i)
{
BOOST_CHECK(CreateBlock(pblocktemplate));
if (coinbases.size() < num_coinbases)
coinbases.push_back(CTransaction(pblocktemplate->block.vtx[0]));
}
delete pblocktemplate;
return true;
}
bool CreateBlocks(unsigned int num_blocks, unsigned int num_txs)
{
CBlockTemplate *pblocktemplate;
BOOST_CHECK(pblocktemplate = CreateNewBlock(Params(), scriptPubKey));
BOOST_CHECK(pblocktemplate->block.vtx.size() == num_txs);
pblocktemplate->block.hashPrevBlock = chainActive.Tip()->GetBlockHash();
for (unsigned int i = 0; i <num_blocks; ++i)
{
BOOST_CHECK(CreateBlock(pblocktemplate));
}
delete pblocktemplate;
return true;
}
BOOST_AUTO_TEST_CASE(claimtrie_insert_update_claim)
{
fRequireStandard = false;
BOOST_CHECK(pclaimTrie->nCurrentHeight == chainActive.Height() + 1);
LOCK(cs_main);
std::string sName1("atest");
std::string sName2("btest");
std::string sName3("atest123");
std::string sValue1("testa");
std::string sValue2("testb");
std::string sValue3("123testa");
std::vector<unsigned char> vchName1(sName1.begin(), sName1.end());
std::vector<unsigned char> vchName2(sName2.begin(), sName2.end());
std::vector<unsigned char> vchName3(sName3.begin(), sName3.end());
std::vector<unsigned char> vchValue1(sValue1.begin(), sValue1.end());
std::vector<unsigned char> vchValue2(sValue2.begin(), sValue2.end());
std::vector<unsigned char> vchValue3(sValue3.begin(), sValue3.end());
std::vector<CTransaction> coinbases;
BOOST_CHECK(CreateCoinbases(6, coinbases));
uint256 hash0(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
//BOOST_CHECK(pclaimTrie->getMerkleHash() == hash0);
CMutableTransaction tx1 = BuildTransaction(coinbases[1]);
tx1.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName1 << vchValue1 << OP_2DROP << OP_DROP << OP_TRUE;
uint160 tx1ClaimId = ClaimIdHash(tx1.GetHash(), 0);
std::vector<unsigned char> vchTx1ClaimId(tx1ClaimId.begin(), tx1ClaimId.end());
COutPoint tx1OutPoint(tx1.GetHash(), 0);
CMutableTransaction tx2 = BuildTransaction(coinbases[1]);
tx2.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName2 << vchValue2 << OP_2DROP << OP_DROP << OP_TRUE;
tx2.vout[0].nValue = tx1.vout[0].nValue - 1;
COutPoint tx2OutPoint(tx2.GetHash(), 0);
CMutableTransaction tx3 = BuildTransaction(tx1);
tx3.vout[0].scriptPubKey = CScript() << OP_UPDATE_CLAIM << vchName1 << vchTx1ClaimId << vchValue1 << OP_2DROP << OP_2DROP << OP_TRUE;
tx3.vout[0].nValue -= 10000;
COutPoint tx3OutPoint(tx3.GetHash(), 0);
CMutableTransaction tx4 = BuildTransaction(tx2);
tx4.vout[0].scriptPubKey = CScript() << OP_TRUE;
COutPoint tx4OutPoint(tx4.GetHash(), 0);
CMutableTransaction tx5 = BuildTransaction(coinbases[2]);
tx5.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName2 << vchValue2 << OP_2DROP << OP_DROP << OP_TRUE;
COutPoint tx5OutPoint(tx5.GetHash(), 0);
CMutableTransaction tx6 = BuildTransaction(tx3);
tx6.vout[0].scriptPubKey = CScript() << OP_TRUE;
COutPoint tx6OutPoint(tx6.GetHash(), 0);
CMutableTransaction tx7 = BuildTransaction(coinbases[3]);
tx7.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName1 << vchValue2 << OP_2DROP << OP_DROP << OP_TRUE;
tx7.vout[0].nValue = tx1.vout[0].nValue - 10001;
uint160 tx7ClaimId = ClaimIdHash(tx7.GetHash(), 0);
std::vector<unsigned char> vchTx7ClaimId(tx7ClaimId.begin(), tx7ClaimId.end());
COutPoint tx7OutPoint(tx7.GetHash(), 0);
CMutableTransaction tx8 = BuildTransaction(tx3, 0, 2);
tx8.vout[0].scriptPubKey = CScript() << OP_UPDATE_CLAIM << vchName1 << vchTx1ClaimId << vchValue1 << OP_2DROP << OP_2DROP << OP_TRUE;
tx8.vout[0].nValue = tx8.vout[0].nValue - 1;
tx8.vout[1].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName1 << vchValue2 << OP_2DROP << OP_DROP << OP_TRUE;
COutPoint tx8OutPoint0(tx8.GetHash(), 0);
COutPoint tx8OutPoint1(tx8.GetHash(), 1);
CMutableTransaction tx9 = BuildTransaction(tx7);
tx9.vout[0].scriptPubKey = CScript() << OP_UPDATE_CLAIM << vchName1 << vchTx7ClaimId << vchValue2 << OP_2DROP << OP_2DROP << OP_TRUE;
tx9.vout[0].nValue -= 10000;
COutPoint tx9OutPoint(tx9.GetHash(), 0);
CMutableTransaction tx10 = BuildTransaction(coinbases[4]);
tx10.vout[0].scriptPubKey = CScript() << OP_UPDATE_CLAIM << vchName1 << vchTx1ClaimId << vchValue1 << OP_2DROP << OP_2DROP << OP_TRUE;
COutPoint tx10OutPoint(tx10.GetHash(), 0);
CMutableTransaction tx11 = BuildTransaction(tx10);
tx11.vout[0].scriptPubKey = CScript() << OP_UPDATE_CLAIM << vchName1 << vchTx1ClaimId << vchValue1 << OP_2DROP << OP_2DROP << OP_TRUE;
COutPoint tx11OutPoint(tx11.GetHash(), 0);
CMutableTransaction tx12 = BuildTransaction(tx10);
tx12.vout[0].scriptPubKey = CScript() << OP_TRUE;
COutPoint tx12OutPoint(tx12.GetHash(), 0);
CMutableTransaction tx13 = BuildTransaction(coinbases[5]);
tx13.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName3 << vchValue3 << OP_2DROP << OP_DROP << OP_TRUE;
COutPoint tx13OutPoint(tx13.GetHash(), 0);
CClaimValue val;
int nThrowaway;
std::vector<uint256> blocks_to_invalidate;
// Verify claims for uncontrolled names go in immediately
AddToMempool(tx7);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx7OutPoint);
// Verify claims for controlled names are delayed, and that the bigger claim wins when inserted
BOOST_CHECK(CreateBlocks(5, 1));
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(5, 1));
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(1, 1));
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx1OutPoint);
// Verify updates to the best claim get inserted immediately, and others don't.
AddToMempool(tx3);
AddToMempool(tx9);
BOOST_CHECK(CreateBlocks(1, 3));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx1OutPoint));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx7OutPoint));
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx3OutPoint);
BOOST_CHECK(pclaimTrie->haveClaimInQueue(sName1, tx9OutPoint, nThrowaway));
// Disconnect all blocks until the first block, and then reattach them, in memory only
//FlushStateToDisk();
CCoinsViewCache coins(pcoinsTip);
CClaimTrieCache trieCache(pclaimTrie);
CBlockIndex* pindexState = chainActive.Tip();
CValidationState state;
CBlockIndex* pindex;
for (pindex = chainActive.Tip(); pindex && pindex->pprev; pindex=pindex->pprev)
{
CBlock block;
BOOST_CHECK(ReadBlockFromDisk(block, pindex, Params().GetConsensus()));
if (pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage)
{
bool fClean = true;
BOOST_CHECK(DisconnectBlock(block, state, pindex, coins, trieCache, &fClean));
pindexState = pindex->pprev;
}
}
while (pindex != chainActive.Tip())
{
pindex = chainActive.Next(pindex);
CBlock block;
BOOST_CHECK(ReadBlockFromDisk(block, pindex, Params().GetConsensus()));
BOOST_CHECK(ConnectBlock(block, state, pindex,coins,trieCache));
}
// Roll back the last block, make sure tx1 and tx7 are put back in the trie
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx1OutPoint);
BOOST_CHECK(pclaimTrie->haveClaim(sName1, tx7OutPoint));
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->empty());
// Roll all the way back, make sure all txs are out of the trie
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(!pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->getMerkleHash() == hash0);
BOOST_CHECK(pclaimTrie->queueEmpty());
// Test undoing a claim before the claim gets into the trie
// Put tx1 in the chain, then advance a few blocks.
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(10, 1));
// Put tx7 in the chain, verify it goes into the queue
AddToMempool(tx7);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
// Undo that block and make sure it's not in the queue
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// Make sure it's not in the queue
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// Go back to the beginning
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// Test spend a claim which was just inserted into the trie
// Immediately spend tx2 with tx4, verify nothing gets put in the trie
AddToMempool(tx2);
AddToMempool(tx4);
BOOST_CHECK(CreateBlocks(1, 3));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// Verify that if a claim in the queue is spent, it does not get into the trie
// Put tx5 into the chain, advance until it's in the trie for a few blocks
AddToMempool(tx5);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(CreateBlocks(5, 1));
// Put tx2 into the chain, and then advance a few blocks but not far enough for it to get into the trie
AddToMempool(tx2);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(pclaimTrie->haveClaimInQueue(sName2, tx2OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(3, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
// Spend tx2 with tx4, and then advance to where tx2 would be inserted into the trie and verify it hasn't happened
AddToMempool(tx4);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(5, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->haveClaim(sName2, tx2OutPoint));
// Undo spending tx2 with tx4, and then advance and verify tx2 is inserted into the trie when it should be
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(CreateBlocks(1, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->haveClaimInQueue(sName2, tx2OutPoint, nThrowaway));
BOOST_CHECK(CreateBlocks(2, 1));
BOOST_CHECK(pclaimTrie->haveClaim(sName2, tx2OutPoint));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName2, val));
BOOST_CHECK(val.outPoint == tx5OutPoint);
BOOST_CHECK(pclaimTrie->haveClaim(sName2, tx2OutPoint));
// Test undoing a spend which involves a claim in the trie
// spend tx2, which is in the trie, with tx4
AddToMempool(tx4);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->haveClaim(sName2, tx2OutPoint));
// undo spending tx2 with tx4, and verify tx2 is back in the trie
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName2, val));
BOOST_CHECK(val.outPoint == tx5OutPoint);
BOOST_CHECK(pclaimTrie->haveClaim(sName2, tx2OutPoint));
// roll back to the beginning
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// Test undoing a spent update which updated a claim still in the queue
// Create the claim that will cause the others to be in the queue
AddToMempool(tx7);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(CreateBlocks(5, 1));
// Create the original claim (tx1)
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
//blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->haveClaimInQueue(sName1, tx1OutPoint, nThrowaway));
// move forward some, but not far enough for the claim to get into the trie
BOOST_CHECK(CreateBlocks(2, 1));
// update the original claim (tx3 spends tx1)
AddToMempool(tx3);
BOOST_CHECK(CreateBlocks(1, 2));
//blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx1OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx1OutPoint));
BOOST_CHECK(pclaimTrie->haveClaimInQueue(sName1, tx3OutPoint, nThrowaway));
// spend the update (tx6 spends tx3)
AddToMempool(tx6);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx3OutPoint));
// undo spending the update (undo tx6 spending tx3)
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
// make sure the update (tx3) still goes into effect when it's supposed to
BOOST_CHECK(CreateBlocks(8, 1));
BOOST_CHECK(CreateBlocks(1, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx3OutPoint);
BOOST_CHECK(CreateBlocks(1, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->haveClaim(sName1, tx3OutPoint));
// roll all the way back
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// Test undoing an spent update which updated the best claim to a name
// move forward until the original claim is inserted into the trie
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(5, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx1OutPoint);
// update the original claim (tx3 spends tx1)
AddToMempool(tx3);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx3OutPoint);
// spend the update (tx6 spends tx3)
AddToMempool(tx6);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// undo spending the update (undo tx6 spending tx3)
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx3OutPoint);
// Test having two updates to a claim in the same transaction
// Add both txouts (tx8 spends tx3)
AddToMempool(tx8);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
// ensure txout 0 made it into the trie and txout 1 did not
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx8OutPoint0);
// roll forward until tx8 output 1 gets into the trie
BOOST_CHECK(CreateBlocks(6, 1));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(!pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(1, 1));
// ensure txout 1 made it into the trie and is now in control
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx8OutPoint1);
// roll back to before tx8
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// roll all the way back
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// make sure invalid updates don't wreak any havoc
// put tx1 into the trie
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->getInfoForName(sName1, val));
BOOST_CHECK(val.outPoint == tx1OutPoint);
BOOST_CHECK(pclaimTrie->queueEmpty());
// advance a few blocks
BOOST_CHECK(CreateBlocks(5, 1));
// put in bad tx10
AddToMempool(tx10);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx10OutPoint, nThrowaway));
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll back, make sure nothing bad happens
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// put it back in
AddToMempool(tx10);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx10OutPoint, nThrowaway));
BOOST_CHECK(pclaimTrie->queueEmpty());
// update it
AddToMempool(tx11);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx11OutPoint, nThrowaway));
BOOST_CHECK(pclaimTrie->queueEmpty());
BOOST_CHECK(CreateBlocks(10, 1));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx11OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx11OutPoint));
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll back to before the update
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx11OutPoint));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx11OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx10OutPoint));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx10OutPoint, nThrowaway));
BOOST_CHECK(pclaimTrie->queueEmpty());
// make sure tx10 would have gotten into the trie, then run tests again
BOOST_CHECK(CreateBlocks(10, 1));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx10OutPoint));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx10OutPoint, nThrowaway));
BOOST_CHECK(pclaimTrie->queueEmpty());
// update it
AddToMempool(tx11);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx11OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx11OutPoint));
BOOST_CHECK(pclaimTrie->queueEmpty());
// make sure tx11 would have gotten into the trie
BOOST_CHECK(CreateBlocks(20, 1));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx11OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx11OutPoint));
BOOST_CHECK(!pclaimTrie->haveClaimInQueue(sName1, tx10OutPoint, nThrowaway));
BOOST_CHECK(!pclaimTrie->haveClaim(sName1, tx10OutPoint));
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll all the way back
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// Put tx10 and tx11 in without tx1 in
AddToMempool(tx10);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// update with tx11
AddToMempool(tx11);
BOOST_CHECK(CreateBlocks(1, 2));
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll back to before tx11
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// spent tx10 with tx12 instead which is not a claim operation of any kind
AddToMempool(tx12);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll all the way back
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
// make sure all claim for names which exist in the trie but have no
// values get inserted immediately
blocks_to_invalidate.push_back(chainActive.Tip()->GetBlockHash());
AddToMempool(tx13);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
AddToMempool(tx1);
BOOST_CHECK(CreateBlocks(1, 2));
BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK(pclaimTrie->queueEmpty());
// roll back
BOOST_CHECK(RemoveBlock(blocks_to_invalidate.back()));
blocks_to_invalidate.pop_back();
}
BOOST_AUTO_TEST_CASE(claimtrienode_serialize_unserialize)
{
fRequireStandard = false;
CDataStream ss(SER_DISK, 0);
uint160 hash160;
std::string name="name";
std::string addr="addr";
CClaimTrieNode n1;
CClaimTrieNode n2;
CClaimValue throwaway;
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
n1.hash.SetHex("0000000000000000000000000000000000000000000000000000000000000001");
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
n1.hash.SetHex("a79e8a5b28f7fa5e8836a4b48da9988bdf56ce749f81f413cb754f963a516200");
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
CClaimValue v1(COutPoint(uint256S("0000000000000000000000000000000000000000000000000000000000000001"), 0), hash160, 50, 0, 100,addr,name);
CClaimValue v2(COutPoint(uint256S("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"), 1), hash160, 100, 1, 101,addr,name);
n1.insertClaim(v1);
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
n1.insertClaim(v2);
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
n1.removeClaim(v1.outPoint, throwaway);
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
n1.removeClaim(v2.outPoint, throwaway);
BOOST_CHECK(n1 != n2);
ss << n1;
ss >> n2;
BOOST_CHECK(n1 == n2);
}
bool verify_proof(const CClaimTrieProof proof, uint256 rootHash, const std::string& name)
{
uint256 previousComputedHash;
std::string computedReverseName;
bool verifiedValue = false;
for (std::vector<CClaimTrieProofNode>::const_reverse_iterator itNodes = proof.nodes.rbegin(); itNodes != proof.nodes.rend(); ++itNodes)
{
bool foundChildInChain = false;
std::vector<unsigned char> vchToHash;
for (std::vector<std::pair<unsigned char, uint256> >::const_iterator itChildren = itNodes->children.begin(); itChildren != itNodes->children.end(); ++itChildren)
{
vchToHash.push_back(itChildren->first);
uint256 childHash;
if (itChildren->second.IsNull())
{
if (previousComputedHash.IsNull())
{
return false;
}
if (foundChildInChain)
{
return false;
}
foundChildInChain = true;
computedReverseName += itChildren->first;
childHash = previousComputedHash;
}
else
{
childHash = itChildren->second;
}
vchToHash.insert(vchToHash.end(), childHash.begin(), childHash.end());
}
if (itNodes != proof.nodes.rbegin() && !foundChildInChain)
{
return false;
}
if (itNodes->hasValue)
{
uint256 valHash;
if (itNodes->valHash.IsNull())
{
if (itNodes != proof.nodes.rbegin())
{
return false;
}
if (!proof.hasValue)
{
return false;
}
valHash = getValueHash(proof.outPoint,
proof.nHeightOfLastTakeover);
verifiedValue = true;
}
else
{
valHash = itNodes->valHash;
}
vchToHash.insert(vchToHash.end(), valHash.begin(), valHash.end());
}
else if (proof.hasValue && itNodes == proof.nodes.rbegin())
{
return false;
}
CHash256 hasher;
std::vector<unsigned char> vchHash(hasher.OUTPUT_SIZE);
hasher.Write(vchToHash.data(), vchToHash.size());
hasher.Finalize(&(vchHash[0]));
uint256 calculatedHash(vchHash);
previousComputedHash = calculatedHash;
}
if (previousComputedHash != rootHash)
{
return false;
}
if (proof.hasValue && !verifiedValue)
{
return false;
}
std::string::reverse_iterator itComputedName = computedReverseName.rbegin();
std::string::const_iterator itName = name.begin();
for (; itName != name.end() && itComputedName != computedReverseName.rend(); ++itName, ++itComputedName)
{
if (*itName != *itComputedName)