Repository navigation
Expand file tree
/
Copy pathevo_snapshot_tests.cpp
More file actions
1418 lines (1311 loc) · 69.9 KB
/
Copy pathevo_snapshot_tests.cpp
File metadata and controls
1418 lines (1311 loc) · 69.9 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) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <clientversion.h>
#include <consensus/amount.h>
#include <consensus/merkle.h>
#include <evo/cbtx.h>
#include <evo/creditpool.h>
#include <evo/evodb.h>
#include <evo/mnhftx.h>
#include <evo/netinfo.h>
#include <evo/snapshot.h>
#include <hash.h>
#include <llmq/blockprocessor.h>
#include <llmq/context.h>
#include <llmq/quorumsman.h>
#include <llmq/signhash.h>
#include <llmq/snapshot.h>
#include <llmq/utils.h>
#include <masternode/meta.h>
#include <node/context.h>
#include <streams.h>
#include <validation.h>
#include <versionbits.h>
#include <algorithm>
#include <cstddef>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <boost/test/unit_test.hpp>
namespace {
uint256 H(uint8_t value)
{
uint256 hash;
hash.begin()[0] = value;
return hash;
}
uint256 CollidingH(uint8_t suffix)
{
uint256 hash;
std::fill_n(hash.begin(), 8, 0xa5);
hash.begin()[8] = suffix;
return hash;
}
uint160 H160(uint8_t value)
{
uint160 hash;
hash.begin()[0] = value;
return hash;
}
CDeterministicMNCPtr MN(uint64_t internal_id, uint8_t hash_suffix, MnType type, int version, uint8_t address_tag)
{
auto state{std::make_shared<CDeterministicMNState>()};
state->nVersion = version;
state->nRegisteredHeight = 10 + internal_id;
state->nLastPaidHeight = 20 + internal_id;
state->nPoSePenalty = internal_id;
state->keyIDOwner = CKeyID{H160(address_tag)};
state->keyIDVoting = CKeyID{H160(address_tag + 20)};
state->scriptPayout = CScript{} << OP_RETURN << std::vector<unsigned char>{address_tag, 1};
state->scriptOperatorPayout = CScript{} << OP_RETURN << std::vector<unsigned char>{address_tag, 2};
state->netInfo = NetInfoInterface::MakeNetInfo(version);
BOOST_REQUIRE_EQUAL(state->netInfo->AddEntry(NetInfoPurpose::CORE_P2P,
strprintf("1.1.1.%d:%d", address_tag, Params().GetDefaultPort())),
NetInfoStatus::Success);
if (type == MnType::Evo) {
state->platformNodeID = H160(address_tag + 40);
// Platform on another address, with a domain first for HTTPS, as DEPLOYMENT_IPV4_OPTIONAL allows, on the
// standard ports, which mainnet pins. Such states load on every network: the IPv4 rule applies to ProTxs,
// at the height they are mined at.
BOOST_REQUIRE_EQUAL(state->netInfo->AddEntry(NetInfoPurpose::PLATFORM_P2P,
strprintf("2.2.2.%d:26656", address_tag)),
NetInfoStatus::Success);
BOOST_REQUIRE_EQUAL(state->netInfo->AddEntry(NetInfoPurpose::PLATFORM_HTTPS,
strprintf("evo%d.example.org:443", address_tag)),
NetInfoStatus::Success);
}
auto dmn{std::make_shared<CDeterministicMN>(internal_id, type)};
dmn->proTxHash = CollidingH(hash_suffix);
dmn->collateralOutpoint = COutPoint(H(address_tag + 80), internal_id);
dmn->nOperatorReward = address_tag * 10;
state->UpdateConfirmedHash(dmn->proTxHash, H(address_tag + 100));
dmn->pdmnState = std::move(state);
return dmn;
}
CDeterministicMNList MNList(const uint256& block_hash, int height, bool reverse)
{
CDeterministicMNList list{block_hash, height, 10};
std::vector<CDeterministicMNCPtr> mns{
MN(2, 3, MnType::Regular, ProTxVersion::LegacyBLS, 3),
MN(5, 1, MnType::Evo, ProTxVersion::ExtAddr, 5),
MN(7, 2, MnType::Regular, ProTxVersion::LegacyBLS, 7),
};
if (reverse) std::reverse(mns.begin(), mns.end());
for (const auto& dmn : mns) list.AddMN(dmn, /*fBumpTotalCount=*/false);
return list;
}
evo::MinedQuorumCommitment Commitment(Consensus::LLMQType type, uint8_t quorum, uint8_t mined, bool rotated,
int16_t index = 0)
{
llmq::CFinalCommitment commitment;
commitment.nVersion = rotated ? llmq::CFinalCommitment::BASIC_BLS_INDEXED_QUORUM_VERSION
: llmq::CFinalCommitment::BASIC_BLS_NON_INDEXED_QUORUM_VERSION;
commitment.llmqType = type;
commitment.quorumHash = H(quorum);
commitment.quorumIndex = index;
const auto& params{evo::SnapshotLLMQParams(type)};
commitment.signers.resize(params.size);
commitment.validMembers.resize(params.size);
return {H(quorum), H(quorum + 120), std::move(commitment), H(mined)};
}
evo::EvoSnapshot SyntheticSnapshot(bool reverse_representation = false)
{
evo::EvoSnapshot snapshot;
snapshot.base_block_hash = H(42);
snapshot.mn_list = MNList(snapshot.base_block_hash, 500, reverse_representation);
snapshot.credit_pool.locked = 123456;
snapshot.credit_pool.currentLimit = 700;
snapshot.credit_pool.latelyUnlocked = 11;
if (reverse_representation) {
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(15));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(8));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(7));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(9));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Remove(9));
snapshot.mnhf_signals.emplace(9, 30);
snapshot.mnhf_signals.emplace(2, 12);
} else {
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(7));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(8));
BOOST_REQUIRE(snapshot.credit_pool.indexes.Add(15));
snapshot.mnhf_signals.emplace(2, 12);
snapshot.mnhf_signals.emplace(9, 30);
}
evo::QuorumSnapshotData plain;
plain.llmq_type = Consensus::LLMQType::LLMQ_TEST;
plain.active_commitments = {Commitment(plain.llmq_type, 11, 51, false), Commitment(plain.llmq_type, 12, 52, false)};
plain.safety_commitments = {Commitment(plain.llmq_type, 10, 50, false)};
evo::QuorumSnapshotData rotated;
rotated.llmq_type = Consensus::LLMQType::LLMQ_TEST_DIP0024;
rotated.rotation_enabled = true;
rotated.active_commitments = {Commitment(rotated.llmq_type, 31, 71, true, 0),
Commitment(rotated.llmq_type, 32, 72, true, 1)};
rotated.safety_commitments = {Commitment(rotated.llmq_type, 21, 61, true, 0),
Commitment(rotated.llmq_type, 22, 62, true, 1)};
for (uint8_t i{1}; i <= evo::EVO_SNAPSHOT_ROTATION_CYCLES; ++i) {
const auto mode{i == 2 ? SnapshotSkipMode::MODE_SKIPPING_ENTRIES : SnapshotSkipMode::MODE_NO_SKIPPING};
rotated.rotation_snapshots.push_back(
{H(40 + i), H(100 + i), llmq::CQuorumSnapshot{{true, false, true, false}, mode, i == 2 ? std::vector<int>{1} : std::vector<int>{}}});
}
snapshot.quorums = {std::move(plain), std::move(rotated)};
std::set<uint256> work_hashes;
std::set<std::pair<Consensus::LLMQType, uint256>> modifier_keys;
for (const auto& data : snapshot.quorums) {
for (const auto* commitments : {&data.active_commitments, &data.safety_commitments}) {
for (const auto& entry : *commitments) {
work_hashes.insert(entry.work_block_hash);
modifier_keys.emplace(data.llmq_type, entry.work_block_hash);
}
}
for (const auto& entry : data.rotation_snapshots) {
work_hashes.insert(entry.work_block_hash);
modifier_keys.emplace(data.llmq_type, entry.work_block_hash);
}
}
CDeterministicMNList previous{snapshot.mn_list};
uint256 previous_hash{snapshot.base_block_hash};
int height{499};
for (const auto& work_hash : work_hashes) {
auto list{MNList(work_hash, height--, reverse_representation)};
snapshot.historical_mn_list_diffs.push_back({previous_hash, work_hash, list.GetHeightForSnapshotCodec(),
list.GetTotalRegisteredCount(), evo::CanonicalMNListHash(list),
previous.BuildDiff(list)});
previous_hash = work_hash;
previous = std::move(list);
}
for (const auto& [type, work_hash] : modifier_keys) {
snapshot.quorum_modifiers.push_back({type, work_hash, H(static_cast<uint8_t>(150 + snapshot.quorum_modifiers.size()))});
}
if (reverse_representation) {
std::reverse(snapshot.quorums.begin(), snapshot.quorums.end());
std::reverse(snapshot.historical_mn_list_diffs.begin(), snapshot.historical_mn_list_diffs.end());
std::reverse(snapshot.quorum_modifiers.begin(), snapshot.quorum_modifiers.end());
for (auto& data : snapshot.quorums) {
std::reverse(data.active_commitments.begin(), data.active_commitments.end());
std::reverse(data.safety_commitments.begin(), data.safety_commitments.end());
std::reverse(data.rotation_snapshots.begin(), data.rotation_snapshots.end());
}
}
return snapshot;
}
CDataStream SerializeSnapshot(const evo::EvoSnapshot& snapshot)
{
CDataStream stream{SER_DISK, CLIENT_VERSION};
stream << snapshot;
return stream;
}
void CheckInvalid(evo::EvoSnapshot snapshot) { BOOST_CHECK_THROW(snapshot.Validate(), std::ios_base::failure); }
// The snapshot decoder inserts attacker-chosen update IDs straight into this
// map, so its hash must not be the identity an adversary can drive into one
// bucket.
static_assert(std::is_same_v<decltype(CDeterministicMNListDiff::updatedMNs)::hasher, StaticSaltedHasher>);
} // namespace
BOOST_AUTO_TEST_SUITE(evo_snapshot_tests)
BOOST_FIXTURE_TEST_CASE(populated_roundtrip_and_representation_independence, BasicTestingSetup)
{
const auto forward{SyntheticSnapshot()};
const auto reverse{SyntheticSnapshot(/*reverse_representation=*/true)};
const auto forward_bytes{SerializeSnapshot(forward)};
const auto reverse_bytes{SerializeSnapshot(reverse)};
BOOST_CHECK_EQUAL_COLLECTIONS(forward_bytes.begin(), forward_bytes.end(), reverse_bytes.begin(), reverse_bytes.end());
BOOST_CHECK(evo::CanonicalMNListHash(forward.mn_list) == evo::CanonicalMNListHash(reverse.mn_list));
BOOST_CHECK(GetEvoSnapshotHash(forward) == GetEvoSnapshotHash(reverse));
CDataStream input{forward_bytes};
evo::EvoSnapshot decoded;
input >> decoded;
BOOST_CHECK(input.empty());
const auto decoded_bytes{SerializeSnapshot(decoded)};
BOOST_CHECK_EQUAL_COLLECTIONS(forward_bytes.begin(), forward_bytes.end(), decoded_bytes.begin(), decoded_bytes.end());
BOOST_CHECK(evo::CanonicalMNListHash(decoded.mn_list) == evo::CanonicalMNListHash(forward.mn_list));
BOOST_CHECK_EQUAL(decoded.mn_list.GetCounts().total(), 3U);
BOOST_CHECK_EQUAL(decoded.historical_mn_list_diffs.size(), forward.historical_mn_list_diffs.size());
BOOST_CHECK(decoded.credit_pool.indexes.Contains(7));
BOOST_CHECK(decoded.credit_pool.indexes.Contains(8));
BOOST_CHECK(decoded.credit_pool.indexes.Contains(15));
BOOST_CHECK(decoded.mnhf_signals == forward.mnhf_signals);
for (const auto internal_id : {2U, 5U, 7U}) {
const auto original{forward.mn_list.GetMNByInternalId(internal_id)};
BOOST_REQUIRE(original);
const auto by_hash{decoded.mn_list.GetMN(original->proTxHash)};
const auto by_id{decoded.mn_list.GetMNByInternalId(internal_id)};
const auto by_collateral{decoded.mn_list.GetUniquePropertyMN(original->collateralOutpoint)};
const auto by_owner{decoded.mn_list.GetUniquePropertyMN(original->pdmnState->keyIDOwner)};
const auto by_service{decoded.mn_list.GetMNByService(original->pdmnState->netInfo->GetPrimary())};
BOOST_REQUIRE(by_hash);
BOOST_REQUIRE(by_id);
BOOST_REQUIRE(by_collateral);
BOOST_REQUIRE(by_owner);
BOOST_REQUIRE(by_service);
BOOST_CHECK(by_hash->proTxHash == original->proTxHash);
BOOST_CHECK(by_id->proTxHash == original->proTxHash);
BOOST_CHECK(by_collateral->proTxHash == original->proTxHash);
BOOST_CHECK(by_owner->proTxHash == original->proTxHash);
BOOST_CHECK(by_service->proTxHash == original->proTxHash);
}
}
BOOST_AUTO_TEST_CASE(reconstruction_horizon_height_enumeration)
{
const auto rotated{evo::SnapshotLLMQParams(Consensus::LLMQType::LLMQ_TEST_DIP0024)};
const auto plain{evo::SnapshotLLMQParams(Consensus::LLMQType::LLMQ_TEST)};
const int base_height{20 * rotated.dkgInterval + 7};
const auto heights{evo::EvoSnapshotReconstructionHeights(base_height, {rotated, plain})};
BOOST_REQUIRE_EQUAL(heights.size(), evo::EVO_SNAPSHOT_ROTATION_CYCLES +
evo::SnapshotCommitmentCount(plain, false));
const int rotated_h{base_height - base_height % rotated.dkgInterval};
for (size_t i{0}; i < evo::EVO_SNAPSHOT_ROTATION_CYCLES; ++i) {
const int expected_cycle{rotated_h - static_cast<int>(i + 1) * rotated.dkgInterval};
BOOST_CHECK(heights[i].rotation);
BOOST_CHECK_EQUAL(heights[i].quorum_height, expected_cycle);
BOOST_CHECK_EQUAL(heights[i].work_height, expected_cycle - llmq::WORK_DIFF_DEPTH);
}
const int plain_h{base_height - base_height % plain.dkgInterval};
for (size_t i{0}; i < evo::SnapshotCommitmentCount(plain, false); ++i) {
const auto& height{heights[evo::EVO_SNAPSHOT_ROTATION_CYCLES + i]};
BOOST_CHECK(!height.rotation);
BOOST_CHECK_EQUAL(height.quorum_height, plain_h - static_cast<int>(i) * plain.dkgInterval);
BOOST_CHECK_EQUAL(height.work_height, height.quorum_height - llmq::WORK_DIFF_DEPTH);
}
}
BOOST_AUTO_TEST_CASE(rotation_bitset_larger_than_quorum_roundtrips)
{
const auto& params{evo::SnapshotLLMQParams(Consensus::LLMQType::LLMQ_TEST_DIP0024)};
evo::QuorumSnapshotEntry entry;
entry.cycle_base_block_hash = H(1);
entry.work_block_hash = H(2);
entry.snapshot.activeQuorumMembers.resize(params.size + 3);
entry.snapshot.activeQuorumMembers[params.size + 1] = true;
entry.snapshot.mnSkipListMode = SnapshotSkipMode::MODE_NO_SKIPPING;
CDataStream stream{SER_DISK, CLIENT_VERSION};
evo::WriteRotationSnapshot(stream, entry);
const auto decoded{evo::ReadRotationSnapshot(stream, params)};
BOOST_CHECK(stream.empty());
BOOST_CHECK_EQUAL(decoded.snapshot.activeQuorumMembers.size(), params.size + 3U);
BOOST_CHECK(decoded.snapshot.activeQuorumMembers[params.size + 1]);
}
BOOST_FIXTURE_TEST_CASE(populated_v3_golden_value, BasicTestingSetup)
{
BOOST_CHECK_EQUAL(GetEvoSnapshotHash(SyntheticSnapshot()).ToString(),
"8a2883207069e92589ffcce1f9867736881ecdf04ccfc89b492570740e7d3f31");
}
BOOST_FIXTURE_TEST_CASE(canonical_mn_reader_rejects_order_and_counter, BasicTestingSetup)
{
BOOST_CHECK(evo::CanonicalMNListHash(CDeterministicMNList{}) ==
evo::CanonicalMNListHash(CDeterministicMNList{}));
const auto write_raw = [](uint32_t total, std::vector<CDeterministicMNCPtr> mns) {
CDataStream stream{SER_DISK, CLIENT_VERSION};
stream << H(42) << 42 << total;
WriteCompactSize(stream, mns.size());
for (const auto& dmn : mns) stream << *dmn;
return stream;
};
auto unsorted{write_raw(10, {MN(2, 2, MnType::Regular, ProTxVersion::LegacyBLS, 2),
MN(1, 1, MnType::Regular, ProTxVersion::LegacyBLS, 1)})};
BOOST_CHECK_THROW(evo::UnserializeCanonicalMNList(unsorted), std::ios_base::failure);
auto bad_counter{write_raw(2, {MN(2, 1, MnType::Regular, ProTxVersion::LegacyBLS, 1)})};
BOOST_CHECK_THROW(evo::UnserializeCanonicalMNList(bad_counter), std::ios_base::failure);
}
BOOST_FIXTURE_TEST_CASE(diff_chain_roundtrip_and_canonical_determinism, BasicTestingSetup)
{
const auto base{MNList(H(10), 100, false)};
auto target{base};
target.RemoveMN(base.GetMNByInternalId(2)->proTxHash);
target.AddMN(MN(8, 8, MnType::Regular, ProTxVersion::LegacyBLS, 8));
for (const uint64_t id : {5, 7}) {
const auto dmn{target.GetMNByInternalId(id)};
auto state{std::make_shared<CDeterministicMNState>(*dmn->pdmnState)};
state->nLastPaidHeight += static_cast<int>(id);
target.UpdateMN(*dmn, state);
}
const auto diff{base.BuildDiff(target)};
auto permuted{diff};
std::reverse(permuted.addedMNs.begin(), permuted.addedMNs.end());
std::vector<std::pair<uint64_t, CDeterministicMNStateDiff>> updates(permuted.updatedMNs.begin(),
permuted.updatedMNs.end());
std::reverse(updates.begin(), updates.end());
permuted.updatedMNs.clear();
for (auto& update : updates) permuted.updatedMNs.emplace(std::move(update));
CDataStream canonical{SER_DISK, CLIENT_VERSION};
CDataStream reordered{SER_DISK, CLIENT_VERSION};
evo::SerializeCanonicalMNListDiff(canonical, diff);
evo::SerializeCanonicalMNListDiff(reordered, permuted);
BOOST_CHECK_EQUAL_COLLECTIONS(canonical.begin(), canonical.end(), reordered.begin(), reordered.end());
auto decoded{evo::UnserializeCanonicalMNListDiff(canonical)};
auto reconstructed{base};
reconstructed.ApplyDiffForSnapshot(H(11), 99, target.GetTotalRegisteredCount(), decoded);
target.ApplyDiffForSnapshot(H(11), 99, target.GetTotalRegisteredCount(), CDeterministicMNListDiff{});
BOOST_CHECK(evo::CanonicalMNListHash(reconstructed) == evo::CanonicalMNListHash(target));
BOOST_CHECK(canonical.empty());
}
BOOST_FIXTURE_TEST_CASE(inactive_mn_roundtrip, BasicTestingSetup)
{
for (const int version : {ProTxVersion::LegacyBLS, ProTxVersion::ExtAddr}) {
auto inactive{std::make_shared<CDeterministicMN>(*MN(2, 3, MnType::Regular, version, 3))};
auto state{std::make_shared<CDeterministicMNState>(*inactive->pdmnState)};
state->netInfo = NetInfoInterface::MakeNetInfo(version);
state->BanIfNotBanned(100);
inactive->pdmnState = state;
BOOST_REQUIRE(state->netInfo->IsEmpty());
evo::EvoSnapshot snapshot;
snapshot.base_block_hash = H(42);
snapshot.mn_list = CDeterministicMNList{snapshot.base_block_hash, 500, 10};
snapshot.mn_list.AddMN(inactive, /*fBumpTotalCount=*/false);
BOOST_CHECK_NO_THROW(snapshot.Validate());
auto bytes{SerializeSnapshot(snapshot)};
evo::EvoSnapshot decoded;
BOOST_REQUIRE_NO_THROW(bytes >> decoded);
BOOST_CHECK(bytes.empty());
BOOST_CHECK(GetEvoSnapshotHash(snapshot) == GetEvoSnapshotHash(decoded));
// Cover both historical additions and updates to empty network info.
for (const bool addition : {false, true}) {
CDeterministicMNList base{H(41), 501, 10};
if (!addition) base.AddMN(MN(2, 3, MnType::Regular, version, 3), /*fBumpTotalCount=*/false);
const auto diff{base.BuildDiff(snapshot.mn_list)};
CDataStream encoded{SER_DISK, CLIENT_VERSION};
evo::SerializeCanonicalMNListDiff(encoded, diff);
const auto decoded_diff{evo::UnserializeCanonicalMNListDiff(encoded)};
evo::EvoSnapshot historical;
historical.base_block_hash = base.GetBlockHash();
historical.mn_list = base;
historical.historical_mn_list_diffs = {{base.GetBlockHash(), snapshot.base_block_hash, 500, 10,
evo::CanonicalMNListHash(snapshot.mn_list), decoded_diff}};
std::map<uint256, CDeterministicMNList> lists;
std::string error;
BOOST_REQUIRE_MESSAGE(evo::ReconstructHistoricalMNLists(historical, lists, error), error);
BOOST_REQUIRE_EQUAL(lists.size(), 1U);
BOOST_CHECK(evo::CanonicalMNListHash(lists.begin()->second) == evo::CanonicalMNListHash(snapshot.mn_list));
}
}
}
BOOST_FIXTURE_TEST_CASE(historical_diff_applies_exchanged_unique_properties, BasicTestingSetup)
{
// Between two work blocks a surviving MN can take an address another
// survivor still holds at the first endpoint, and a new registration can
// take an address an updated MN released. Both endpoint lists are valid,
// so the diff between them must apply regardless of update order.
const auto older{MNList(H(10), 100, false)};
const auto with_net_info = [](const CDeterministicMN& dmn, std::shared_ptr<NetInfoInterface> net_info) {
auto state{std::make_shared<CDeterministicMNState>(*dmn.pdmnState)};
state->netInfo = std::move(net_info);
auto copy{std::make_shared<CDeterministicMN>(dmn)};
copy->pdmnState = std::move(state);
return copy;
};
const auto a{older.GetMNByInternalId(2)};
const auto b{older.GetMNByInternalId(7)};
auto fresh{NetInfoInterface::MakeNetInfo(b->pdmnState->nVersion)};
BOOST_REQUIRE_EQUAL(fresh->AddEntry(NetInfoPurpose::CORE_P2P, strprintf("1.1.1.9:%d", Params().GetDefaultPort())),
NetInfoStatus::Success);
CDeterministicMNList newer{H(11), 101, 10};
newer.AddMN(with_net_info(*a, b->pdmnState->netInfo), /*fBumpTotalCount=*/false);
newer.AddMN(with_net_info(*b, fresh), /*fBumpTotalCount=*/false);
newer.AddMN(older.GetMNByInternalId(5), /*fBumpTotalCount=*/false);
newer.AddMN(with_net_info(*MN(8, 8, MnType::Regular, ProTxVersion::LegacyBLS, 8), a->pdmnState->netInfo),
/*fBumpTotalCount=*/false);
const CDeterministicMNList* endpoints[]{&older, &newer};
for (const auto* from : endpoints) {
const auto& to{from == &older ? newer : older};
CDataStream encoded{SER_DISK, CLIENT_VERSION};
evo::SerializeCanonicalMNListDiff(encoded, from->BuildDiff(to));
const auto diff{evo::UnserializeCanonicalMNListDiff(encoded)};
BOOST_REQUIRE_EQUAL(diff.updatedMNs.size(), 2U);
BOOST_REQUIRE_EQUAL(diff.addedMNs.size() + diff.removedMns.size(), 1U);
auto applied{*from};
applied.ApplyDiffForSnapshot(to.GetBlockHash(), to.GetHeightForSnapshotCodec(), to.GetTotalRegisteredCount(), diff);
BOOST_CHECK(evo::CanonicalMNListHash(applied) == evo::CanonicalMNListHash(to));
// A diff whose result would hold one address twice is still rejected.
CDeterministicMNListDiff conflicting;
conflicting.updatedMNs.emplace(2, diff.updatedMNs.at(2));
auto rejected{*from};
BOOST_CHECK_THROW(rejected.ApplyDiffForSnapshot(to.GetBlockHash(), to.GetHeightForSnapshotCodec(),
to.GetTotalRegisteredCount(), conflicting),
std::runtime_error);
}
// The full reconstruction path, which re-checks every encoding around the
// apply, accepts the exchange as well.
evo::EvoSnapshot snapshot;
snapshot.base_block_hash = newer.GetBlockHash();
snapshot.mn_list = newer;
snapshot.historical_mn_list_diffs = {{newer.GetBlockHash(), older.GetBlockHash(), older.GetHeightForSnapshotCodec(),
older.GetTotalRegisteredCount(), evo::CanonicalMNListHash(older),
newer.BuildDiff(older)}};
std::map<uint256, CDeterministicMNList> lists;
std::string error;
BOOST_REQUIRE_MESSAGE(evo::ReconstructHistoricalMNLists(snapshot, lists, error), error);
BOOST_REQUIRE_EQUAL(lists.size(), 1U);
BOOST_CHECK(evo::CanonicalMNListHash(lists.begin()->second) == evo::CanonicalMNListHash(older));
}
BOOST_FIXTURE_TEST_CASE(historical_diff_additions_require_new_identities, BasicTestingSetup)
{
const auto original{MNList(H(11), 101, false)};
const auto original_hash{evo::CanonicalMNListHash(original)};
const auto existing{original.GetMNByInternalId(2)};
auto same_hash{std::make_shared<CDeterministicMN>(*MN(8, 8, MnType::Regular, ProTxVersion::LegacyBLS, 8))};
same_hash->proTxHash = existing->proTxHash;
const std::vector<CDeterministicMNCPtr> additions{
existing,
MN(2, 8, MnType::Regular, ProTxVersion::LegacyBLS, 8),
same_hash,
};
for (const bool remove_existing : {false, true}) {
for (const auto& addition : additions) {
auto list{original};
CDeterministicMNListDiff diff;
if (remove_existing) diff.removedMns.emplace(existing->GetInternalId());
diff.addedMNs.push_back(addition);
BOOST_CHECK_EXCEPTION(list.ApplyDiffForSnapshot(H(10), 100, original.GetTotalRegisteredCount(), diff),
std::runtime_error, [](const auto& e) {
return std::string{e.what()} ==
"historical MN-diff addition reuses an existing identity";
});
BOOST_CHECK(evo::CanonicalMNListHash(list) == original_hash);
}
}
}
BOOST_FIXTURE_TEST_CASE(malformed_lazy_operator_keys_are_rejected, BasicTestingSetup)
{
const auto noncanonical = [](const auto& e) {
return std::string{e.what()}.find("noncanonical MN object encoding") != std::string::npos;
};
const std::vector<std::byte> undecodable(CBLSPublicKey::SerSize, std::byte{0xff});
for (const int version : {ProTxVersion::LegacyBLS, ProTxVersion::BasicBLS}) {
const bool legacy{version == ProTxVersion::LegacyBLS};
auto mn{std::make_shared<CDeterministicMN>(*MN(2, 3, MnType::Regular, version, 3))};
auto state{std::make_shared<CDeterministicMNState>(*mn->pdmnState)};
CDataStream key_bytes{SER_DISK, CLIENT_VERSION};
key_bytes.write(undecodable);
key_bytes >> CBLSLazyPublicKeyVersionWrapper(state->pubKeyOperator, legacy);
mn->pdmnState = state;
// The lazy wrapper re-emits undecodable bytes verbatim until the key is
// first read, after which it emits the empty key: two encodings of one
// object, so the decoder must reject the bytes outright.
CDataStream lazy{SER_DISK, CLIENT_VERSION};
lazy << *mn;
BOOST_CHECK(!mn->pdmnState->pubKeyOperator.Get().IsValid());
CDataStream materialized{SER_DISK, CLIENT_VERSION};
materialized << *mn;
BOOST_REQUIRE(lazy.str() != materialized.str());
CDataStream list{SER_DISK, CLIENT_VERSION};
list << H(42) << 500 << uint32_t{10};
WriteCompactSize(list, 1);
list.write(Span{lazy});
BOOST_CHECK_EXCEPTION(evo::UnserializeCanonicalMNList(list), std::ios_base::failure, noncanonical);
CDataStream addition{SER_DISK, CLIENT_VERSION};
WriteCompactSize(addition, 1);
addition.write(Span{lazy});
WriteCompactSize(addition, 0);
WriteCompactSize(addition, 0);
BOOST_CHECK_EXCEPTION(evo::UnserializeCanonicalMNListDiff(addition), std::ios_base::failure, noncanonical);
CDeterministicMNStateDiff state_diff;
state_diff.fields = CDeterministicMNStateDiff::Field_nVersion | CDeterministicMNStateDiff::Field_pubKeyOperator;
state_diff.state.nVersion = version;
key_bytes.write(undecodable);
key_bytes >> CBLSLazyPublicKeyVersionWrapper(state_diff.state.pubKeyOperator, legacy);
CDataStream update{SER_DISK, CLIENT_VERSION};
WriteCompactSize(update, 0);
WriteCompactSize(update, 1);
WriteVarInt<CDataStream, VarIntMode::DEFAULT, uint64_t>(update, 2);
update << state_diff;
WriteCompactSize(update, 0);
BOOST_CHECK_EXCEPTION(evo::UnserializeCanonicalMNListDiff(update), std::ios_base::failure, noncanonical);
// An in-memory snapshot holding the unread bytes must not hash either.
lazy.clear();
lazy << *MN(2, 3, MnType::Regular, version, 3);
auto unread{std::make_shared<CDeterministicMN>(deserialize, lazy)};
BOOST_REQUIRE(lazy.empty());
auto unread_state{std::make_shared<CDeterministicMNState>(*unread->pdmnState)};
key_bytes.write(undecodable);
key_bytes >> CBLSLazyPublicKeyVersionWrapper(unread_state->pubKeyOperator, legacy);
unread->pdmnState = unread_state;
evo::EvoSnapshot snapshot;
snapshot.base_block_hash = H(42);
snapshot.mn_list = CDeterministicMNList{snapshot.base_block_hash, 500, 10};
snapshot.mn_list.AddMN(unread, /*fBumpTotalCount=*/false);
BOOST_CHECK_EXCEPTION(snapshot.Validate(), std::ios_base::failure, noncanonical);
BOOST_CHECK_THROW(GetEvoSnapshotHash(snapshot), std::ios_base::failure);
// A decodable key keeps one encoding whether or not it has been read.
CBLSSecretKey secret;
BOOST_REQUIRE(secret.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", legacy));
auto keyed_state{std::make_shared<CDeterministicMNState>(*unread_state)};
keyed_state->pubKeyOperator.Set(secret.GetPublicKey(), legacy);
unread->pdmnState = keyed_state;
BOOST_CHECK_NO_THROW(snapshot.Validate());
const auto before_read{GetEvoSnapshotHash(snapshot)};
auto bytes{SerializeSnapshot(snapshot)};
evo::EvoSnapshot decoded;
BOOST_REQUIRE_NO_THROW(bytes >> decoded);
BOOST_CHECK(decoded.mn_list.GetMNByInternalId(2)->pdmnState->pubKeyOperator.Get().IsValid());
BOOST_CHECK(GetEvoSnapshotHash(decoded) == before_read);
}
}
BOOST_FIXTURE_TEST_CASE(unset_mn_id_is_rejected, BasicTestingSetup)
{
const auto mn{MN(2, 3, MnType::Regular, ProTxVersion::LegacyBLS, 3)};
CDataStream malformed{SER_DISK, CLIENT_VERSION};
malformed << mn->proTxHash;
WriteVarInt<CDataStream, VarIntMode::DEFAULT, uint64_t>(malformed, std::numeric_limits<uint64_t>::max());
malformed << mn->collateralOutpoint << mn->nOperatorReward << mn->pdmnState << mn->nType;
const auto invalid_id = [](const auto& e) {
return std::string{e.what()}.find("invalid canonical MN internalId") != std::string::npos;
};
CDataStream base{SER_DISK, CLIENT_VERSION};
base << H(42) << 500 << uint32_t{10};
WriteCompactSize(base, 1);
base.write(Span{malformed});
BOOST_CHECK_EXCEPTION(evo::UnserializeCanonicalMNList(base), std::ios_base::failure, invalid_id);
CDataStream addition{SER_DISK, CLIENT_VERSION};
WriteCompactSize(addition, 1);
addition.write(Span{malformed});
WriteCompactSize(addition, 0);
WriteCompactSize(addition, 0);
BOOST_CHECK_EXCEPTION(evo::UnserializeCanonicalMNListDiff(addition), std::ios_base::failure, invalid_id);
}
BOOST_FIXTURE_TEST_CASE(commitment_bls_wire_encoding_is_canonical, BasicTestingSetup)
{
CBLSSecretKey key;
BOOST_REQUIRE(key.SetHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", false));
for (const bool legacy : {false, true}) {
for (const bool indexed : {false, true}) {
auto entry{Commitment(Consensus::LLMQType::LLMQ_TEST, 11, 51, indexed)};
if (legacy) {
entry.commitment.nVersion = indexed ? llmq::CFinalCommitment::LEGACY_BLS_INDEXED_QUORUM_VERSION
: llmq::CFinalCommitment::LEGACY_BLS_NON_INDEXED_QUORUM_VERSION;
}
entry.commitment.quorumPublicKey = key.GetPublicKey();
entry.commitment.quorumSig = key.Sign(H(1), legacy);
entry.commitment.membersSig = key.Sign(H(2), legacy);
const auto encode = [&](int opposite_field) {
auto& qc{entry.commitment};
CDataStream bytes{SER_DISK, CLIENT_VERSION};
bytes << entry.quorum_base_block_hash << entry.work_block_hash << qc.nVersion << qc.llmqType
<< qc.quorumHash;
if (indexed) bytes << qc.quorumIndex;
bytes << DYNBITSET(qc.signers) << DYNBITSET(qc.validMembers)
<< CBLSPublicKeyVersionWrapper(qc.quorumPublicKey, opposite_field == 0 ? !legacy : legacy)
<< qc.quorumVvecHash
<< CBLSSignatureVersionWrapper(qc.quorumSig, opposite_field == 1 ? !legacy : legacy)
<< CBLSSignatureVersionWrapper(qc.membersSig, opposite_field == 2 ? !legacy : legacy)
<< entry.mined_block_hash;
return bytes;
};
auto canonical{encode(-1)};
BOOST_CHECK_NO_THROW(evo::ReadMinedQuorumCommitment(canonical));
BOOST_CHECK(canonical.empty());
if (legacy) continue;
for (int field{0}; field < 3; ++field) {
auto bytes{encode(field)};
auto ordinary_input{bytes};
evo::MinedQuorumCommitment ordinary;
BOOST_REQUIRE_NO_THROW(ordinary_input >> ordinary);
CDataStream normalized{SER_DISK, CLIENT_VERSION};
normalized << ordinary;
BOOST_REQUIRE(bytes.str() != normalized.str());
BOOST_CHECK_EXCEPTION(evo::ReadMinedQuorumCommitment(bytes), std::ios_base::failure, [](const auto& e) {
return std::string{e.what()}.find("noncanonical evo snapshot commitment encoding") != std::string::npos;
});
}
}
}
}
BOOST_FIXTURE_TEST_CASE(historical_diff_decode_has_cumulative_operation_budget, BasicTestingSetup)
{
CDeterministicMNListDiff one_removal;
one_removal.removedMns.emplace(1);
CDataStream first{SER_DISK, CLIENT_VERSION};
CDataStream second{SER_DISK, CLIENT_VERSION};
evo::SerializeCanonicalMNListDiff(first, one_removal);
evo::SerializeCanonicalMNListDiff(second, one_removal);
size_t remaining_operations{1};
const auto decoded{evo::UnserializeCanonicalMNListDiff(first, remaining_operations)};
BOOST_CHECK_EQUAL(decoded.removedMns.size(), 1U);
BOOST_CHECK_EQUAL(remaining_operations, 0U);
BOOST_CHECK_THROW(evo::UnserializeCanonicalMNListDiff(second, remaining_operations),
std::ios_base::failure);
BOOST_CHECK(evo::EvoSnapshotMaxHistoricalMNLists() < 2'048U);
}
BOOST_FIXTURE_TEST_CASE(context_free_validation_matrix, BasicTestingSetup)
{
auto snapshot{SyntheticSnapshot()};
snapshot.version++;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.base_block_hash = H(1);
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
std::reverse(snapshot.quorums.begin(), snapshot.quorums.end());
BOOST_CHECK_THROW(snapshot.Validate(/*require_canonical_order=*/true), std::ios_base::failure);
snapshot = SyntheticSnapshot();
std::reverse(snapshot.historical_mn_list_diffs.begin(), snapshot.historical_mn_list_diffs.end());
BOOST_CHECK_THROW(snapshot.Validate(/*require_canonical_order=*/true), std::ios_base::failure);
snapshot = SyntheticSnapshot();
std::reverse(snapshot.quorums[0].active_commitments.begin(), snapshot.quorums[0].active_commitments.end());
BOOST_CHECK_THROW(snapshot.Validate(/*require_canonical_order=*/true), std::ios_base::failure);
snapshot = SyntheticSnapshot();
std::reverse(snapshot.quorums[1].rotation_snapshots.begin(), snapshot.quorums[1].rotation_snapshots.end());
BOOST_CHECK_THROW(snapshot.Validate(/*require_canonical_order=*/true), std::ios_base::failure);
snapshot = SyntheticSnapshot();
snapshot.historical_mn_list_diffs[0].block_hash = H(1);
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[1].rotation_snapshots[0].work_block_hash = H(1);
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[0].llmq_type = Consensus::LLMQType::LLMQ_NONE;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[0].rotation_enabled = true;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[0].active_commitments.pop_back();
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[0].safety_commitments.clear();
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[1].rotation_snapshots.pop_back();
CheckInvalid(snapshot);
// Parameter-derived history counts are ceilings. Young chains and newly
// activated quorum types legitimately carry fewer commitments and cycles;
// chain-aware validation and the base CbTx establish completeness later.
snapshot = SyntheticSnapshot();
snapshot.quorums.clear();
snapshot.historical_mn_list_diffs.clear();
snapshot.quorum_modifiers.clear();
evo::QuorumSnapshotData partial;
partial.llmq_type = Consensus::LLMQType::LLMQ_TEST;
snapshot.quorums.emplace_back(std::move(partial));
BOOST_CHECK_NO_THROW(snapshot.Validate());
snapshot = SyntheticSnapshot();
snapshot.credit_pool.locked = -1;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.credit_pool.currentLimit = snapshot.credit_pool.locked + 1;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.credit_pool.latelyUnlocked = MAX_MONEY + 1;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.mnhf_signals.emplace(VERSIONBITS_NUM_BITS, 10);
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.mnhf_signals.emplace(11, -1);
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.mnhf_signals.emplace(11, snapshot.mn_list.GetHeightForSnapshotCodec() + 1);
CheckInvalid(snapshot);
const auto mutate_commitment = [](auto mutation) {
auto value{SyntheticSnapshot()};
mutation(value.quorums[0].active_commitments[0]);
CheckInvalid(std::move(value));
};
mutate_commitment([](auto& e) { e.commitment.validMembers.resize(e.commitment.signers.size() + 1); });
mutate_commitment([](auto& e) {
e.commitment.signers.clear();
e.commitment.validMembers.clear();
});
mutate_commitment([](auto& e) { e.quorum_base_block_hash.SetNull(); });
mutate_commitment([](auto& e) { e.mined_block_hash.SetNull(); });
mutate_commitment([](auto& e) { e.commitment.llmqType = Consensus::LLMQType::LLMQ_TEST_PLATFORM; });
mutate_commitment([](auto& e) { e.commitment.quorumHash = H(99); });
mutate_commitment([](auto& e) { e.commitment.nVersion = llmq::CFinalCommitment::BASIC_BLS_INDEXED_QUORUM_VERSION; });
mutate_commitment([](auto& e) { e.commitment.nVersion = 99; });
snapshot = SyntheticSnapshot();
snapshot.quorums[1].active_commitments[1].commitment.quorumIndex = 0;
CheckInvalid(snapshot);
snapshot = SyntheticSnapshot();
snapshot.quorums[1].active_commitments[1].commitment.quorumIndex = 2;
CheckInvalid(snapshot);
const auto mutate_rotation = [](auto mutation) {
auto value{SyntheticSnapshot()};
mutation(value.quorums[1].rotation_snapshots[0]);
CheckInvalid(std::move(value));
};
mutate_rotation([](auto& e) { e.cycle_base_block_hash.SetNull(); });
mutate_rotation([](auto& e) { e.work_block_hash.SetNull(); });
mutate_rotation([](auto& e) { e.snapshot.mnSkipListMode = static_cast<SnapshotSkipMode>(9); });
mutate_rotation([](auto& e) { e.snapshot.activeQuorumMembers.resize(evo::EVO_SNAPSHOT_MAX_MNS + 1); });
mutate_rotation([](auto& e) { e.snapshot.mnSkipList = {-1}; });
// A cycle's skip list accumulates across every quorum index, so lengths
// beyond a single quorum's size and negative wraparound deltas after the
// first (absolute) entry are legitimate.
auto aggregate_skips{SyntheticSnapshot()};
auto& rotation_entry{aggregate_skips.quorums[1].rotation_snapshots[0]};
const auto& rotation_params{evo::SnapshotLLMQParams(aggregate_skips.quorums[1].llmq_type)};
rotation_entry.snapshot.mnSkipListMode = SnapshotSkipMode::MODE_SKIPPING_ENTRIES;
rotation_entry.snapshot.mnSkipList.assign(static_cast<size_t>(rotation_params.size) + 2, 1);
rotation_entry.snapshot.mnSkipList.front() = 3;
rotation_entry.snapshot.mnSkipList.back() = -2;
BOOST_CHECK_NO_THROW(aggregate_skips.Validate());
}
BOOST_FIXTURE_TEST_CASE(bounded_readers_reject_claimed_sizes_first, BasicTestingSetup)
{
CDataStream mn_stream{SER_DISK, CLIENT_VERSION};
mn_stream << H(1) << 1 << uint32_t{0};
WriteCompactSize(mn_stream, evo::EVO_SNAPSHOT_MAX_MNS + 1);
BOOST_CHECK_THROW(evo::UnserializeCanonicalMNList(mn_stream), std::ios_base::failure);
BOOST_CHECK(mn_stream.empty());
const auto decode_quorum = [](CDataStream stream) {
evo::QuorumSnapshotData data;
stream >> data;
};
const auto& plain_params{evo::SnapshotLLMQParams(Consensus::LLMQType::LLMQ_TEST)};
const size_t commitment_limit{evo::SnapshotCommitmentCount(plain_params, false)};
CDataStream active{SER_DISK, CLIENT_VERSION};
active << Consensus::LLMQType::LLMQ_TEST << false;
WriteCompactSize(active, plain_params.signingActiveQuorumCount + 1);
BOOST_CHECK_THROW(decode_quorum(active), std::ios_base::failure);
CDataStream safety{SER_DISK, CLIENT_VERSION};
safety << Consensus::LLMQType::LLMQ_TEST << false;
WriteCompactSize(safety, 0);
WriteCompactSize(safety, commitment_limit - plain_params.signingActiveQuorumCount + 1);
BOOST_CHECK_THROW(decode_quorum(safety), std::ios_base::failure);
CDataStream rotations{SER_DISK, CLIENT_VERSION};
rotations << Consensus::LLMQType::LLMQ_TEST_DIP0024 << true;
WriteCompactSize(rotations, 0);
WriteCompactSize(rotations, 0);
WriteCompactSize(rotations, evo::EVO_SNAPSHOT_ROTATION_CYCLES + 1);
BOOST_CHECK_THROW(decode_quorum(rotations), std::ios_base::failure);
const auto& rotated_params{evo::SnapshotLLMQParams(Consensus::LLMQType::LLMQ_TEST_DIP0024)};
CDataStream bitset{SER_DISK, CLIENT_VERSION};
bitset << H(1) << H(2) << SnapshotSkipMode::MODE_NO_SKIPPING;
WriteCompactSize(bitset, evo::EVO_SNAPSHOT_MAX_MNS + 1);
BOOST_CHECK_THROW(evo::ReadRotationSnapshot(bitset, rotated_params), std::ios_base::failure);
CDataStream skip_list{SER_DISK, CLIENT_VERSION};
skip_list << H(1) << H(2) << SnapshotSkipMode::MODE_NO_SKIPPING;
WriteCompactSize(skip_list, 0);
WriteCompactSize(skip_list, rotated_params.size + 1);
BOOST_CHECK_THROW(evo::ReadRotationSnapshot(skip_list, rotated_params), std::ios_base::failure);
CDataStream commitment_bits{SER_DISK, CLIENT_VERSION};
auto oversized_commitment{Commitment(Consensus::LLMQType::LLMQ_TEST, 1, 2, false)};
oversized_commitment.commitment.signers.resize(plain_params.size + 1);
commitment_bits << Consensus::LLMQType::LLMQ_TEST << false;
WriteCompactSize(commitment_bits, 1);
commitment_bits << oversized_commitment;
evo::QuorumSnapshotData oversized_data;
BOOST_CHECK_EXCEPTION(commitment_bits >> oversized_data, std::ios_base::failure, [](const auto& e) {
return std::string{e.what()}.find("inconsistent evo snapshot commitment bitset sizes") != std::string::npos;
});
// The valid-members bitset payload, BLS material, and mined-block hash
// remain unread: rejection occurs at the mismatched claimed size.
BOOST_CHECK_GT(commitment_bits.size(), uint256::size());
const auto commitment_prefix = [](CDataStream& s) {
s << Consensus::LLMQType::LLMQ_TEST << false;
WriteCompactSize(s, 1);
s << H(1) << H(2) << uint16_t{llmq::CFinalCommitment::BASIC_BLS_NON_INDEXED_QUORUM_VERSION}
<< Consensus::LLMQType::LLMQ_TEST << H(1);
};
CDataStream over_ceiling{SER_DISK, CLIENT_VERSION};
commitment_prefix(over_ceiling);
WriteCompactSize(over_ceiling, evo::EVO_SNAPSHOT_MAX_QUORUM_SIZE + 1);
BOOST_CHECK_THROW(decode_quorum(over_ceiling), std::ios_base::failure);
CDataStream empty_bits{SER_DISK, CLIENT_VERSION};
commitment_prefix(empty_bits);
WriteCompactSize(empty_bits, 0);
BOOST_CHECK_THROW(decode_quorum(empty_bits), std::ios_base::failure);
auto oversized_payout_mn{std::const_pointer_cast<CDeterministicMN>(
MN(8, 8, MnType::Regular, ProTxVersion::ExtAddr, 8))};
auto payout_state{std::make_shared<CDeterministicMNState>(*oversized_payout_mn->pdmnState)};
payout_state->payouts.resize(evo::EVO_SNAPSHOT_MAX_PAYOUT_SHARES + 1);
oversized_payout_mn->pdmnState = std::move(payout_state);
CDataStream payouts{SER_DISK, CLIENT_VERSION};
payouts << H(42) << 42 << uint32_t{10};
WriteCompactSize(payouts, 1);
payouts << *oversized_payout_mn;
BOOST_CHECK_THROW(evo::UnserializeCanonicalMNList(payouts), std::ios_base::failure);
CDataStream wrapped_string{SER_DISK, CLIENT_VERSION};
WriteCompactSize(wrapped_string, evo::EVO_SNAPSHOT_MAX_MN_COMPACT_ITEMS + 1);
wrapped_string << uint8_t{0x42};
evo::SnapshotBoundedInput bounded_string{wrapped_string, evo::EVO_SNAPSHOT_MAX_MN_COMPACT_ITEMS};
std::string decoded_string;
BOOST_CHECK_EXCEPTION(bounded_string >> decoded_string, std::ios_base::failure,
[](const auto& e) { return std::string{e.what()}.find("CompactSize budget exceeded") != std::string::npos; });
BOOST_CHECK(decoded_string.empty());
BOOST_REQUIRE_EQUAL(wrapped_string.size(), 1U);
BOOST_CHECK_EQUAL(std::to_integer<uint8_t>(wrapped_string.data()[0]), 0x42);
const auto snapshot_prefix = [](CDataStream& stream) {
stream << evo::EVO_SNAPSHOT_VERSION << H(42);
evo::SerializeCanonicalMNList(stream, CDeterministicMNList{H(42), 1, 0});
};
CDataStream quorum_types{SER_DISK, CLIENT_VERSION};
snapshot_prefix(quorum_types);
WriteCompactSize(quorum_types, Consensus::available_llmqs.size() + 1);
evo::EvoSnapshot decoded;
BOOST_CHECK_THROW(quorum_types >> decoded, std::ios_base::failure);
CDataStream history{SER_DISK, CLIENT_VERSION};
snapshot_prefix(history);
WriteCompactSize(history, 0);
WriteCompactSize(history, evo::EvoSnapshotMaxHistoricalMNLists() + 1);
BOOST_CHECK_THROW(history >> decoded, std::ios_base::failure);
CDataStream signals{SER_DISK, CLIENT_VERSION};
snapshot_prefix(signals);
WriteCompactSize(signals, 0);
WriteCompactSize(signals, 0);
WriteCompactSize(signals, 0);
signals << CCreditPool{};
WriteCompactSize(signals, VERSIONBITS_NUM_BITS + 1);
BOOST_CHECK_THROW(signals >> decoded, std::ios_base::failure);
CDataStream ranges{SER_DISK, CLIENT_VERSION};
snapshot_prefix(ranges);
WriteCompactSize(ranges, 0);
WriteCompactSize(ranges, 0);
WriteCompactSize(ranges, 0);
ranges << CAmount{0} << CAmount{0} << CAmount{0};
WriteCompactSize(ranges, evo::EVO_SNAPSHOT_MAX_RANGES + 1);
BOOST_CHECK_THROW(ranges >> decoded, std::ios_base::failure);
BOOST_CHECK(ranges.empty());
const auto snapshot_bytes{SerializeSnapshot(SyntheticSnapshot())};
DomainPort domain;
BOOST_REQUIRE_EQUAL(domain.Set("evo5.example.org", 443), DomainPort::Status::Success);
CDataStream encoded_domain{SER_DISK, CLIENT_VERSION};
encoded_domain << domain;
const auto domain_pos{std::search(snapshot_bytes.begin(), snapshot_bytes.end(),
encoded_domain.begin(), encoded_domain.end())};
BOOST_REQUIRE(domain_pos != snapshot_bytes.end());
CDataStream oversized_domain{SER_DISK, CLIENT_VERSION};
const size_t domain_offset{static_cast<size_t>(std::distance(snapshot_bytes.begin(), domain_pos))};
oversized_domain.write(Span{snapshot_bytes}.first(domain_offset));
constexpr size_t MAX_DOMAIN_LENGTH{253};
WriteCompactSize(oversized_domain, MAX_DOMAIN_LENGTH + 1);
const std::string oversized_addr(MAX_DOMAIN_LENGTH + 1, 'a');
oversized_domain.write(MakeByteSpan(oversized_addr));
const size_t serialized_addr_size{encoded_domain.size() - sizeof(uint16_t)};
oversized_domain.write(Span{snapshot_bytes}.subspan(domain_offset + serialized_addr_size));
BOOST_CHECK_EXCEPTION(oversized_domain >> decoded, std::ios_base::failure,
[](const auto& e) { return std::string{e.what()}.find("String length limit exceeded") != std::string::npos; });
}
BOOST_FIXTURE_TEST_CASE(commitment_sizes_are_format_bounded_not_param_exact, BasicTestingSetup)
{
// -llmqtestparams and -llmqdevnetparams change the effective quorum size at
// runtime, so commitments whose bitsets differ from the static default must
// pass this layer; exact sizing is established by chain-aware validation.
auto snapshot{SyntheticSnapshot()};
const auto& params{evo::SnapshotLLMQParams(snapshot.quorums[0].llmq_type)};
for (auto& entry : snapshot.quorums[0].active_commitments) {
entry.commitment.signers.assign(params.size + 5, false);
entry.commitment.validMembers.assign(params.size + 5, true);
}
BOOST_CHECK_NO_THROW(snapshot.Validate(/*require_canonical_order=*/true));
const auto bytes{SerializeSnapshot(snapshot)};
CDataStream input{bytes};
evo::EvoSnapshot decoded;
BOOST_CHECK_NO_THROW(input >> decoded);
BOOST_CHECK(input.empty());
const auto reencoded{SerializeSnapshot(decoded)};
BOOST_CHECK_EQUAL_COLLECTIONS(bytes.begin(), bytes.end(), reencoded.begin(), reencoded.end());
}
BOOST_FIXTURE_TEST_CASE(canonical_sort_preserves_input, BasicTestingSetup)
{
const auto snapshot{SyntheticSnapshot(/*reverse_representation=*/true)};
const auto original{SerializeSnapshot(snapshot)};
const auto first_type{snapshot.quorums.front().llmq_type};
const auto sorted{evo::CanonicallySortedCopy(snapshot.quorums)};
BOOST_CHECK(evo::IsCanonicallySorted(sorted));
BOOST_CHECK(!evo::IsCanonicallySorted(snapshot.quorums));
BOOST_CHECK(snapshot.quorums.front().llmq_type == first_type);
const auto after{SerializeSnapshot(snapshot)};
BOOST_CHECK_EQUAL_COLLECTIONS(original.begin(), original.end(), after.begin(), after.end());
}
BOOST_FIXTURE_TEST_CASE(rotation_flag_wire_encoding_is_canonical, BasicTestingSetup)
{
for (const uint8_t flag : {0, 1, 2, 255}) {
CDataStream input{SER_DISK, CLIENT_VERSION};
input << Consensus::LLMQType::LLMQ_TEST_DIP0024 << flag;
for (int i{0}; i < 3; ++i) WriteCompactSize(input, 0);
evo::QuorumSnapshotData decoded;
if (flag <= 1) {
BOOST_CHECK_NO_THROW(input >> decoded);
BOOST_CHECK_EQUAL(decoded.rotation_enabled, flag != 0);
} else {
BOOST_CHECK_EXCEPTION(input >> decoded, std::ios_base::failure, [](const auto& e) {
return std::string{e.what()}.find("noncanonical evo quorum rotation flag") != std::string::npos;
});