Skip to content

Commit 86722d1

Browse files
committed
Enhance PollResult and AVW to handle pools better
1 parent a881378 commit 86722d1

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

src/gridcoin/voting/registry.cpp

+26-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "gridcoin/voting/payloads.h"
1313
#include "gridcoin/voting/registry.h"
1414
#include "gridcoin/voting/vote.h"
15+
#include "gridcoin/voting/result.h"
1516
#include "gridcoin/support/block_finder.h"
1617
#include "node/blockstorage.h"
1718
#include "txdb.h"
@@ -368,7 +369,7 @@ std::optional<int> PollReference::GetEndingHeight() const
368369
return std::nullopt;
369370
}
370371

371-
std::optional<CAmount> PollReference::GetActiveVoteWeight() const
372+
std::optional<CAmount> PollReference::GetActiveVoteWeight(const PollResultOption& result) const
372373
{
373374
// Instrument this so we can log real time performance.
374375
g_timer.InitTimer(__func__, LogInstance().WillLogCategory(BCLog::LogFlags::VOTE));
@@ -394,6 +395,28 @@ std::optional<CAmount> PollReference::GetActiveVoteWeight() const
394395
__func__, pindex_end->nHeight);
395396
}
396397

398+
// determine the pools that did NOT vote in the poll (via the result passed in). Only pools that did not
399+
// vote contribute to the magnitude correction for pools.
400+
std::vector<MiningPool> pools_not_voting;
401+
const std::vector<MiningPool>& mining_pools = g_mining_pools.GetMiningPools();
402+
403+
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: %u out of %u pool cpids voted.",
404+
__func__,
405+
result->m_pools_voted.size(),
406+
mining_pools.size());
407+
408+
for (const auto& pool : mining_pools) {
409+
if (result && std::find(result->m_pools_voted.begin(), result->m_pools_voted.end(),
410+
pool.m_cpid) == result->m_pools_voted.end()) {
411+
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: pool with cpid %s did not vote and will be removed from magnitude "
412+
"in the AVW calculation.",
413+
__func__,
414+
pool.m_cpid.ToString());
415+
416+
pools_not_voting.push_back(pool);
417+
}
418+
}
419+
397420
// Since this calculation by its very nature is going to be heavyweight, we are going to
398421
// dispense with using the heavyweight averaging functions, and instead accumulate the necessary
399422
// values directly in a for loop that traverses the index. This will do it all in a single index
@@ -439,7 +462,7 @@ std::optional<CAmount> PollReference::GetActiveVoteWeight() const
439462

440463
superblock_well_formed = superblock.WellFormed();
441464

442-
for (const auto& pool : g_mining_pools.GetMiningPools()) {
465+
for (const auto& pool : pools_not_voting) {
443466
scaled_pool_magnitude += superblock.m_cpids.MagnitudeOf(pool.m_cpid).Scaled();
444467
}
445468

@@ -489,7 +512,7 @@ std::optional<CAmount> PollReference::GetActiveVoteWeight() const
489512
if (claim && claim->ContainsSuperblock()) {
490513
const GRC::Superblock& superblock = *claim->m_superblock;
491514

492-
for (const auto& pool : g_mining_pools.GetMiningPools()) {
515+
for (const auto& pool : pools_not_voting) {
493516
scaled_pool_magnitude += superblock.m_cpids.MagnitudeOf(pool.m_cpid).Scaled();
494517
}
495518

src/gridcoin/voting/registry.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class PollReference
127127
//!
128128
std::optional<int> GetEndingHeight() const;
129129

130-
std::optional<CAmount> GetActiveVoteWeight() const;
130+
std::optional<CAmount> GetActiveVoteWeight(const PollResultOption &result) const;
131131

132132
//!
133133
//! \brief Record a transaction that contains a response to the poll.

src/gridcoin/voting/result.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "gridcoin/voting/result.h"
1111
#include "gridcoin/voting/poll.h"
1212
#include "gridcoin/voting/vote.h"
13+
#include "gridcoin/researcher.h"
1314
#include "txdb.h"
1415
#include "util/reverse_iterator.h"
1516

@@ -23,6 +24,8 @@ using ResponseDetail = PollResult::ResponseDetail;
2324
using VoteDetail = PollResult::VoteDetail;
2425
using Weight = PollResult::Weight;
2526

27+
extern MiningPools g_mining_pools;
28+
2629
namespace {
2730
//!
2831
//! \brief Used for mapping legacy vote answer labels to poll choice offsets.
@@ -803,12 +806,15 @@ class VoteCounter
803806
for (auto& vote : m_votes) {
804807
result.TallyVote(std::move(vote));
805808
}
809+
810+
result.m_pools_voted = m_pools_voted;
806811
}
807812

808813
private:
809814
CTxDB& m_txdb;
810815
const Poll& m_poll;
811816
std::vector<VoteDetail> m_votes;
817+
std::vector<Cpid> m_pools_voted;
812818
Weight m_magnitude_factor;
813819
VoteResolver m_resolver;
814820
LegacyVoteCounterContext m_legacy;
@@ -912,6 +918,24 @@ class VoteCounter
912918

913919
CalculateWeight(detail, m_magnitude_factor);
914920

921+
const std::vector<MiningPool>& mining_pools = g_mining_pools.GetMiningPools();
922+
923+
// Record if a pool votes
924+
if (detail.m_mining_id.TryCpid()) {
925+
for (const auto& pool : mining_pools) {
926+
if (detail.m_mining_id == pool.m_cpid) {
927+
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: Pool with CPID %s voted on poll %s.",
928+
__func__,
929+
detail.m_mining_id.TryCpid()->ToString(),
930+
m_poll.m_title);
931+
932+
m_pools_voted.push_back(*detail.m_mining_id.TryCpid());
933+
934+
break;
935+
}
936+
}
937+
}
938+
915939
m_votes.emplace_back(std::move(detail));
916940
}
917941

@@ -1083,7 +1107,8 @@ PollResult::PollResult(Poll poll)
10831107
: m_poll(std::move(poll))
10841108
, m_total_weight(0)
10851109
, m_invalid_votes(0)
1086-
, m_finished(poll.Expired(GetAdjustedTime()))
1110+
, m_pools_voted({})
1111+
, m_finished(m_poll.Expired(GetAdjustedTime()))
10871112
{
10881113
m_responses.resize(m_poll.Choices().size());
10891114
}

src/gridcoin/voting/result.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ class PollResult
7777
bool Empty() const;
7878
};
7979

80-
const Poll m_poll; //!< The poll associated with the result.
81-
Weight m_total_weight; //!< Aggregate weight of all the votes submitted.
82-
size_t m_invalid_votes; //!< Number of votes that failed validation.
83-
bool m_finished; //!< Whether the poll finished as of this result.
80+
const Poll m_poll; //!< The poll associated with the result.
81+
Weight m_total_weight; //!< Aggregate weight of all the votes submitted.
82+
size_t m_invalid_votes; //!< Number of votes that failed validation.
83+
std::vector<Cpid> m_pools_voted; //!< Cpids of pools that actually voted
84+
bool m_finished; //!< Whether the poll finished as of this result.
8485

8586
//!
8687
//! \brief The aggregated voting weight tallied for each poll choice.

src/qt/voting/votingmodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ std::optional<PollItem> BuildPollItem(const PollRegistry::Sequence::Iterator& it
6161
item.m_total_votes = result->m_votes.size();
6262
item.m_total_weight = result->m_total_weight / COIN;
6363

64-
if (auto active_vote_weight = ref.GetActiveVoteWeight()) {
64+
if (auto active_vote_weight = ref.GetActiveVoteWeight(result)) {
6565
item.m_active_weight = *active_vote_weight / COIN;
6666
} else {
6767
item.m_active_weight = 0;

src/rpc/voting.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ UniValue PollResultToJson(const PollResult& result, const PollReference& poll_re
9898
json.pushKV("invalid_votes", (uint64_t)result.m_invalid_votes);
9999
json.pushKV("total_weight", ValueFromAmount(result.m_total_weight));
100100

101-
if (auto active_vote_weight = poll_ref.GetActiveVoteWeight()) {
101+
if (auto active_vote_weight = poll_ref.GetActiveVoteWeight(result)) {
102102
json.pushKV("active_vote_weight", ValueFromAmount(*active_vote_weight));
103103
json.pushKV("vote_percent_avw", (double) result.m_total_weight / (double) *active_vote_weight * 100.0);
104104
}

0 commit comments

Comments
 (0)