Is it important that we log the entire message when enqueueing? Because if not, and we log e.g. just the function name, I think we don't really need any of these macros?
<details>
<summary>git diff on 8c4439a5d1</summary>
diff --git a/src/validationinterface.cpp b/src/validationinterface.cpp
index cc1abcd6cd..038e9d220b 100644
--- a/src/validationinterface.cpp
+++ b/src/validationinterface.cpp
@@ -154,123 +154,100 @@ void ValidationSignals::SyncWithValidationInterfaceQueue()
promise.get_future().wait();
}
-// Use a macro instead of a function for conditional logging to prevent
-// evaluating arguments when logging is not enabled.
-//
-// NOTE: The lambda captures the event by move
-#define ENQUEUE_AND_LOG_EVENT(event, log_msg) \
- do { \
- static_assert(std::is_rvalue_reference_v<decltype((event))>, \
- "event must be passed as an rvalue"); \
- static_assert(!std::is_const_v<std::remove_reference_t<decltype((log_msg))>>, \
- "log_msg must be mutable reference"); \
- LOG_EVENT("Enqueuing %s", log_msg); \
- m_internals->m_task_runner->insert([local_log_msg = std::move(log_msg), local_event = (event)] { \
- LOG_EVENT("%s", local_log_msg); \
- local_event(); \
- }); \
- } while (0)
-
-#define LOG_MSG(fmt, ...) \
- (ShouldLog(BCLog::VALIDATION, BCLog::Level::Debug) ? tfm::format((fmt), __VA_ARGS__) : std::string{})
-
-#define LOG_EVENT(fmt, ...) \
- LogDebug(BCLog::VALIDATION, fmt, __VA_ARGS__)
-
void ValidationSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
// the chain actually updates. One way to ensure this is for the caller to invoke this signal
// in the same critical section where the chain is updated
- auto log_msg = LOG_MSG("%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
- pindexNew->GetBlockHash().ToString(),
- pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
- fInitialDownload);
- auto event = [pindexNew, pindexFork, fInitialDownload, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([pindexNew, pindexFork, fInitialDownload, this] {
+ LogDebug(BCLog::VALIDATION, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
+ pindexNew->GetBlockHash().ToString(),
+ pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
+ fInitialDownload);
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd)
{
- LOG_EVENT("%s: new block hash=%s block height=%d", __func__, new_tip.GetBlockHash().ToString(), new_tip.nHeight);
+ LogDebug(BCLog::VALIDATION, "%s: new block hash=%s block height=%d", __func__, new_tip.GetBlockHash().ToString(), new_tip.nHeight);
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ActiveTipChange(new_tip, is_ibd); });
}
void ValidationSignals::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence)
{
- auto log_msg = LOG_MSG("%s: txid=%s wtxid=%s", __func__,
- tx.info.m_tx->GetHash().ToString(),
- tx.info.m_tx->GetWitnessHash().ToString());
- auto event = [tx, mempool_sequence, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([tx, mempool_sequence, this] {
+ LogDebug(BCLog::VALIDATION, "%s: txid=%s wtxid=%s", __func__,
+ tx.info.m_tx->GetHash().ToString(),
+ tx.info.m_tx->GetWitnessHash().ToString());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {
- auto log_msg = LOG_MSG("%s: txid=%s wtxid=%s reason=%s", __func__,
- tx->GetHash().ToString(),
- tx->GetWitnessHash().ToString(),
- RemovalReasonToString(reason));
- auto event = [tx, reason, mempool_sequence, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([tx, reason, mempool_sequence, this] {
+ LogDebug(BCLog::VALIDATION, "%s: txid=%s wtxid=%s reason=%s", __func__,
+ tx->GetHash().ToString(),
+ tx->GetWitnessHash().ToString(),
+ RemovalReasonToString(reason));
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::BlockConnected(const ChainstateRole& role, std::shared_ptr<const CBlock> pblock, const CBlockIndex* pindex)
{
- auto log_msg = LOG_MSG("%s: block hash=%s block height=%d", __func__,
- pblock->GetHash().ToString(),
- pindex->nHeight);
- auto event = [role, pblock = std::move(pblock), pindex, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([role, pblock = std::move(pblock), pindex, this] {
+ LogDebug(BCLog::VALIDATION, "%s: block hash=%s block height=%d", __func__,
+ pblock->GetHash().ToString(),
+ pindex->nHeight);
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockConnected(role, pblock, pindex); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight)
{
- auto log_msg = LOG_MSG("%s: block height=%s txs removed=%s", __func__,
- nBlockHeight,
- txs_removed_for_block.size());
- auto event = [txs_removed_for_block, nBlockHeight, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([txs_removed_for_block, nBlockHeight, this] {
+ LogDebug(BCLog::VALIDATION, "%s: block height=%d txs removed=%d", __func__,
+ nBlockHeight,
+ txs_removed_for_block.size());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::BlockDisconnected(std::shared_ptr<const CBlock> pblock, const CBlockIndex* pindex)
{
- auto log_msg = LOG_MSG("%s: block hash=%s block height=%d", __func__,
- pblock->GetHash().ToString(),
- pindex->nHeight);
- auto event = [pblock = std::move(pblock), pindex, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([pblock = std::move(pblock), pindex, this] {
+ LogDebug(BCLog::VALIDATION, "%s: block hash=%s block height=%d", __func__,
+ pblock->GetHash().ToString(),
+ pindex->nHeight);
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockDisconnected(pblock, pindex); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::ChainStateFlushed(const ChainstateRole& role, const CBlockLocator& locator)
{
- auto log_msg = LOG_MSG("%s: block hash=%s", __func__,
- locator.IsNull() ? "null" : locator.vHave.front().ToString());
- auto event = [role, locator, this] {
+ LogDebug(BCLog::VALIDATION, "Enqueuing %s event", __func__);
+ m_internals->m_task_runner->insert([role, locator, this] {
+ LogDebug(BCLog::VALIDATION, "%s: block hash=%s", __func__,
+ locator.IsNull() ? "null" : locator.vHave.front().ToString());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ChainStateFlushed(role, locator); });
- };
- ENQUEUE_AND_LOG_EVENT(std::move(event), log_msg);
+ });
}
void ValidationSignals::BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state)
{
- LOG_EVENT("%s: block hash=%s state=%s", __func__,
- block->GetHash().ToString(), state.ToString());
+ LogDebug(BCLog::VALIDATION, "%s: block hash=%s state=%s", __func__,
+ block->GetHash().ToString(), state.ToString());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockChecked(block, state); });
}
void ValidationSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
- LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
+ LogDebug(BCLog::VALIDATION, "%s: block hash=%s", __func__, block->GetHash().ToString());
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
}
</details>