Skip to content

Commit a06e845

Browse files
committed
BlockTip struct created and connected to notifyHeaderTip and notifyBlockTip signals.
1 parent 2f86720 commit a06e845

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/interfaces/node.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,15 @@ class NodeImpl : public Node
315315
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
316316
{
317317
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
318-
fn(sync_state, block->GetBlockHash(), block->nHeight, block->GetBlockTime(),
318+
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
319319
GuessVerificationProgress(Params().TxData(), block));
320320
}));
321321
}
322322
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
323323
{
324324
return MakeHandler(
325325
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
326-
fn(sync_state, block->GetBlockHash(), block->nHeight, block->GetBlockTime(),
326+
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
327327
/* verification progress is unused when a header was received */ 0);
328328
}));
329329
}

src/interfaces/node.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct bilingual_str;
3636
namespace interfaces {
3737
class Handler;
3838
class Wallet;
39+
struct BlockTip;
3940

4041
//! Top-level interface for a bitcoin node (bitcoind process).
4142
class Node
@@ -253,12 +254,12 @@ class Node
253254

254255
//! Register handler for block tip messages.
255256
using NotifyBlockTipFn =
256-
std::function<void(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>;
257+
std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
257258
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
258259

259260
//! Register handler for header tip messages.
260261
using NotifyHeaderTipFn =
261-
std::function<void(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>;
262+
std::function<void(SynchronizationState, interfaces::BlockTip tip, double verification_progress)>;
262263
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
263264

264265
//! Return pointer to internal chain interface, useful for testing.
@@ -268,6 +269,13 @@ class Node
268269
//! Return implementation of Node interface.
269270
std::unique_ptr<Node> MakeNode();
270271

272+
//! Block tip (could be a header or not, depends on the subscribed signal).
273+
struct BlockTip {
274+
int block_height;
275+
int64_t block_time;
276+
uint256 block_hash;
277+
};
278+
271279
} // namespace interfaces
272280

273281
#endif // BITCOIN_INTERFACES_NODE_H

src/qt/clientmodel.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,15 @@ static void BannedListChanged(ClientModel *clientmodel)
244244
assert(invoked);
245245
}
246246

247-
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, const uint256 block_hash, int height, int64_t blockTime, double verificationProgress, bool fHeader)
247+
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
248248
{
249249
if (fHeader) {
250250
// cache best headers time and height to reduce future cs_main locks
251-
clientmodel->cachedBestHeaderHeight = height;
252-
clientmodel->cachedBestHeaderTime = blockTime;
251+
clientmodel->cachedBestHeaderHeight = tip.block_height;
252+
clientmodel->cachedBestHeaderTime = tip.block_time;
253253
} else {
254-
clientmodel->m_cached_num_blocks = height;
255-
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = block_hash;);
254+
clientmodel->m_cached_num_blocks = tip.block_height;
255+
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;);
256256
}
257257

258258
// Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex.
@@ -264,8 +264,8 @@ static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_
264264
}
265265

266266
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
267-
Q_ARG(int, height),
268-
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
267+
Q_ARG(int, tip.block_height),
268+
Q_ARG(QDateTime, QDateTime::fromTime_t(tip.block_time)),
269269
Q_ARG(double, verificationProgress),
270270
Q_ARG(bool, fHeader),
271271
Q_ARG(SynchronizationState, sync_state));
@@ -281,8 +281,8 @@ void ClientModel::subscribeToCoreSignals()
281281
m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1));
282282
m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this));
283283
m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this));
284-
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, false));
285-
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, true));
284+
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, false));
285+
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, true));
286286
}
287287

288288
void ClientModel::unsubscribeFromCoreSignals()

src/qt/clientmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ClientModel : public QObject
7777

7878
bool getProxyInfo(std::string& ip_port) const;
7979

80-
// caches for the best header, number of blocks
80+
// caches for the best header: hash, number of blocks and block time
8181
mutable std::atomic<int> cachedBestHeaderHeight;
8282
mutable std::atomic<int64_t> cachedBestHeaderTime;
8383
mutable std::atomic<int> m_cached_num_blocks{-1};

0 commit comments

Comments
 (0)