Skip to content

Commit

Permalink
fix: avoid voting for the same trigger multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Jul 25, 2024
1 parent 3850128 commit fc7425b
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <netfulfilledman.h>
#include <netmessagemaker.h>
#include <protocol.h>
#include <ranges.h>
#include <shutdown.h>
#include <spork.h>
#include <util/time.h>
Expand Down Expand Up @@ -733,21 +734,36 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
if (trigger_opt.has_value()) {
// We should never vote "yes" on another trigger or the same trigger twice
assert(!votedFundingYesTriggerHash.has_value());
// Vote YES-FUNDING for the trigger we like
// Vote YES-FUNDING for the trigger we like, unless we already did
const uint256 gov_sb_hash = trigger_opt.value().GetHash();
if (!VoteFundingTrigger(gov_sb_hash, VOTE_OUTCOME_YES, connman, peerman, mn_activeman)) {
if (vote_rec_t voteRecord; trigger_opt.value().GetCurrentMNVotes(mn_activeman.GetOutPoint(), voteRecord)) {
const auto& strFunc = __func__;
ranges::any_of(voteRecord.mapInstances, [&](const auto& voteInstancePair){
if (voteInstancePair.first == VOTE_SIGNAL_FUNDING) {
if (voteInstancePair.second.eOutcome == VOTE_OUTCOME_YES) {
votedFundingYesTriggerHash = gov_sb_hash;
}
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Not voting YES-FUNDING for trigger:%s, we voted %s for it already\n",
strFunc, gov_sb_hash.ToString(), CGovernanceVoting::ConvertOutcomeToString(voteInstancePair.second.eOutcome));
return true;
}
return false;
});
} else if (VoteFundingTrigger(gov_sb_hash, VOTE_OUTCOME_YES, connman, peerman, mn_activeman)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting YES-FUNDING for new trigger:%s success\n", __func__, gov_sb_hash.ToString());
votedFundingYesTriggerHash = gov_sb_hash;
} else {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting YES-FUNDING for new trigger:%s failed\n", __func__, gov_sb_hash.ToString());
// this should never happen, bail out
return;
}
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting YES-FUNDING for new trigger:%s success\n", __func__, gov_sb_hash.ToString());
votedFundingYesTriggerHash = gov_sb_hash;
}

// Vote NO-FUNDING for the rest of the active triggers
const auto activeTriggers = GetActiveTriggers();
for (const auto& trigger : activeTriggers) {
const uint256 trigger_hash = trigger->GetGovernanceObject(*this)->GetHash();
const auto govobj = trigger->GetGovernanceObject(*this);
const uint256 trigger_hash = govobj->GetHash();
if (trigger->GetBlockHeight() <= nCachedBlockHeight) {
// ignore triggers from the past
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Not voting NO-FUNDING for outdated trigger:%s\n", __func__, trigger_hash.ToString());
Expand All @@ -758,6 +774,19 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Not voting NO-FUNDING for trigger:%s, we voted yes for it already\n", __func__, trigger_hash.ToString());
continue;
}
if (vote_rec_t voteRecord; govobj->GetCurrentMNVotes(mn_activeman.GetOutPoint(), voteRecord)) {
const auto& strFunc = __func__;
if (ranges::any_of(voteRecord.mapInstances, [&](const auto& voteInstancePair){
if (voteInstancePair.first == VOTE_SIGNAL_FUNDING) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Not voting NO-FUNDING for trigger:%s, we voted %s for it already\n",
strFunc, trigger_hash.ToString(), CGovernanceVoting::ConvertOutcomeToString(voteInstancePair.second.eOutcome));
return true;
}
return false;
})) {
continue;
}
}
if (!VoteFundingTrigger(trigger_hash, VOTE_OUTCOME_NO, connman, peerman, mn_activeman)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting NO-FUNDING for trigger:%s failed\n", __func__, trigger_hash.ToString());
// failing here is ok-ish
Expand Down

0 comments on commit fc7425b

Please sign in to comment.