Bitcoin ABC  0.24.7
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>
40  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 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 
101  assert(!m_internals);
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 }
CValidationInterface
Implement this to subscribe to events generated in validation.
Definition: validationinterface.h:83
CValidationInterface::TransactionRemovedFromMempool
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
Definition: validationinterface.h:143
block.h
CMainSignals::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: validationinterface.cpp:230
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:35
CMainSignals::CallbacksPending
size_t CallbacksPending()
Definition: validationinterface.cpp:115
CValidationInterface::NewPoWValidBlock
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...
Definition: validationinterface.h:190
RegisterSharedValidationInterface
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
Definition: validationinterface.cpp:126
MainSignalsInstance::Unregister
void Unregister(CValidationInterface *callbacks)
Definition: validationinterface.cpp:60
BlockValidationState
Definition: validation.h:138
MainSignalsInstance::MainSignalsInstance
MainSignalsInstance(CScheduler *pscheduler)
Definition: validationinterface.cpp:48
MainSignalsInstance
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
Definition: validationinterface.cpp:27
REVERSE_LOCK
#define REVERSE_LOCK(g)
Definition: sync.h:233
transaction.h
CValidationInterface::BlockDisconnected
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
Definition: validationinterface.h:159
MainSignalsInstance::m_schedulerClient
SingleThreadedSchedulerClient m_schedulerClient
Definition: validationinterface.cpp:46
CBlockLocator::vHave
std::vector< BlockHash > vHave
Definition: block.h:101
validationinterface.h
CBlockLocator::IsNull
bool IsNull() const
Definition: block.h:118
validation.h
AnnotatedMixin< std::mutex >
CMainSignals::BlockChecked
void BlockChecked(const CBlock &, const BlockValidationState &)
Definition: validationinterface.cpp:263
g_signals
static CMainSignals g_signals
Definition: validationinterface.cpp:98
CMainSignals::BlockDisconnected
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: validationinterface.cpp:241
scheduler.h
CBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: blockindex.h:133
GetMainSignals
CMainSignals & GetMainSignals()
Definition: validationinterface.cpp:122
ValidationState::ToString
std::string ToString() const
Definition: validation.h:124
MainSignalsInstance::Iterate
void Iterate(F &&f)
Definition: validationinterface.cpp:85
CMainSignals::UpdatedBlockTip
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
Definition: validationinterface.cpp:186
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:162
CValidationInterface::BlockConnected
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
Definition: validationinterface.h:152
AssertLockNotHeld
#define AssertLockNotHeld(cs)
Definition: sync.h:92
MainSignalsInstance::Clear
void Clear()
Clear unregisters every previously registered callback, erasing every map entry.
Definition: validationinterface.cpp:75
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
ENQUEUE_AND_LOG_EVENT
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
Definition: validationinterface.cpp:174
MainSignalsInstance::ListEntry
List entries consist of a callback pointer and reference count.
Definition: validationinterface.cpp:34
MainSignalsInstance::Register
void Register(std::shared_ptr< CValidationInterface > callbacks)
Definition: validationinterface.cpp:51
CValidationInterface::ChainStateFlushed
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
Definition: validationinterface.h:177
CMainSignals::TransactionRemovedFromMempool
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
Definition: validationinterface.cpp:217
CMainSignals::FlushBackgroundCallbacks
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
Definition: validationinterface.cpp:109
UnregisterSharedValidationInterface
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:140
CValidationInterface::TransactionAddedToMempool
virtual void TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
Definition: validationinterface.h:108
CMainSignals::RegisterBackgroundSignalScheduler
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once)
Definition: validationinterface.cpp:100
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
CMainSignals::UnregisterBackgroundSignalScheduler
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
Definition: validationinterface.cpp:105
chain.h
UnregisterValidationInterface
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:145
CMainSignals::TransactionAddedToMempool
void TransactionAddedToMempool(const CTransactionRef &, uint64_t mempool_sequence)
Definition: validationinterface.cpp:206
UnregisterAllValidationInterfaces
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
Definition: validationinterface.cpp:151
LOG_EVENT
#define LOG_EVENT(fmt,...)
Definition: validationinterface.cpp:184
CBlock
Definition: block.h:55
MainSignalsInstance::m_mutex
Mutex m_mutex
Definition: validationinterface.cpp:29
LOCK
#define LOCK(cs)
Definition: sync.h:241
MemPoolRemovalReason
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:400
RegisterValidationInterface
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
Definition: validationinterface.cpp:133
MainSignalsInstance::GUARDED_BY
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
MainSignalsInstance::ListEntry::count
int count
Definition: validationinterface.cpp:36
logging.h
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CMainSignals
Definition: validationinterface.h:196
SingleThreadedSchedulerClient
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:124
CMainSignals::m_internals
std::unique_ptr< MainSignalsInstance > m_internals
Definition: validationinterface.h:198
CMainSignals::NewPoWValidBlock
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
Definition: validationinterface.cpp:272
CBlockLocator
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
CMainSignals::ChainStateFlushed
void ChainStateFlushed(const CBlockLocator &)
Definition: validationinterface.cpp:252
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CallFunctionInValidationInterfaceQueue
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
Definition: validationinterface.cpp:158
CBlockHeader::GetHash
BlockHash GetHash() const
Definition: block.cpp:11
CValidationInterface::BlockChecked
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
Definition: validationinterface.h:184
CValidationInterface::UpdatedBlockTip
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
Definition: validationinterface.h:100
WAIT_LOCK
#define WAIT_LOCK(cs, name)
Definition: sync.h:249
MainSignalsInstance::ListEntry::callbacks
std::shared_ptr< CValidationInterface > callbacks
Definition: validationinterface.cpp:35