Bitcoin Core  24.99.0
P2P Digital Currency
validationinterface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 
6 #include <validationinterface.h>
7 
8 #include <attributes.h>
9 #include <chain.h>
10 #include <consensus/validation.h>
11 #include <logging.h>
12 #include <primitives/block.h>
13 #include <primitives/transaction.h>
14 #include <scheduler.h>
15 
16 #include <future>
17 #include <unordered_map>
18 #include <utility>
19 
20 std::string RemovalReasonToString(const MemPoolRemovalReason& r) noexcept;
21 
31 {
32 private:
38  struct ListEntry { std::shared_ptr<CValidationInterface> callbacks; int count = 1; };
39  std::list<ListEntry> m_list GUARDED_BY(m_mutex);
40  std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map GUARDED_BY(m_mutex);
41 
42 public:
43  // We are not allowed to assume the scheduler only runs in one thread,
44  // but must ensure all callbacks happen in-order, so we end up creating
45  // our own queue here :(
47 
48  explicit MainSignalsImpl(CScheduler& scheduler LIFETIMEBOUND) : m_schedulerClient(scheduler) {}
49 
50  void Register(std::shared_ptr<CValidationInterface> callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
51  {
52  LOCK(m_mutex);
53  auto inserted = m_map.emplace(callbacks.get(), m_list.end());
54  if (inserted.second) inserted.first->second = m_list.emplace(m_list.end());
55  inserted.first->second->callbacks = std::move(callbacks);
56  }
57 
59  {
60  LOCK(m_mutex);
61  auto it = m_map.find(callbacks);
62  if (it != m_map.end()) {
63  if (!--it->second->count) m_list.erase(it->second);
64  m_map.erase(it);
65  }
66  }
67 
73  {
74  LOCK(m_mutex);
75  for (const auto& entry : m_map) {
76  if (!--entry.second->count) m_list.erase(entry.second);
77  }
78  m_map.clear();
79  }
80 
81  template<typename F> void Iterate(F&& f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
82  {
83  WAIT_LOCK(m_mutex, lock);
84  for (auto it = m_list.begin(); it != m_list.end();) {
85  ++it->count;
86  {
87  REVERSE_LOCK(lock);
88  f(*it->callbacks);
89  }
90  it = --it->count ? std::next(it) : m_list.erase(it);
91  }
92  }
93 };
94 
96 
98 {
100  m_internals = std::make_unique<MainSignalsImpl>(scheduler);
101 }
102 
104 {
105  m_internals.reset(nullptr);
106 }
107 
109 {
110  if (m_internals) {
111  m_internals->m_schedulerClient.EmptyQueue();
112  }
113 }
114 
116 {
117  if (!m_internals) return 0;
118  return m_internals->m_schedulerClient.CallbacksPending();
119 }
120 
122 {
123  return g_signals;
124 }
125 
126 void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
127 {
128  // Each connection captures the shared_ptr to ensure that each callback is
129  // executed before the subscriber is destroyed. For more details see #18338.
130  g_signals.m_internals->Register(std::move(callbacks));
131 }
132 
134 {
135  // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
136  // is managed by the caller.
138 }
139 
140 void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
141 {
142  UnregisterValidationInterface(callbacks.get());
143 }
144 
146 {
147  if (g_signals.m_internals) {
148  g_signals.m_internals->Unregister(callbacks);
149  }
150 }
151 
153 {
154  if (!g_signals.m_internals) {
155  return;
156  }
157  g_signals.m_internals->Clear();
158 }
159 
160 void CallFunctionInValidationInterfaceQueue(std::function<void()> func)
161 {
162  g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
163 }
164 
166 {
168  // Block until the validation queue drains
169  std::promise<void> promise;
171  promise.set_value();
172  });
173  promise.get_future().wait();
174 }
175 
176 // Use a macro instead of a function for conditional logging to prevent
177 // evaluating arguments when logging is not enabled.
178 //
179 // NOTE: The lambda captures all local variables by value.
180 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
181  do { \
182  auto local_name = (name); \
183  LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
184  m_internals->m_schedulerClient.AddToProcessQueue([=] { \
185  LOG_EVENT(fmt, local_name, __VA_ARGS__); \
186  event(); \
187  }); \
188  } while (0)
189 
190 #define LOG_EVENT(fmt, ...) \
191  LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
192 
193 void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
194  // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
195  // the chain actually updates. One way to ensure this is for the caller to invoke this signal
196  // in the same critical section where the chain is updated
197 
198  auto event = [pindexNew, pindexFork, fInitialDownload, this] {
199  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload); });
200  };
201  ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
202  pindexNew->GetBlockHash().ToString(),
203  pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
204  fInitialDownload);
205 }
206 
207 void CMainSignals::TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {
208  auto event = [tx, mempool_sequence, this] {
209  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); });
210  };
211  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
212  tx->GetHash().ToString(),
213  tx->GetWitnessHash().ToString());
214 }
215 
216 void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {
217  auto event = [tx, reason, mempool_sequence, this] {
218  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); });
219  };
220  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s reason=%s", __func__,
221  tx->GetHash().ToString(),
222  tx->GetWitnessHash().ToString(),
223  RemovalReasonToString(reason));
224 }
225 
226 void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
227  auto event = [pblock, pindex, this] {
228  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockConnected(pblock, pindex); });
229  };
230  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
231  pblock->GetHash().ToString(),
232  pindex->nHeight);
233 }
234 
235 void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
236 {
237  auto event = [pblock, pindex, this] {
238  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockDisconnected(pblock, pindex); });
239  };
240  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
241  pblock->GetHash().ToString(),
242  pindex->nHeight);
243 }
244 
246  auto event = [locator, this] {
247  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ChainStateFlushed(locator); });
248  };
249  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
250  locator.IsNull() ? "null" : locator.vHave.front().ToString());
251 }
252 
253 void CMainSignals::BlockChecked(const CBlock& block, const BlockValidationState& state) {
254  LOG_EVENT("%s: block hash=%s state=%s", __func__,
255  block.GetHash().ToString(), state.ToString());
256  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockChecked(block, state); });
257 }
258 
259 void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
260  LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
261  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
262 }
#define LIFETIMEBOUND
Definition: attributes.h:16
uint256 GetHash() const
Definition: block.cpp:11
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
uint256 GetBlockHash() const
Definition: chain.h:259
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:163
void TransactionAddedToMempool(const CTransactionRef &, 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 ChainStateFlushed(const CBlockLocator &)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:39
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 TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
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 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.
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.
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
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
MainSignalsImpl(CScheduler &scheduler LIFETIMEBOUND)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:124
std::string ToString() const
Definition: validation.h:127
std::string ToString() const
Definition: uint256.cpp:55
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:121
std::vector< uint256 > vHave
Definition: block.h:122
bool IsNull() const
Definition: block.h:141
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:263
#define AssertLockNotHeld(cs)
Definition: sync.h:148
#define LOCK(cs)
Definition: sync.h:258
#define REVERSE_LOCK(g)
Definition: sync.h:244
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:231
assert(!tx.IsCoinBase())
CMainSignals & GetMainSignals()
#define LOG_EVENT(fmt,...)
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
Definition: txmempool.cpp:1145
static CMainSignals g_signals
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
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.