Skip to content

Commit d44dd51

Browse files
committed
Merge #18152: qt: Use SynchronizationState enum for signals to GUI
a0d0f1c refactor: Remove Node:: queries from GUI (Hennadii Stepanov) 06d519f qt: Add SynchronizationState enum to signal parameter (Hennadii Stepanov) 3c709aa refactor: Remove Node::getReindex() call from GUI (Hennadii Stepanov) 1dab574 refactor: Pass SynchronizationState enum to GUI (Hennadii Stepanov) 2bec309 refactor: Remove unused bool parameter in RPCNotifyBlockChange() (Hennadii Stepanov) 1df7701 refactor: Remove unused bool parameter in BlockNotifyGenesisWait() (Hennadii Stepanov) Pull request description: This PR is a followup of #18121 and: - addresses confusion about GUI notification throttling conditions (**luke-jr**'s [comment](bitcoin/bitcoin#18121 (comment)), **ryanofsky**'s [comment](bitcoin/bitcoin#18121 (comment))) - removes `isInitialBlockDownload()` call from the GUI back to the node (on macOS). See: **ryanofsky**'s [comment](bitcoin/bitcoin#18121 (review)) ACKs for top commit: jonasschnelli: Core Review ACK a0d0f1c (modulo [question](bitcoin/bitcoin#18152 (review))). ryanofsky: Code review ACK a0d0f1c. Only changes since last review were rebase and tweaking SynchronizationState enum declaration as suggested (thanks!) Tree-SHA512: b6a712a710666e763aeee0d5440de1391a4c6c8f7fa661888773e1ba59e9e0f83654ee384d4edc704031be7eb25616e5eca2a6e26058d3efb7f64c47f9ed7316
2 parents e773685 + a0d0f1c commit d44dd51

14 files changed

+73
-50
lines changed

src/init.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
#include <validationinterface.h>
6060
#include <walletinitinterface.h>
6161

62+
#include <functional>
63+
#include <set>
6264
#include <stdint.h>
6365
#include <stdio.h>
64-
#include <set>
6566

6667
#ifndef WIN32
6768
#include <attributes.h>
@@ -350,13 +351,13 @@ static void registerSignalHandler(int signal, void(*handler)(int))
350351
static boost::signals2::connection rpc_notify_block_change_connection;
351352
static void OnRPCStarted()
352353
{
353-
rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(&RPCNotifyBlockChange);
354+
rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(std::bind(RPCNotifyBlockChange, std::placeholders::_2));
354355
}
355356

356357
static void OnRPCStopped()
357358
{
358359
rpc_notify_block_change_connection.disconnect();
359-
RPCNotifyBlockChange(false, nullptr);
360+
RPCNotifyBlockChange(nullptr);
360361
g_best_block_cv.notify_all();
361362
LogPrint(BCLog::RPC, "RPC stopped.\n");
362363
}
@@ -604,9 +605,9 @@ std::string LicenseInfo()
604605
}
605606

606607
#if HAVE_SYSTEM
607-
static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex)
608+
static void BlockNotifyCallback(SynchronizationState sync_state, const CBlockIndex* pBlockIndex)
608609
{
609-
if (initialSync || !pBlockIndex)
610+
if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex)
610611
return;
611612

612613
std::string strCmd = gArgs.GetArg("-blocknotify", "");
@@ -622,7 +623,7 @@ static bool fHaveGenesis = false;
622623
static Mutex g_genesis_wait_mutex;
623624
static std::condition_variable g_genesis_wait_cv;
624625

625-
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
626+
static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
626627
{
627628
if (pBlockIndex != nullptr) {
628629
{
@@ -1701,7 +1702,7 @@ bool AppInitMain(NodeContext& node)
17011702
}
17021703

17031704
const CBlockIndex* tip = chainstate->m_chain.Tip();
1704-
RPCNotifyBlockChange(true, tip);
1705+
RPCNotifyBlockChange(tip);
17051706
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
17061707
strLoadError = _("The block database contains a block which appears to be from the future. "
17071708
"This may be due to your computer's date and time being set incorrectly. "
@@ -1826,7 +1827,7 @@ bool AppInitMain(NodeContext& node)
18261827
// No locking, as this happens before any background thread is started.
18271828
boost::signals2::connection block_notify_genesis_wait_connection;
18281829
if (::ChainActive().Tip() == nullptr) {
1829-
block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(BlockNotifyGenesisWait);
1830+
block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(std::bind(BlockNotifyGenesisWait, std::placeholders::_2));
18301831
} else {
18311832
fHaveGenesis = true;
18321833
}

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/bitcoin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <uint256.h>
3535
#include <util/system.h>
3636
#include <util/threadnames.h>
37+
#include <validation.h>
3738

3839
#include <memory>
3940

@@ -61,6 +62,7 @@ Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
6162
// Declare meta types used for QMetaObject::invokeMethod
6263
Q_DECLARE_METATYPE(bool*)
6364
Q_DECLARE_METATYPE(CAmount)
65+
Q_DECLARE_METATYPE(SynchronizationState)
6466
Q_DECLARE_METATYPE(uint256)
6567

6668
static QString GetLangTerritory()
@@ -435,6 +437,7 @@ int GuiMain(int argc, char* argv[])
435437

436438
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
437439
qRegisterMetaType<bool*>();
440+
qRegisterMetaType<SynchronizationState>();
438441
#ifdef ENABLE_WALLET
439442
qRegisterMetaType<WalletModel*>();
440443
#endif

src/qt/bitcoingui.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <ui_interface.h>
3838
#include <util/system.h>
3939
#include <util/translation.h>
40+
#include <validation.h>
4041

4142
#include <QAction>
4243
#include <QApplication>
@@ -567,7 +568,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
567568
connect(_clientModel, &ClientModel::networkActiveChanged, this, &BitcoinGUI::setNetworkActive);
568569

569570
modalOverlay->setKnownBestHeight(_clientModel->getHeaderTipHeight(), QDateTime::fromTime_t(_clientModel->getHeaderTipTime()));
570-
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), m_node.getVerificationProgress(), false);
571+
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), m_node.getVerificationProgress(), false, SynchronizationState::INIT_DOWNLOAD);
571572
connect(_clientModel, &ClientModel::numBlocksChanged, this, &BitcoinGUI::setNumBlocks);
572573

573574
// Receive and report messages from client model
@@ -926,11 +927,15 @@ void BitcoinGUI::openOptionsDialogWithTab(OptionsDialog::Tab tab)
926927
dlg.exec();
927928
}
928929

929-
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
930+
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header, SynchronizationState sync_state)
930931
{
931932
// Disabling macOS App Nap on initial sync, disk and reindex operations.
932933
#ifdef Q_OS_MAC
933-
(m_node.isInitialBlockDownload() || m_node.getReindex() || m_node.getImporting()) ? m_app_nap_inhibitor->disableAppNap() : m_app_nap_inhibitor->enableAppNap();
934+
if (sync_state == SynchronizationState::POST_INIT) {
935+
m_app_nap_inhibitor->enableAppNap();
936+
} else {
937+
m_app_nap_inhibitor->disableAppNap();
938+
}
934939
#endif
935940

936941
if (modalOverlay)

src/qt/bitcoingui.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WalletFrame;
3838
class WalletModel;
3939
class HelpMessageDialog;
4040
class ModalOverlay;
41+
enum class SynchronizationState;
4142

4243
namespace interfaces {
4344
class Handler;
@@ -213,7 +214,7 @@ public Q_SLOTS:
213214
/** Set network state shown in the UI */
214215
void setNetworkActive(bool networkActive);
215216
/** Set number of blocks and last block date shown in the UI */
216-
void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers);
217+
void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers, SynchronizationState sync_state);
217218

218219
/** Notify the user of an event from the core network or transaction handling code.
219220
@param[in] title the message box / notification title

src/qt/clientmodel.cpp

Lines changed: 17 additions & 20 deletions
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,17 +235,8 @@ 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
{
239-
// lock free async UI updates in case we have a new block tip
240-
// during initial sync, only update the UI if the last update
241-
// was > 250ms (MODEL_UPDATE_DELAY) ago
242-
int64_t now = 0;
243-
if (initialSync)
244-
now = GetTimeMillis();
245-
246-
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
247-
248240
if (fHeader) {
249241
// cache best headers time and height to reduce future cs_main locks
250242
clientmodel->cachedBestHeaderHeight = height;
@@ -253,17 +245,22 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int heig
253245
clientmodel->m_cached_num_blocks = height;
254246
}
255247

256-
// During initial sync, block notifications, and header notifications from reindexing are both throttled.
257-
if (!initialSync || (fHeader && !clientmodel->node().getReindex()) || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
258-
//pass an async signal to the UI thread
259-
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
260-
Q_ARG(int, height),
261-
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
262-
Q_ARG(double, verificationProgress),
263-
Q_ARG(bool, fHeader));
264-
assert(invoked);
265-
nLastUpdateNotification = now;
248+
// Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex.
249+
const bool throttle = (sync_state != SynchronizationState::POST_INIT && !fHeader) || sync_state == SynchronizationState::INIT_REINDEX;
250+
const int64_t now = throttle ? GetTimeMillis() : 0;
251+
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
252+
if (throttle && now < nLastUpdateNotification + MODEL_UPDATE_DELAY) {
253+
return;
266254
}
255+
256+
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
257+
Q_ARG(int, height),
258+
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
259+
Q_ARG(double, verificationProgress),
260+
Q_ARG(bool, fHeader),
261+
Q_ARG(SynchronizationState, sync_state));
262+
assert(invoked);
263+
nLastUpdateNotification = now;
267264
}
268265

269266
void ClientModel::subscribeToCoreSignals()

src/qt/clientmodel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#include <memory>
1313

1414
class BanTableModel;
15+
class CBlockIndex;
1516
class OptionsModel;
1617
class PeerTableModel;
17-
18-
class CBlockIndex;
18+
enum class SynchronizationState;
1919

2020
namespace interfaces {
2121
class Handler;
@@ -100,7 +100,7 @@ class ClientModel : public QObject
100100

101101
Q_SIGNALS:
102102
void numConnectionsChanged(int count);
103-
void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, bool header);
103+
void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, bool header, SynchronizationState sync_state);
104104
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes);
105105
void networkActiveChanged(bool networkActive);
106106
void alertsChanged(const QString &warnings);

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static UniValue getbestblockhash(const JSONRPCRequest& request)
205205
return ::ChainActive().Tip()->GetBlockHash().GetHex();
206206
}
207207

208-
void RPCNotifyBlockChange(bool ibd, const CBlockIndex * pindex)
208+
void RPCNotifyBlockChange(const CBlockIndex* pindex)
209209
{
210210
if(pindex) {
211211
std::lock_guard<std::mutex> lock(cs_blockchange);

src/rpc/blockchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
3030
double GetDifficulty(const CBlockIndex* blockindex);
3131

3232
/** Callback for when block tip changed. */
33-
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
33+
void RPCNotifyBlockChange(const CBlockIndex*);
3434

3535
/** Block description to JSON */
3636
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false) LOCKS_EXCLUDED(cs_main);

0 commit comments

Comments
 (0)