0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

refactor: Pass SynchronizationState enum to GUI

Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
This commit is contained in:
Hennadii Stepanov 2020-03-04 20:05:42 +02:00
parent 2bec309ad6
commit 1dab574edf
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
8 changed files with 35 additions and 16 deletions

View file

@ -605,9 +605,9 @@ std::string LicenseInfo()
} }
#if HAVE_SYSTEM #if HAVE_SYSTEM
static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex) static void BlockNotifyCallback(SynchronizationState sync_state, const CBlockIndex* pBlockIndex)
{ {
if (initialSync || !pBlockIndex) if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex)
return; return;
std::string strCmd = gArgs.GetArg("-blocknotify", ""); std::string strCmd = gArgs.GetArg("-blocknotify", "");

View file

@ -308,16 +308,16 @@ public:
} }
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
{ {
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](bool initial_download, const CBlockIndex* block) { return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
fn(initial_download, block->nHeight, block->GetBlockTime(), fn(sync_state, block->nHeight, block->GetBlockTime(),
GuessVerificationProgress(Params().TxData(), block)); GuessVerificationProgress(Params().TxData(), block));
})); }));
} }
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
{ {
return MakeHandler( return MakeHandler(
::uiInterface.NotifyHeaderTip_connect([fn](bool initial_download, const CBlockIndex* block) { ::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
fn(initial_download, block->nHeight, block->GetBlockTime(), fn(sync_state, block->nHeight, block->GetBlockTime(),
/* verification progress is unused when a header was received */ 0); /* verification progress is unused when a header was received */ 0);
})); }));
} }

View file

@ -27,6 +27,7 @@ class Coin;
class RPCTimerInterface; class RPCTimerInterface;
class UniValue; class UniValue;
class proxyType; class proxyType;
enum class SynchronizationState;
enum class WalletCreationStatus; enum class WalletCreationStatus;
struct CNodeStateStats; struct CNodeStateStats;
struct NodeContext; struct NodeContext;
@ -249,12 +250,12 @@ public:
//! Register handler for block tip messages. //! Register handler for block tip messages.
using NotifyBlockTipFn = using NotifyBlockTipFn =
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>; std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0; virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
//! Register handler for header tip messages. //! Register handler for header tip messages.
using NotifyHeaderTipFn = using NotifyHeaderTipFn =
std::function<void(bool initial_download, int height, int64_t block_time, double verification_progress)>; std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0; virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
//! Return pointer to internal chain interface, useful for testing. //! Return pointer to internal chain interface, useful for testing.

View file

@ -15,6 +15,7 @@
#include <net.h> #include <net.h>
#include <netbase.h> #include <netbase.h>
#include <util/system.h> #include <util/system.h>
#include <validation.h>
#include <stdint.h> #include <stdint.h>
@ -234,8 +235,10 @@ static void BannedListChanged(ClientModel *clientmodel)
assert(invoked); assert(invoked);
} }
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int height, int64_t blockTime, double verificationProgress, bool fHeader) static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, int height, int64_t blockTime, double verificationProgress, bool fHeader)
{ {
const bool initialSync = sync_state != SynchronizationState::POST_INIT;
// lock free async UI updates in case we have a new block tip // lock free async UI updates in case we have a new block tip
// during initial sync, only update the UI if the last update // during initial sync, only update the UI if the last update
// was > 250ms (MODEL_UPDATE_DELAY) ago // was > 250ms (MODEL_UPDATE_DELAY) ago

View file

@ -49,8 +49,8 @@ void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { re
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); } void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); } void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); } void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
void CClientUIInterface::NotifyBlockTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(b, i); } void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
void CClientUIInterface::NotifyHeaderTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(b, i); } void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(s, i); }
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); } void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
bool InitError(const bilingual_str& str) bool InitError(const bilingual_str& str)

View file

@ -11,6 +11,7 @@
#include <string> #include <string>
class CBlockIndex; class CBlockIndex;
enum class SynchronizationState;
struct bilingual_str; struct bilingual_str;
namespace boost { namespace boost {
@ -110,10 +111,10 @@ public:
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible); ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
/** New block has been accepted */ /** New block has been accepted */
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, bool, const CBlockIndex*); ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
/** Best header has changed */ /** Best header has changed */
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, bool, const CBlockIndex*); ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, const CBlockIndex*);
/** Banlist did change. */ /** Banlist did change. */
ADD_SIGNALS_DECL_WRAPPER(BannedListChanged, void, void); ADD_SIGNALS_DECL_WRAPPER(BannedListChanged, void, void);

View file

@ -2800,6 +2800,13 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
return true; return true;
} }
static SynchronizationState GetSynchronizationState(bool init)
{
if (!init) return SynchronizationState::POST_INIT;
if (::fReindex) return SynchronizationState::INIT_REINDEX;
return SynchronizationState::INIT_DOWNLOAD;
}
static bool NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) { static bool NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) {
bool fNotify = false; bool fNotify = false;
bool fInitialBlockDownload = false; bool fInitialBlockDownload = false;
@ -2817,7 +2824,7 @@ static bool NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) {
} }
// Send block tip changed notifications without cs_main // Send block tip changed notifications without cs_main
if (fNotify) { if (fNotify) {
uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader); uiInterface.NotifyHeaderTip(GetSynchronizationState(fInitialBlockDownload), pindexHeader);
} }
return fNotify; return fNotify;
} }
@ -2906,7 +2913,7 @@ bool CChainState::ActivateBestChain(BlockValidationState &state, const CChainPar
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload); GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
// Always notify the UI if a new block tip was connected // Always notify the UI if a new block tip was connected
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip); uiInterface.NotifyBlockTip(GetSynchronizationState(fInitialDownload), pindexNewTip);
} }
} }
// When we reach this point, we switched to a new tip (stored in pindexNewTip). // 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
// Only notify about a new block tip if the active chain was modified. // Only notify about a new block tip if the active chain was modified.
if (pindex_was_in_chain) { if (pindex_was_in_chain) {
uiInterface.NotifyBlockTip(IsInitialBlockDownload(), to_mark_failed->pprev); uiInterface.NotifyBlockTip(GetSynchronizationState(IsInitialBlockDownload()), to_mark_failed->pprev);
} }
return true; return true;
} }

View file

@ -103,6 +103,13 @@ struct BlockHasher
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
}; };
/** Current sync state passed to tip changed callbacks. */
enum class SynchronizationState {
INIT_REINDEX,
INIT_DOWNLOAD,
POST_INIT
};
extern RecursiveMutex cs_main; extern RecursiveMutex cs_main;
extern CBlockPolicyEstimator feeEstimator; extern CBlockPolicyEstimator feeEstimator;
extern CTxMemPool mempool; extern CTxMemPool mempool;