Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/cachemultimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down
31 changes: 4 additions & 27 deletions src/governance/vote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <timedata.h>
#include <util/string.h>

#include <tuple>

static_assert(CGovernanceVote::COMPACT_SIG_SIZE == CPubKey::COMPACT_SIGNATURE_SIZE);
static_assert(CGovernanceVote::BLS_SIG_SIZE == CBLSSignature::SerSize);

Expand Down Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions src/test/cachemultimap_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ BOOST_AUTO_TEST_CASE(cachemultimap_test)
CacheMultiMap<int,int> mapTest4;
mapTest4 = cmmapTest1;
BOOST_CHECK(Compare(cmmapTest1, mapTest4));

CacheMultiMap<int, int> 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<int> values;
BOOST_CHECK(full_cache.GetAll(1, values));
BOOST_CHECK(values == std::vector<int>{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<int>{4, 5}));
}

BOOST_AUTO_TEST_SUITE_END()
52 changes: 50 additions & 2 deletions src/test/governance_vote_processing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGovernanceVote> 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()};
Expand All @@ -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<uint256>{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);
Expand All @@ -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
Expand All @@ -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.
Expand Down
Loading