BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
validationinterface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <primitives/block.h>
9 #include <scheduler.h>
10 #include <txmempool.h>
11 #include <util.h>
12 #include <validation.h>
13 
14 #include <list>
15 #include <atomic>
16 #include <future>
17 
18 #include <boost/signals2/signal.hpp>
19 
21  boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
22  boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
23  boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
24  boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
25  boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
26  boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
27  boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
28  boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
29  boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
30  boost::signals2::signal<void (const uint256 &)> BlockFound;
31 
32  // We are not allowed to assume the scheduler only runs in one thread,
33  // but must ensure all callbacks happen in-order, so we end up creating
34  // our own queue here :(
36 
37  explicit MainSignalsInstance(CScheduler *pscheduler) : m_schedulerClient(pscheduler) {}
38 };
39 
40 static CMainSignals g_signals;
41 
43  assert(!m_internals);
44  m_internals.reset(new MainSignalsInstance(&scheduler));
45 }
46 
48  m_internals.reset(nullptr);
49 }
50 
52  if (m_internals) {
53  m_internals->m_schedulerClient.EmptyQueue();
54  }
55 }
56 
58  if (!m_internals) return 0;
59  return m_internals->m_schedulerClient.CallbacksPending();
60 }
61 
63  pool.NotifyEntryRemoved.connect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
64 }
65 
67  pool.NotifyEntryRemoved.disconnect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
68 }
69 
71 {
72  return g_signals;
73 }
74 
76  g_signals.m_internals->UpdatedBlockTip.connect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
77  g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
78  g_signals.m_internals->BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
79  g_signals.m_internals->BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
80  g_signals.m_internals->TransactionRemovedFromMempool.connect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
81  g_signals.m_internals->ChainStateFlushed.connect(boost::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, _1));
82  g_signals.m_internals->Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
83  g_signals.m_internals->BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
84  g_signals.m_internals->NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
85  g_signals.m_internals->BlockFound.connect(boost::bind(&CValidationInterface::BlockFound, pwalletIn, _1));
86 }
87 
89  g_signals.m_internals->BlockChecked.disconnect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
90  g_signals.m_internals->Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
91  g_signals.m_internals->ChainStateFlushed.disconnect(boost::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, _1));
92  g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
93  g_signals.m_internals->BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
94  g_signals.m_internals->BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
95  g_signals.m_internals->TransactionRemovedFromMempool.disconnect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
96  g_signals.m_internals->UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
97  g_signals.m_internals->NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
98  g_signals.m_internals->BlockFound.disconnect(boost::bind(&CValidationInterface::BlockFound, pwalletIn, _1));
99 }
100 
102  if (!g_signals.m_internals) {
103  return;
104  }
105  g_signals.m_internals->BlockChecked.disconnect_all_slots();
106  g_signals.m_internals->Broadcast.disconnect_all_slots();
107  g_signals.m_internals->ChainStateFlushed.disconnect_all_slots();
108  g_signals.m_internals->TransactionAddedToMempool.disconnect_all_slots();
109  g_signals.m_internals->BlockConnected.disconnect_all_slots();
110  g_signals.m_internals->BlockDisconnected.disconnect_all_slots();
111  g_signals.m_internals->TransactionRemovedFromMempool.disconnect_all_slots();
112  g_signals.m_internals->UpdatedBlockTip.disconnect_all_slots();
113  g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
114  g_signals.m_internals->BlockFound.disconnect_all_slots();
115 }
116 
117 void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
118  g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
119 }
120 
123  // Block until the validation queue drains
124  std::promise<void> promise;
126  promise.set_value();
127  });
128  promise.get_future().wait();
129 }
130 
132  if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
133  m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
134  m_internals->TransactionRemovedFromMempool(ptx);
135  });
136  }
137 }
138 
139 void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
140  // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
141  // the chain actually updates. One way to ensure this is for the caller to invoke this signal
142  // in the same critical section where the chain is updated
143 
144  m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] {
145  m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
146  });
147 }
148 
150  m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
151  m_internals->TransactionAddedToMempool(ptx);
152  });
153 }
154 
155 void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>>& pvtxConflicted) {
156  m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, pvtxConflicted, this] {
157  m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);
158  });
159 }
160 
161 void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &pblock) {
162  m_internals->m_schedulerClient.AddToProcessQueue([pblock, this] {
163  m_internals->BlockDisconnected(pblock);
164  });
165 }
166 
168  m_internals->m_schedulerClient.AddToProcessQueue([locator, this] {
169  m_internals->ChainStateFlushed(locator);
170  });
171 }
172 
173 void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) {
174  m_internals->Broadcast(nBestBlockTime, connman);
175 }
176 
177 void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {
178  m_internals->BlockChecked(block, state);
179 }
180 
181 void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
182  m_internals->NewPoWValidBlock(pindex, block);
183 }
184 
186  m_internals->BlockFound(hash);
187 }
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...
virtual void BlockChecked(const CBlock &, const CValidationState &)
Notifies listeners of a block validation result.
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:128
boost::signals2::signal< void(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex, const std::vector< CTransactionRef > &)> BlockConnected
Definition: block.h:74
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman *connman)
Tells listeners to broadcast their data.
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...
boost::signals2::signal< void(const CBlock &, const CValidationState &)> BlockChecked
void BlockFound(const uint256 &)
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:345
void UnregisterAllValidationInterfaces()
Unregister all wallets from core.
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
void Broadcast(int64_t nBestBlockTime, CConnman *connman)
void BlockChecked(const CBlock &, const CValidationState &)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
Implement this to subscribe to events generated in validation.
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block)
Notifies listeners of a block being disconnected.
CCriticalSection cs_main
Definition: validation.cpp:216
Removed for conflict with in-block transaction.
Removed for block.
SingleThreadedSchedulerClient m_schedulerClient
MainSignalsInstance(CScheduler *pscheduler)
boost::signals2::signal< void(const CBlockIndex *, const std::shared_ptr< const CBlock > &)> NewPoWValidBlock
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
void RegisterWithMempoolSignals(CTxMemPool &pool)
Register with mempool to call TransactionRemovedFromMempool callbacks.
CMainSignals & GetMainSignals()
void UnregisterWithMempoolSignals(CTxMemPool &pool)
Unregister with mempool.
Definition: net.h:115
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
void ChainStateFlushed(const CBlockLocator &)
void RegisterValidationInterface(CValidationInterface *pwalletIn)
Register a wallet to receive updates from core.
virtual void BlockFound(const uint256 &hash)
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once) ...
Capture information about block/transaction validation.
Definition: validation.h:26
256-bit opaque blob.
Definition: uint256.h:122
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:441
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason)
boost::signals2::signal< void(const uint256 &)> BlockFound
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx)
Notifies listeners of a transaction leaving mempool.
boost::signals2::signal< void(const std::shared_ptr< const CBlock > &)> BlockDisconnected
#define AssertLockNotHeld(cs)
Definition: sync.h:71
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(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
Definition: txmempool.h:663
void BlockDisconnected(const std::shared_ptr< const CBlock > &)
boost::signals2::signal< void(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
boost::signals2::signal< void(int64_t nBestBlockTime, CConnman *connman)> Broadcast
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 &)