From 8a45e6e0c438303b5db0bad9eba151b3e6931354 Mon Sep 17 00:00:00 2001 From: pasta Date: Wed, 11 Nov 2026 02:20:32 +0000 Subject: [PATCH] fix: order distinct governance votes in the orphan cache Compare the vote identity fields lexicographically so signed votes from different masternodes are retained for one missing parent. Preserve duplicate rejection and the existing total-value cache bound. Reacquire the cache index after eviction because pruning the last old value for the destination key invalidates its map reference. Cover ordering, two applied orphan votes, and replacement at capacity in the existing unit tests. Co-Authored-By: GPT 6.1 Sol --- src/cachemultimap.h | 6 +-- src/governance/vote.cpp | 31 ++--------- src/test/cachemultimap_tests.cpp | 18 +++++++ src/test/governance_vote_processing_tests.cpp | 52 ++++++++++++++++++- 4 files changed, 74 insertions(+), 33 deletions(-) diff --git a/src/cachemultimap.h b/src/cachemultimap.h index 88f3f12c2bea..3a63ff287eeb 100644 --- a/src/cachemultimap.h +++ b/src/cachemultimap.h @@ -90,9 +90,7 @@ class CacheMultiMap if(mit == mapIndex.end()) { mit = mapIndex.emplace(key, it_map_t()).first; } - it_map_t& mapIt = mit->second; - - if(mapIt.count(value) > 0) { + if (mit->second.count(value) > 0) { // Don't insert duplicates return false; } @@ -101,7 +99,7 @@ class CacheMultiMap PruneLast(); } listItems.push_front(item_t(key, value)); - mapIt.emplace(value, listItems.begin()); + mapIndex[key].emplace(value, listItems.begin()); return true; } diff --git a/src/governance/vote.cpp b/src/governance/vote.cpp index 2b5265c62d69..7d4532095181 100644 --- a/src/governance/vote.cpp +++ b/src/governance/vote.cpp @@ -16,6 +16,8 @@ #include #include +#include + static_assert(CGovernanceVote::COMPACT_SIG_SIZE == CPubKey::COMPACT_SIGNATURE_SIZE); static_assert(CGovernanceVote::BLS_SIG_SIZE == CBLSSignature::SerSize); @@ -262,31 +264,6 @@ bool operator==(const CGovernanceVote& vote1, const CGovernanceVote& vote2) bool operator<(const CGovernanceVote& vote1, const CGovernanceVote& vote2) { - bool fResult = (vote1.masternodeOutpoint < vote2.masternodeOutpoint); - if (!fResult) { - return false; - } - fResult = (vote1.masternodeOutpoint == vote2.masternodeOutpoint); - - fResult = fResult && (vote1.nParentHash < vote2.nParentHash); - if (!fResult) { - return false; - } - fResult = (vote1.nParentHash == vote2.nParentHash); - - fResult = fResult && (vote1.nVoteOutcome < vote2.nVoteOutcome); - if (!fResult) { - return false; - } - fResult = (vote1.nVoteOutcome == vote2.nVoteOutcome); - - fResult = fResult && (vote1.nVoteSignal == vote2.nVoteSignal); - if (!fResult) { - return false; - } - fResult = (vote1.nVoteSignal == vote2.nVoteSignal); - - fResult = fResult && (vote1.nTime < vote2.nTime); - - return fResult; + return std::tie(vote1.masternodeOutpoint, vote1.nParentHash, vote1.nVoteOutcome, vote1.nVoteSignal, vote1.nTime) < + std::tie(vote2.masternodeOutpoint, vote2.nParentHash, vote2.nVoteOutcome, vote2.nVoteSignal, vote2.nTime); } diff --git a/src/test/cachemultimap_tests.cpp b/src/test/cachemultimap_tests.cpp index 46712b831b98..10ba4c66f0d8 100644 --- a/src/test/cachemultimap_tests.cpp +++ b/src/test/cachemultimap_tests.cpp @@ -164,6 +164,24 @@ BOOST_AUTO_TEST_CASE(cachemultimap_test) CacheMultiMap mapTest4; mapTest4 = cmmapTest1; BOOST_CHECK(Compare(cmmapTest1, mapTest4)); + + CacheMultiMap full_cache{2}; + BOOST_CHECK(full_cache.Insert(1, 1)); + BOOST_CHECK(full_cache.Insert(2, 2)); + BOOST_CHECK(full_cache.Insert(1, 3)); + std::vector values; + BOOST_CHECK(full_cache.GetAll(1, values)); + BOOST_CHECK(values == std::vector{3}); + BOOST_CHECK_EQUAL(full_cache.GetSize(), 2U); + + BOOST_CHECK(full_cache.Insert(1, 4)); + BOOST_CHECK(!full_cache.HasKey(2)); + BOOST_CHECK(!full_cache.Insert(1, 4)); + BOOST_CHECK(full_cache.Insert(1, 5)); + BOOST_CHECK_EQUAL(full_cache.GetSize(), 2U); + values.clear(); + BOOST_CHECK(full_cache.GetAll(1, values)); + BOOST_CHECK((values == std::vector{4, 5})); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/governance_vote_processing_tests.cpp b/src/test/governance_vote_processing_tests.cpp index 2b2f6f28bfb3..78323996a904 100644 --- a/src/test/governance_vote_processing_tests.cpp +++ b/src/test/governance_vote_processing_tests.cpp @@ -232,11 +232,50 @@ struct TestGovernanceStore : GovernanceStore { BOOST_FIXTURE_TEST_SUITE(governance_vote_processing_tests, GovernanceVoteSetup) +BOOST_FIXTURE_TEST_CASE(vote_ordering, BasicTestingSetup) +{ + const COutPoint collateral{uint256::ONE, 0}; + const auto make_vote = [](const COutPoint& mn, const uint256& parent, vote_signal_enum_t signal, + vote_outcome_enum_t outcome, int64_t timestamp) { + CGovernanceVote vote{mn, parent, signal, outcome}; + vote.SetTime(timestamp); + return vote; + }; + const std::vector votes{ + make_vote(collateral, uint256::ONE, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1000), + make_vote(collateral, uint256::ONE, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1001), + make_vote(collateral, uint256::ONE, VOTE_SIGNAL_VALID, VOTE_OUTCOME_YES, 1000), + make_vote(collateral, uint256::ONE, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_NO, 1000), + make_vote(collateral, uint256S("02"), VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1000), + make_vote(COutPoint{uint256::ONE, 1}, uint256::ONE, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1000), + make_vote(COutPoint{uint256S("02"), 0}, uint256::ONE, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES, 1000), + }; + for (size_t i = 0; i < votes.size(); ++i) { + BOOST_CHECK(!(votes[i] < votes[i])); + for (size_t j = i + 1; j < votes.size(); ++j) { + BOOST_CHECK(!(votes[i] == votes[j])); + BOOST_CHECK(votes[i] < votes[j]); + BOOST_CHECK(!(votes[j] < votes[i])); + } + } + auto resigned = votes.front(); + resigned.SetSignature({1, 2, 3}); + BOOST_CHECK(resigned == votes.front()); + BOOST_CHECK(!(resigned < votes.front())); + BOOST_CHECK(!(votes.front() < resigned)); +} + // 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; + 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); + const COutPoint second_collateral{protx.GetHash(), 0}; + MineBlock({protx}); // Build the proposal but keep it out of the manager: the vote arrives first. const uint256 parent_hash{MakeProposal(uint256{}).GetHash()}; @@ -258,6 +297,14 @@ BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives) BOOST_CHECK_EQUAL(hash_to_request, parent_hash); BOOST_CHECK(govman.GetOrphanVoteObjectHashes() == std::vector{parent_hash}); + CGovernanceVote second_vote{second_collateral, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES}; + SignWithVotingKey(second_vote, second_voting_key); + BOOST_REQUIRE(second_vote.IsValid(tip_mn_list(), /*useVotingKey=*/true)); + BOOST_CHECK(!govman.ProcessVote(second_vote, exception, hash_to_request)); + BOOST_CHECK_EQUAL(hash_to_request, parent_hash); + BOOST_CHECK(!govman.ProcessVote(second_vote, exception, hash_to_request)); + BOOST_CHECK(hash_to_request.IsNull()); + const uint256 collateral_hash{ConfirmProposalCollateral(parent_hash)}; CGovernanceObject proposal{MakeProposal(collateral_hash)}; BOOST_REQUIRE_EQUAL(proposal.GetHash(), parent_hash); @@ -267,8 +314,9 @@ BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives) // 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(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 2); BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, mn_collateral).size(), 1U); + BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, second_collateral).size(), 1U); BOOST_CHECK(govman.GetOrphanVoteObjectHashes().empty()); // Known gap, pinned here so a fix has to update this test: CheckOrphanVotes() applies the // vote to the object but never indexes it in cmapVoteToObject, so the vote inv it relays @@ -280,7 +328,7 @@ BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives) 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_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 2); } // Votes are only counted if they carry a signature from a registered masternode.