Skip to content

Commit 04394f9

Browse files
laanwjabc-bot
authored and
abc-bot
committed
Merge #16688: log: Add validation interface logging
Summary: f9abf4ab6d3d3e4d4b7e90723020b5422a141a6f Add logging for CValidationInterface events (Jeffrey Czyz) 6edebacb2191373e76d79a4972d6192300976096 Refactor FormatStateMessage for clarity (Jeffrey Czyz) 72f3227c83810936e7a334304e5fd7c6dab8e91b Format CValidationState properly in all cases (Jeffrey Czyz) 428ac70095253225f64462ee15c595644747f376 Add VALIDATION to BCLog::LogFlags (Jeffrey Czyz) Pull request description: Add logging of `CValidationInterface` callbacks using a new `VALIDATIONINTERFACE` log flag (see #12994). A separate flag is desirable as the logging can be noisy and thus may need to be disabled without affecting other logging. This could help debug issues where there may be race conditions at play, such as #12978. ACKs for top commit: jnewbery: ACK f9abf4ab6d3d3e4d4b7e90723020b5422a141a6f hebasto: ACK f9abf4ab6d3d3e4d4b7e90723020b5422a141a6f ariard: ACK f9abf4a, only changes since 0cadb12 are replacing log indication `VALIDATIONINTERFACE` by `VALIDATION` and avoiding a forward declaration with a new include ryanofsky: Code review ACK f9abf4ab6d3d3e4d4b7e90723020b5422a141a6f. Just suggested changes since last review (thanks!) Tree-SHA512: 3e0f6e2c8951cf46fbad3ff440971d95d526df2a52a2e4d6452a82785c63d53accfdabae66b0b30e2fe0b00737f8d5cb717edbad1460b63acb11a72c8f5d4236 Partial backport of [[bitcoin/bitcoin#16688 | PR16688]] The changes to `FormatStateMessage` are completely absent because I did not want to mangle them for the sake of backporting this change, and that particular change depends on a massive stack that isn't necessary to take advantage of this patch. Test Plan: `ninja check check-functional` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D6652
1 parent 094184b commit 04394f9

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

src/logging.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const CLogCategoryDesc LogCategories[] = {
122122
{BCLog::COINDB, "coindb"},
123123
{BCLog::QT, "qt"},
124124
{BCLog::LEVELDB, "leveldb"},
125+
{BCLog::VALIDATION, "validation"},
125126
{BCLog::ALL, "1"},
126127
{BCLog::ALL, "all"},
127128
};

src/logging.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum LogFlags : uint32_t {
5454
COINDB = (1 << 18),
5555
QT = (1 << 19),
5656
LEVELDB = (1 << 20),
57+
VALIDATION = (1 << 21),
5758
ALL = ~uint32_t(0),
5859
};
5960

src/validationinterface.cpp

+53-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
#include <validationinterface.h>
77

8+
#include <chain.h>
9+
#include <consensus/validation.h>
10+
#include <logging.h>
811
#include <primitives/block.h>
12+
#include <primitives/transaction.h>
913
#include <scheduler.h>
14+
#include <util/validation.h>
1015

1116
#include <future>
1217
#include <tuple>
@@ -159,6 +164,22 @@ void SyncWithValidationInterfaceQueue() {
159164
promise.get_future().wait();
160165
}
161166

167+
// Use a macro instead of a function for conditional logging to prevent
168+
// evaluating arguments when logging is not enabled.
169+
//
170+
// NOTE: The lambda captures all local variables by value.
171+
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
172+
do { \
173+
auto local_name = (name); \
174+
LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
175+
m_internals->m_schedulerClient.AddToProcessQueue([=] { \
176+
LOG_EVENT(fmt, local_name, __VA_ARGS__); \
177+
event(); \
178+
}); \
179+
} while (0)
180+
181+
#define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
182+
162183
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew,
163184
const CBlockIndex *pindexFork,
164185
bool fInitialDownload) {
@@ -167,48 +188,65 @@ void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew,
167188
// for the caller to invoke this signal in the same critical section where
168189
// the chain is updated
169190

170-
m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork,
171-
fInitialDownload, this] {
191+
auto event = [pindexNew, pindexFork, fInitialDownload, this] {
172192
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
173-
});
193+
};
194+
ENQUEUE_AND_LOG_EVENT(
195+
event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
196+
pindexNew->GetBlockHash().ToString(),
197+
pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
198+
fInitialDownload);
174199
}
175200

176201
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
177-
m_internals->m_schedulerClient.AddToProcessQueue(
178-
[ptx, this] { m_internals->TransactionAddedToMempool(ptx); });
202+
auto event = [ptx, this] { m_internals->TransactionAddedToMempool(ptx); };
203+
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
204+
ptx->GetHash().ToString());
179205
}
180206

181207
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
182-
m_internals->m_schedulerClient.AddToProcessQueue(
183-
[ptx, this] { m_internals->TransactionRemovedFromMempool(ptx); });
208+
auto event = [ptx, this] {
209+
m_internals->TransactionRemovedFromMempool(ptx);
210+
};
211+
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
212+
ptx->GetHash().ToString());
184213
}
185214

186215
void CMainSignals::BlockConnected(
187216
const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex,
188217
const std::shared_ptr<const std::vector<CTransactionRef>> &pvtxConflicted) {
189-
m_internals->m_schedulerClient.AddToProcessQueue(
190-
[pblock, pindex, pvtxConflicted, this] {
191-
m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
192-
});
218+
auto event = [pblock, pindex, pvtxConflicted, this] {
219+
m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
220+
};
221+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
222+
pblock->GetHash().ToString(), pindex->nHeight);
193223
}
194224

195225
void CMainSignals::BlockDisconnected(
196226
const std::shared_ptr<const CBlock> &pblock) {
197-
m_internals->m_schedulerClient.AddToProcessQueue(
198-
[pblock, this] { m_internals->BlockDisconnected(pblock); });
227+
// TODO: This function was refactored as part of an out-of-order backport
228+
// of https://github.com/bitcoin/bitcoin/pull/16688
229+
auto event = [pblock, this] { m_internals->BlockDisconnected(pblock); };
230+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
231+
pblock->GetHash().ToString());
199232
}
200233

201234
void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) {
202-
m_internals->m_schedulerClient.AddToProcessQueue(
203-
[locator, this] { m_internals->ChainStateFlushed(locator); });
235+
auto event = [locator, this] { m_internals->ChainStateFlushed(locator); };
236+
ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
237+
locator.IsNull() ? "null"
238+
: locator.vHave.front().ToString());
204239
}
205240

206241
void CMainSignals::BlockChecked(const CBlock &block,
207242
const CValidationState &state) {
243+
LOG_EVENT("%s: block hash=%s state=%s", __func__,
244+
block.GetHash().ToString(), FormatStateMessage(state));
208245
m_internals->BlockChecked(block, state);
209246
}
210247

211248
void CMainSignals::NewPoWValidBlock(
212249
const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
250+
LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
213251
m_internals->NewPoWValidBlock(pindex, block);
214252
}

test/lint/lint-format-strings.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char *fmt, const Args &... args)"),
2020
("src/util/system.cpp",
2121
"strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"),
22+
("src/validationinterface.cpp",
23+
"LogPrint(BCLog::VALIDATION, fmt \"\\n\", __VA_ARGS__)"),
2224
("src/tinyformat.h", "printf(const char *fmt, const Args &... args)"),
2325
("src/tinyformat.h", "printf(const char *fmt, TINYFORMAT_VARARGS(n))"),
2426
("src/wallet/wallet.h",

0 commit comments

Comments
 (0)