Skip to content

Commit

Permalink
Introduce changeable magnitude weight factors for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 22, 2024
1 parent 56d274b commit d36dea0
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/gridcoin/voting/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or https://opensource.org/licenses/mit-license.php.

#include "gridcoin/protocol.h"
#include "main.h"
#include "gridcoin/beacon.h"
#include "gridcoin/quorum.h"
Expand Down Expand Up @@ -787,7 +788,7 @@ class VoteCounter
//! \param supply The total network money supply as of the latest
//! block in the poll window in units of 1/100000000 GRC.
//!
void EnableMagnitudeWeight(SuperblockPtr superblock, const uint64_t supply)
void EnableMagnitudeWeight(SuperblockPtr superblock, const uint64_t supply, const Fraction magnitude_weight_factor)
{
const uint64_t total_mag = superblock->m_cpids.TotalMagnitude();

Expand All @@ -796,7 +797,9 @@ class VoteCounter
}

// Use integer arithmetic to avoid floating-point discrepancies:
m_magnitude_factor = supply / total_mag * 100 / 567;
m_magnitude_factor = supply / total_mag
* (uint64_t) magnitude_weight_factor.GetNumerator()
/ (uint64_t) magnitude_weight_factor.GetDenominator();
m_resolver.SetSuperblock(std::move(superblock));
}

Expand Down Expand Up @@ -1133,6 +1136,30 @@ CAmount ResolveMoneySupplyForPoll(const Poll& poll)

return pindex->nMoneySupply;
}

//!
//! \brief Fetch the applicable magnitude factor for the poll. This is the magnitude factor of the last entry in the
//! protocol registry database that has a timestamp at or before the poll start timestamp.
//!
//! \param poll Poll for which to fetch the Magnitude Weight Factor.
//! \return Fraction Magnitude factor expressed as a Fraction.
//!
Fraction ResolveMagnitudeWeightFactorForPoll(const Poll& poll)
{
Fraction magnitude_weight_factor = Params().GetConsensus().DefaultMagnitudeWeightFactor;

// Find the current protocol entry value for Magnitude Weight Factor, if it exists.
ProtocolEntryOption protocol_entry = GetProtocolRegistry().TryLastBeforeTimestamp("magnitudeweightfactor", poll.m_timestamp);

// If their is an entry prior or equal in timestemp to the start of the poll and it is active then set the magnitude weight
// factor to that value. If the last entry is not active (i.e. deleted), then leave at the default.
if (protocol_entry != nullptr && protocol_entry->m_status == ProtocolEntryStatus::ACTIVE) {
magnitude_weight_factor = Fraction().FromString(protocol_entry->m_value);
}

return magnitude_weight_factor;
}

} // Anonymous namespace

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1186,7 +1213,8 @@ PollResultOption PollResult::BuildFor(const PollReference& poll_ref)
if (result.m_poll.IncludesMagnitudeWeight()) {
counter.EnableMagnitudeWeight(
ResolveSuperblockForPoll(result.m_poll),
ResolveMoneySupplyForPoll(result.m_poll));
ResolveMoneySupplyForPoll(result.m_poll),
ResolveMagnitudeWeightFactorForPoll(result.m_poll));
}

LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: number of votes = %u for poll %s",
Expand Down

0 comments on commit d36dea0

Please sign in to comment.