Bitcoin Core  27.1.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 <kernel/chain.h>
12 #include <kernel/mempool_entry.h>
13 #include <logging.h>
14 #include <primitives/block.h>
15 #include <primitives/transaction.h>
16 #include <scheduler.h>
17 
18 #include <future>
19 #include <unordered_map>
20 #include <utility>
21 
22 std::string RemovalReasonToString(const MemPoolRemovalReason& r) noexcept;
23 
33 {
34 private:
40  struct ListEntry { std::shared_ptr<CValidationInterface> callbacks; int count = 1; };
41  std::list<ListEntry> m_list GUARDED_BY(m_mutex);
42  std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map GUARDED_BY(m_mutex);
43 
44 public:
45  // We are not allowed to assume the scheduler only runs in one thread,
46  // but must ensure all callbacks happen in-order, so we end up creating
47  // our own queue here :(
49 
50  explicit MainSignalsImpl(CScheduler& scheduler LIFETIMEBOUND) : m_schedulerClient(scheduler) {}
51 
52  void Register(std::shared_ptr<CValidationInterface> callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
53  {
54  LOCK(m_mutex);
55  auto inserted = m_map.emplace(callbacks.get(), m_list.end());
56  if (inserted.second) inserted.first->second = m_list.emplace(m_list.end());
57  inserted.first->second->callbacks = std::move(callbacks);
58  }
59 
61  {
62  LOCK(m_mutex);
63  auto it = m_map.find(callbacks);
64  if (it != m_map.end()) {
65  if (!--it->second->count) m_list.erase(it->second);
66  m_map.erase(it);
67  }
68  }
69 
75  {
76  LOCK(m_mutex);
77  for (const auto& entry : m_map) {
78  if (!--entry.second->count) m_list.erase(entry.second);
79  }
80  m_map.clear();
81  }
82 
83  template<typename F> void Iterate(F&& f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
84  {
85  WAIT_LOCK(m_mutex, lock);
86  for (auto it = m_list.begin(); it != m_list.end();) {
87  ++it->count;
88  {
89  REVERSE_LOCK(lock);
90  f(*it->callbacks);
91  }
92  it = --it->count ? std::next(it) : m_list.erase(it);
93  }
94  }
95 };
96 
98 
100 {
102  m_internals = std::make_unique<MainSignalsImpl>(scheduler);
103 }
104 
106 {
107  m_internals.reset(nullptr);
108 }
109 
111 {
112  if (m_internals) {
113  m_internals->m_schedulerClient.EmptyQueue();
114  }
115 }
116 
118 {
119  if (!m_internals) return 0;
120  return m_internals->m_schedulerClient.CallbacksPending();
121 }
122 
124 {
125  return g_signals;
126 }
127 
128 void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
129 {
130  // Each connection captures the shared_ptr to ensure that each callback is
131  // executed before the subscriber is destroyed. For more details see #18338.
132  g_signals.m_internals->Register(std::move(callbacks));
133 }
134 
136 {
137  // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
138  // is managed by the caller.
140 }
141 
142 void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
143 {
144  UnregisterValidationInterface(callbacks.get());
145 }
146 
148 {
149  if (g_signals.m_internals) {
150  g_signals.m_internals->Unregister(callbacks);
151  }
152 }
153 
155 {
156  if (!g_signals.m_internals) {
157  return;
158  }
159  g_signals.m_internals->Clear();
160 }
161 
162 void CallFunctionInValidationInterfaceQueue(std::function<void()> func)
163 {
164  g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
165 }
166 
168 {
170  // Block until the validation queue drains
171  std::promise<void> promise;
173  promise.set_value();
174  });
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, ...) \
193  LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
194 
195 void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
196  // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
197  // the chain actually updates. One way to ensure this is for the caller to invoke this signal
198  // in the same critical section where the chain is updated
199 
200  auto event = [pindexNew, pindexFork, fInitialDownload, this] {
201  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload); });
202  };
203  ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
204  pindexNew->GetBlockHash().ToString(),
205  pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
206  fInitialDownload);
207 }
208 
209 void CMainSignals::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence)
210 {
211  auto event = [tx, mempool_sequence, this] {
212  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); });
213  };
214  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
215  tx.info.m_tx->GetHash().ToString(),
216  tx.info.m_tx->GetWitnessHash().ToString());
217 }
218 
219 void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {
220  auto event = [tx, reason, mempool_sequence, this] {
221  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); });
222  };
223  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s reason=%s", __func__,
224  tx->GetHash().ToString(),
225  tx->GetWitnessHash().ToString(),
226  RemovalReasonToString(reason));
227 }
228 
229 void CMainSignals::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
230  auto event = [role, pblock, pindex, this] {
231  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockConnected(role, pblock, pindex); });
232  };
233  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
234  pblock->GetHash().ToString(),
235  pindex->nHeight);
236 }
237 
238 void CMainSignals::MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight)
239 {
240  auto event = [txs_removed_for_block, nBlockHeight, this] {
241  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight); });
242  };
243  ENQUEUE_AND_LOG_EVENT(event, "%s: block height=%s txs removed=%s", __func__,
244  nBlockHeight,
245  txs_removed_for_block.size());
246 }
247 
248 void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
249 {
250  auto event = [pblock, pindex, this] {
251  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockDisconnected(pblock, pindex); });
252  };
253  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
254  pblock->GetHash().ToString(),
255  pindex->nHeight);
256 }
257 
259  auto event = [role, locator, this] {
260  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ChainStateFlushed(role, locator); });
261  };
262  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
263  locator.IsNull() ? "null" : locator.vHave.front().ToString());
264 }
265 
266 void CMainSignals::BlockChecked(const CBlock& block, const BlockValidationState& state) {
267  LOG_EVENT("%s: block hash=%s state=%s", __func__,
268  block.GetHash().ToString(), state.ToString());
269  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockChecked(block, state); });
270 }
271 
272 void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
273  LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
274  m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
275 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
Definition: scheduler.h:123
std::unique_ptr< MainSignalsImpl > m_internals
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
assert(!tx.IsCoinBase())
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:123
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected Provides the block that was disconnected.
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: block.h:68
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
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...
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
#define REVERSE_LOCK(g)
Definition: sync.h:243
int count
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
MainSignalsImpl(CScheduler &scheduler LIFETIMEBOUND)
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Implement this to subscribe to events generated in validation and mempool.
void Iterate(F &&f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
uint256 GetBlockHash() const
Definition: chain.h:258
#define LIFETIMEBOUND
Definition: attributes.h:16
static CMainSignals g_signals
void Unregister(CValidationInterface *callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:25
#define LOG_EVENT(fmt,...)
void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &, unsigned int nBlockHeight)
#define LOCK(cs)
Definition: sync.h:257
std::string ToString() const
Definition: validation.h:128
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Clear unregisters every previously registered callback, erasing every map entry.
CMainSignals & GetMainSignals()
virtual void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight)
#define WAIT_LOCK(cs, name)
Definition: sync.h:262
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
std::string ToString() const
Definition: uint256.cpp:55
std::shared_ptr< CValidationInterface > callbacks
uint256 GetHash() const
Definition: block.cpp:11
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once) ...
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
void Register(std::shared_ptr< CValidationInterface > callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
SingleThreadedSchedulerClient m_schedulerClient
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:149
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
virtual void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
const CTransactionRef m_tx
#define AssertLockNotHeld(cs)
Definition: sync.h:147
virtual void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
MainSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:38
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockChecked(const CBlock &, const BlockValidationState &)
List entries consist of a callback pointer and reference count.
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)