Skip to content

Commit

Permalink
Ensure ResolveMagnitudeWeightFactor returns Fraction(100, 567) prior …
Browse files Browse the repository at this point in the history
…to block v13
  • Loading branch information
jamescowens committed Oct 25, 2024
1 parent 1b7cbfb commit a0560d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/gridcoin/voting/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,20 @@ int64_t Poll::Expiration() const
return m_timestamp + (m_duration_days * 86400);
}

Fraction Poll::ResolveMagnitudeWeightFactor() const
Fraction Poll::ResolveMagnitudeWeightFactor(CBlockIndex *index) const
{
Fraction magnitude_weight_factor = Params().GetConsensus().DefaultMagnitudeWeightFactor;

// Find the current protocol entry value for Magnitude Weight Factor, if it exists.
// Before V13 magnitude weight factor is 1 / 5.67.
if (!IsV13Enabled(index->nHeight)) {
return Fraction(100, 567);
}

// Find the current protocol entry value for Magnitude Weight Factor, if it exists.
ProtocolEntryOption protocol_entry = GetProtocolRegistry().TryLastBeforeTimestamp("magnitudeweightfactor", 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 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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/gridcoin/voting/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,11 @@ class Poll
//! \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 index CBlockIndex of block containing poll transaction (poll start).
//!
//! \return Fraction Magnitude factor expressed as a Fraction.
//!
Fraction ResolveMagnitudeWeightFactor() const;
Fraction ResolveMagnitudeWeightFactor(CBlockIndex* index) const;

//!
//! \brief Get the set of possible answers to the poll.
Expand Down
4 changes: 2 additions & 2 deletions src/gridcoin/voting/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ PollOption PollReference::TryReadFromDisk(CTxDB& txdb) const

// This is a critical initialization, because the magnitude weight factor is only stored
// in memory and is not serialized.
payload.m_poll.m_magnitude_weight_factor = payload.m_poll.ResolveMagnitudeWeightFactor();
payload.m_poll.m_magnitude_weight_factor = payload.m_poll.ResolveMagnitudeWeightFactor(GetStartingBlockIndexPtr());
m_magnitude_weight_factor = payload.m_poll.m_magnitude_weight_factor;

LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: reference.m_timestamp = %" PRId64 " , poll.m_timestamp = %" PRId64 " , "
Expand Down Expand Up @@ -1033,7 +1033,7 @@ void PollRegistry::AddPoll(const ContractContext& ctx) EXCLUSIVE_LOCKS_REQUIRED(
poll_ref.m_type = payload->m_poll.m_type.Value();
poll_ref.m_timestamp = ctx.m_tx.nTime;
poll_ref.m_duration_days = payload->m_poll.m_duration_days;
poll_ref.m_magnitude_weight_factor = payload->m_poll.ResolveMagnitudeWeightFactor();
poll_ref.m_magnitude_weight_factor = payload->m_poll.ResolveMagnitudeWeightFactor(poll_ref.GetStartingBlockIndexPtr());

m_latest_poll = &poll_ref;

Expand Down

0 comments on commit a0560d6

Please sign in to comment.