Skip to content

Commit 9cd5b4a

Browse files
committed
Refactor beaconstatus to separate actual back-end
calls into GetBeaconStatus so that this can be consumed easily by the GUI. Implement simple BeaconStatus struct. Implement initial BitcoinGUI::updateBeaconIcon() function.
1 parent 7fa60e5 commit 9cd5b4a

File tree

4 files changed

+122
-21
lines changed

4 files changed

+122
-21
lines changed

src/beacon.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "appcache.h"
99
#include "contract/contract.h"
1010
#include "key.h"
11+
#include "neuralnet/researcher.h"
12+
#include "neuralnet/tally.h"
1113

1214
std::string RetrieveBeaconValueWithMaxAge(const std::string& cpid, int64_t iMaxSeconds);
1315
std::string ExtractXML(const std::string& XMLdata, const std::string& key, const std::string& key_end);
@@ -316,3 +318,25 @@ BeaconConsensus GetConsensusBeaconList()
316318

317319
return Consensus;
318320
}
321+
322+
// -------------------------- Both In and Out
323+
BeaconStatus GetBeaconStatus(std::string& sCPID)
324+
{
325+
BeaconStatus beacon_status;
326+
327+
LOCK(cs_main);
328+
329+
if (sCPID.empty())
330+
{
331+
sCPID = NN::GetPrimaryCpid();
332+
}
333+
334+
beacon_status.sPubKey = GetBeaconPublicKey(sCPID, false);
335+
beacon_status.iBeaconTimestamp = BeaconTimeStamp(sCPID);
336+
beacon_status.timestamp = TimestampToHRDate(beacon_status.iBeaconTimestamp);
337+
beacon_status.hasBeacon = HasActiveBeacon(sCPID);
338+
beacon_status.dPriorSBMagnitude = NN::Tally::GetMagnitude(NN::MiningId::Parse(sCPID));
339+
beacon_status.is_mine = (sCPID == NN::GetPrimaryCpid());
340+
341+
return beacon_status;
342+
}

src/beacon.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ struct BeaconConsensus
2525
BeaconMap mBeaconMap;
2626
};
2727

28+
//Note this should be replaced when we redo the beacon structures...
29+
struct BeaconStatus
30+
{
31+
std::string sPubKey;
32+
int64_t iBeaconTimestamp;
33+
std::string timestamp;
34+
bool hasBeacon;
35+
double dPriorSBMagnitude;
36+
bool is_mine;
37+
};
38+
2839
//!
2940
//! \brief Generate beacon key pair.
3041
//!
@@ -100,3 +111,10 @@ bool ImportBeaconKeysFromConfig(const std::string& cpid, CWallet* wallet);
100111
//! \return A list of active beacons.
101112
//!
102113
BeaconConsensus GetConsensusBeaconList();
114+
115+
//!
116+
//! \brief Get beacon status for a specified CPID.
117+
//!
118+
//! \return Beacon status structure for specific CPID.
119+
//!
120+
BeaconStatus GetBeaconStatus(std::string& sCPID);

src/qt/bitcoingui.cpp

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "contract/polls.h"
8585
#include "contract/contract.h"
8686
#include "neuralnet/researcher.h"
87+
#include "beacon.h"
8788

8889
#include <iostream>
8990
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
@@ -1628,6 +1629,70 @@ void BitcoinGUI::updateScraperIcon(int scraperEventtype, int status)
16281629

16291630
void BitcoinGUI::updateBeaconIcon()
16301631
{
1631-
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_green").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1632-
labelBeaconIcon->setToolTip(tr("Iconsize = %1.").arg(QString(std::to_string(STATUSBAR_ICONSIZE).c_str())));
1632+
std::string sCPID;
1633+
double beacon_age = 0;
1634+
double time_to_expiration = 0;
1635+
double advertise_threshold = 0;
1636+
1637+
BeaconStatus beacon_status = GetBeaconStatus(sCPID);
1638+
1639+
if (sCPID == "INVESTOR")
1640+
{
1641+
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_grey").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1642+
labelBeaconIcon->setToolTip(tr("Wallet status is INVESTOR. No beacon required. If this is not expected, "
1643+
"please check your email entry in the config file and your BOINC installation.").arg(QString(sCPID.c_str())));
1644+
}
1645+
1646+
if (beacon_status.hasBeacon)
1647+
{
1648+
beacon_age = (double) (GetAdjustedTime() - beacon_status.iBeaconTimestamp) / (double) (24 * 60 * 60);
1649+
time_to_expiration = (double) (MaxBeaconAge() - beacon_age) / (24 * 60 * 60);
1650+
advertise_threshold = BeaconAgeAdvertiseThreshold() / (24 * 60 * 60);
1651+
1652+
// If beacon does not need to be renewed...
1653+
if (beacon_age < advertise_threshold)
1654+
{
1655+
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_green").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1656+
labelBeaconIcon->setToolTip(tr("CPID: %1\n"
1657+
"Beacon age: %2 day(s)"
1658+
"Expires: %3 day(s)\n"
1659+
"Beacon status is good.").arg(QString(sCPID.c_str()))
1660+
.arg(QString(std::to_string(std::round(beacon_age)).c_str()))
1661+
.arg(QString(std::to_string(std::round(time_to_expiration)).c_str())));
1662+
}
1663+
// If between start of period where able to renew and 15 days left until expiration... (time to renew!)
1664+
else if (beacon_age >= advertise_threshold && time_to_expiration > 15.0)
1665+
{
1666+
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_yellow").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1667+
labelBeaconIcon->setToolTip(tr("CPID: %1\n"
1668+
"Beacon age: %2 day(s)\n"
1669+
"Expires: %3 day(s)\n"
1670+
"Beacon should be renewed.").arg(QString(sCPID.c_str()))
1671+
.arg(QString(std::to_string(std::round(beacon_age)).c_str()))
1672+
.arg(QString(std::to_string(std::round(time_to_expiration)).c_str())));
1673+
}
1674+
// If magnitude is zero (which is common for new beacons and lapsed beacons that have been renewed)...
1675+
else if (!beacon_status.dPriorSBMagnitude)
1676+
{
1677+
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_yellow").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1678+
labelBeaconIcon->setToolTip(tr("CPID: %1\n"
1679+
"Beacon age: %2 day(s)\n"
1680+
"Expires: %3 day(s)\n"
1681+
"Magnitude is zero, which may prevent staking with research rewards."
1682+
"Please check your magnitude after the next superblock.").arg(QString(sCPID.c_str()))
1683+
.arg(QString(std::to_string(std::round(beacon_age)).c_str()))
1684+
.arg(QString(std::to_string(std::round(time_to_expiration)).c_str())));
1685+
}
1686+
// If only 15 days left to renew, red alert!)
1687+
else if (time_to_expiration <= 15.0)
1688+
{
1689+
labelBeaconIcon->setPixmap(QIcon(":/icons/beacon_red").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
1690+
labelBeaconIcon->setToolTip(tr("CPID: %1\n"
1691+
"Beacon age: %2 day(s)\n"
1692+
"Expires: %3 day(s)\n"
1693+
"BEACON SHOULD BE RENEWED IMMEDIATELY TO PREVENT LAPSE.").arg(QString(sCPID.c_str()))
1694+
.arg(QString(std::to_string(std::round(beacon_age)).c_str()))
1695+
.arg(QString(std::to_string(std::round(time_to_expiration)).c_str())));
1696+
}
1697+
}
16331698
}

src/rpcblockchain.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -805,42 +805,36 @@ UniValue beaconstatus(const UniValue& params, bool fHelp)
805805

806806
// Search for beacon, and report on beacon status.
807807

808-
std::string sCPID = NN::GetPrimaryCpid();
808+
std::string sCPID;
809809

810810
if (params.size() > 0)
811811
sCPID = params[0].get_str();
812812

813-
LOCK(cs_main);
814-
815-
std::string sPubKey = GetBeaconPublicKey(sCPID, false);
816-
int64_t iBeaconTimestamp = BeaconTimeStamp(sCPID);
817-
std::string timestamp = TimestampToHRDate(iBeaconTimestamp);
818-
bool hasBeacon = HasActiveBeacon(sCPID);
813+
// If sCPID is supplied, uses that. If not, then sCPID is filled in from GetPrimaryCPID.
814+
BeaconStatus beacon_status = GetBeaconStatus(sCPID);
819815

820816
res.pushKV("CPID", sCPID);
821-
res.pushKV("Beacon Exists",YesNo(hasBeacon));
822-
res.pushKV("Beacon Timestamp",timestamp.c_str());
823-
res.pushKV("Public Key", sPubKey.c_str());
824-
res.pushKV("Private Key", "not-shown"); //TODO: show the key?
817+
res.pushKV("Beacon Exists", YesNo(beacon_status.hasBeacon));
818+
res.pushKV("Beacon Timestamp", beacon_status.timestamp.c_str());
819+
res.pushKV("Public Key", beacon_status.sPubKey.c_str());
825820

826821
std::string sErr = "";
827822

828-
if (sPubKey.empty())
823+
if (beacon_status.sPubKey.empty())
829824
sErr += "Public Key Missing. ";
830825

831826
// Prior superblock Magnitude
832-
double dMagnitude = NN::Tally::GetMagnitude(NN::MiningId::Parse(sCPID));
833-
834-
res.pushKV("Magnitude (As of last superblock)", dMagnitude);
827+
res.pushKV("Magnitude (As of last superblock)", beacon_status.dPriorSBMagnitude);
835828

836-
const bool is_mine = sCPID == NN::GetPrimaryCpid();
837-
res.pushKV("Mine", is_mine);
829+
res.pushKV("Mine", beacon_status.is_mine);
838830

839-
if (is_mine && dMagnitude == 0)
831+
if (beacon_status.is_mine && beacon_status.dPriorSBMagnitude == 0)
840832
res.pushKV("Warning","Your magnitude is 0 as of the last superblock: this may keep you from staking POR blocks.");
841833

842-
if (!sPubKey.empty() && is_mine)
834+
if (!beacon_status.sPubKey.empty() && beacon_status.is_mine)
843835
{
836+
LOCK(cs_main);
837+
844838
EnsureWalletIsUnlocked();
845839

846840
bool bResult;

0 commit comments

Comments
 (0)