Skip to content

Commit e792794

Browse files
committed
Refactor to remove the GlobalStatus data structure
This removes the legacy GlobalStatus container which the GUI uses to poll for information from core state and replaces it with the "push" pattern employed by other GUI features where we can avoid the locks. It fixes intermittent block height lag in the GUI during the initial sync. Because some information sources for the overview page changed, this moves the API to fetch the current poll title to the voting model.
1 parent 877c8a9 commit e792794

14 files changed

+258
-400
lines changed

src/main.cpp

+5-136
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "gridcoin/staking/kernel.h"
2727
#include "gridcoin/staking/reward.h"
2828
#include "gridcoin/staking/spam.h"
29-
#include "gridcoin/staking/status.h"
3029
#include "gridcoin/superblock.h"
3130
#include "gridcoin/support/xml.h"
3231
#include "gridcoin/tally.h"
@@ -128,9 +127,6 @@ int nGrandfather = 1034700;
128127

129128
int64_t nGenesisSupply = 340569880;
130129

131-
// Stats for Main Screen:
132-
GlobalStatus g_GlobalStatus;
133-
134130
bool fColdBoot = true;
135131
bool fEnforceCanonical = true;
136132
bool fUseFastIndex = false;
@@ -153,108 +149,6 @@ arith_uint256 GetChainTrust(const CBlockIndex* pindex)
153149
return g_chain_trust.GetTrust(pindex);
154150
}
155151

156-
void GlobalStatus::SetGlobalStatus(bool force)
157-
{
158-
// Only update if the previous update is >= 4 seconds old or force is specified to avoid
159-
// unnecessary calculations.
160-
if (force || GetAdjustedTime() - update_time >= 4)
161-
{
162-
// These are atomics and do not need a lock on cs_errors_lock to update. But the global variable
163-
// and functions called need a lock on cs_main.
164-
{
165-
LOCK(cs_main);
166-
167-
blocks = nBestHeight;
168-
netWeight = GRC::GetEstimatedNetworkWeight() / 80.0;
169-
difficulty = GRC::GetCurrentDifficulty();
170-
etts = GRC::GetEstimatedTimetoStake();
171-
}
172-
173-
update_time = GetAdjustedTime();
174-
175-
try
176-
{
177-
unsigned long stk_dropped;
178-
179-
{
180-
staking = g_miner_status.StakingActive();
181-
coinWeight = g_miner_status.GetSearchReport().CoinWeight();
182-
able_to_stake = g_miner_status.StakingEnabled();
183-
ReasonNotStaking = g_miner_status.FormatErrors();
184-
185-
errors.clear();
186-
187-
if (difficulty < 0.1)
188-
{
189-
errors += _("Low difficulty!; ");
190-
}
191-
192-
if (!ReasonNotStaking.empty())
193-
{
194-
errors += _("Miner: ") + ReasonNotStaking;
195-
}
196-
197-
stk_dropped = g_miner_status.GetSearchReport().KernelsRejected();
198-
}
199-
200-
if (stk_dropped)
201-
{
202-
errors += "Rejected " + ToString(stk_dropped) + " stakes;";
203-
}
204-
205-
return;
206-
}
207-
catch (std::exception& e)
208-
{
209-
errors = _("Error obtaining status.");
210-
211-
LogPrintf("Error obtaining status");
212-
return;
213-
}
214-
}
215-
}
216-
217-
const GlobalStatus::globalStatusType GlobalStatus::GetGlobalStatus()
218-
{
219-
globalStatusType globalStatus;
220-
221-
globalStatus.update_time = update_time;
222-
globalStatus.blocks = blocks;
223-
globalStatus.difficulty = difficulty;
224-
globalStatus.netWeight = netWeight;
225-
globalStatus.coinWeight = coinWeight;
226-
globalStatus.etts = etts;
227-
228-
globalStatus.able_to_stake = able_to_stake;
229-
globalStatus.staking = staking;
230-
231-
LOCK(cs_errors_lock);
232-
233-
globalStatus.ReasonNotStaking = ReasonNotStaking;
234-
globalStatus.errors = errors;
235-
236-
return globalStatus;
237-
}
238-
239-
const GlobalStatus::globalStatusStringType GlobalStatus::GetGlobalStatusStrings()
240-
{
241-
const globalStatusType& globalStatus = GetGlobalStatus();
242-
243-
globalStatusStringType globalStatusStrings;
244-
245-
if (update_time > 0)
246-
{
247-
globalStatusStrings.blocks = ToString(globalStatus.blocks);
248-
globalStatusStrings.difficulty = RoundToString(globalStatus.difficulty, 3);
249-
globalStatusStrings.netWeight = RoundToString(globalStatus.netWeight, 2);
250-
globalStatusStrings.coinWeight = RoundToString(globalStatus.coinWeight, 2);
251-
252-
globalStatusStrings.errors = globalStatus.errors;
253-
}
254-
255-
return globalStatusStrings;
256-
}
257-
258152
void RegisterWallet(CWallet* pwalletIn)
259153
{
260154
{
@@ -2037,6 +1931,8 @@ bool SetBestChain(CTxDB& txdb, CBlock &blockNew, CBlockIndex* pindexNew)
20371931
boost::thread t(runCommand, strCmd); // thread runs free
20381932
}
20391933

1934+
uiInterface.NotifyBlocksChanged(fIsInitialDownload, pindexNew->nHeight, pindexNew->GetBlockTime());
1935+
20401936
return GridcoinServices();
20411937
}
20421938

@@ -2105,7 +2001,6 @@ bool CBlock::AddToBlockIndex(unsigned int nFile, unsigned int nBlockPos, const u
21052001
hashPrevBestCoinBase = vtx[0].GetHash();
21062002
}
21072003

2108-
uiInterface.NotifyBlocksChanged();
21092004
return true;
21102005
}
21112006

@@ -2434,19 +2329,6 @@ arith_uint256 CBlockIndex::GetBlockTrust() const
24342329

24352330
bool GridcoinServices()
24362331
{
2437-
// This is only necessary if the GUI is running. It is also really only necessary during
2438-
// rapid block influx during sync. SetGlobalStatus runs from the ClientModel timer with the force
2439-
// parameter set as well. Not sure any of this is necessary, since the overview page updateglobalstatus
2440-
// and UpdateBoincUtilization run a "hard" GlobalStatus update anyway, on a MODEL_UPDATE_DELAY timer.
2441-
if (fQtActive && (nBestHeight % 125) == 0 && nBestHeight > 0)
2442-
{
2443-
// Do a "soft" GlobalStatus update. In addition to the 125 block ladder above, the "soft" update
2444-
// will only actually update if more than 4 seconds has elapsed since the last call.
2445-
g_GlobalStatus.SetGlobalStatus();
2446-
// Emit the NotifyBlocksChanged signal. Note that this signal is not actually hooked up right now.
2447-
uiInterface.NotifyBlocksChanged();
2448-
}
2449-
24502332
// Block version 9 tally transition:
24512333
//
24522334
// This block controls the switch to a new tallying system introduced with
@@ -3035,33 +2917,20 @@ string GetWarnings(string strFor)
30352917
}
30362918
}
30372919

3038-
const GlobalStatus::globalStatusType status = g_GlobalStatus.GetGlobalStatus();
3039-
3040-
if (!strStatusBar.empty() && !status.errors.empty()) {
3041-
strStatusBar += "; ";
3042-
}
3043-
3044-
strStatusBar += status.errors;
3045-
30462920
if (strFor == "statusbar")
2921+
{
30472922
return strStatusBar;
2923+
}
2924+
30482925
assert(!"GetWarnings() : invalid parameter");
30492926
return "error";
30502927
}
30512928

3052-
3053-
3054-
3055-
3056-
3057-
3058-
30592929
//////////////////////////////////////////////////////////////////////////////
30602930
//
30612931
// Messages
30622932
//
30632933

3064-
30652934
bool static AlreadyHave(CTxDB& txdb, const CInv& inv)
30662935
{
30672936
switch (inv.type)

src/main.h

-72
Original file line numberDiff line numberDiff line change
@@ -108,78 +108,6 @@ extern std::string msMiningErrorsExcluded;
108108

109109
extern int nGrandfather;
110110

111-
class GlobalStatus
112-
{
113-
public:
114-
GlobalStatus()
115-
{
116-
update_time = 0;
117-
118-
blocks = 0;
119-
difficulty = 0.0;
120-
netWeight = 0.0;
121-
coinWeight = 0.0;
122-
etts = 0.0;
123-
124-
able_to_stake = false;
125-
staking = false;
126-
127-
ReasonNotStaking = std::string();
128-
errors = std::string();
129-
}
130-
131-
struct globalStatusType
132-
{
133-
int64_t update_time;
134-
135-
int blocks;
136-
double difficulty;
137-
double netWeight;
138-
double coinWeight;
139-
double etts;
140-
141-
bool able_to_stake;
142-
bool staking;
143-
144-
std::string ReasonNotStaking;
145-
std::string errors;
146-
};
147-
148-
struct globalStatusStringType
149-
{
150-
std::string blocks;
151-
std::string difficulty;
152-
std::string netWeight;
153-
std::string coinWeight;
154-
155-
std::string errors;
156-
};
157-
158-
void SetGlobalStatus(bool force = false);
159-
const globalStatusType GetGlobalStatus();
160-
const globalStatusStringType GetGlobalStatusStrings();
161-
162-
private:
163-
std::atomic<int64_t> update_time;
164-
165-
std::atomic<int> blocks;
166-
std::atomic<double> difficulty;
167-
std::atomic<double> netWeight;
168-
std::atomic<double> coinWeight;
169-
std::atomic<double> etts;
170-
171-
std::atomic<bool> able_to_stake;
172-
std::atomic<bool> staking;
173-
174-
// This lock is only needed to protect the ReasonNotStaking and errors string.
175-
CCriticalSection cs_errors_lock;
176-
177-
std::string ReasonNotStaking;
178-
std::string errors;
179-
};
180-
181-
extern GlobalStatus g_GlobalStatus;
182-
183111
class CReserveKey;
184112
class CTxDB;
185113
class CTxIndex;

0 commit comments

Comments
 (0)