Skip to content

Commit

Permalink
Implement accrual limit warning
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Feb 6, 2023
1 parent dd24b99 commit 0f512f8
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/gridcoin/accrual/computer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class IAccrualComputer
//!
virtual CAmount PaymentPerDayLimit() const = 0;

//!
//! \brief Return an accrual value that is nearing the limit based on accrual rate.
//! \return Value of near limit in CAmount units.
//!
virtual CAmount NearRewardLimit() const = 0;

//!
//! \brief Determine whether the account exceeded the daily payment limit.
//!
Expand Down
10 changes: 10 additions & 0 deletions src/gridcoin/accrual/newbie.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class NewbieAccrualComputer : public IAccrualComputer
return MaxReward();
}

CAmount NearRewardLimit() const override
{
// This returns MaxReward() - 2 * ExpectedDaily() or 1/2 of MaxReward(), whichever
// is greater

CAmount threshold = std::max(MaxReward() / 2, MaxReward() - 2 * ExpectedDaily());

return threshold;
}

bool ExceededRecentPayments() const override
{
return RawAccrual() > PaymentPerDayLimit();
Expand Down
5 changes: 5 additions & 0 deletions src/gridcoin/accrual/null.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class NullAccrualComputer : public IAccrualComputer
return 0;
}

CAmount NearRewardLimit() const override
{
return 0;
}

bool ExceededRecentPayments() const override
{
return false;
Expand Down
10 changes: 10 additions & 0 deletions src/gridcoin/accrual/research_age.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ class ResearchAgeComputer : public IAccrualComputer
return m_account.AverageLifetimeMagnitude() * m_magnitude_unit * COIN * 5;
}

CAmount NearRewardLimit() const override
{
// This returns MaxReward() - 2 * ExpectedDaily() or 1/2 of MaxReward(), whichever
// is greater

CAmount threshold = std::max(MaxReward() / 2, MaxReward() - 2 * ExpectedDaily());

return threshold;
}

//!
//! \brief Determine whether the account exceeded the daily payment limit.
//!
Expand Down
10 changes: 10 additions & 0 deletions src/gridcoin/accrual/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ class SnapshotAccrualComputer : public IAccrualComputer, SnapshotCalculator
return MaxReward();
}

CAmount NearRewardLimit() const override
{
// This returns MaxReward() - 2 * ExpectedDaily() or 1/2 of MaxReward(), whichever
// is greater

CAmount threshold = std::max(MaxReward() / 2, MaxReward() - 2 * ExpectedDaily());

return threshold;
}

bool ExceededRecentPayments() const override
{
return RawAccrual() > PaymentPerDayLimit();
Expand Down
15 changes: 15 additions & 0 deletions src/gridcoin/researcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,21 @@ CAmount Researcher::Accrual() const
return Tally::GetAccrual(*cpid, now, pindexBest);
}

CAmount Researcher::AccrualNearLimit() const
{
const CpidOption cpid = m_mining_id.TryCpid();

if (!cpid || !pindexBest) {
return false;
}

const int64_t now = OutOfSyncByAge() ? pindexBest->nTime : GetAdjustedTime();

LOCK(cs_main);

return Tally::AccrualNearLimit(*cpid, now, pindexBest);
}

ResearcherStatus Researcher::Status() const
{
if (Eligible()) {
Expand Down
6 changes: 6 additions & 0 deletions src/gridcoin/researcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ class Researcher
//!
CAmount Accrual() const;

//!
//! \brief Value of account accrual that is near MaxReward() based on accrual rate..
//! \return CAmount value. This is implemented in IAccrualComputer::NearRewardLimit().
//!
CAmount AccrualNearLimit() const;

//!
//! \brief Get a value that indicates how the wallet participates in the
//! research reward protocol.
Expand Down
8 changes: 8 additions & 0 deletions src/gridcoin/tally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,14 @@ CAmount Tally::GetAccrual(
return GetComputer(cpid, payment_time, last_block_ptr)->Accrual();
}

CAmount Tally::AccrualNearLimit(
const Cpid cpid,
const int64_t payment_time,
const CBlockIndex* const last_block_ptr)
{
return (GetComputer(cpid, payment_time, last_block_ptr)->NearRewardLimit()) ;
}

//!
//! \brief Compute "catch-up" accrual to correct for newbie accrual bug.
//!
Expand Down
16 changes: 16 additions & 0 deletions src/gridcoin/tally.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ class Tally
const int64_t payment_time,
const CBlockIndex* const last_block_ptr);

//!
//! \brief A value of the accrual that is near the MaxReward for the accrual computer in context based on
//! the rate of accrual. This is defined in the implementation of the virtual method NearRewardLimit()
//! in IAccrualComputer.
//!
//! \param cpid CPID to calculate research accrual for.
//! \param payment_time Time of payment to calculate rewards at.
//! \param last_block_ptr Refers to the block for the reward.
//!
//! \return CAmount of account accrual that is near the MaxReward.
//!
static CAmount AccrualNearLimit(
const Cpid cpid,
const int64_t payment_time,
const CBlockIndex* const last_block_ptr);

//!
//! \brief Compute "catch-up" accrual to correct for newbie accrual bug.
//!
Expand Down
68 changes: 46 additions & 22 deletions src/qt/forms/overviewpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>948</width>
<height>635</height>
<height>755</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -788,10 +788,10 @@
<property name="spacing">
<number>12</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="statusTextLabel">
<item row="1" column="0">
<widget class="QLabel" name="magnitudeTextLabel">
<property name="text">
<string>Status:</string>
<string>Magnitude:</string>
</property>
<property name="isRowHeader" stdset="0">
<bool>true</bool>
Expand All @@ -817,18 +817,24 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="magnitudeTextLabel">
<item row="1" column="1">
<widget class="QLabel" name="magnitudeLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Magnitude:</string>
<string notr="true"/>
</property>
<property name="isRowHeader" stdset="0">
<bool>true</bool>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="magnitudeLabel">
<item row="2" column="1">
<widget class="QLabel" name="accrualLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
Expand All @@ -843,6 +849,16 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="statusTextLabel">
<property name="text">
<string>Status:</string>
</property>
<property name="isRowHeader" stdset="0">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="accrualTextLabel">
<property name="text">
Expand All @@ -853,19 +869,25 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="accrualLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="2" column="2">
<widget class="QLabel" name="accrualLimitWarningIconlabel">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>You are approaching the accrual limit of 16384 GRC. If you have a relatively low balance, you should request payment via MRC so that you do not lose earned rewards.</string>
</property>
<property name="text">
<string notr="true"/>
<string/>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
<property name="pixmap">
<pixmap resource="../bitcoin.qrc">:/icons/warning</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
Expand Down Expand Up @@ -1108,6 +1130,8 @@
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../bitcoin.qrc"/>
</resources>
<connections/>
</ui>
14 changes: 13 additions & 1 deletion src/qt/overviewpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ OverviewPage::OverviewPage(QWidget *parent) :
ui->stakingGridLayout->setVerticalSpacing(verticalSpacing);
ui->researcherGridLayout->setVerticalSpacing(verticalSpacing);

// scale warning icon
int warning_icon_size = GRC::ScalePx(this, 21);
ui->accrualLimitWarningIconlabel->setMaximumSize(QSize(warning_icon_size, warning_icon_size));

// Recent Transactions
ui->listTransactions->setItemDelegate(txdelegate);
ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
Expand Down Expand Up @@ -285,6 +289,7 @@ void OverviewPage::setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBa
bool showImmature = immatureBalance != 0;
ui->immatureLabel->setVisible(showImmature);
ui->immatureTextLabel->setVisible(showImmature);

}

void OverviewPage::setHeight(int height, int height_of_peers, bool in_sync)
Expand Down Expand Up @@ -501,7 +506,14 @@ void OverviewPage::updatePendingAccrual()
unit = walletModel->getOptionsModel()->getDisplayUnit();
}

ui->accrualLabel->setText(researcherModel->formatAccrual(unit));
bool near_limit = false;

ui->accrualLabel->setText(researcherModel->formatAccrual(unit, near_limit));
if (near_limit) {
ui->accrualLimitWarningIconlabel->show();
} else {
ui->accrualLimitWarningIconlabel->hide();
}
}

void OverviewPage::updateResearcherAlert()
Expand Down
16 changes: 14 additions & 2 deletions src/qt/researcher/researchermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ bool ResearcherModel::needsBeaconAuth() const
return m_beacon->m_public_key != m_pending_beacon->m_public_key;
}

CAmount ResearcherModel::accrualNearLimit() const
{
return m_researcher->AccrualNearLimit();
}

CAmount ResearcherModel::getAccrual() const
{
return m_researcher->Accrual();
Expand Down Expand Up @@ -353,16 +358,23 @@ QString ResearcherModel::formatMagnitude() const
return text;
}

QString ResearcherModel::formatAccrual(const int display_unit) const
QString ResearcherModel::formatAccrual(const int display_unit, bool& near_limit) const
{
QString text;

// We only do the actual accrual calculation once. The AccrualNearLimit() is lighter.
CAmount accrual = m_researcher->Accrual();

near_limit = (accrual >= m_researcher->AccrualNearLimit());

if (outOfSync()) {
text = "...";
} else {
text = BitcoinUnits::formatWithPrivacy(display_unit, m_researcher->Accrual(), m_privacy_enabled);
text = BitcoinUnits::formatWithPrivacy(display_unit, accrual, m_privacy_enabled);
}



return text;
}

Expand Down
3 changes: 2 additions & 1 deletion src/qt/researcher/researchermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ class ResearcherModel : public QObject
bool hasSplitCpid() const;
bool needsBeaconAuth() const;

CAmount accrualNearLimit() const;
CAmount getAccrual() const;

QString email() const;
QString formatCpid() const;
QString formatMagnitude() const;
QString formatAccrual(const int display_unit) const;
QString formatAccrual(const int display_unit, bool& near_limit) const;
QString formatStatus() const;
QString formatBoincPath() const;

Expand Down
4 changes: 3 additions & 1 deletion src/qt/researcher/researcherwizardsummarypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void ResearcherWizardSummaryPage::refreshSummary()
ui->cpidLabel->setText(m_researcher_model->formatCpid());
ui->statusLabel->setText(m_researcher_model->formatStatus());
ui->magnitudeLabel->setText(m_researcher_model->formatMagnitude());
ui->accrualLabel->setText(m_researcher_model->formatAccrual(unit));

bool near_limit = false;
ui->accrualLabel->setText(m_researcher_model->formatAccrual(unit, near_limit));
ui->reviewBeaconAuthButton->setVisible(m_researcher_model->needsBeaconAuth());

ui->beaconDetailsIconLabel->setPixmap(
Expand Down

0 comments on commit 0f512f8

Please sign in to comment.