Bitcoin ABC  0.26.3
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 
6 #include <validationinterface.h>
7 
8 #include <chain.h>
9 #include <consensus/validation.h>
10 #include <logging.h>
11 #include <primitives/block.h>
12 #include <primitives/transaction.h>
13 #include <scheduler.h>
14 
15 #include <future>
16 #include <tuple>
17 #include <unordered_map>
18 #include <utility>
19 
28 private:
34  struct ListEntry {
35  std::shared_ptr<CValidationInterface> callbacks;
36  int count = 1;
37  };
38  std::list<ListEntry> m_list GUARDED_BY(m_mutex);
39  std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
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 MainSignalsInstance(CScheduler *pscheduler)
49  : m_schedulerClient(pscheduler) {}
50 
51  void Register(std::shared_ptr<CValidationInterface> callbacks) {
52  LOCK(m_mutex);
53  auto inserted = m_map.emplace(callbacks.get(), m_list.end());
54  if (inserted.second) {
55  inserted.first->second = m_list.emplace(m_list.end());
56  }
57  inserted.first->second->callbacks = std::move(callbacks);
58  }
59 
60  void Unregister(CValidationInterface *callbacks) {
61  LOCK(m_mutex);
62  auto it = m_map.find(callbacks);
63  if (it != m_map.end()) {
64  if (!--it->second->count) {
65  m_list.erase(it->second);
66  }
67  m_map.erase(it);
68  }
69  }
70 
75  void Clear() {
76  LOCK(m_mutex);
77  for (const auto &entry : m_map) {
78  if (!--entry.second->count) {
79  m_list.erase(entry.second);
80  }
81  }
82  m_map.clear();
83  }
84 
85  template <typename F> void Iterate(F &&f) {
86  WAIT_LOCK(m_mutex, lock);
87  for (auto it = m_list.begin(); it != m_list.end();) {
88  ++it->count;
89  {
90  REVERSE_LOCK(lock);
91  f(*it->callbacks);
92  }
93  it = --it->count ? std::next(it) : m_list.erase(it);
94  }
95  }
96 };
97 
99 
102  m_internals.reset(new MainSignalsInstance(&scheduler));
103 }
104 
106  m_internals.reset(nullptr);
107 }
108 
110  if (m_internals) {
111  m_internals->m_schedulerClient.EmptyQueue();
112  }
113 }
114 
116  if (!m_internals) {
117  return 0;
118  }
119  return m_internals->m_schedulerClient.CallbacksPending();
120 }
121 
123  return g_signals;
124 }
125 
127  std::shared_ptr<CValidationInterface> callbacks) {
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  // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
135  // is managed by the caller.
137  {callbacks, [](CValidationInterface *) {}});
138 }
139 
141  std::shared_ptr<CValidationInterface> callbacks) {
142  UnregisterValidationInterface(callbacks.get());
143 }
144 
146  if (g_signals.m_internals) {
147  g_signals.m_internals->Unregister(callbacks);
148  }
149 }
150 
152  if (!g_signals.m_internals) {
153  return;
154  }
155  g_signals.m_internals->Clear();
156 }
157 
158 void CallFunctionInValidationInterfaceQueue(std::function<void()> func) {
159  g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
160 }
161 
164  // Block until the validation queue drains
165  std::promise<void> promise;
166  CallFunctionInValidationInterfaceQueue([&promise] { promise.set_value(); });
167  promise.get_future().wait();
168 }
169 
170 // Use a macro instead of a function for conditional logging to prevent
171 // evaluating arguments when logging is not enabled.
172 //
173 // NOTE: The lambda captures all local variables by value.
174 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
175  do { \
176  auto local_name = (name); \
177  LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
178  m_internals->m_schedulerClient.AddToProcessQueue([=] { \
179  LOG_EVENT(fmt, local_name, __VA_ARGS__); \
180  event(); \
181  }); \
182  } while (0)
183 
184 #define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
185 
187  const CBlockIndex *pindexFork,
188  bool fInitialDownload) {
189  // Dependencies exist that require UpdatedBlockTip events to be delivered in
190  // the order in which the chain actually updates. One way to ensure this is
191  // for the caller to invoke this signal in the same critical section where
192  // the chain is updated
193 
194  auto event = [pindexNew, pindexFork, fInitialDownload, this] {
195  m_internals->Iterate([&](CValidationInterface &callbacks) {
196  callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
197  });
198  };
200  event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
201  pindexNew->GetBlockHash().ToString(),
202  pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
203  fInitialDownload);
204 }
205 
207  uint64_t mempool_sequence) {
208  auto event = [tx, mempool_sequence, this] {
209  m_internals->Iterate([&](CValidationInterface &callbacks) {
210  callbacks.TransactionAddedToMempool(tx, mempool_sequence);
211  });
212  };
213  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
214  tx->GetHash().ToString());
215 }
216 
218  MemPoolRemovalReason reason,
219  uint64_t mempool_sequence) {
220  auto event = [tx, reason, mempool_sequence, this] {
221  m_internals->Iterate([&](CValidationInterface &callbacks) {
222  callbacks.TransactionRemovedFromMempool(tx, reason,
223  mempool_sequence);
224  });
225  };
226  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s", __func__,
227  tx->GetHash().ToString());
228 }
229 
230 void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock,
231  const CBlockIndex *pindex) {
232  auto event = [pblock, pindex, this] {
233  m_internals->Iterate([&](CValidationInterface &callbacks) {
234  callbacks.BlockConnected(pblock, pindex);
235  });
236  };
237  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
238  pblock->GetHash().ToString(), pindex->nHeight);
239 }
240 
242  const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
243  auto event = [pblock, pindex, this] {
244  m_internals->Iterate([&](CValidationInterface &callbacks) {
245  callbacks.BlockDisconnected(pblock, pindex);
246  });
247  };
248  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
249  pblock->GetHash().ToString());
250 }
251 
253  auto event = [locator, this] {
254  m_internals->Iterate([&](CValidationInterface &callbacks) {
255  callbacks.ChainStateFlushed(locator);
256  });
257  };
258  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
259  locator.IsNull() ? "null"
260  : locator.vHave.front().ToString());
261 }
262 
264  const BlockValidationState &state) {
265  LOG_EVENT("%s: block hash=%s state=%s", __func__,
266  block.GetHash().ToString(), state.ToString());
267  m_internals->Iterate([&](CValidationInterface &callbacks) {
268  callbacks.BlockChecked(block, state);
269  });
270 }
271 
273  const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
274  LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
275  m_internals->Iterate([&](CValidationInterface &callbacks) {
276  callbacks.NewPoWValidBlock(pindex, block);
277  });
278 }
279 
281  LOG_EVENT("%s: block hash=%s", __func__,
282  pindex ? pindex->GetBlockHash().ToString() : "null");
283  m_internals->Iterate([&](CValidationInterface &callbacks) {
284  callbacks.BlockFinalized(pindex);
285  });
286 }
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:111
BlockHash GetHash() const
Definition: block.cpp:11
Definition: block.h:55
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
BlockHash GetBlockHash() const
Definition: blockindex.h:147
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
std::unique_ptr< MainSignalsInstance > m_internals
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 &)
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 BlockFinalized(const CBlockIndex *)
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:36
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 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.
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:132
std::string ToString() const
Definition: validation.h:126
std::string ToString() const
Definition: uint256.h:78
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:100
std::vector< BlockHash > vHave
Definition: block.h:101
bool IsNull() const
Definition: block.h:118
List entries consist of a callback pointer and reference count.
int count
std::shared_ptr< CValidationInterface > callbacks
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
std::unordered_map< CValidationInterface *, std::list< ListEntry >::iterator > m_map GUARDED_BY(m_mutex)
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
SingleThreadedSchedulerClient m_schedulerClient
void Unregister(CValidationInterface *callbacks)
void Register(std::shared_ptr< CValidationInterface > callbacks)
void Clear()
Clear unregisters every previously registered callback, erasing every map entry.
MainSignalsInstance(CScheduler *pscheduler)
#define WAIT_LOCK(cs, name)
Definition: sync.h:251
#define AssertLockNotHeld(cs)
Definition: sync.h:97
#define LOCK(cs)
Definition: sync.h:243
#define REVERSE_LOCK(g)
Definition: sync.h:235
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:350
assert(!tx.IsCoinBase())
CMainSignals & GetMainSignals()
#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...
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.