Skip to content

Commit 80f715d

Browse files
committed
Refactor AVW calculations into the poll Result class
Implement integer based poll validation boolan in result based on poll type avw rules.
1 parent 0672e91 commit 80f715d

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
lines changed

src/gridcoin/voting/registry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ ClaimMessage GRC::PackPollMessage(const Poll& poll, const CTransaction& tx)
311311

312312
PollReference::PollReference()
313313
: m_ptxid(nullptr)
314+
, m_payload_version(0)
315+
, m_type(PollType::UNKNOWN)
314316
, m_ptitle(nullptr)
315317
, m_timestamp(0)
316318
, m_duration_days(0)
@@ -356,6 +358,16 @@ uint256 PollReference::Txid() const
356358
return *m_ptxid;
357359
}
358360

361+
uint32_t PollReference::GetPollPayloadVersion() const
362+
{
363+
return m_payload_version;
364+
}
365+
366+
PollType PollReference::GetPollType() const
367+
{
368+
return m_type;
369+
}
370+
359371
const std::string& PollReference::Title() const
360372
{
361373
if (!m_ptitle) {
@@ -909,6 +921,8 @@ void PollRegistry::AddPoll(const ContractContext& ctx)
909921

910922
PollReference& poll_ref = result_pair.first->second;
911923
poll_ref.m_ptitle = &title;
924+
poll_ref.m_payload_version = payload->m_version;
925+
poll_ref.m_type = payload->m_poll.m_type.Value();
912926
poll_ref.m_timestamp = ctx.m_tx.nTime;
913927
poll_ref.m_duration_days = payload->m_poll.m_duration_days;
914928

src/gridcoin/voting/registry.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "gridcoin/contract/handler.h"
99
#include "gridcoin/voting/filter.h"
1010
#include "gridcoin/voting/fwd.h"
11+
#include "uint256.h"
12+
#include <map>
1113

1214
class CTxDB;
1315

@@ -55,6 +57,16 @@ class PollReference
5557
//!
5658
uint256 Txid() const;
5759

60+
//!
61+
//! \brief Get the poll (payload) version
62+
//!
63+
uint32_t GetPollPayloadVersion() const;
64+
65+
//!
66+
//! \brief Get the poll type
67+
//!
68+
PollType GetPollType() const;
69+
5870
//!
5971
//! \brief Get the title of the associated poll.
6072
//!
@@ -127,6 +139,11 @@ class PollReference
127139
//!
128140
std::optional<int> GetEndingHeight() const;
129141

142+
//!
143+
//! \brief Computes the Active Vote Weight for the poll, which is used to determine whether the poll is validated.
144+
//! \param result: The actual tabulated votes (poll result)
145+
//! \return ActiveVoteWeight
146+
//!
130147
std::optional<CAmount> GetActiveVoteWeight(const PollResultOption &result) const;
131148

132149
//!
@@ -146,6 +163,8 @@ class PollReference
146163

147164
private:
148165
const uint256* m_ptxid; //!< Hash of the poll transaction.
166+
uint32_t m_payload_version; //!< Version of the poll (payload).
167+
PollType m_type; //!< Type of the poll.
149168
const std::string* m_ptitle; //!< Title of the poll.
150169
int64_t m_timestamp; //!< Timestamp of the poll transaction.
151170
uint32_t m_duration_days; //!< Number of days the poll remains active.

src/gridcoin/voting/result.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,9 @@ PollResult::PollResult(Poll poll)
11081108
, m_total_weight(0)
11091109
, m_invalid_votes(0)
11101110
, m_pools_voted({})
1111+
, m_active_vote_weight()
1112+
, m_vote_percent_avw()
1113+
, m_poll_results_validated()
11111114
, m_finished(m_poll.Expired(GetAdjustedTime()))
11121115
{
11131116
m_responses.resize(m_poll.Choices().size());
@@ -1128,6 +1131,25 @@ PollResultOption PollResult::BuildFor(const PollReference& poll_ref)
11281131

11291132
counter.CountVotes(result, poll_ref.Votes());
11301133

1134+
if (auto active_vote_weight = poll_ref.GetActiveVoteWeight(result)) {
1135+
result.m_active_vote_weight = active_vote_weight;
1136+
1137+
result.m_vote_percent_avw = (double) result.m_total_weight / (double) *result.m_active_vote_weight * 100.0;
1138+
1139+
// For purposes of validation, integer arithmetic is used.
1140+
uint32_t vote_percent_avw_for_validation = (uint32_t)((int64_t) result.m_total_weight * (int64_t) 100
1141+
/ (int64_t) *result.m_active_vote_weight);
1142+
1143+
// For v1 and v2 polls, there is only one type, SURVEY, that was used on the blockchain for all polls, so this
1144+
// can only be done for v3+.
1145+
if (poll_ref.GetPollPayloadVersion() > 2) {
1146+
uint32_t min_vote_percent_avw_for_validation
1147+
= Poll::POLL_TYPE_RULES[(int) poll_ref.GetPollType()].m_min_vote_percent_AVW;
1148+
1149+
result.m_poll_results_validated = (vote_percent_avw_for_validation >= min_vote_percent_avw_for_validation);
1150+
}
1151+
}
1152+
11311153
return result;
11321154
}
11331155

src/gridcoin/voting/result.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ 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-
std::vector<Cpid> m_pools_voted; //!< Cpids of pools that actually voted
84-
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+
std::optional<CAmount> m_active_vote_weight; //!< Active vote weight of poll.
85+
std::optional<double> m_vote_percent_avw; //!< Vote weight percent of AVW.
86+
std::optional<bool> m_poll_results_validated; //!< Whether the poll's AVW is >= the minimum AVW for the poll.
87+
bool m_finished; //!< Whether the poll finished as of this result.
8588

8689
//!
8790
//! \brief The aggregated voting weight tallied for each poll choice.

src/qt/voting/votingmodel.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ std::optional<PollItem> BuildPollItem(const PollRegistry::Sequence::Iterator& it
6262
item.m_total_votes = result->m_votes.size();
6363
item.m_total_weight = result->m_total_weight / COIN;
6464

65-
if (auto active_vote_weight = ref.GetActiveVoteWeight(result)) {
66-
item.m_active_weight = *active_vote_weight / COIN;
67-
} else {
68-
item.m_active_weight = 0;
65+
item.m_active_weight = 0;
66+
if (result->m_active_vote_weight) {
67+
item.m_active_weight = *result->m_active_vote_weight / COIN;
6968
}
7069

7170
item.m_vote_percent_AVW = 0;
72-
if (item.m_active_weight > 0) {
73-
item.m_vote_percent_AVW = (double) item.m_total_weight / (double) item.m_active_weight * 100.0;
71+
if (result->m_vote_percent_avw) {
72+
item.m_vote_percent_AVW = *result->m_vote_percent_avw;
7473
}
7574

7675
item.m_finished = result->m_finished;

src/rpc/voting.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,16 @@ UniValue PollResultToJson(const PollResult& result, const PollReference& poll_re
101101
json.pushKV("invalid_votes", (uint64_t)result.m_invalid_votes);
102102
json.pushKV("total_weight", ValueFromAmount(result.m_total_weight));
103103

104-
if (auto active_vote_weight = poll_ref.GetActiveVoteWeight(result)) {
105-
json.pushKV("active_vote_weight", ValueFromAmount(*active_vote_weight));
106-
json.pushKV("vote_percent_avw", (double) result.m_total_weight / (double) *active_vote_weight * 100.0);
104+
if (result.m_active_vote_weight) {
105+
json.pushKV("active_vote_weight", ValueFromAmount(*result.m_active_vote_weight));
106+
}
107+
108+
if (result.m_vote_percent_avw) {
109+
json.pushKV("vote_percent_avw", *result.m_vote_percent_avw);
110+
}
111+
112+
if (result.m_poll_results_validated) {
113+
json.pushKV("poll_results_validated", *result.m_poll_results_validated);
107114
}
108115

109116
if (!result.m_votes.empty()) {

0 commit comments

Comments
 (0)