Bitcoin Core  0.19.99
P2P Digital Currency
validationinterface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <util/validation.h>
15 
16 #include <future>
17 #include <unordered_map>
18 #include <utility>
19 
20 #include <boost/signals2/signal.hpp>
21 
23  boost::signals2::scoped_connection UpdatedBlockTip;
24  boost::signals2::scoped_connection TransactionAddedToMempool;
25  boost::signals2::scoped_connection BlockConnected;
26  boost::signals2::scoped_connection BlockDisconnected;
27  boost::signals2::scoped_connection TransactionRemovedFromMempool;
28  boost::signals2::scoped_connection ChainStateFlushed;
29  boost::signals2::scoped_connection BlockChecked;
30  boost::signals2::scoped_connection NewPoWValidBlock;
31 };
32 
34  boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
35  boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
36  boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
37  boost::signals2::signal<void (const std::shared_ptr<const CBlock>&, const CBlockIndex* pindex)> BlockDisconnected;
38  boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
39  boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
40  boost::signals2::signal<void (const CBlock&, const BlockValidationState&)> BlockChecked;
41  boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
42 
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  std::unordered_map<CValidationInterface*, ValidationInterfaceConnections> m_connMainSignals;
48 
49  explicit MainSignalsInstance(CScheduler *pscheduler) : m_schedulerClient(pscheduler) {}
50 };
51 
53 
55  assert(!m_internals);
56  m_internals.reset(new MainSignalsInstance(&scheduler));
57 }
58 
60  m_internals.reset(nullptr);
61 }
62 
64  if (m_internals) {
65  m_internals->m_schedulerClient.EmptyQueue();
66  }
67 }
68 
70  if (!m_internals) return 0;
71  return m_internals->m_schedulerClient.CallbacksPending();
72 }
73 
75 {
76  return g_signals;
77 }
78 
80  ValidationInterfaceConnections& conns = g_signals.m_internals->m_connMainSignals[pwalletIn];
81  conns.UpdatedBlockTip = g_signals.m_internals->UpdatedBlockTip.connect(std::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
82  conns.TransactionAddedToMempool = g_signals.m_internals->TransactionAddedToMempool.connect(std::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, std::placeholders::_1));
83  conns.BlockConnected = g_signals.m_internals->BlockConnected.connect(std::bind(&CValidationInterface::BlockConnected, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
84  conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1, std::placeholders::_2));
85  conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1));
86  conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1));
87  conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
88  conns.NewPoWValidBlock = g_signals.m_internals->NewPoWValidBlock.connect(std::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, std::placeholders::_1, std::placeholders::_2));
89 }
90 
92  if (g_signals.m_internals) {
93  g_signals.m_internals->m_connMainSignals.erase(pwalletIn);
94  }
95 }
96 
98  if (!g_signals.m_internals) {
99  return;
100  }
101  g_signals.m_internals->m_connMainSignals.clear();
102 }
103 
104 void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
105  g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
106 }
107 
110  // Block until the validation queue drains
111  std::promise<void> promise;
113  promise.set_value();
114  });
115  promise.get_future().wait();
116 }
117 
118 // Use a macro instead of a function for conditional logging to prevent
119 // evaluating arguments when logging is not enabled.
120 //
121 // NOTE: The lambda captures all local variables by value.
122 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
123  do { \
124  auto local_name = (name); \
125  LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
126  m_internals->m_schedulerClient.AddToProcessQueue([=] { \
127  LOG_EVENT(fmt, local_name, __VA_ARGS__); \
128  event(); \
129  }); \
130  } while (0)
131 
132 #define LOG_EVENT(fmt, ...) \
133  LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
134 
135 void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
136  // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
137  // the chain actually updates. One way to ensure this is for the caller to invoke this signal
138  // in the same critical section where the chain is updated
139 
140  auto event = [pindexNew, pindexFork, fInitialDownload, this] {
141  m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
142  };
143  ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
144  pindexNew->GetBlockHash().ToString(),
145  pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
146  fInitialDownload);
147 }
148 
150  auto event = [ptx, this] {
151  m_internals->TransactionAddedToMempool(ptx);
152  };
153  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
154  ptx->GetHash().ToString(),
155  ptx->GetWitnessHash().ToString());
156 }
157 
159  auto event = [ptx, this] {
160  m_internals->TransactionRemovedFromMempool(ptx);
161  };
162  ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
163  ptx->GetHash().ToString(),
164  ptx->GetWitnessHash().ToString());
165 }
166 
167 void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>>& pvtxConflicted) {
168  auto event = [pblock, pindex, pvtxConflicted, this] {
169  m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
170  };
171  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
172  pblock->GetHash().ToString(),
173  pindex->nHeight);
174 }
175 
176 void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
177 {
178  auto event = [pblock, pindex, this] {
179  m_internals->BlockDisconnected(pblock, pindex);
180  };
181  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
182  pblock->GetHash().ToString(),
183  pindex->nHeight);
184 }
185 
187  auto event = [locator, this] {
188  m_internals->ChainStateFlushed(locator);
189  };
190  ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
191  locator.IsNull() ? "null" : locator.vHave.front().ToString());
192 }
193 
194 void CMainSignals::BlockChecked(const CBlock& block, const BlockValidationState& state) {
195  LOG_EVENT("%s: block hash=%s state=%s", __func__,
196  block.GetHash().ToString(), FormatStateMessage(state));
197  m_internals->BlockChecked(block, state);
198 }
199 
200 void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
201  LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
202  m_internals->NewPoWValidBlock(pindex, block);
203 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
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:97
boost::signals2::signal< void(const CTransactionRef &)> TransactionAddedToMempool
std::unique_ptr< MainSignalsInstance > m_internals
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
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:126
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
boost::signals2::scoped_connection BlockChecked
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: block.h:72
boost::signals2::signal< void(const CBlock &, const BlockValidationState &)> BlockChecked
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...
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
void TransactionRemovedFromMempool(const CTransactionRef &)
void UnregisterAllValidationInterfaces()
Unregister all wallets from core.
bool IsNull() const
Definition: block.h:149
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
boost::signals2::scoped_connection NewPoWValidBlock
static CScheduler scheduler
Definition: init.cpp:160
boost::signals2::scoped_connection BlockDisconnected
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
Implement this to subscribe to events generated in validation.
uint256 GetBlockHash() const
Definition: chain.h:233
std::string FormatStateMessage(const ValidationState &state)
Convert ValidationState to a human-readable message for logging.
Definition: validation.cpp:11
static CMainSignals g_signals
boost::signals2::scoped_connection UpdatedBlockTip
#define LOG_EVENT(fmt,...)
std::unordered_map< CValidationInterface *, ValidationInterfaceConnections > m_connMainSignals
boost::signals2::scoped_connection ChainStateFlushed
SingleThreadedSchedulerClient m_schedulerClient
MainSignalsInstance(CScheduler *pscheduler)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:106
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
CMainSignals & GetMainSignals()
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:60
std::vector< uint256 > vHave
Definition: block.h:128
void ChainStateFlushed(const CBlockLocator &)
boost::signals2::scoped_connection TransactionAddedToMempool
boost::signals2::scoped_connection TransactionRemovedFromMempool
void RegisterValidationInterface(CValidationInterface *pwalletIn)
Register a wallet to receive updates from core.
uint256 GetHash() const
Definition: block.cpp:11
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once) ...
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
boost::signals2::scoped_connection BlockConnected
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx)
Notifies listeners of a transaction leaving mempool.
#define AssertLockNotHeld(cs)
Definition: sync.h:72
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn)
Notifies listeners of a transaction having been added to mempool.
boost::signals2::signal< void(const CBlockLocator &)> ChainStateFlushed
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex, const std::vector< CTransactionRef > &txnConflicted)
Notifies listeners of a block being connected.
boost::signals2::signal< void(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockChecked(const CBlock &, const BlockValidationState &)
boost::signals2::signal< void(const CTransactionRef &)> TransactionRemovedFromMempool
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex, const std::shared_ptr< const std::vector< CTransactionRef >> &)
void TransactionAddedToMempool(const CTransactionRef &)