Skip to content

Commit 0cadb12

Browse files
committed
Add logging for CValidationInterface events
This could help debug issues where there may be race conditions at play, such as #12978. Fixes #12994.
1 parent ee4d66a commit 0cadb12

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/validationinterface.cpp

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
#include <validationinterface.h>
77

8+
#include <chain.h>
9+
#include <logging.h>
810
#include <primitives/block.h>
11+
#include <primitives/transaction.h>
912
#include <scheduler.h>
13+
#include <util/validation.h>
1014

1115
#include <future>
1216
#include <unordered_map>
@@ -110,52 +114,88 @@ void SyncWithValidationInterfaceQueue() {
110114
promise.get_future().wait();
111115
}
112116

117+
// Use a macro instead of a function for conditional logging to prevent
118+
// evaluating arguments when logging is not enabled.
119+
//
120+
// NOTE: The lambda captures all local variables by value.
121+
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
122+
do { \
123+
auto local_name = (name); \
124+
LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
125+
m_internals->m_schedulerClient.AddToProcessQueue([=] { \
126+
LOG_EVENT(fmt, local_name, __VA_ARGS__); \
127+
event(); \
128+
}); \
129+
} while (0)
130+
131+
#define LOG_EVENT(fmt, ...) \
132+
LogPrint(BCLog::VALIDATIONINTERFACE, fmt "\n", __VA_ARGS__)
113133

114134
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
115135
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
116136
// the chain actually updates. One way to ensure this is for the caller to invoke this signal
117137
// in the same critical section where the chain is updated
118138

119-
m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] {
139+
auto event = [pindexNew, pindexFork, fInitialDownload, this] {
120140
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
121-
});
141+
};
142+
ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
143+
pindexNew->GetBlockHash().ToString(),
144+
pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
145+
fInitialDownload);
122146
}
123147

124148
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
125-
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
149+
auto event = [ptx, this] {
126150
m_internals->TransactionAddedToMempool(ptx);
127-
});
151+
};
152+
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
153+
ptx->GetHash().ToString(),
154+
ptx->GetWitnessHash().ToString());
128155
}
129156

130157
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
131-
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
158+
auto event = [ptx, this] {
132159
m_internals->TransactionRemovedFromMempool(ptx);
133-
});
160+
};
161+
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
162+
ptx->GetHash().ToString(),
163+
ptx->GetWitnessHash().ToString());
134164
}
135165

136166
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>>& pvtxConflicted) {
137-
m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, pvtxConflicted, this] {
167+
auto event = [pblock, pindex, pvtxConflicted, this] {
138168
m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
139-
});
169+
};
170+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
171+
pblock->GetHash().ToString(),
172+
pindex->nHeight);
140173
}
141174

142-
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
143-
{
144-
m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, this] {
175+
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) {
176+
auto event = [pblock, pindex, this] {
145177
m_internals->BlockDisconnected(pblock, pindex);
146-
});
178+
};
179+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
180+
pblock->GetHash().ToString(),
181+
pindex->nHeight);
147182
}
148183

149184
void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) {
150-
m_internals->m_schedulerClient.AddToProcessQueue([locator, this] {
185+
auto event = [locator, this] {
151186
m_internals->ChainStateFlushed(locator);
152-
});
187+
};
188+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
189+
locator.IsNull() ? "null" : locator.vHave.front().ToString());
153190
}
154191

155192
void CMainSignals::BlockChecked(const CBlock& block, const BlockValidationState& state) {
193+
LOG_EVENT("%s: block hash=%s state=%s", __func__,
194+
block.GetHash().ToString(), FormatStateMessage(state));
156195
m_internals->BlockChecked(block, state);
157196
}
158197

159198
void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
199+
LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
160200
m_internals->NewPoWValidBlock(pindex, block);
161201
}

test/lint/lint-format-strings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
("src/index/base.cpp", "FatalError(const char* fmt, const Args&... args)"),
1818
("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args)"),
1919
("src/util/system.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
20+
("src/validationinterface.cpp", "LogPrint(BCLog::VALIDATIONINTERFACE, fmt \"\\n\", __VA_ARGS__)"),
2021
("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"),
2122
("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"),
2223
("src/logging.h", "LogPrintf(const char* fmt, const Args&... args)"),

0 commit comments

Comments
 (0)