Skip to content

Commit 6723381

Browse files
Merge pull request dashpay#5804 from knst/bp-18152
backport: bitcoin#18152
2 parents 95fad52 + 5758a33 commit 6723381

14 files changed

+73
-55
lines changed

src/init.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@
104104

105105
#include <statsd_client.h>
106106

107+
#include <functional>
108+
#include <set>
107109
#include <stdint.h>
108110
#include <stdio.h>
109111
#include <memory>
110112
#include <optional>
111-
#include <set>
112113
#include <thread>
113114
#include <vector>
114115

@@ -458,13 +459,13 @@ static void registerSignalHandler(int signal, void(*handler)(int))
458459
static boost::signals2::connection rpc_notify_block_change_connection;
459460
static void OnRPCStarted()
460461
{
461-
rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(&RPCNotifyBlockChange);
462+
rpc_notify_block_change_connection = uiInterface.NotifyBlockTip_connect(std::bind(RPCNotifyBlockChange, std::placeholders::_2));
462463
}
463464

464465
static void OnRPCStopped()
465466
{
466467
rpc_notify_block_change_connection.disconnect();
467-
RPCNotifyBlockChange(false, nullptr);
468+
RPCNotifyBlockChange(nullptr);
468469
g_best_block_cv.notify_all();
469470
LogPrint(BCLog::RPC, "RPC stopped.\n");
470471
}
@@ -815,7 +816,7 @@ static bool fHaveGenesis = false;
815816
static Mutex g_genesis_wait_mutex;
816817
static std::condition_variable g_genesis_wait_cv;
817818

818-
static void BlockNotifyGenesisWait(bool, const CBlockIndex *pBlockIndex)
819+
static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
819820
{
820821
if (pBlockIndex != nullptr) {
821822
{
@@ -2108,8 +2109,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
21082109
MIN_BLOCKS_TO_KEEP);
21092110
}
21102111

2111-
CBlockIndex* tip = chainstate->m_chain.Tip();
2112-
RPCNotifyBlockChange(true, tip);
2112+
const CBlockIndex* tip = chainstate->m_chain.Tip();
2113+
RPCNotifyBlockChange(tip);
21132114
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
21142115
strLoadError = _("The block database contains a block which appears to be from the future. "
21152116
"This may be due to your computer's date and time being set incorrectly. "
@@ -2345,16 +2346,16 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
23452346
// No locking, as this happens before any background thread is started.
23462347
boost::signals2::connection block_notify_genesis_wait_connection;
23472348
if (::ChainActive().Tip() == nullptr) {
2348-
block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(BlockNotifyGenesisWait);
2349+
block_notify_genesis_wait_connection = uiInterface.NotifyBlockTip_connect(std::bind(BlockNotifyGenesisWait, std::placeholders::_2));
23492350
} else {
23502351
fHaveGenesis = true;
23512352
}
23522353

23532354
#if HAVE_SYSTEM
23542355
if (args.IsArgSet("-blocknotify")) {
23552356
const std::string block_notify = args.GetArg("-blocknotify", "");
2356-
const auto BlockNotifyCallback = [block_notify](bool initialSync, const CBlockIndex* pBlockIndex) {
2357-
if (initialSync || !pBlockIndex)
2357+
const auto BlockNotifyCallback = [block_notify](SynchronizationState sync_state, const CBlockIndex* pBlockIndex) {
2358+
if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex)
23582359
return;
23592360

23602361
std::string strCmd = block_notify;

src/interfaces/node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ class Node
347347

348348
//! Register handler for block tip messages.
349349
using NotifyBlockTipFn =
350-
std::function<void(bool initial_download, interfaces::BlockTip tip, double verification_progress)>;
350+
std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
351351
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
352352

353353
//! Register handler for chainlock messages.
@@ -357,7 +357,7 @@ class Node
357357

358358
//! Register handler for header tip messages.
359359
using NotifyHeaderTipFn =
360-
std::function<void(bool initial_download, interfaces::BlockTip tip, double verification_progress)>;
360+
std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
361361
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
362362

363363
//! Register handler for masternode list update messages.

src/node/interfaces.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ class NodeImpl : public Node
488488
}
489489
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
490490
{
491-
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](bool initial_download, const CBlockIndex* block) {
492-
fn(initial_download, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
491+
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
492+
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
493493
GuessVerificationProgress(Params().TxData(), block));
494494
}));
495495
}
@@ -502,8 +502,8 @@ class NodeImpl : public Node
502502
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
503503
{
504504
return MakeHandler(
505-
::uiInterface.NotifyHeaderTip_connect([fn](bool initial_download, const CBlockIndex* block) {
506-
fn(initial_download, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
505+
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
506+
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
507507
/* verification progress is unused when a header was received */ 0);
508508
}));
509509
}

src/node/ui_interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { re
5555
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5656
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
5757
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
58-
void CClientUIInterface::NotifyBlockTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(b, i); }
58+
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
5959
void CClientUIInterface::NotifyChainLock(const std::string& bestChainLockHash, int bestChainLockHeight) { return g_ui_signals.NotifyChainLock(bestChainLockHash, bestChainLockHeight); }
60-
void CClientUIInterface::NotifyHeaderTip(bool b, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(b, i); }
60+
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyHeaderTip(s, i); }
6161
void CClientUIInterface::NotifyMasternodeListChanged(const CDeterministicMNList& list, const CBlockIndex* i) { return g_ui_signals.NotifyMasternodeListChanged(list, i); }
6262
void CClientUIInterface::NotifyAdditionalDataSyncProgressChanged(double nSyncProgress) { return g_ui_signals.NotifyAdditionalDataSyncProgressChanged(nSyncProgress); }
6363
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }

src/node/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
class CDeterministicMNList;
@@ -101,13 +102,13 @@ class CClientUIInterface
101102
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
102103

103104
/** New block has been accepted */
104-
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, bool, const CBlockIndex*);
105+
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
105106

106107
/** New chainlock block has been accepted */
107108
ADD_SIGNALS_DECL_WRAPPER(NotifyChainLock, void, const std::string& bestChainLockHash, int bestChainLockHeight);
108109

109110
/** Best header has changed */
110-
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, bool, const CBlockIndex*);
111+
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, const CBlockIndex*);
111112

112113
/** Masternode list has changed */
113114
ADD_SIGNALS_DECL_WRAPPER(NotifyMasternodeListChanged, void, const CDeterministicMNList&, const CBlockIndex*);

src/qt/bitcoin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <util/system.h>
3939
#include <util/threadnames.h>
4040
#include <util/translation.h>
41+
#include <validation.h>
4142

4243
#include <memory>
4344

@@ -67,6 +68,7 @@ Q_IMPORT_PLUGIN(QMacStylePlugin);
6768
// Declare meta types used for QMetaObject::invokeMethod
6869
Q_DECLARE_METATYPE(bool*)
6970
Q_DECLARE_METATYPE(CAmount)
71+
Q_DECLARE_METATYPE(SynchronizationState)
7072
Q_DECLARE_METATYPE(uint256)
7173

7274
static QString GetLangTerritory()
@@ -483,6 +485,7 @@ int GuiMain(int argc, char* argv[])
483485

484486
// Register meta types used for QMetaObject::invokeMethod and Qt::QueuedConnection
485487
qRegisterMetaType<bool*>();
488+
qRegisterMetaType<SynchronizationState>();
486489
#ifdef ENABLE_WALLET
487490
qRegisterMetaType<WalletModel*>();
488491
#endif

src/qt/bitcoingui.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <qt/masternodelist.h>
4242
#include <util/system.h>
4343
#include <util/translation.h>
44+
#include <validation.h>
4445

4546
#include <QAction>
4647
#include <QApplication>
@@ -809,7 +810,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel, interfaces::BlockAndH
809810
connect(_clientModel, &ClientModel::networkActiveChanged, this, &BitcoinGUI::setNetworkActive);
810811

811812
modalOverlay->setKnownBestHeight(tip_info->header_height, QDateTime::fromTime_t(tip_info->header_time));
812-
setNumBlocks(tip_info->block_height, QDateTime::fromTime_t(tip_info->block_time), QString::fromStdString(tip_info->block_hash.ToString()), tip_info->verification_progress, false);
813+
setNumBlocks(tip_info->block_height, QDateTime::fromTime_t(tip_info->block_time), QString::fromStdString(tip_info->block_hash.ToString()), tip_info->verification_progress, false, SynchronizationState::INIT_DOWNLOAD);
813814
connect(_clientModel, &ClientModel::numBlocksChanged, this, &BitcoinGUI::setNumBlocks);
814815

815816
connect(_clientModel, &ClientModel::additionalDataSyncProgressChanged, this, &BitcoinGUI::setAdditionalDataSyncProgress);
@@ -1254,7 +1255,7 @@ void BitcoinGUI::updateNetworkState()
12541255
}
12551256

12561257
if (fNetworkBecameActive || fNetworkBecameInactive) {
1257-
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), QString::fromStdString(m_node.getLastBlockHash()), m_node.getVerificationProgress(), false);
1258+
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), QString::fromStdString(m_node.getLastBlockHash()), m_node.getVerificationProgress(), false, SynchronizationState::INIT_DOWNLOAD);
12581259
}
12591260

12601261
nCountPrev = count;
@@ -1372,11 +1373,11 @@ void BitcoinGUI::updateWidth()
13721373
resize(nWidth, height());
13731374
}
13741375

1375-
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool header)
1376+
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool header, SynchronizationState sync_state)
13761377
{
13771378
#ifdef Q_OS_MAC
13781379
// Disabling macOS App Nap on initial sync, disk, reindex operations and mixing.
1379-
bool disableAppNap = !m_node.masternodeSync().isSynced();
1380+
bool disableAppNap = !m_node.masternodeSync().isSynced() || sync_state != SynchronizationState::POST_INIT;
13801381
#ifdef ENABLE_WALLET
13811382
if (enableWallet) {
13821383
for (const auto& wallet : m_node.walletLoader().getWallets()) {
@@ -1495,7 +1496,7 @@ void BitcoinGUI::setAdditionalDataSyncProgress(double nSyncProgress)
14951496

14961497
// If masternodeSync->Reset() has been called make sure status bar shows the correct information.
14971498
if (nSyncProgress == -1) {
1498-
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), QString::fromStdString(m_node.getLastBlockHash()), m_node.getVerificationProgress(), false);
1499+
setNumBlocks(m_node.getNumBlocks(), QDateTime::fromTime_t(m_node.getLastBlockTime()), QString::fromStdString(m_node.getLastBlockHash()), m_node.getVerificationProgress(), false, SynchronizationState::INIT_DOWNLOAD);
14991500
if (clientModel->getNumConnections()) {
15001501
labelBlocksIcon->show();
15011502
startSpinner();

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;
@@ -266,7 +267,7 @@ public Q_SLOTS:
266267
/** Get restart command-line parameters and request restart */
267268
void handleRestart(QStringList args);
268269
/** Set number of blocks and last block date shown in the UI */
269-
void setNumBlocks(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool headers);
270+
void setNumBlocks(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool headers, SynchronizationState sync_state);
270271
/** Set additional data sync status shown in the UI */
271272
void setAdditionalDataSyncProgress(double nSyncProgress);
272273

src/qt/clientmodel.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net.h>
2020
#include <netbase.h>
2121
#include <util/system.h>
22+
#include <validation.h>
2223

2324
#include <stdint.h>
2425
#include <functional>
@@ -293,17 +294,8 @@ static void BannedListChanged(ClientModel *clientmodel)
293294
assert(invoked);
294295
}
295296

296-
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
297+
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
297298
{
298-
// lock free async UI updates in case we have a new block tip
299-
// during initial sync, only update the UI if the last update
300-
// was > 250ms (MODEL_UPDATE_DELAY) ago
301-
int64_t now = 0;
302-
if (initialSync)
303-
now = GetTimeMillis();
304-
305-
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
306-
307299
if (fHeader) {
308300
// cache best headers time and height to reduce future cs_main locks
309301
clientmodel->cachedBestHeaderHeight = tip.block_height;
@@ -313,18 +305,23 @@ static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, interfac
313305
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;);
314306
}
315307

316-
// During initial sync, block notifications, and header notifications from reindexing are both throttled.
317-
if (!initialSync || (fHeader && !clientmodel->node().getReindex()) || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
318-
//pass an async signal to the UI thread
319-
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
320-
Q_ARG(int, tip.block_height),
321-
Q_ARG(QDateTime, QDateTime::fromTime_t(tip.block_time)),
322-
Q_ARG(QString, QString::fromStdString(tip.block_hash.ToString())),
323-
Q_ARG(double, verificationProgress),
324-
Q_ARG(bool, fHeader));
325-
assert(invoked);
326-
nLastUpdateNotification = now;
308+
// Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex.
309+
const bool throttle = (sync_state != SynchronizationState::POST_INIT && !fHeader) || sync_state == SynchronizationState::INIT_REINDEX;
310+
const int64_t now = throttle ? GetTimeMillis() : 0;
311+
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
312+
if (throttle && now < nLastUpdateNotification + MODEL_UPDATE_DELAY) {
313+
return;
327314
}
315+
316+
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
317+
Q_ARG(int, tip.block_height),
318+
Q_ARG(QDateTime, QDateTime::fromTime_t(tip.block_time)),
319+
Q_ARG(QString, QString::fromStdString(tip.block_hash.ToString())),
320+
Q_ARG(double, verificationProgress),
321+
Q_ARG(bool, fHeader),
322+
Q_ARG(SynchronizationState, sync_state));
323+
assert(invoked);
324+
nLastUpdateNotification = now;
328325
}
329326

330327
static void NotifyChainLock(ClientModel *clientmodel, const std::string& bestChainLockHash, int bestChainLockHeight)

src/qt/clientmodel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include <uint256.h>
1818

1919
class BanTableModel;
20+
class CBlockIndex;
2021
class OptionsModel;
2122
class PeerTableModel;
22-
23-
class CBlockIndex;
23+
enum class SynchronizationState;
2424

2525
QT_BEGIN_NAMESPACE
2626
class QTimer;
@@ -128,7 +128,7 @@ class ClientModel : public QObject
128128
void numConnectionsChanged(int count);
129129
void masternodeListChanged() const;
130130
void chainLockChanged(const QString& bestChainLockHash, int bestChainLockHeight);
131-
void numBlocksChanged(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool header);
131+
void numBlocksChanged(int count, const QDateTime& blockDate, const QString& blockHash, double nVerificationProgress, bool header, SynchronizationState sync_state);
132132
void additionalDataSyncProgressChanged(double nSyncProgress);
133133
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes);
134134
void islockCountChanged(size_t count);

0 commit comments

Comments
 (0)