Bitcoin ABC 0.31.8
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 <kernel/chain.h>
12#include <logging.h>
13#include <primitives/block.h>
15#include <scheduler.h>
16
17#include <future>
18#include <tuple>
19#include <unordered_map>
20#include <utility>
21
22std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept;
23
34private:
40 struct ListEntry {
41 std::shared_ptr<CValidationInterface> callbacks;
42 int count = 1;
43 };
44 std::list<ListEntry> m_list GUARDED_BY(m_mutex);
45 std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
47
48public:
49 // We are not allowed to assume the scheduler only runs in one thread,
50 // but must ensure all callbacks happen in-order, so we end up creating
51 // our own queue here :(
53
55 : m_schedulerClient(scheduler) {}
56
57 void Register(std::shared_ptr<CValidationInterface> callbacks)
60 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
61 if (inserted.second) {
62 inserted.first->second = m_list.emplace(m_list.end());
63 }
64 inserted.first->second->callbacks = std::move(callbacks);
65 }
66
70 auto it = m_map.find(callbacks);
71 if (it != m_map.end()) {
72 if (!--it->second->count) {
73 m_list.erase(it->second);
74 }
75 m_map.erase(it);
76 }
77 }
78
85 for (const auto &entry : m_map) {
86 if (!--entry.second->count) {
87 m_list.erase(entry.second);
88 }
89 }
90 m_map.clear();
91 }
92
93 template <typename F>
95 WAIT_LOCK(m_mutex, lock);
96 for (auto it = m_list.begin(); it != m_list.end();) {
97 ++it->count;
98 {
99 REVERSE_LOCK(lock);
100 f(*it->callbacks);
101 }
102 it = --it->count ? std::next(it) : m_list.erase(it);
103 }
104 }
105};
106
108
111 m_internals = std::make_unique<MainSignalsImpl>(scheduler);
112}
113
115 m_internals.reset(nullptr);
116}
117
119 if (m_internals) {
120 m_internals->m_schedulerClient.EmptyQueue();
121 }
122}
123
125 if (!m_internals) {
126 return 0;
127 }
128 return m_internals->m_schedulerClient.CallbacksPending();
129}
130
132 return g_signals;
133}
134
136 std::shared_ptr<CValidationInterface> callbacks) {
137 // Each connection captures the shared_ptr to ensure that each callback is
138 // executed before the subscriber is destroyed. For more details see #18338.
139 g_signals.m_internals->Register(std::move(callbacks));
140}
141
143 // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
144 // is managed by the caller.
146 {callbacks, [](CValidationInterface *) {}});
147}
148
150 std::shared_ptr<CValidationInterface> callbacks) {
151 UnregisterValidationInterface(callbacks.get());
152}
153
156 g_signals.m_internals->Unregister(callbacks);
157 }
158}
159
161 if (!g_signals.m_internals) {
162 return;
163 }
164 g_signals.m_internals->Clear();
165}
166
167void CallFunctionInValidationInterfaceQueue(std::function<void()> func) {
168 g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
169}
170
173 // Block until the validation queue drains
174 std::promise<void> promise;
175 CallFunctionInValidationInterfaceQueue([&promise] { promise.set_value(); });
176 promise.get_future().wait();
177}
178
179// Use a macro instead of a function for conditional logging to prevent
180// evaluating arguments when logging is not enabled.
181//
182// NOTE: The lambda captures all local variables by value.
183#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
184 do { \
185 auto local_name = (name); \
186 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
187 m_internals->m_schedulerClient.AddToProcessQueue([=] { \
188 LOG_EVENT(fmt, local_name, __VA_ARGS__); \
189 event(); \
190 }); \
191 } while (0)
192
193#define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
194
196 const CBlockIndex *pindexFork,
197 bool fInitialDownload) {
198 // Dependencies exist that require UpdatedBlockTip events to be delivered in
199 // the order in which the chain actually updates. One way to ensure this is
200 // for the caller to invoke this signal in the same critical section where
201 // the chain is updated
202
203 auto event = [pindexNew, pindexFork, fInitialDownload, this] {
204 m_internals->Iterate([&](CValidationInterface &callbacks) {
205 callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
206 });
207 };
209 event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
210 pindexNew->GetBlockHash().ToString(),
211 pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
212 fInitialDownload);
213}
214
216 const CTransactionRef &tx,
217 std::shared_ptr<const std::vector<Coin>> spent_coins,
218 uint64_t mempool_sequence) {
219 auto event = [tx, spent_coins, mempool_sequence, this] {
220 m_internals->Iterate([&](CValidationInterface &callbacks) {
221 callbacks.TransactionAddedToMempool(tx, spent_coins,
222 mempool_sequence);
223 });
224 };
225 ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
226 tx->GetHash().ToString());
227}
228
231 uint64_t mempool_sequence) {
232 auto event = [tx, reason, mempool_sequence, this] {
233 m_internals->Iterate([&](CValidationInterface &callbacks) {
234 callbacks.TransactionRemovedFromMempool(tx, reason,
235 mempool_sequence);
236 });
237 };
238 ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s reason=%s", __func__,
239 tx->GetHash().ToString(),
240 RemovalReasonToString(reason));
241}
242
244 const std::shared_ptr<const CBlock> &pblock,
245 const CBlockIndex *pindex) {
246 auto event = [role, pblock, pindex, this] {
247 m_internals->Iterate([&](CValidationInterface &callbacks) {
248 callbacks.BlockConnected(role, pblock, pindex);
249 });
250 };
251 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
252 pblock->GetHash().ToString(), pindex->nHeight);
253}
254
256 const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
257 auto event = [pblock, pindex, this] {
258 m_internals->Iterate([&](CValidationInterface &callbacks) {
259 callbacks.BlockDisconnected(pblock, pindex);
260 });
261 };
262 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
263 pblock->GetHash().ToString());
264}
265
267 const CBlockLocator &locator) {
268 auto event = [role, locator, this] {
269 m_internals->Iterate([&](CValidationInterface &callbacks) {
270 callbacks.ChainStateFlushed(role, locator);
271 });
272 };
273 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
274 locator.IsNull() ? "null"
275 : locator.vHave.front().ToString());
276}
277
279 const BlockValidationState &state) {
280 LOG_EVENT("%s: block hash=%s state=%s", __func__,
281 block.GetHash().ToString(), state.ToString());
282 m_internals->Iterate([&](CValidationInterface &callbacks) {
283 callbacks.BlockChecked(block, state);
284 });
285}
286
288 const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
289 LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
290 m_internals->Iterate([&](CValidationInterface &callbacks) {
291 callbacks.NewPoWValidBlock(pindex, block);
292 });
293}
294
296 auto event = [pindex, this] {
297 m_internals->Iterate([&](CValidationInterface &callbacks) {
298 callbacks.BlockFinalized(pindex);
299 });
300 };
301 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
302 pindex ? pindex->GetBlockHash().ToString() : "null");
303}
304
306 const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
307 auto event = [pindex, block, this] {
308 m_internals->Iterate([&](CValidationInterface &callbacks) {
309 callbacks.BlockInvalidated(pindex, block);
310 });
311 };
312 ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
313 block ? block->GetHash().ToString() : "null");
314}
#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:130
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 UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
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(ChainstateRole, 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(ChainstateRole role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
virtual void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
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 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
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:14
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:109
std::vector< BlockHash > vHave
Definition: block.h:110
bool IsNull() const
Definition: block.h:127
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:158
assert(!tx.IsCoinBase())
#define LOG_EVENT(fmt,...)
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
Definition: txmempool.cpp:1000
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,...)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.