Bitcoin ABC 0.30.5
P2P Digital Currency
validationinterface.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
7
8#include <attributes.h>
9#include <chain.h>
11#include <logging.h>
12#include <primitives/block.h>
14#include <scheduler.h>
15
16#include <future>
17#include <tuple>
18#include <unordered_map>
19#include <utility>
20
21const std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept;
22
33private:
39 struct ListEntry {
40 std::shared_ptr<CValidationInterface> callbacks;
41 int count = 1;
42 };
43 std::list<ListEntry> m_list GUARDED_BY(m_mutex);
44 std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
46
47public:
48 // We are not allowed to assume the scheduler only runs in one thread,
49 // but must ensure all callbacks happen in-order, so we end up creating
50 // our own queue here :(
52
54 : m_schedulerClient(scheduler) {}
55
56 void Register(std::shared_ptr<CValidationInterface> callbacks)
59 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
60 if (inserted.second) {
61 inserted.first->second = m_list.emplace(m_list.end());
62 }
63 inserted.first->second->callbacks = std::move(callbacks);
64 }
65
69 auto it = m_map.find(callbacks);
70 if (it != m_map.end()) {
71 if (!--it->second->count) {
72 m_list.erase(it->second);
73 }
74 m_map.erase(it);
75 }
76 }
77
84 for (const auto &entry : m_map) {
85 if (!--entry.second->count) {
86 m_list.erase(entry.second);
87 }
88 }
89 m_map.clear();
90 }
91
92 template <typename F>
94 WAIT_LOCK(m_mutex, lock);
95 for (auto it = m_list.begin(); it != m_list.end();) {
96 ++it->count;
97 {
98 REVERSE_LOCK(lock);
99 f(*it->callbacks);
100 }
101 it = --it->count ? std::next(it) : m_list.erase(it);
102 }
103 }
104};
105
107
110 m_internals = std::make_unique<MainSignalsImpl>(scheduler);
111}
112
114 m_internals.reset(nullptr);
115}
116
118 if (m_internals) {
119 m_internals->m_schedulerClient.EmptyQueue();
120 }
121}
122
124 if (!m_internals) {
125 return 0;
126 }
127 return m_internals->m_schedulerClient.CallbacksPending();
128}
129
131 return g_signals;
132}
133
135 std::shared_ptr<CValidationInterface> callbacks) {
136 // Each connection captures the shared_ptr to ensure that each callback is
137 // executed before the subscriber is destroyed. For more details see #18338.
138 g_signals.m_internals->Register(std::move(callbacks));
139}
140
142 // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
143 // is managed by the caller.
145 {callbacks, [](CValidationInterface *) {}});
146}
147
149 std::shared_ptr<CValidationInterface> callbacks) {
150 UnregisterValidationInterface(callbacks.get());
151}
152
155 g_signals.m_internals->Unregister(callbacks);
156 }
157}
158
160 if (!g_signals.m_internals) {
161 return;
162 }
163 g_signals.m_internals->Clear();
164}
165
166void CallFunctionInValidationInterfaceQueue(std::function<void()> func) {
167 g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
168}
169
172 // Block until the validation queue drains
173 std::promise<void> promise;
174 CallFunctionInValidationInterfaceQueue([&promise] { promise.set_value(); });
175 promise.get_future().wait();
176}
177
178// Use a macro instead of a function for conditional logging to prevent
179// evaluating arguments when logging is not enabled.
180//
181// NOTE: The lambda captures all local variables by value.
182#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
183 do { \
184 auto local_name = (name); \
185 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
186 m_internals->m_schedulerClient.AddToProcessQueue([=] { \
187 LOG_EVENT(fmt, local_name, __VA_ARGS__); \
188 event(); \
189 }); \
190 } while (0)
191
192#define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
193
195 const CBlockIndex *pindexFork,
196 bool fInitialDownload) {
197 // Dependencies exist that require UpdatedBlockTip events to be delivered in
198 // the order in which the chain actually updates. One way to ensure this is
199 // for the caller to invoke this signal in the same critical section where
200 // the chain is updated
201
202 auto event = [pindexNew, pindexFork, fInitialDownload, this] {
203 m_internals->Iterate([&](CValidationInterface &callbacks) {
204 callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
205 });
206 };
208 event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
209 pindexNew->GetBlockHash().ToString(),
210 pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
211 fInitialDownload);
212}
213
215 const CTransactionRef &tx,
216 std::shared_ptr<const std::vector<Coin>> spent_coins,
217 uint64_t mempool_sequence) {
218 auto event = [tx, spent_coins, mempool_sequence, this] {
219 m_internals->Iterate([&](CValidationInterface &callbacks) {
220 callbacks.TransactionAddedToMempool(tx, spent_coins,
221 mempool_sequence);
222 });
223 };
224 ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
225 tx->GetHash().ToString());
226}
227
230 uint64_t mempool_sequence) {
231 auto event = [tx, reason, mempool_sequence, this] {
232 m_internals->Iterate([&](CValidationInterface &callbacks) {
233 callbacks.TransactionRemovedFromMempool(tx, reason,
234 mempool_sequence);
235 });
236 };
237 ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s reason=%s", __func__,
238 tx->GetHash().ToString(),
239 RemovalReasonToString(reason));
240}
241
242void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock,
243 const CBlockIndex *pindex) {
244 auto event = [pblock, pindex, this] {
245 m_internals->Iterate([&](CValidationInterface &callbacks) {
246 callbacks.BlockConnected(pblock, pindex);
247 });
248 };
249 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
250 pblock->GetHash().ToString(), pindex->nHeight);
251}
252
254 const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
255 auto event = [pblock, pindex, this] {
256 m_internals->Iterate([&](CValidationInterface &callbacks) {
257 callbacks.BlockDisconnected(pblock, pindex);
258 });
259 };
260 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
261 pblock->GetHash().ToString());
262}
263
265 auto event = [locator, this] {
266 m_internals->Iterate([&](CValidationInterface &callbacks) {
267 callbacks.ChainStateFlushed(locator);
268 });
269 };
270 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
271 locator.IsNull() ? "null"
272 : locator.vHave.front().ToString());
273}
274
276 const BlockValidationState &state) {
277 LOG_EVENT("%s: block hash=%s state=%s", __func__,
278 block.GetHash().ToString(), state.ToString());
279 m_internals->Iterate([&](CValidationInterface &callbacks) {
280 callbacks.BlockChecked(block, state);
281 });
282}
283
285 const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
286 LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
287 m_internals->Iterate([&](CValidationInterface &callbacks) {
288 callbacks.NewPoWValidBlock(pindex, block);
289 });
290}
291
293 auto event = [pindex, this] {
294 m_internals->Iterate([&](CValidationInterface &callbacks) {
295 callbacks.BlockFinalized(pindex);
296 });
297 };
298 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
299 pindex ? pindex->GetBlockHash().ToString() : "null");
300}
301
303 const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
304 auto event = [pindex, block, this] {
305 m_internals->Iterate([&](CValidationInterface &callbacks) {
306 callbacks.BlockInvalidated(pindex, block);
307 });
308 };
309 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
310 block ? block->GetHash().ToString() : "null");
311}
#define LIFETIMEBOUND
Definition: attributes.h:16
BlockHash GetHash() const
Definition: block.cpp:11
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
BlockHash GetBlockHash() const
Definition: blockindex.h:146
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
std::unique_ptr< MainSignalsImpl > m_internals
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once)
void BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
void ChainStateFlushed(const CBlockLocator &)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void BlockFinalized(const CBlockIndex *pindex)
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:41
Implement this to subscribe to events generated in validation.
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
Notifies listeners that a block which builds directly on our current tip has been received and connec...
virtual void BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
virtual void BlockFinalized(const CBlockIndex *pindex)
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
virtual void TransactionAddedToMempool(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin > > spent_coins, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
MainSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Clear unregisters every previously registered callback, erasing every map entry.
void Iterate(F &&f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Unregister(CValidationInterface *callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Register(std::shared_ptr< CValidationInterface > callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
std::unordered_map< CValidationInterface *, std::list< ListEntry >::iterator > m_map GUARDED_BY(m_mutex)
SingleThreadedSchedulerClient m_schedulerClient
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
MainSignalsImpl(CScheduler &scheduler LIFETIMEBOUND)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:143
std::string ToString() const
Definition: validation.h:125
std::string ToString() const
Definition: uint256.h:80
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:105
std::vector< BlockHash > vHave
Definition: block.h:106
bool IsNull() const
Definition: block.h:123
List entries consist of a callback pointer and reference count.
int count
std::shared_ptr< CValidationInterface > callbacks
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define AssertLockNotHeld(cs)
Definition: sync.h:163
#define LOCK(cs)
Definition: sync.h:306
#define REVERSE_LOCK(g)
Definition: sync.h:265
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:151
assert(!tx.IsCoinBase())
#define LOG_EVENT(fmt,...)
static CMainSignals g_signals
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
CMainSignals & GetMainSignals()
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
const std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
Definition: txmempool.cpp:822
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.