Skip to content

Commit 1dab574

Browse files
hebastoryanofsky
andcommitted
refactor: Pass SynchronizationState enum to GUI
Co-authored-by: Russell Yanofsky <[email protected]>
1 parent 2bec309 commit 1dab574

File tree

8 files changed

+35
-16
lines changed

8 files changed

+35
-16
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ std::string LicenseInfo()
605605
}
606606

607607
#if HAVE_SYSTEM
608-
static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex)
608+
static void BlockNotifyCallback(SynchronizationState sync_state, const CBlockIndex* pBlockIndex)
609609
{
610-
if (initialSync || !pBlockIndex)
610+
if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex)
611611
return;
612612

613613
std::string strCmd = gArgs.GetArg("-blocknotify", "");

src/interfaces/node.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,16 +308,16 @@ class NodeImpl : public Node
308308
}
309309
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
310310
{
311-
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](bool initial_download, const CBlockIndex* block) {
312-
fn(initial_download, block->nHeight, block->GetBlockTime(),
311+
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
312+
fn(sync_state, block->nHeight, block->GetBlockTime(),
313313
GuessVerificationProgress(Params().TxData(), block));
314314
}));
315315
}
316316
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
317317
{
318318
return MakeHandler(
319-
::uiInterface.NotifyHeaderTip_connect([fn](bool initial_download, const CBlockIndex* block) {
320-
fn(initial_download, block->nHeight, block->GetBlockTime(),
319+
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
320+
fn(sync_state, block->nHeight, block->GetBlockTime(),
321321
/* verification progress is unused when a header was received */ 0);
322322
}));
323323
}

src/interfaces/node.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Coin;
2727
class RPCTimerInterface;
2828
class UniValue;
2929
class proxyType;
30+
enum class SynchronizationState;
3031
enum class WalletCreationStatus;
3132
struct CNodeStateStats;
3233
struct NodeContext;
@@ -249,12 +250,12 @@ class Node
249250

250251
//! Register handler for block tip messages.
251252
using NotifyBlockTipFn =
252-
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
253+
std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
253254
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
254255

255256
//! Register handler for header tip messages.
256257
using NotifyHeaderTipFn =
257-
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>;
258+
std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
258259
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
259260

260261
//! Return pointer to internal chain interface, useful for testing.

src/qt/clientmodel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <net.h>
1616
#include <netbase.h>
1717
#include <util/system.h>
18+
#include <validation.h>
1819

1920
#include <stdint.h>
2021

@@ -234,8 +235,10 @@ static void BannedListChanged(ClientModel *clientmodel)
234235
assert(invoked);
235236
}
236237

237-
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int height, int64_t blockTime, double verificationProgress, bool fHeader)
238+
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, int height, int64_t blockTime, double verificationProgress, bool fHeader)
238239
{
240+
const bool initialSync = sync_state != SynchronizationState::POST_INIT;
241+
239242
// lock free async UI updates in case we have a new block tip
240243
// during initial sync, only update the UI if the last update
241244
// was > 250ms (MODEL_UPDATE_DELAY) ago

src/ui_interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { re
4949
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5050
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
5151
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
52-
void CClientUIInterface::NotifyBlockTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(b, i); }
53-
void CClientUIInterface::NotifyHeaderTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(b, i); }
52+
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
53+
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(s, i); }
5454
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
5555

5656
bool InitError(const bilingual_str& str)

src/ui_interface.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212

1313
class CBlockIndex;
14+
enum class SynchronizationState;
1415
struct bilingual_str;
1516

1617
namespace boost {
@@ -110,10 +111,10 @@ class CClientUIInterface
110111
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
111112

112113
/** New block has been accepted */
113-
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, bool, const CBlockIndex*);
114+
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
114115

115116
/** Best header has changed */
116-
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, bool, const CBlockIndex*);
117+
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, const CBlockIndex*);
117118

118119
/** Banlist did change. */
119120
ADD_SIGNALS_DECL_WRAPPER(BannedListChanged, void, void);

src/validation.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,13 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
28002800
return true;
28012801
}
28022802

2803+
static SynchronizationState GetSynchronizationState(bool init)
2804+
{
2805+
if (!init) return SynchronizationState::POST_INIT;
2806+
if (::fReindex) return SynchronizationState::INIT_REINDEX;
2807+
return SynchronizationState::INIT_DOWNLOAD;
2808+
}
2809+
28032810
static bool NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) {
28042811
bool fNotify = false;
28052812
bool fInitialBlockDownload = false;
@@ -2817,7 +2824,7 @@ static bool NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) {
28172824
}
28182825
// Send block tip changed notifications without cs_main
28192826
if (fNotify) {
2820-
uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
2827+
uiInterface.NotifyHeaderTip(GetSynchronizationState(fInitialBlockDownload), pindexHeader);
28212828
}
28222829
return fNotify;
28232830
}
@@ -2906,7 +2913,7 @@ bool CChainState::ActivateBestChain(BlockValidationState &state, const CChainPar
29062913
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
29072914

29082915
// Always notify the UI if a new block tip was connected
2909-
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
2916+
uiInterface.NotifyBlockTip(GetSynchronizationState(fInitialDownload), pindexNewTip);
29102917
}
29112918
}
29122919
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
@@ -3097,7 +3104,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
30973104

30983105
// Only notify about a new block tip if the active chain was modified.
30993106
if (pindex_was_in_chain) {
3100-
uiInterface.NotifyBlockTip(IsInitialBlockDownload(), to_mark_failed->pprev);
3107+
uiInterface.NotifyBlockTip(GetSynchronizationState(IsInitialBlockDownload()), to_mark_failed->pprev);
31013108
}
31023109
return true;
31033110
}

src/validation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ struct BlockHasher
103103
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
104104
};
105105

106+
/** Current sync state passed to tip changed callbacks. */
107+
enum class SynchronizationState {
108+
INIT_REINDEX,
109+
INIT_DOWNLOAD,
110+
POST_INIT
111+
};
112+
106113
extern RecursiveMutex cs_main;
107114
extern CBlockPolicyEstimator feeEstimator;
108115
extern CTxMemPool mempool;

0 commit comments

Comments
 (0)