BlockTip struct created and connected to notifyHeaderTip and notifyBlockTip signals.

This commit is contained in:
furszy 2020-01-23 21:34:58 -03:00
parent 2f867203b0
commit a06e845e82
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623
4 changed files with 22 additions and 14 deletions

View file

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

View file

@ -36,6 +36,7 @@ struct bilingual_str;
namespace interfaces { namespace interfaces {
class Handler; class Handler;
class Wallet; class Wallet;
struct BlockTip;
//! Top-level interface for a bitcoin node (bitcoind process). //! Top-level interface for a bitcoin node (bitcoind process).
class Node class Node
@ -253,12 +254,12 @@ public:
//! Register handler for block tip messages. //! Register handler for block tip messages.
using NotifyBlockTipFn = using NotifyBlockTipFn =
std::function<void(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>; std::function<void(SynchronizationState, interfaces::BlockTip tip, 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(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>; std::function<void(SynchronizationState, interfaces::BlockTip tip, 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.
@ -268,6 +269,13 @@ public:
//! Return implementation of Node interface. //! Return implementation of Node interface.
std::unique_ptr<Node> MakeNode(); std::unique_ptr<Node> MakeNode();
//! Block tip (could be a header or not, depends on the subscribed signal).
struct BlockTip {
int block_height;
int64_t block_time;
uint256 block_hash;
};
} // namespace interfaces } // namespace interfaces
#endif // BITCOIN_INTERFACES_NODE_H #endif // BITCOIN_INTERFACES_NODE_H

View file

@ -244,15 +244,15 @@ static void BannedListChanged(ClientModel *clientmodel)
assert(invoked); assert(invoked);
} }
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, const uint256 block_hash, int height, int64_t blockTime, double verificationProgress, bool fHeader) static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
{ {
if (fHeader) { if (fHeader) {
// cache best headers time and height to reduce future cs_main locks // cache best headers time and height to reduce future cs_main locks
clientmodel->cachedBestHeaderHeight = height; clientmodel->cachedBestHeaderHeight = tip.block_height;
clientmodel->cachedBestHeaderTime = blockTime; clientmodel->cachedBestHeaderTime = tip.block_time;
} else { } else {
clientmodel->m_cached_num_blocks = height; clientmodel->m_cached_num_blocks = tip.block_height;
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = block_hash;); WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;);
} }
// Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex. // 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_
} }
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection, bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
Q_ARG(int, height), Q_ARG(int, tip.block_height),
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)), Q_ARG(QDateTime, QDateTime::fromTime_t(tip.block_time)),
Q_ARG(double, verificationProgress), Q_ARG(double, verificationProgress),
Q_ARG(bool, fHeader), Q_ARG(bool, fHeader),
Q_ARG(SynchronizationState, sync_state)); Q_ARG(SynchronizationState, sync_state));
@ -281,8 +281,8 @@ void ClientModel::subscribeToCoreSignals()
m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1)); m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1));
m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this)); m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this));
m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this)); m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this));
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)); m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, false));
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)); m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, true));
} }
void ClientModel::unsubscribeFromCoreSignals() void ClientModel::unsubscribeFromCoreSignals()

View file

@ -77,7 +77,7 @@ public:
bool getProxyInfo(std::string& ip_port) const; bool getProxyInfo(std::string& ip_port) const;
// caches for the best header, number of blocks // caches for the best header: hash, number of blocks and block time
mutable std::atomic<int> cachedBestHeaderHeight; mutable std::atomic<int> cachedBestHeaderHeight;
mutable std::atomic<int64_t> cachedBestHeaderTime; mutable std::atomic<int64_t> cachedBestHeaderTime;
mutable std::atomic<int> m_cached_num_blocks{-1}; mutable std::atomic<int> m_cached_num_blocks{-1};