Expand file tree
/
Copy pathgovernance_vote_processing_tests.cpp
More file actions
791 lines (694 loc) · 38 KB
/
Copy pathgovernance_vote_processing_tests.cpp
File metadata and controls
791 lines (694 loc) · 38 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
// 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 <addresstype.h>
#include <bls/bls.h>
#include <consensus/amount.h>
#include <evo/deterministicmns.h>
#include <evo/evochainstate.h>
#include <evo/providertx.h>
#include <evo/specialtx.h>
#include <governance/governance.h>
#include <governance/object.h>
#include <governance/vote.h>
#include <index/govcollateralindex.h>
#include <interfaces/chain.h>
#include <key.h>
#include <key_io.h>
#include <masternode/meta.h>
#include <masternode/sync.h>
#include <messagesigner.h>
#include <netfulfilledman.h>
#include <primitives/transaction.h>
#include <script/solver.h>
#include <streams.h>
#include <test/util/bls.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>
#include <test/util/index.h>
#include <test/util/masternode.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <chrono>
#include <memory>
#include <string>
#include <vector>
using namespace std::chrono_literals;
namespace {
// TestChainSetup only accepts checkpointed chain lengths, so 107 blocks plus
// -dip3params=109:500 is the shortest chain a ProRegTx can be mined on. Same setup as
// TestChainDIP3Setup in evo_deterministicmns_tests.cpp.
constexpr int DIP3_ACTIVATION_HEIGHT{109};
void SignWithVotingKey(CGovernanceVote& vote, const CKey& key)
{
std::vector<unsigned char> sig;
BOOST_REQUIRE(CMessageSigner::SignMessage(vote.GetSignatureString(), sig, key));
vote.SetSignature(sig);
}
void SignWithOperatorKey(CGovernanceVote& vote, const CBLSSecretKey& key)
{
// Vote signatures are basic. CGovernanceVote::CheckSignature() deserializes and verifies them as basic.
vote.SetSignature(key.Sign(vote.GetSignatureHash()).ToByteVector());
}
//! A trigger that is signed by the fixture masternode and passes IsValidLocally, but whose payload
//! is not a superblock, so SuperblockManager::AddTrigger rejects it after it was inserted.
CGovernanceObject MakeFailedTrigger(const COutPoint& mn_collateral, const CBLSSecretKey& operator_key,
int64_t creation_time, int salt)
{
const std::string data{strprintf(R"({"type":2,"salt":%d})", salt)};
CGovernanceObject govobj{uint256{}, /*nRevisionIn=*/1, creation_time, uint256{}, HexStr(data)};
BOOST_REQUIRE(govobj.GetObjectType() == GovernanceObject::TRIGGER);
govobj.SetMasternodeOutpoint(mn_collateral);
govobj.SetSignature(operator_key.Sign(govobj.GetSignatureHash()).ToByteVector());
return govobj;
}
struct GovernanceVoteNotifications final : CValidationInterface {
mutable Mutex cs;
std::vector<uint256> hashes GUARDED_BY(cs);
void NotifyGovernanceVote(const std::shared_ptr<CDeterministicMNList>&,
const std::shared_ptr<const CGovernanceVote>& vote) override EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
hashes.push_back(vote->GetHash());
}
std::vector<uint256> GetHashes() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
{
LOCK(cs);
return hashes;
}
};
// A chain with one registered masternode whose voting (ECDSA) and operator (BLS) keys are known
// to the test, so votes can be signed for real and CGovernanceVote::CheckSignature is actually
// reached. Without a populated masternode list every CGovernanceVote::IsValid() call would
// short-circuit at GetMNByCollateral and no signature would ever be verified.
struct GovernanceVoteSetup : public TestChainSetup {
CKey mn_voting_key;
CBLSSecretKey mn_operator_key;
COutPoint mn_collateral;
SimpleUTXOMap utxos;
std::shared_ptr<GovernanceVoteNotifications> vote_notifications;
//! Fixed so a proposal can be rebuilt bit-for-bit (and keep its hash) once its fee tx is known.
int64_t proposal_time{0};
std::string proposal_payment_address;
explicit GovernanceVoteSetup(const char* dip3_params = "-dip3params=109:500") :
TestChainSetup(DIP3_ACTIVATION_HEIGHT - 2, ChainType::REGTEST, {dip3_params})
{
// A failed BOOST_REQUIRE below throws, and a throwing constructor means no destructor runs.
// Tear the globals down by hand so one broken invariant here doesn't leave a live tx index
// (and a dangling m_node reference in it) for every later test in the binary.
try {
SetUp();
} catch (...) {
TearDown();
throw;
}
}
~GovernanceVoteSetup() { TearDown(); }
void SetUp()
{
// CGovernanceObject::IsCollateralValid() reads the proposal fee tx out of the governance collateral
// index; without it no proposal can ever be accepted, orphan votes included.
SyncWithValidationInterfaceQueue();
m_node.gov_collateral_index = std::make_unique<GovCollateralIndex>(interfaces::MakeChain(m_node),
/*n_cache_size=*/0, /*f_memory=*/true);
BOOST_REQUIRE(m_node.gov_collateral_index->Init());
BOOST_REQUIRE(m_node.gov_collateral_index->StartBackgroundSync());
IndexWaitSynced(*m_node.gov_collateral_index);
utxos = BuildSimpleUtxoMap(m_coinbase_txns);
// Activate DIP3, then register the masternode that votes in these tests.
MineBlocks(1);
auto protx = CreateProRegTx(*m_node.chainman, utxos, /*port=*/1, payout_script(), coinbaseKey, mn_voting_key,
mn_operator_key);
mn_collateral = COutPoint(protx.GetHash(), 0);
MineBlock({protx});
BOOST_REQUIRE(tip_mn_list().GetMNByCollateral(mn_collateral) != nullptr);
BOOST_REQUIRE(m_node.mn_metaman->LoadCache(/*load_cache=*/false));
BOOST_REQUIRE(m_node.netfulfilledman->LoadCache(/*load_cache=*/false));
// Governance relay and CheckAndRemove() are no-ops until the node considers itself synced.
// Two SwitchToNextAsset() calls get there from BLOCKCHAIN; the bound is just a loop guard.
for (int i = 0; i < 5 && !m_node.mn_sync->IsSynced(); ++i) {
m_node.mn_sync->SwitchToNextAsset();
}
BOOST_REQUIRE(m_node.mn_sync->IsSynced());
m_node.govman = std::make_unique<CGovernanceManager>(m_args.GetDataDirNet(), *m_node.mn_metaman,
*m_node.chainman, *m_node.sbman, *m_node.chainman->ActiveEvo().dmnman,
*m_node.mn_sync, m_node.gov_collateral_index.get());
BOOST_REQUIRE(m_node.govman->LoadCache(/*load_cache=*/false));
vote_notifications = std::make_shared<GovernanceVoteNotifications>();
RegisterSharedValidationInterface(vote_notifications);
proposal_time = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
CKey payment_key;
payment_key.MakeNewKey(true);
proposal_payment_address = EncodeDestination(PKHash(payment_key.GetPubKey()));
}
void TearDown()
{
if (vote_notifications) {
SyncWithValidationInterfaceQueue();
UnregisterSharedValidationInterface(vote_notifications);
vote_notifications.reset();
}
// govman holds a reference to node.sbman, so it must go first (matches PrepareShutdown
// ordering in init.cpp).
m_node.govman.reset();
if (m_node.gov_collateral_index) {
SyncWithValidationInterfaceQueue();
m_node.gov_collateral_index->Stop();
m_node.gov_collateral_index.reset();
}
}
CScript coinbase_script() const { return GetScriptForRawPubKey(coinbaseKey.GetPubKey()); }
//! Masternode payouts and the proposal fee change must go to a P2PKH script (bad-protx-payee
//! rejects the raw-pubkey coinbase script, IsCollateralValid() rejects anything else).
CScript payout_script() const { return GetScriptForDestination(PKHash(coinbaseKey.GetPubKey())); }
CDeterministicMNList tip_mn_list() const { return m_node.chainman->ActiveEvo().dmnman->GetListAtChainTip(); }
void MineBlock(const std::vector<CMutableTransaction>& txns)
{
const CBlock block = CreateAndProcessBlock(txns, coinbase_script());
const CBlockIndex* tip{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip())};
BOOST_REQUIRE_EQUAL(tip->GetBlockHash(), block.GetHash());
m_node.chainman->ActiveEvo().dmnman->UpdatedBlockTip(tip);
SetMockTime(GetTime() + 1);
IndexWaitSynced(*m_node.gov_collateral_index);
}
void MineBlocks(int num_blocks)
{
for (int i = 0; i < num_blocks; ++i) {
MineBlock({});
}
}
//! The proposal both the orphan vote and the fee tx below commit to. The object hash
//! deliberately excludes the collateral hash, so the same proposal can be built first with an
//! empty collateral (to learn its hash) and again once the fee tx paying to that hash exists.
CGovernanceObject MakeProposal(const uint256& collateral_hash) const
{
const std::string data{
strprintf("{\"type\":1,\"name\":\"test-proposal\",\"start_epoch\":%d,\"end_epoch\":%d,\"payment_amount\":1.0,\"payment_address\":\"%s\",\"url\":\"https://dash.org\"}",
proposal_time, proposal_time + 100000, proposal_payment_address)};
return CGovernanceObject{uint256{}, /*nRevisionIn=*/1, proposal_time, collateral_hash, HexStr(data)};
}
//! Burns the proposal fee to an OP_RETURN committing to the object hash, the way
//! `gobject prepare` does, and buries it under the confirmations the collateral check needs.
uint256 ConfirmProposalCollateral(const uint256& govobj_hash)
{
CMutableTransaction tx;
// IsCollateralValid() rejects the fee tx unless every output is P2PKH or unspendable, so
// the change cannot go back to the burn script.
const auto spent = FundTransaction(*m_node.chainman, tx, utxos,
CScript() << OP_RETURN << ToByteVector(govobj_hash),
GOVERNANCE_PROPOSAL_FEE_TX, /*script_change=*/payout_script());
SignTransaction(tx, spent, coinbaseKey);
MineBlock({tx});
MineBlocks(GOVERNANCE_FEE_CONFIRMATIONS - 1);
return tx.GetHash();
}
//! A vote's hash covers everything but its signature, so votes that should be distinguishable
//! (e.g. a re-signed vote) need distinct timestamps.
CGovernanceVote MakeVote(const uint256& parent_hash, vote_signal_enum_t signal, vote_outcome_enum_t outcome,
std::chrono::seconds time_offset = 0s) const
{
CGovernanceVote vote{mn_collateral, parent_hash, signal, outcome};
vote.SetTime(NodeClock::now() + time_offset);
return vote;
}
};
struct TestGovernanceStore : GovernanceStore {
using GovernanceStore::last_object_rec;
size_t ObjectCount() const
{
LOCK(cs_store);
return mapObjects.size();
}
};
} // namespace
BOOST_FIXTURE_TEST_SUITE(governance_vote_processing_tests, GovernanceVoteSetup)
struct GovernanceReorgSetup : GovernanceVoteSetup {
GovernanceReorgSetup() :
GovernanceVoteSetup("-dip3params=109:109")
{
}
};
BOOST_FIXTURE_TEST_CASE(votes_follow_masternode_identity_across_reorg, GovernanceReorgSetup)
{
auto& chainman = *m_node.chainman;
auto& govman = *m_node.govman;
CKey owner_key, other_owner_key, voting_key;
CBLSSecretKey operator_key, other_operator_key;
voting_key.MakeNewKey(true);
const auto registration = CreateProRegTx(chainman, utxos, 2, payout_script(), coinbaseKey, owner_key, operator_key);
const auto other_registration = CreateProRegTx(chainman, utxos, 3, payout_script(), coinbaseKey, other_owner_key,
other_operator_key);
MineBlock({registration});
CBlockIndex* fork_block{WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip())};
const auto old_id = tip_mn_list().GetMN(registration.GetHash())->GetInternalId();
CProUpRegTx update;
update.nVersion = ProTxVersionForNextBlock(chainman);
update.proTxHash = registration.GetHash();
update.pubKeyOperator.Set(operator_key.GetPublicKey());
update.keyIDVoting = voting_key.GetPubKey().GetID();
update.scriptPayout = payout_script();
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
const auto spent = FundTransaction(chainman, tx, utxos, payout_script(), COIN);
update.inputsHash = CalcTxInputsHash(CTransaction(tx));
BOOST_REQUIRE(CHashSigner::SignHash(::SerializeSigHash(update), owner_key, update.vchSig));
SetTxPayload(tx, update);
SignTransaction(tx, spent, coinbaseKey);
MineBlock({tx});
govman.UpdatedBlockTip(WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()));
const auto proposal = MakeProposal(uint256::ONE);
const auto parent_hash = proposal.GetHash();
govman.AddGovernanceObjectForTesting(proposal);
auto unchanged_vote = MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES);
SignWithVotingKey(unchanged_vote, mn_voting_key);
CGovernanceVote changed_vote{COutPoint{registration.GetHash(), 0}, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES};
SignWithVotingKey(changed_vote, voting_key);
CGovernanceException exception;
uint256 hash_to_request;
BOOST_REQUIRE(govman.ProcessVote(unchanged_vote, exception, hash_to_request));
BOOST_REQUIRE(govman.ProcessVote(changed_vote, exception, hash_to_request));
BOOST_REQUIRE(govman.HaveVoteForHash(changed_vote.GetHash()));
BlockValidationState state;
BOOST_REQUIRE(chainman.ActiveChainstate().InvalidateBlock(state, fork_block));
MineBlock({other_registration});
MineBlock({registration});
BOOST_REQUIRE(old_id != tip_mn_list().GetMN(registration.GetHash())->GetInternalId());
govman.UpdatedBlockTip(WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()));
BOOST_CHECK(!govman.HaveVoteForHash(changed_vote.GetHash()));
BOOST_CHECK(govman.GetCurrentVotes(parent_hash, changed_vote.GetMasternodeOutpoint()).empty());
BOOST_CHECK(govman.HaveVoteForHash(unchanged_vote.GetHash()));
BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, mn_collateral).size(), 1U);
const auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
}
// A vote that arrives before its parent object must be kept and replayed once the object shows up,
// otherwise every vote that races ahead of its proposal is lost for good.
BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives)
{
auto& govman = *m_node.govman;
// Build the proposal but keep it out of the manager: the vote arrives first.
const uint256 parent_hash{MakeProposal(uint256{}).GetHash()};
BOOST_REQUIRE(!govman.HaveObjectForHash(parent_hash));
CGovernanceVote vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithVotingKey(vote, mn_voting_key);
// Guard: the orphan gate rejects a vote it cannot verify against the masternode list, so if
// the masternode or its signature were not recognized every check below would fail at once.
// Requiring validity up front turns that diffuse failure into one pointed at the setup.
BOOST_REQUIRE(vote.IsValid(tip_mn_list(), /*useVotingKey=*/true));
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetType(), GOVERNANCE_EXCEPTION_WARNING);
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
// The caller uses this to ask the sending peer for the missing object.
BOOST_CHECK_EQUAL(hash_to_request, parent_hash);
BOOST_CHECK(govman.GetOrphanVoteObjectHashes() == std::vector<uint256>{parent_hash});
const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)};
CGovernanceObject proposal{MakeProposal(collateral_hash)};
BOOST_REQUIRE_EQUAL(proposal.GetHash(), parent_hash);
govman.AddGovernanceObject(proposal, /*peer_str=*/"");
BOOST_REQUIRE(govman.HaveObjectForHash(parent_hash));
// The orphan vote is now applied to the object it was waiting for and is no longer pending.
auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, mn_collateral).size(), 1U);
BOOST_CHECK(govman.GetOrphanVoteObjectHashes().empty());
BOOST_CHECK(govman.HaveVoteForHash(vote.GetHash()));
DataStream serialized_vote{};
BOOST_CHECK(govman.SerializeVoteForHash(vote.GetHash(), serialized_vote));
if (!serialized_vote.empty()) {
CGovernanceVote served_vote;
serialized_vote >> GOVERNANCE_NETWORK(served_vote);
BOOST_CHECK_EQUAL(served_vote.GetHash(), vote.GetHash());
BOOST_CHECK(served_vote.IsValid(tip_mn_list(), /*useVotingKey=*/true));
BOOST_CHECK(serialized_vote.empty());
}
// A peer re-sending the same vote must not be punished for it.
CGovernanceException replay_exception;
uint256 replay_hash_to_request;
BOOST_CHECK(!govman.ProcessVote(vote, replay_exception, replay_hash_to_request));
BOOST_CHECK_EQUAL(replay_exception.GetNodePenalty(), 0);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
}
BOOST_AUTO_TEST_CASE(orphan_funding_votes_from_multiple_masternodes_are_applied)
{
CKey second_voting_key;
CBLSSecretKey second_operator_key;
const auto protx{CreateProRegTx(*m_node.chainman, utxos, /*port=*/2, payout_script(), coinbaseKey,
second_voting_key, second_operator_key)};
MineBlock({protx});
const COutPoint second_collateral{protx.GetHash(), 0};
BOOST_REQUIRE(tip_mn_list().GetMNByCollateral(second_collateral) != nullptr);
auto& govman = *m_node.govman;
const uint256 parent_hash{MakeProposal(uint256{}).GetHash()};
CGovernanceVote first_vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithVotingKey(first_vote, mn_voting_key);
CGovernanceVote second_vote{second_collateral, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES};
SignWithVotingKey(second_vote, second_voting_key);
for (const auto& vote : {first_vote, second_vote, second_vote}) {
BOOST_REQUIRE(vote.IsValid(tip_mn_list(), /*useVotingKey=*/true));
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetType(), GOVERNANCE_EXCEPTION_WARNING);
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
}
const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)};
CGovernanceObject proposal{MakeProposal(collateral_hash)};
BOOST_REQUIRE_EQUAL(proposal.GetHash(), parent_hash);
govman.AddGovernanceObject(proposal, /*peer_str=*/"");
const auto stored{govman.FindConstGovernanceObject(parent_hash)};
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 2);
const auto current_votes{govman.GetCurrentVotes(parent_hash, COutPoint{})};
BOOST_REQUIRE_EQUAL(current_votes.size(), 2U);
BOOST_CHECK(govman.GetOrphanVoteObjectHashes().empty());
}
BOOST_AUTO_TEST_CASE(orphan_timestamp_variants_replay_only_the_newest_vote)
{
auto& govman = *m_node.govman;
const uint256 parent_hash{MakeProposal(uint256{}).GetHash()};
uint256 newest_hash;
for (const int offset : {1, 5, 4, 3, 2, 5}) {
CGovernanceVote vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, std::chrono::seconds{offset})};
SignWithVotingKey(vote, mn_voting_key);
if (offset == 5) newest_hash = vote.GetHash();
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
}
const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)};
CGovernanceObject proposal{MakeProposal(collateral_hash)};
govman.AddGovernanceObject(proposal, /*peer_str=*/"");
SyncWithValidationInterfaceQueue();
BOOST_CHECK(vote_notifications->GetHashes() == std::vector<uint256>{newest_hash});
const auto stored{govman.FindConstGovernanceObject(parent_hash)};
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
const auto votes{govman.GetCurrentVotes(parent_hash, mn_collateral)};
BOOST_REQUIRE_EQUAL(votes.size(), 1U);
BOOST_CHECK_EQUAL(votes.front().GetHash(), newest_hash);
}
BOOST_AUTO_TEST_CASE(operator_orphans_do_not_displace_a_voting_key_orphan)
{
auto& govman = *m_node.govman;
const uint256 parent_hash{MakeProposal(uint256{}).GetHash()};
CGovernanceVote operator_vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithOperatorKey(operator_vote, mn_operator_key);
CGovernanceVote voting_vote{operator_vote};
SignWithVotingKey(voting_vote, mn_voting_key);
CGovernanceVote newer_operator_vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 30s)};
SignWithOperatorKey(newer_operator_vote, mn_operator_key);
// The voting-key vote has the same signed fields as the first operator vote and is older than the second.
for (const auto& vote : {operator_vote, voting_vote, newer_operator_vote}) {
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
}
const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)};
CGovernanceObject proposal{MakeProposal(collateral_hash)};
govman.AddGovernanceObject(proposal, /*peer_str=*/"");
SyncWithValidationInterfaceQueue();
BOOST_CHECK(vote_notifications->GetHashes() == std::vector<uint256>{voting_vote.GetHash()});
const auto votes{govman.GetCurrentVotes(parent_hash, mn_collateral)};
BOOST_REQUIRE_EQUAL(votes.size(), 1U);
BOOST_CHECK_EQUAL(votes.front().GetHash(), voting_vote.GetHash());
}
BOOST_AUTO_TEST_CASE(old_key_orphan_does_not_suppress_a_current_key_vote)
{
// Before DIP3 enforcement, registrar updates must keep the owner and voting keys equal.
MineBlocks(500 - tip_mn_list().GetHeight());
auto& govman = *m_node.govman;
const uint256 parent_hash{MakeProposal(uint256{}).GetHash()};
CGovernanceVote old_vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 30s)};
SignWithVotingKey(old_vote, mn_voting_key);
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(old_vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
CKey new_voting_key;
new_voting_key.MakeNewKey(true);
CProUpRegTx update;
update.nVersion = ProTxVersionForNextBlock(*m_node.chainman);
update.proTxHash = mn_collateral.hash;
update.pubKeyOperator.Set(mn_operator_key.GetPublicKey());
update.keyIDVoting = new_voting_key.GetPubKey().GetID();
update.scriptPayout = payout_script();
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
const auto spent{FundTransaction(*m_node.chainman, tx, utxos, payout_script(), 1 * COIN)};
update.inputsHash = CalcTxInputsHash(CTransaction{tx});
BOOST_REQUIRE(CHashSigner::SignHash(::SerializeSigHash(update), mn_voting_key, update.vchSig));
SetTxPayload(tx, update);
SignTransaction(tx, spent, coinbaseKey);
MineBlock({tx});
BOOST_REQUIRE(!old_vote.IsValid(tip_mn_list(), /*useVotingKey=*/true));
CGovernanceVote current_vote{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1s)};
SignWithVotingKey(current_vote, new_voting_key);
BOOST_REQUIRE(current_vote.IsValid(tip_mn_list(), /*useVotingKey=*/true));
BOOST_CHECK(!govman.ProcessVote(current_vote, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 0);
const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)};
CGovernanceObject proposal{MakeProposal(collateral_hash)};
govman.AddGovernanceObject(proposal, /*peer_str=*/"");
SyncWithValidationInterfaceQueue();
BOOST_CHECK(vote_notifications->GetHashes() == std::vector<uint256>{current_vote.GetHash()});
const auto votes{govman.GetCurrentVotes(parent_hash, mn_collateral)};
BOOST_REQUIRE_EQUAL(votes.size(), 1U);
BOOST_CHECK_EQUAL(votes.front().GetHash(), current_vote.GetHash());
}
// Votes are only counted if they carry a signature from a registered masternode.
BOOST_AUTO_TEST_CASE(unsigned_and_unknown_masternode_votes_are_rejected)
{
auto& govman = *m_node.govman;
const CGovernanceObject proposal{MakeProposal(uint256::ONE)};
const uint256 parent_hash{proposal.GetHash()};
govman.AddGovernanceObjectForTesting(proposal);
BOOST_REQUIRE(govman.HaveObjectForHash(parent_hash));
// Forged signature: right masternode, wrong key.
CKey attacker_key;
attacker_key.MakeNewKey(true);
CGovernanceVote forged{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithVotingKey(forged, attacker_key);
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(forged, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 20);
BOOST_CHECK(!govman.HaveVoteForHash(forged.GetHash()));
// Re-sending the same invalid vote is still rejected and punished.
CGovernanceException repeat_exception;
BOOST_CHECK(!govman.ProcessVote(forged, repeat_exception, hash_to_request));
BOOST_CHECK_EQUAL(repeat_exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(repeat_exception.GetNodePenalty(), 20);
// Forged operator signature: right masternode, wrong BLS key. A non-funding signal, so the
// operator-key (BLS) verification path is the one that must reject it.
CBLSSecretKey attacker_operator_key;
attacker_operator_key.MakeNewKey();
CGovernanceVote forged_bls{MakeVote(parent_hash, VOTE_SIGNAL_VALID, VOTE_OUTCOME_YES)};
SignWithOperatorKey(forged_bls, attacker_operator_key);
CGovernanceException forged_bls_exception;
BOOST_CHECK(!govman.ProcessVote(forged_bls, forged_bls_exception, hash_to_request));
BOOST_CHECK_EQUAL(forged_bls_exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(forged_bls_exception.GetNodePenalty(), 20);
BOOST_CHECK(!govman.HaveVoteForHash(forged_bls.GetHash()));
// Correctly signed, but the outpoint belongs to no masternode.
CKey stranger_key;
stranger_key.MakeNewKey(true);
CGovernanceVote stranger{COutPoint{uint256S("42"), 0}, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES};
stranger.SetTime(NodeClock::now());
SignWithVotingKey(stranger, stranger_key);
CGovernanceException stranger_exception;
BOOST_CHECK(!govman.ProcessVote(stranger, stranger_exception, hash_to_request));
BOOST_CHECK_EQUAL(stranger_exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(stranger_exception.GetNodePenalty(), 20);
// A vote dated too far in the future is rejected even with a valid signature.
CGovernanceVote from_the_future{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 2h)};
SignWithVotingKey(from_the_future, mn_voting_key);
CGovernanceException future_exception;
BOOST_CHECK(!govman.ProcessVote(from_the_future, future_exception, hash_to_request));
BOOST_CHECK_EQUAL(future_exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(future_exception.GetNodePenalty(), 20);
auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 0);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_VALID), 0);
}
BOOST_AUTO_TEST_CASE(invalid_signature_does_not_suppress_valid_vote)
{
auto& govman = *m_node.govman;
const CGovernanceObject proposal{MakeProposal(uint256::ONE)};
const uint256 parent_hash{proposal.GetHash()};
govman.AddGovernanceObjectForTesting(proposal);
auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
CKey attacker_key;
attacker_key.MakeNewKey(true);
CGovernanceVote forged{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithVotingKey(forged, attacker_key);
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(forged, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 20);
vote_rec_t vote_record;
BOOST_CHECK(!stored->GetCurrentMNVotes(mn_collateral, vote_record));
CGovernanceVote legitimate{forged};
SignWithVotingKey(legitimate, mn_voting_key);
BOOST_CHECK_EQUAL(forged.GetHash(), legitimate.GetHash());
BOOST_CHECK(govman.ProcessVote(legitimate, exception, hash_to_request));
BOOST_CHECK(govman.HaveVoteForHash(legitimate.GetHash()));
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
CBLSSecretKey attacker_operator_key;
attacker_operator_key.MakeNewKey();
CGovernanceVote forged_bls{MakeVote(parent_hash, VOTE_SIGNAL_VALID, VOTE_OUTCOME_YES)};
SignWithOperatorKey(forged_bls, attacker_operator_key);
BOOST_CHECK(!govman.ProcessVote(forged_bls, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 20);
BOOST_REQUIRE(stored->GetCurrentMNVotes(mn_collateral, vote_record));
BOOST_CHECK(!vote_record.mapInstances.contains(VOTE_SIGNAL_VALID));
CGovernanceVote legitimate_bls{forged_bls};
SignWithOperatorKey(legitimate_bls, mn_operator_key);
BOOST_CHECK_EQUAL(forged_bls.GetHash(), legitimate_bls.GetHash());
BOOST_CHECK(govman.ProcessVote(legitimate_bls, exception, hash_to_request));
BOOST_CHECK(govman.HaveVoteForHash(legitimate_bls.GetHash()));
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_VALID), 1);
}
// Funding a proposal is the one decision reserved for the voting key; every other signal may be
// signed by the operator (BLS) key. Mixing the two up must not be accepted either way around.
//
// Rate checks are live on this path (unlike the orphan replay above, which disables them), so each
// vote below has to be the first *accepted* one for its (masternode, signal) pair: a rejected vote
// leaves no vote instance behind, but a second accepted vote for the same signal would be turned
// away by the 1h GOVERNANCE_UPDATE_MIN limit instead of the rule under test.
BOOST_AUTO_TEST_CASE(proposal_funding_votes_require_the_voting_key)
{
auto& govman = *m_node.govman;
const CGovernanceObject proposal{MakeProposal(uint256::ONE)};
const uint256 parent_hash{proposal.GetHash()};
govman.AddGovernanceObjectForTesting(proposal);
auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
// Operator key on a funding vote: valid BLS signature, wrong key for this signal.
CGovernanceVote funding_by_operator{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithOperatorKey(funding_by_operator, mn_operator_key);
BOOST_REQUIRE(funding_by_operator.IsValid(tip_mn_list(), /*useVotingKey=*/false));
CGovernanceException exception;
uint256 hash_to_request;
BOOST_CHECK(!govman.ProcessVote(funding_by_operator, exception, hash_to_request));
BOOST_CHECK_EQUAL(exception.GetType(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
BOOST_CHECK_EQUAL(exception.GetNodePenalty(), 20);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 0);
// Voting key on a funding vote: accepted and counted. Dated a second later so it is a vote in
// its own right rather than the rejected one above with a different signature glued on.
CGovernanceVote funding_by_voting_key{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1s)};
SignWithVotingKey(funding_by_voting_key, mn_voting_key);
CGovernanceException funding_exception;
BOOST_CHECK(govman.ProcessVote(funding_by_voting_key, funding_exception, hash_to_request));
BOOST_CHECK(govman.HaveVoteForHash(funding_by_voting_key.GetHash()));
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
// Operator key on a non-funding signal: accepted and counted.
CGovernanceVote valid_by_operator{MakeVote(parent_hash, VOTE_SIGNAL_VALID, VOTE_OUTCOME_YES)};
SignWithOperatorKey(valid_by_operator, mn_operator_key);
CGovernanceException valid_exception;
BOOST_CHECK(govman.ProcessVote(valid_by_operator, valid_exception, hash_to_request));
BOOST_CHECK(govman.HaveVoteForHash(valid_by_operator.GetHash()));
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_VALID), 1);
// A known good vote received twice is dropped quietly, not punished.
CGovernanceException duplicate_exception;
BOOST_CHECK(!govman.ProcessVote(funding_by_voting_key, duplicate_exception, hash_to_request));
BOOST_CHECK_EQUAL(duplicate_exception.GetNodePenalty(), 0);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
}
BOOST_AUTO_TEST_CASE(legacy_governance_cache_is_discarded)
{
const uint256 parent_hash{MakeProposal(uint256::ONE).GetHash()};
CKey attacker_key;
attacker_key.MakeNewKey(true);
CGovernanceVote forged{MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES)};
SignWithVotingKey(forged, attacker_key);
CacheMap<uint256, CGovernanceVote> legacy_invalid_votes{3};
legacy_invalid_votes.Insert(forged.GetHash(), forged);
const auto proposal = std::make_shared<CGovernanceObject>(MakeProposal(uint256::ONE));
const std::map<uint256, std::shared_ptr<CGovernanceObject>> objects{{proposal->GetHash(), proposal}};
// A version 16 cache, including its invalid-vote field, is dropped as a whole.
DataStream legacy{};
legacy << std::string{"CGovernanceManager-Version-16"}
<< std::map<uint256, int64_t>{}
<< legacy_invalid_votes
<< CacheMultiMap<uint256, governance::OrphanVote>{3}
<< GOVERNANCE_DISK(objects)
<< std::map<COutPoint, TestGovernanceStore::last_object_rec>{}
<< CDeterministicMNList{};
TestGovernanceStore legacy_store;
legacy_store.Unserialize(legacy);
BOOST_CHECK_EQUAL(legacy_store.ObjectCount(), 0U);
// The current format has no invalid-vote field and round-trips.
DataStream current{};
current << std::string{"CGovernanceManager-Version-17"}
<< std::map<uint256, int64_t>{}
<< CacheMultiMap<uint256, governance::OrphanVote>{3}
<< GOVERNANCE_DISK(objects)
<< std::map<COutPoint, TestGovernanceStore::last_object_rec>{}
<< CDeterministicMNList{};
TestGovernanceStore store;
store.Unserialize(current);
BOOST_CHECK_EQUAL(store.ObjectCount(), 1U);
DataStream saved{};
store.Serialize(saved);
TestGovernanceStore reloaded;
reloaded.Unserialize(saved);
BOOST_CHECK_EQUAL(reloaded.ObjectCount(), 1U);
BOOST_CHECK(saved.empty());
}
// MasternodeRateCheck allows a masternode that has no rate-buffer entry yet, and only accepted
// triggers used to create one. A key whose triggers all fail AddTrigger was therefore never
// throttled: every one cost a BLS verification and a mapErasedGovernanceObjects entry.
BOOST_AUTO_TEST_CASE(failed_triggers_advance_the_masternode_rate_limit)
{
const int64_t now{GetTime()};
constexpr int attempts{12};
int stored{0};
for (int i = 0; i < attempts; ++i) {
CGovernanceObject govobj{MakeFailedTrigger(mn_collateral, mn_operator_key, now + i, /*salt=*/i)};
const uint256 hash{govobj.GetHash()};
// Rate rejection is not misbehaviour, so ProcessObject reports success either way.
WITH_LOCK(::cs_main, BOOST_CHECK(m_node.govman->ProcessObject(/*peer_str=*/"test-peer", hash, govobj)));
if (m_node.govman->HaveObjectForHash(hash)) {
++stored;
BOOST_CHECK(Assert(m_node.govman->FindGovernanceObject(hash))->IsSetCachedDelete());
}
}
// The rate is only computed over a full buffer, so the attempt that would fill it is the
// first one rated, and these timestamps are far too close together to pass.
BOOST_CHECK_EQUAL(stored, RATE_BUFFER_SIZE - 1);
BOOST_CHECK(m_node.govman->FetchRelayInventory().empty());
}
// Rate accounting now runs before AddTrigger, but scheduling the deferred re-announcement of a
// trigger created close to the future-deviation limit must not: a trigger that AddTrigger rejected
// would be announced, and served on GETDATA, once it aged past RELIABLE_PROPAGATION_TIME.
BOOST_AUTO_TEST_CASE(failed_trigger_is_not_scheduled_for_deferred_relay)
{
const int64_t now{GetTime()};
// Inside (now + MAX_TIME_FUTURE_DEVIATION - RELIABLE_PROPAGATION_TIME, now + MAX_TIME_FUTURE_DEVIATION],
// i.e. 1h - 60s < offset <= 1h: accepted by MasternodeRateCheck, too new to propagate reliably.
CGovernanceObject govobj{MakeFailedTrigger(mn_collateral, mn_operator_key, now + 3550, /*salt=*/0)};
const uint256 hash{govobj.GetHash()};
WITH_LOCK(::cs_main, BOOST_CHECK(m_node.govman->ProcessObject(/*peer_str=*/"test-peer", hash, govobj)));
BOOST_REQUIRE(m_node.govman->HaveObjectForHash(hash));
BOOST_CHECK(!m_node.govman->HaveSyncableObjectForHash(hash));
BOOST_CHECK(m_node.govman->FetchRelayInventory().empty());
// Age it past RELIABLE_PROPAGATION_TIME and run the deferred-relay pass.
SetMockTime(std::chrono::seconds{now + 120});
m_node.govman->UpdatedBlockTip(WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip()));
BOOST_CHECK(m_node.govman->FetchRelayInventory().empty());
}
BOOST_AUTO_TEST_SUITE_END()