/tmp/bitcoin/src/validationinterface.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 <kernel/mempool_entry.h> |
11 | | #include <kernel/mempool_removal_reason.h> |
12 | | #include <kernel/types.h> |
13 | | #include <primitives/block.h> |
14 | | #include <primitives/transaction.h> |
15 | | #include <util/check.h> |
16 | | #include <util/log.h> |
17 | | #include <util/task_runner.h> |
18 | | |
19 | | #include <future> |
20 | | #include <memory> |
21 | | #include <unordered_map> |
22 | | #include <utility> |
23 | | |
24 | | using kernel::ChainstateRole; |
25 | | |
26 | | /** |
27 | | * ValidationSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks. |
28 | | * |
29 | | * A std::unordered_map is used to track what callbacks are currently |
30 | | * registered, and a std::list is used to store the callbacks that are |
31 | | * currently registered as well as any callbacks that are just unregistered |
32 | | * and about to be deleted when they are done executing. |
33 | | */ |
34 | | class ValidationSignalsImpl |
35 | | { |
36 | | private: |
37 | | Mutex m_mutex; |
38 | | //! List entries consist of a callback pointer and reference count. The |
39 | | //! count is equal to the number of current executions of that entry, plus 1 |
40 | | //! if it's registered. It cannot be 0 because that would imply it is |
41 | | //! unregistered and also not being executed (so shouldn't exist). |
42 | | struct ListEntry { std::shared_ptr<CValidationInterface> callbacks; int count = 1; }; |
43 | | std::list<ListEntry> m_list GUARDED_BY(m_mutex); |
44 | | std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map GUARDED_BY(m_mutex); |
45 | | |
46 | | public: |
47 | | std::unique_ptr<util::TaskRunnerInterface> m_task_runner; |
48 | | |
49 | | explicit ValidationSignalsImpl(std::unique_ptr<util::TaskRunnerInterface> task_runner) |
50 | 1.26k | : m_task_runner{std::move(Assert(task_runner))} {} |
51 | | |
52 | | void Register(std::shared_ptr<CValidationInterface> callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
53 | 259k | { |
54 | 259k | LOCK(m_mutex); |
55 | 259k | auto inserted = m_map.emplace(callbacks.get(), m_list.end()); |
56 | 259k | if (inserted.second) inserted.first->second = m_list.emplace(m_list.end()); |
57 | 259k | inserted.first->second->callbacks = std::move(callbacks); |
58 | 259k | } |
59 | | |
60 | | void Unregister(CValidationInterface* callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
61 | 259k | { |
62 | 259k | LOCK(m_mutex); |
63 | 259k | auto it = m_map.find(callbacks); |
64 | 259k | if (it != m_map.end()) { |
65 | 259k | if (!--it->second->count) m_list.erase(it->second); |
66 | 259k | m_map.erase(it); |
67 | 259k | } |
68 | 259k | } |
69 | | |
70 | | //! Clear unregisters every previously registered callback, erasing every |
71 | | //! map entry. After this call, the list may still contain callbacks that |
72 | | //! are currently executing, but it will be cleared when they are done |
73 | | //! executing. |
74 | | void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
75 | 1.11k | { |
76 | 1.11k | LOCK(m_mutex); |
77 | 1.11k | for (const auto& entry : m_map) { |
78 | 1 | if (!--entry.second->count) m_list.erase(entry.second); |
79 | 1 | } |
80 | 1.11k | m_map.clear(); |
81 | 1.11k | } |
82 | | |
83 | | template<typename F> void Iterate(F&& f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) |
84 | 693k | { |
85 | 693k | WAIT_LOCK(m_mutex, lock); |
86 | 2.18M | for (auto it = m_list.begin(); it != m_list.end();) { |
87 | 1.49M | ++it->count; |
88 | 1.49M | { |
89 | 1.49M | REVERSE_LOCK(lock, m_mutex); |
90 | 1.49M | f(*it->callbacks); |
91 | 1.49M | } |
92 | 1.49M | it = --it->count ? std::next(it) : m_list.erase(it); |
93 | 1.49M | } |
94 | 693k | } validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::UpdatedBlockTip(CBlockIndex const*, CBlockIndex const*, bool)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::UpdatedBlockTip(CBlockIndex const*, CBlockIndex const*, bool)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 93.5k | { | 85 | 93.5k | WAIT_LOCK(m_mutex, lock); | 86 | 306k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 213k | ++it->count; | 88 | 213k | { | 89 | 213k | REVERSE_LOCK(lock, m_mutex); | 90 | 213k | f(*it->callbacks); | 91 | 213k | } | 92 | 213k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 213k | } | 94 | 93.5k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::ActiveTipChange(CBlockIndex const&, bool)::$_0>(ValidationSignals::ActiveTipChange(CBlockIndex const&, bool)::$_0&&) Line | Count | Source | 84 | 96.3k | { | 85 | 96.3k | WAIT_LOCK(m_mutex, lock); | 86 | 322k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 226k | ++it->count; | 88 | 226k | { | 89 | 226k | REVERSE_LOCK(lock, m_mutex); | 90 | 226k | f(*it->callbacks); | 91 | 226k | } | 92 | 226k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 226k | } | 94 | 96.3k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::TransactionAddedToMempool(NewMempoolTransactionInfo const&, unsigned long)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::TransactionAddedToMempool(NewMempoolTransactionInfo const&, unsigned long)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 49.9k | { | 85 | 49.9k | WAIT_LOCK(m_mutex, lock); | 86 | 132k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 82.8k | ++it->count; | 88 | 82.8k | { | 89 | 82.8k | REVERSE_LOCK(lock, m_mutex); | 90 | 82.8k | f(*it->callbacks); | 91 | 82.8k | } | 92 | 82.8k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 82.8k | } | 94 | 49.9k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::TransactionRemovedFromMempool(std::shared_ptr<CTransaction const> const&, MemPoolRemovalReason, unsigned long)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::TransactionRemovedFromMempool(std::shared_ptr<CTransaction const> const&, MemPoolRemovalReason, unsigned long)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 1.96k | { | 85 | 1.96k | WAIT_LOCK(m_mutex, lock); | 86 | 5.99k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 4.03k | ++it->count; | 88 | 4.03k | { | 89 | 4.03k | REVERSE_LOCK(lock, m_mutex); | 90 | 4.03k | f(*it->callbacks); | 91 | 4.03k | } | 92 | 4.03k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 4.03k | } | 94 | 1.96k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::BlockConnected(kernel::ChainstateRole const&, std::shared_ptr<CBlock const>, CBlockIndex const*)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::BlockConnected(kernel::ChainstateRole const&, std::shared_ptr<CBlock const>, CBlockIndex const*)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 106k | { | 85 | 106k | WAIT_LOCK(m_mutex, lock); | 86 | 347k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 240k | ++it->count; | 88 | 240k | { | 89 | 240k | REVERSE_LOCK(lock, m_mutex); | 90 | 240k | f(*it->callbacks); | 91 | 240k | } | 92 | 240k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 240k | } | 94 | 106k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::MempoolTransactionsRemovedForBlock(std::vector<RemovedMempoolTransactionInfo, std::allocator<RemovedMempoolTransactionInfo>> const&, unsigned int)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::MempoolTransactionsRemovedForBlock(std::vector<RemovedMempoolTransactionInfo, std::allocator<RemovedMempoolTransactionInfo>> const&, unsigned int)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 106k | { | 85 | 106k | WAIT_LOCK(m_mutex, lock); | 86 | 345k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 239k | ++it->count; | 88 | 239k | { | 89 | 239k | REVERSE_LOCK(lock, m_mutex); | 90 | 239k | f(*it->callbacks); | 91 | 239k | } | 92 | 239k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 239k | } | 94 | 106k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::BlockDisconnected(std::shared_ptr<CBlock const>, CBlockIndex const*)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::BlockDisconnected(std::shared_ptr<CBlock const>, CBlockIndex const*)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 14.2k | { | 85 | 14.2k | WAIT_LOCK(m_mutex, lock); | 86 | 43.2k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 29.0k | ++it->count; | 88 | 29.0k | { | 89 | 29.0k | REVERSE_LOCK(lock, m_mutex); | 90 | 29.0k | f(*it->callbacks); | 91 | 29.0k | } | 92 | 29.0k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 29.0k | } | 94 | 14.2k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::ChainStateFlushed(kernel::ChainstateRole const&, CBlockLocator const&)::$_0::operator()() const::'lambda'(CValidationInterface&)>(ValidationSignals::ChainStateFlushed(kernel::ChainstateRole const&, CBlockLocator const&)::$_0::operator()() const::'lambda'(CValidationInterface&)&&) Line | Count | Source | 84 | 2.33k | { | 85 | 2.33k | WAIT_LOCK(m_mutex, lock); | 86 | 5.15k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 2.82k | ++it->count; | 88 | 2.82k | { | 89 | 2.82k | REVERSE_LOCK(lock, m_mutex); | 90 | 2.82k | f(*it->callbacks); | 91 | 2.82k | } | 92 | 2.82k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 2.82k | } | 94 | 2.33k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::BlockChecked(std::shared_ptr<CBlock const> const&, BlockValidationState const&)::$_0>(ValidationSignals::BlockChecked(std::shared_ptr<CBlock const> const&, BlockValidationState const&)::$_0&&) Line | Count | Source | 84 | 143k | { | 85 | 143k | WAIT_LOCK(m_mutex, lock); | 86 | 410k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 266k | ++it->count; | 88 | 266k | { | 89 | 266k | REVERSE_LOCK(lock, m_mutex); | 90 | 266k | f(*it->callbacks); | 91 | 266k | } | 92 | 266k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 266k | } | 94 | 143k | } |
validationinterface.cpp:void ValidationSignalsImpl::Iterate<ValidationSignals::NewPoWValidBlock(CBlockIndex const*, std::shared_ptr<CBlock const> const&)::$_0>(ValidationSignals::NewPoWValidBlock(CBlockIndex const*, std::shared_ptr<CBlock const> const&)::$_0&&) Line | Count | Source | 84 | 79.1k | { | 85 | 79.1k | WAIT_LOCK(m_mutex, lock); | 86 | 264k | for (auto it = m_list.begin(); it != m_list.end();) { | 87 | 185k | ++it->count; | 88 | 185k | { | 89 | 185k | REVERSE_LOCK(lock, m_mutex); | 90 | 185k | f(*it->callbacks); | 91 | 185k | } | 92 | 185k | it = --it->count ? std::next(it) : m_list.erase(it); | 93 | 185k | } | 94 | 79.1k | } |
|
95 | | }; |
96 | | |
97 | | ValidationSignals::ValidationSignals(std::unique_ptr<util::TaskRunnerInterface> task_runner) |
98 | 1.26k | : m_internals{std::make_unique<ValidationSignalsImpl>(std::move(task_runner))} {} |
99 | | |
100 | 1.26k | ValidationSignals::~ValidationSignals() = default; |
101 | | |
102 | | void ValidationSignals::FlushBackgroundCallbacks() |
103 | 1.26k | { |
104 | 1.26k | m_internals->m_task_runner->flush(); |
105 | 1.26k | } |
106 | | |
107 | | size_t ValidationSignals::CallbacksPending() |
108 | 128k | { |
109 | 128k | return m_internals->m_task_runner->size(); |
110 | 128k | } |
111 | | |
112 | | void ValidationSignals::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks) |
113 | 259k | { |
114 | | // Each connection captures the shared_ptr to ensure that each callback is |
115 | | // executed before the subscriber is destroyed. For more details see #18338. |
116 | 259k | m_internals->Register(std::move(callbacks)); |
117 | 259k | } |
118 | | |
119 | | void ValidationSignals::RegisterValidationInterface(CValidationInterface* callbacks) |
120 | 2.19k | { |
121 | | // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle |
122 | | // is managed by the caller. |
123 | 2.19k | RegisterSharedValidationInterface({callbacks, [](CValidationInterface*){}}); |
124 | 2.19k | } |
125 | | |
126 | | void ValidationSignals::UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks) |
127 | 257k | { |
128 | 257k | UnregisterValidationInterface(callbacks.get()); |
129 | 257k | } |
130 | | |
131 | | void ValidationSignals::UnregisterValidationInterface(CValidationInterface* callbacks) |
132 | 259k | { |
133 | 259k | m_internals->Unregister(callbacks); |
134 | 259k | } |
135 | | |
136 | | void ValidationSignals::UnregisterAllValidationInterfaces() |
137 | 1.11k | { |
138 | 1.11k | m_internals->Clear(); |
139 | 1.11k | } |
140 | | |
141 | | void ValidationSignals::CallFunctionInValidationInterfaceQueue(std::function<void()> func) |
142 | 16.4k | { |
143 | 16.4k | m_internals->m_task_runner->insert(std::move(func)); |
144 | 16.4k | } |
145 | | |
146 | | void ValidationSignals::SyncWithValidationInterfaceQueue() |
147 | 16.4k | { |
148 | 16.4k | AssertLockNotHeld(cs_main); |
149 | | // Block until the validation queue drains |
150 | 16.4k | std::promise<void> promise; |
151 | 16.4k | CallFunctionInValidationInterfaceQueue([&promise] { |
152 | 16.4k | promise.set_value(); |
153 | 16.4k | }); |
154 | 16.4k | promise.get_future().wait(); |
155 | 16.4k | } |
156 | | |
157 | | // Use a macro instead of a function for conditional logging to prevent |
158 | | // evaluating arguments when logging is not enabled. |
159 | | #define ENQUEUE_AND_LOG_EVENT(event, log_msg) \ |
160 | 376k | do { \ |
161 | 376k | static_assert(std::is_rvalue_reference_v<decltype((event))>, \ |
162 | 376k | "event must be passed as an rvalue"); \ |
163 | 376k | static_assert(std::is_rvalue_reference_v<decltype((log_msg))>, \ |
164 | 376k | "log_msg must be passed as an rvalue"); \ |
165 | 376k | auto enqueue_log_msg = (log_msg); \ |
166 | 376k | LOG_EVENT("Enqueuing %s", enqueue_log_msg); \ |
167 | 376k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ |
168 | 374k | LOG_EVENT("%s", local_log_msg); \ |
169 | 374k | local_event(); \ |
170 | 374k | }); \ validationinterface.cpp:ValidationSignals::UpdatedBlockTip(CBlockIndex const*, CBlockIndex const*, bool)::$_1::operator()() const Line | Count | Source | 167 | 93.5k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 93.5k | LOG_EVENT("%s", local_log_msg); \ | 169 | 93.5k | local_event(); \ | 170 | 93.5k | }); \ |
validationinterface.cpp:ValidationSignals::TransactionAddedToMempool(NewMempoolTransactionInfo const&, unsigned long)::$_1::operator()() const Line | Count | Source | 167 | 49.9k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 49.9k | LOG_EVENT("%s", local_log_msg); \ | 169 | 49.9k | local_event(); \ | 170 | 49.9k | }); \ |
validationinterface.cpp:ValidationSignals::TransactionRemovedFromMempool(std::shared_ptr<CTransaction const> const&, MemPoolRemovalReason, unsigned long)::$_1::operator()() const Line | Count | Source | 167 | 1.96k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 1.96k | LOG_EVENT("%s", local_log_msg); \ | 169 | 1.96k | local_event(); \ | 170 | 1.96k | }); \ |
validationinterface.cpp:ValidationSignals::BlockConnected(kernel::ChainstateRole const&, std::shared_ptr<CBlock const>, CBlockIndex const*)::$_1::operator()() const Line | Count | Source | 167 | 106k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 106k | LOG_EVENT("%s", local_log_msg); \ | 169 | 106k | local_event(); \ | 170 | 106k | }); \ |
validationinterface.cpp:ValidationSignals::MempoolTransactionsRemovedForBlock(std::vector<RemovedMempoolTransactionInfo, std::allocator<RemovedMempoolTransactionInfo>> const&, unsigned int)::$_1::operator()() const Line | Count | Source | 167 | 106k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 106k | LOG_EVENT("%s", local_log_msg); \ | 169 | 106k | local_event(); \ | 170 | 106k | }); \ |
validationinterface.cpp:ValidationSignals::BlockDisconnected(std::shared_ptr<CBlock const>, CBlockIndex const*)::$_1::operator()() const Line | Count | Source | 167 | 14.2k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 14.2k | LOG_EVENT("%s", local_log_msg); \ | 169 | 14.2k | local_event(); \ | 170 | 14.2k | }); \ |
validationinterface.cpp:ValidationSignals::ChainStateFlushed(kernel::ChainstateRole const&, CBlockLocator const&)::$_1::operator()() const Line | Count | Source | 167 | 2.33k | m_internals->m_task_runner->insert([local_log_msg = std::move(enqueue_log_msg), local_event = (event)] { \ | 168 | 2.33k | LOG_EVENT("%s", local_log_msg); \ | 169 | 2.33k | local_event(); \ | 170 | 2.33k | }); \ |
|
171 | 376k | } while (0) |
172 | | |
173 | | #define LOG_MSG(fmt, ...) \ |
174 | 376k | (ShouldLog(BCLog::VALIDATION, BCLog::Level::Debug) ? tfm::format((fmt), __VA_ARGS__) : std::string{}) |
175 | | |
176 | | #define LOG_EVENT(fmt, ...) \ |
177 | 1.06M | LogDebug(BCLog::VALIDATION, fmt, __VA_ARGS__) |
178 | | |
179 | 93.5k | void ValidationSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) { |
180 | | // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which |
181 | | // the chain actually updates. One way to ensure this is for the caller to invoke this signal |
182 | | // in the same critical section where the chain is updated |
183 | | |
184 | 93.5k | auto log_msg = LOG_MSG("%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__, |
185 | 93.5k | pindexNew->GetBlockHash().ToString(), |
186 | 93.5k | pindexFork ? pindexFork->GetBlockHash().ToString() : "null", |
187 | 93.5k | fInitialDownload); |
188 | 93.5k | auto event = [pindexNew, pindexFork, fInitialDownload, this] { |
189 | 213k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload); }); |
190 | 93.5k | }; |
191 | 93.5k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
192 | 93.5k | } |
193 | | |
194 | | void ValidationSignals::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd) |
195 | 96.3k | { |
196 | 96.3k | LOG_EVENT("%s: new block hash=%s block height=%d", __func__, new_tip.GetBlockHash().ToString(), new_tip.nHeight); |
197 | 226k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ActiveTipChange(new_tip, is_ibd); }); |
198 | 96.3k | } |
199 | | |
200 | | void ValidationSignals::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) |
201 | 49.9k | { |
202 | 49.9k | auto log_msg = LOG_MSG("%s: txid=%s wtxid=%s", __func__, |
203 | 49.9k | tx.info.m_tx->GetHash().ToString(), |
204 | 49.9k | tx.info.m_tx->GetWitnessHash().ToString()); |
205 | 49.9k | auto event = [tx, mempool_sequence, this] { |
206 | 82.8k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); }); |
207 | 49.9k | }; |
208 | 49.9k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
209 | 49.9k | } |
210 | | |
211 | 1.96k | void ValidationSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) { |
212 | 1.96k | auto log_msg = LOG_MSG("%s: txid=%s wtxid=%s reason=%s", __func__, |
213 | 1.96k | tx->GetHash().ToString(), |
214 | 1.96k | tx->GetWitnessHash().ToString(), |
215 | 1.96k | RemovalReasonToString(reason)); |
216 | 1.96k | auto event = [tx, reason, mempool_sequence, this] { |
217 | 4.03k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); }); |
218 | 1.96k | }; |
219 | 1.96k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
220 | 1.96k | } |
221 | | |
222 | | void ValidationSignals::BlockConnected(const ChainstateRole& role, std::shared_ptr<const CBlock> pblock, const CBlockIndex* pindex) |
223 | 106k | { |
224 | 106k | auto log_msg = LOG_MSG("%s: block hash=%s block height=%d", __func__, |
225 | 106k | pblock->GetHash().ToString(), |
226 | 106k | pindex->nHeight); |
227 | 106k | auto event = [role, pblock = std::move(pblock), pindex, this] { |
228 | 240k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockConnected(role, pblock, pindex); }); |
229 | 106k | }; |
230 | 106k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
231 | 106k | } |
232 | | |
233 | | void ValidationSignals::MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) |
234 | 106k | { |
235 | 106k | auto log_msg = LOG_MSG("%s: block height=%s txs removed=%s", __func__, |
236 | 106k | nBlockHeight, |
237 | 106k | txs_removed_for_block.size()); |
238 | 106k | auto event = [txs_removed_for_block, nBlockHeight, this] { |
239 | 239k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight); }); |
240 | 106k | }; |
241 | 106k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
242 | 106k | } |
243 | | |
244 | | void ValidationSignals::BlockDisconnected(std::shared_ptr<const CBlock> pblock, const CBlockIndex* pindex) |
245 | 14.2k | { |
246 | 14.2k | auto log_msg = LOG_MSG("%s: block hash=%s block height=%d", __func__, |
247 | 14.2k | pblock->GetHash().ToString(), |
248 | 14.2k | pindex->nHeight); |
249 | 14.2k | auto event = [pblock = std::move(pblock), pindex, this] { |
250 | 29.0k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockDisconnected(pblock, pindex); }); |
251 | 14.2k | }; |
252 | 14.2k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
253 | 14.2k | } |
254 | | |
255 | | void ValidationSignals::ChainStateFlushed(const ChainstateRole& role, const CBlockLocator& locator) |
256 | 3.35k | { |
257 | 3.35k | auto log_msg = LOG_MSG("%s: block hash=%s", __func__, |
258 | 3.35k | locator.IsNull() ? "null" : locator.vHave.front().ToString()); |
259 | 3.35k | auto event = [role, locator, this] { |
260 | 2.82k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ChainStateFlushed(role, locator); }); |
261 | 2.33k | }; |
262 | 3.35k | ENQUEUE_AND_LOG_EVENT(std::move(event), std::move(log_msg)); |
263 | 3.35k | } |
264 | | |
265 | | void ValidationSignals::BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) |
266 | 143k | { |
267 | 143k | LOG_EVENT("%s: block hash=%s state=%s", __func__, |
268 | 143k | block->GetHash().ToString(), state.ToString()); |
269 | 266k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockChecked(block, state); }); |
270 | 143k | } |
271 | | |
272 | 79.1k | void ValidationSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) { |
273 | 79.1k | LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString()); |
274 | 185k | m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); }); |
275 | 79.1k | } |