|
5 | 5 |
|
6 | 6 | #include <validationinterface.h> |
7 | 7 |
|
| 8 | +#include <chain.h> |
| 9 | +#include <logging.h> |
8 | 10 | #include <primitives/block.h> |
| 11 | +#include <primitives/transaction.h> |
9 | 12 | #include <scheduler.h> |
| 13 | +#include <util/validation.h> |
10 | 14 |
|
11 | 15 | #include <future> |
12 | 16 | #include <unordered_map> |
@@ -110,52 +114,88 @@ void SyncWithValidationInterfaceQueue() { |
110 | 114 | promise.get_future().wait(); |
111 | 115 | } |
112 | 116 |
|
| 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__) |
113 | 133 |
|
114 | 134 | void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) { |
115 | 135 | // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which |
116 | 136 | // the chain actually updates. One way to ensure this is for the caller to invoke this signal |
117 | 137 | // in the same critical section where the chain is updated |
118 | 138 |
|
119 | | - m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] { |
| 139 | + auto event = [pindexNew, pindexFork, fInitialDownload, this] { |
120 | 140 | 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); |
122 | 146 | } |
123 | 147 |
|
124 | 148 | void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) { |
125 | | - m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] { |
| 149 | + auto event = [ptx, this] { |
126 | 150 | 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()); |
128 | 155 | } |
129 | 156 |
|
130 | 157 | void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) { |
131 | | - m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] { |
| 158 | + auto event = [ptx, this] { |
132 | 159 | 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()); |
134 | 164 | } |
135 | 165 |
|
136 | 166 | 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] { |
138 | 168 | 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); |
140 | 173 | } |
141 | 174 |
|
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] { |
145 | 177 | 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); |
147 | 182 | } |
148 | 183 |
|
149 | 184 | void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) { |
150 | | - m_internals->m_schedulerClient.AddToProcessQueue([locator, this] { |
| 185 | + auto event = [locator, this] { |
151 | 186 | 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()); |
153 | 190 | } |
154 | 191 |
|
155 | 192 | 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)); |
156 | 195 | m_internals->BlockChecked(block, state); |
157 | 196 | } |
158 | 197 |
|
159 | 198 | void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) { |
| 199 | + LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString()); |
160 | 200 | m_internals->NewPoWValidBlock(pindex, block); |
161 | 201 | } |
0 commit comments