Skip to content

Commit

Permalink
Fix for AVW not correct with modified magnitude weight factor
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 24, 2024
1 parent 138ab6a commit 27341ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
26 changes: 22 additions & 4 deletions src/gridcoin/voting/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,19 @@ PollOption PollReference::TryReadFromDisk(CTxDB& txdb) const
auto payload = contract.PullPayloadAs<PollPayload>();
// The time for the poll is the time of the containing transaction.
payload.m_poll.m_timestamp = tx.nTime;
m_timestamp = tx.nTime;

// 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();
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 " , "
"poll.m_magnitude_weight_factor = %s",
__func__,
m_timestamp,
payload.m_poll.m_timestamp,
payload.m_poll.m_magnitude_weight_factor.ToString());

return std::move(payload.m_poll);
}
Expand Down Expand Up @@ -482,6 +491,11 @@ std::optional<int> PollReference::GetEndingHeight() const EXCLUSIVE_LOCKS_REQUIR
return std::nullopt;
}

Fraction PollReference::GetMagnitudeWeightFactor() const
{
return m_magnitude_weight_factor;
}

std::optional<CAmount> PollReference::GetActiveVoteWeight(const PollResultOption& result) const
{
// Instrument this so we can log real time performance.
Expand Down Expand Up @@ -653,13 +667,15 @@ std::optional<CAmount> PollReference::GetActiveVoteWeight(const PollResultOption
// If voting logging category is active, log the first block and every superblock
if (blocks == 1 || pindex->IsSuperblock()) {
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: tally_active_vote_weight: net_weight = %f, money_supply = %f, "
"pool_magnitude = %f, network_magnitude = %f, block height = %i, "
"blocks = %u, active_vote_weight_tally = %f, active_vote_weight = %f.",
"pool_magnitude = %f, network_magnitude = %f, magnitude weight factor = %s, "
"block height = %i, blocks = %u, active_vote_weight_tally = %f, "
"active_vote_weight = %f.",
__func__,
net_weight.getdouble() / (double) COIN,
money_supply.getdouble() / (double) COIN,
scaled_pool_magnitude.getdouble() / 100.0,
scaled_network_magnitude.getdouble() / 100.0,
m_magnitude_weight_factor.ToString(),
pindex->nHeight,
blocks,
active_vote_weight_tally.getdouble() / (double) COIN,
Expand All @@ -670,13 +686,15 @@ std::optional<CAmount> PollReference::GetActiveVoteWeight(const PollResultOption
// Log the last block and break if at pindex_end.
if (pindex == pindex_end) {
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: tally_active_vote_weight: net_weight = %f, money_supply = %f, "
"pool_magnitude = %f, network_magnitude = %f, block height = %i, "
"blocks = %u, active_vote_weight_tally = %f, active_vote_weight = %f.",
"pool_magnitude = %f, network_magnitude = %f, magnitude weight factor = %s, "
"block height = %i, blocks = %u, active_vote_weight_tally = %f, "
"active_vote_weight = %f.",
__func__,
net_weight.getdouble() / (double) COIN,
money_supply.getdouble() / (double) COIN,
scaled_pool_magnitude.getdouble() / 100.0,
scaled_network_magnitude.getdouble() / 100.0,
m_magnitude_weight_factor.ToString(),
pindex->nHeight,
blocks,
active_vote_weight_tally.getdouble() / (double) COIN,
Expand Down
25 changes: 16 additions & 9 deletions src/gridcoin/voting/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class PollReference
//!
std::optional<int> GetEndingHeight() const;

//!
//! \brief Get the magnitude weight factor at the poll start.
//!
//! \return Fraction representing magnitude weight factor.
//!
Fraction GetMagnitudeWeightFactor() const;

//!
//! \brief Computes the Active Vote Weight for the poll, which is used to determine whether the poll is validated.
//! \param result: The actual tabulated votes (poll result)
Expand All @@ -176,15 +183,15 @@ class PollReference
void UnlinkVote(const uint256 txid);

private:
uint256 m_txid; //!< Hash of the poll transaction.
uint32_t m_payload_version; //!< Version of the poll (payload).
PollType m_type; //!< Type of the poll.
const std::string* m_ptitle; //!< Title of the poll for indexing/mapping purposes.
std::string m_title; //!< Original title of the poll for display purposes.
int64_t m_timestamp; //!< Timestamp of the poll transaction.
uint32_t m_duration_days; //!< Number of days the poll remains active.
std::vector<uint256> m_votes; //!< Hashes of the linked vote transactions.
Fraction m_magnitude_weight_factor; //!< Magnitude weight factor for the poll (defined at poll start).
uint256 m_txid; //!< Hash of the poll transaction.
uint32_t m_payload_version; //!< Version of the poll (payload).
PollType m_type; //!< Type of the poll.
const std::string* m_ptitle; //!< Title of the poll for indexing/mapping purposes.
std::string m_title; //!< Original title of the poll for display purposes.
mutable int64_t m_timestamp; //!< Timestamp of the poll transaction.
uint32_t m_duration_days; //!< Number of days the poll remains active.
std::vector<uint256> m_votes; //!< Hashes of the linked vote transactions.
mutable Fraction m_magnitude_weight_factor; //!< Magnitude weight factor for the poll (defined at poll start).
}; // PollReference

//!
Expand Down
5 changes: 5 additions & 0 deletions src/gridcoin/voting/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,11 @@ PollResultOption PollResult::BuildFor(const PollReference& poll_ref)

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

LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: poll_ref.Time() = %" PRId64 " poll.GetMagnitudeWeightFactor() = %s",
__func__,
poll_ref.Time(),
poll_ref.GetMagnitudeWeightFactor().ToString());

if (auto active_vote_weight = poll_ref.GetActiveVoteWeight(result)) {
result.m_active_vote_weight = active_vote_weight;

Expand Down

0 comments on commit 27341ee

Please sign in to comment.