Branch data Line data Source code
1 : : // Copyright (c) 2018-2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <addrdb.h>
6 : : #include <banman.h>
7 : : #include <blockfilter.h>
8 : : #include <chain.h>
9 : : #include <chainparams.h>
10 : : #include <common/args.h>
11 : : #include <consensus/validation.h>
12 : : #include <deploymentstatus.h>
13 : : #include <external_signer.h>
14 : : #include <index/blockfilterindex.h>
15 : : #include <init.h>
16 : : #include <interfaces/chain.h>
17 : : #include <interfaces/handler.h>
18 : : #include <interfaces/mining.h>
19 : : #include <interfaces/node.h>
20 : : #include <interfaces/wallet.h>
21 : : #include <kernel/chain.h>
22 : : #include <kernel/context.h>
23 : : #include <kernel/mempool_entry.h>
24 : : #include <logging.h>
25 : : #include <mapport.h>
26 : : #include <net.h>
27 : : #include <net_processing.h>
28 : : #include <netaddress.h>
29 : : #include <netbase.h>
30 : : #include <node/blockstorage.h>
31 : : #include <node/coin.h>
32 : : #include <node/context.h>
33 : : #include <node/interface_ui.h>
34 : : #include <node/mini_miner.h>
35 : : #include <node/miner.h>
36 : : #include <node/transaction.h>
37 : : #include <node/types.h>
38 : : #include <node/warnings.h>
39 : : #include <policy/feerate.h>
40 : : #include <policy/fees.h>
41 : : #include <policy/policy.h>
42 : : #include <policy/rbf.h>
43 : : #include <policy/settings.h>
44 : : #include <primitives/block.h>
45 : : #include <primitives/transaction.h>
46 : : #include <rpc/protocol.h>
47 : : #include <rpc/server.h>
48 : : #include <support/allocators/secure.h>
49 : : #include <sync.h>
50 : : #include <txmempool.h>
51 : : #include <uint256.h>
52 : : #include <univalue.h>
53 : : #include <util/check.h>
54 : : #include <util/result.h>
55 : : #include <util/signalinterrupt.h>
56 : : #include <util/string.h>
57 : : #include <util/translation.h>
58 : : #include <validation.h>
59 : : #include <validationinterface.h>
60 : :
61 : : #include <config/bitcoin-config.h> // IWYU pragma: keep
62 : :
63 : : #include <any>
64 : : #include <memory>
65 : : #include <optional>
66 : : #include <utility>
67 : :
68 : : #include <boost/signals2/signal.hpp>
69 : :
70 : : using interfaces::BlockTip;
71 : : using interfaces::Chain;
72 : : using interfaces::FoundBlock;
73 : : using interfaces::Handler;
74 : : using interfaces::MakeSignalHandler;
75 : : using interfaces::Mining;
76 : : using interfaces::Node;
77 : : using interfaces::WalletLoader;
78 : : using node::BlockAssembler;
79 : : using util::Join;
80 : :
81 : : namespace node {
82 : : // All members of the classes in this namespace are intentionally public, as the
83 : : // classes themselves are private.
84 : : namespace {
85 : : #ifdef ENABLE_EXTERNAL_SIGNER
86 : : class ExternalSignerImpl : public interfaces::ExternalSigner
87 : : {
88 : : public:
89 : : ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
90 : : std::string getName() override { return m_signer.m_name; }
91 : : ::ExternalSigner m_signer;
92 : : };
93 : : #endif
94 : :
95 : : class NodeImpl : public Node
96 : : {
97 : : public:
98 [ # # # # ]: 0 : explicit NodeImpl(NodeContext& context) { setContext(&context); }
99 : 0 : void initLogging() override { InitLogging(args()); }
100 : 0 : void initParameterInteraction() override { InitParameterInteraction(args()); }
101 [ # # # # : 0 : bilingual_str getWarnings() override { return Join(Assert(m_context->warnings)->GetMessages(), Untranslated("<hr />")); }
# # ]
102 : 0 : int getExitStatus() override { return Assert(m_context)->exit_status.load(); }
103 : 0 : uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
104 : 0 : bool baseInitialize() override
105 : : {
106 [ # # ]: 0 : if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false;
107 [ # # ]: 0 : if (!AppInitParameterInteraction(args())) return false;
108 : :
109 : 0 : m_context->warnings = std::make_unique<node::Warnings>();
110 : 0 : m_context->kernel = std::make_unique<kernel::Context>();
111 : 0 : m_context->ecc_context = std::make_unique<ECC_Context>();
112 [ # # ]: 0 : if (!AppInitSanityChecks(*m_context->kernel)) return false;
113 : :
114 [ # # ]: 0 : if (!AppInitLockDataDirectory()) return false;
115 [ # # ]: 0 : if (!AppInitInterfaces(*m_context)) return false;
116 : :
117 : 0 : return true;
118 : 0 : }
119 : 0 : bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
120 : : {
121 [ # # ]: 0 : if (AppInitMain(*m_context, tip_info)) return true;
122 : : // Error during initialization, set exit status before continue
123 : 0 : m_context->exit_status.store(EXIT_FAILURE);
124 : 0 : return false;
125 : 0 : }
126 : 0 : void appShutdown() override
127 : : {
128 : 0 : Interrupt(*m_context);
129 : 0 : Shutdown(*m_context);
130 : 0 : }
131 : 0 : void startShutdown() override
132 : : {
133 [ # # ]: 0 : if (!(*Assert(Assert(m_context)->shutdown))()) {
134 : 0 : LogError("Failed to send shutdown signal\n");
135 : 0 : }
136 : : // Stop RPC for clean shutdown if any of waitfor* commands is executed.
137 [ # # # # : 0 : if (args().GetBoolArg("-server", false)) {
# # ]
138 : 0 : InterruptRPC();
139 : 0 : StopRPC();
140 : 0 : }
141 : 0 : }
142 : 0 : bool shutdownRequested() override { return ShutdownRequested(*Assert(m_context)); };
143 : 0 : bool isSettingIgnored(const std::string& name) override
144 : : {
145 : 0 : bool ignored = false;
146 : 0 : args().LockSettings([&](common::Settings& settings) {
147 [ # # ]: 0 : if (auto* options = common::FindKey(settings.command_line_options, name)) {
148 : 0 : ignored = !options->empty();
149 : 0 : }
150 : 0 : });
151 : 0 : return ignored;
152 : 0 : }
153 : 0 : common::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); }
154 : 0 : void updateRwSetting(const std::string& name, const common::SettingsValue& value) override
155 : : {
156 : 0 : args().LockSettings([&](common::Settings& settings) {
157 [ # # ]: 0 : if (value.isNull()) {
158 : 0 : settings.rw_settings.erase(name);
159 : 0 : } else {
160 : 0 : settings.rw_settings[name] = value;
161 : : }
162 : 0 : });
163 : 0 : args().WriteSettingsFile();
164 : 0 : }
165 : 0 : void forceSetting(const std::string& name, const common::SettingsValue& value) override
166 : : {
167 : 0 : args().LockSettings([&](common::Settings& settings) {
168 [ # # ]: 0 : if (value.isNull()) {
169 : 0 : settings.forced_settings.erase(name);
170 : 0 : } else {
171 : 0 : settings.forced_settings[name] = value;
172 : : }
173 : 0 : });
174 : 0 : }
175 : 0 : void resetSettings() override
176 : : {
177 : 0 : args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
178 : 0 : args().LockSettings([&](common::Settings& settings) {
179 : 0 : settings.rw_settings.clear();
180 : 0 : });
181 : 0 : args().WriteSettingsFile();
182 : 0 : }
183 : 0 : void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
184 : 0 : bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
185 : 0 : size_t getNodeCount(ConnectionDirection flags) override
186 : : {
187 [ # # ]: 0 : return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
188 : : }
189 : 0 : bool getNodesStats(NodesStats& stats) override
190 : : {
191 : 0 : stats.clear();
192 : :
193 [ # # ]: 0 : if (m_context->connman) {
194 : 0 : std::vector<CNodeStats> stats_temp;
195 [ # # ]: 0 : m_context->connman->GetNodeStats(stats_temp);
196 : :
197 [ # # ]: 0 : stats.reserve(stats_temp.size());
198 [ # # ]: 0 : for (auto& node_stats_temp : stats_temp) {
199 [ # # # # ]: 0 : stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());
200 : 0 : }
201 : :
202 : : // Try to retrieve the CNodeStateStats for each node.
203 [ # # ]: 0 : if (m_context->peerman) {
204 [ # # ]: 0 : TRY_LOCK(::cs_main, lockMain);
205 [ # # # # ]: 0 : if (lockMain) {
206 [ # # ]: 0 : for (auto& node_stats : stats) {
207 : 0 : std::get<1>(node_stats) =
208 [ # # ]: 0 : m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
209 : 0 : }
210 : 0 : }
211 : 0 : }
212 : 0 : return true;
213 : 0 : }
214 : 0 : return false;
215 : 0 : }
216 : 0 : bool getBanned(banmap_t& banmap) override
217 : : {
218 [ # # ]: 0 : if (m_context->banman) {
219 : 0 : m_context->banman->GetBanned(banmap);
220 : 0 : return true;
221 : : }
222 : 0 : return false;
223 : 0 : }
224 : 0 : bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
225 : : {
226 [ # # ]: 0 : if (m_context->banman) {
227 : 0 : m_context->banman->Ban(net_addr, ban_time_offset);
228 : 0 : return true;
229 : : }
230 : 0 : return false;
231 : 0 : }
232 : 0 : bool unban(const CSubNet& ip) override
233 : : {
234 [ # # ]: 0 : if (m_context->banman) {
235 : 0 : m_context->banman->Unban(ip);
236 : 0 : return true;
237 : : }
238 : 0 : return false;
239 : 0 : }
240 : 0 : bool disconnectByAddress(const CNetAddr& net_addr) override
241 : : {
242 [ # # ]: 0 : if (m_context->connman) {
243 : 0 : return m_context->connman->DisconnectNode(net_addr);
244 : : }
245 : 0 : return false;
246 : 0 : }
247 : 0 : bool disconnectById(NodeId id) override
248 : : {
249 [ # # ]: 0 : if (m_context->connman) {
250 : 0 : return m_context->connman->DisconnectNode(id);
251 : : }
252 : 0 : return false;
253 : 0 : }
254 : 0 : std::vector<std::unique_ptr<interfaces::ExternalSigner>> listExternalSigners() override
255 : : {
256 : : #ifdef ENABLE_EXTERNAL_SIGNER
257 : : std::vector<ExternalSigner> signers = {};
258 : : const std::string command = args().GetArg("-signer", "");
259 : : if (command == "") return {};
260 : : ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
261 : : std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
262 : : result.reserve(signers.size());
263 : : for (auto& signer : signers) {
264 : : result.emplace_back(std::make_unique<ExternalSignerImpl>(std::move(signer)));
265 : : }
266 : : return result;
267 : : #else
268 : : // This result is indistinguishable from a successful call that returns
269 : : // no signers. For the current GUI this doesn't matter, because the wallet
270 : : // creation dialog disables the external signer checkbox in both
271 : : // cases. The return type could be changed to std::optional<std::vector>
272 : : // (or something that also includes error messages) if this distinction
273 : : // becomes important.
274 : 0 : return {};
275 : : #endif // ENABLE_EXTERNAL_SIGNER
276 : : }
277 [ # # ]: 0 : int64_t getTotalBytesRecv() override { return m_context->connman ? m_context->connman->GetTotalBytesRecv() : 0; }
278 [ # # ]: 0 : int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; }
279 [ # # ]: 0 : size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; }
280 [ # # ]: 0 : size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; }
281 [ # # ]: 0 : size_t getMempoolMaxUsage() override { return m_context->mempool ? m_context->mempool->m_opts.max_size_bytes : 0; }
282 : 0 : bool getHeaderTip(int& height, int64_t& block_time) override
283 : : {
284 : 0 : LOCK(::cs_main);
285 [ # # ]: 0 : auto best_header = chainman().m_best_header;
286 [ # # ]: 0 : if (best_header) {
287 : 0 : height = best_header->nHeight;
288 [ # # ]: 0 : block_time = best_header->GetBlockTime();
289 : 0 : return true;
290 : : }
291 : 0 : return false;
292 : 0 : }
293 : 0 : std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() override
294 : : {
295 [ # # ]: 0 : if (m_context->connman)
296 : 0 : return m_context->connman->getNetLocalAddresses();
297 : : else
298 : 0 : return {};
299 : 0 : }
300 : 0 : int getNumBlocks() override
301 : : {
302 : 0 : LOCK(::cs_main);
303 [ # # # # : 0 : return chainman().ActiveChain().Height();
# # ]
304 : 0 : }
305 : 0 : uint256 getBestBlockHash() override
306 : : {
307 [ # # # # : 0 : const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
# # ]
308 [ # # ]: 0 : return tip ? tip->GetBlockHash() : chainman().GetParams().GenesisBlock().GetHash();
309 : 0 : }
310 : 0 : int64_t getLastBlockTime() override
311 : : {
312 : 0 : LOCK(::cs_main);
313 [ # # # # : 0 : if (chainman().ActiveChain().Tip()) {
# # ]
314 [ # # # # ]: 0 : return chainman().ActiveChain().Tip()->GetBlockTime();
315 : : }
316 [ # # # # ]: 0 : return chainman().GetParams().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
317 : 0 : }
318 : 0 : double getVerificationProgress() override
319 : : {
320 [ # # # # ]: 0 : return GuessVerificationProgress(chainman().GetParams().TxData(), WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()));
321 : : }
322 : 0 : bool isInitialBlockDownload() override
323 : : {
324 : 0 : return chainman().IsInitialBlockDownload();
325 : : }
326 : 0 : bool isLoadingBlocks() override { return chainman().m_blockman.LoadingBlocks(); }
327 : 0 : void setNetworkActive(bool active) override
328 : : {
329 [ # # ]: 0 : if (m_context->connman) {
330 : 0 : m_context->connman->SetNetworkActive(active);
331 : 0 : }
332 : 0 : }
333 [ # # ]: 0 : bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
334 : 0 : CFeeRate getDustRelayFee() override
335 : : {
336 [ # # ]: 0 : if (!m_context->mempool) return CFeeRate{DUST_RELAY_TX_FEE};
337 : 0 : return m_context->mempool->m_opts.dust_relay_feerate;
338 : 0 : }
339 : 0 : UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
340 : : {
341 : 0 : JSONRPCRequest req;
342 [ # # ]: 0 : req.context = m_context;
343 [ # # ]: 0 : req.params = params;
344 [ # # ]: 0 : req.strMethod = command;
345 [ # # ]: 0 : req.URI = uri;
346 [ # # ]: 0 : return ::tableRPC.execute(req);
347 : 0 : }
348 : 0 : std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
349 : 0 : void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) override { RPCSetTimerInterfaceIfUnset(iface); }
350 : 0 : void rpcUnsetTimerInterface(RPCTimerInterface* iface) override { RPCUnsetTimerInterface(iface); }
351 : 0 : std::optional<Coin> getUnspentOutput(const COutPoint& output) override
352 : : {
353 : 0 : LOCK(::cs_main);
354 [ # # ]: 0 : Coin coin;
355 [ # # # # : 0 : if (chainman().ActiveChainstate().CoinsTip().GetCoin(output, coin)) return coin;
# # # # #
# ]
356 : 0 : return {};
357 : 0 : }
358 : 0 : TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) override
359 : : {
360 [ # # ]: 0 : return BroadcastTransaction(*m_context, std::move(tx), err_string, max_tx_fee, /*relay=*/ true, /*wait_callback=*/ false);
361 : 0 : }
362 : 0 : WalletLoader& walletLoader() override
363 : : {
364 : 0 : return *Assert(m_context->wallet_loader);
365 : : }
366 : 0 : std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
367 : : {
368 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.InitMessage_connect(fn));
369 : 0 : }
370 : 0 : std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
371 : : {
372 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
373 : 0 : }
374 : 0 : std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
375 : : {
376 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
377 : 0 : }
378 : 0 : std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
379 : : {
380 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn));
381 : 0 : }
382 : 0 : std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) override
383 : : {
384 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.InitWallet_connect(fn));
385 : 0 : }
386 : 0 : std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
387 : : {
388 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
389 : 0 : }
390 : 0 : std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
391 : : {
392 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
393 : 0 : }
394 : 0 : std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
395 : : {
396 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.NotifyAlertChanged_connect(fn));
397 : 0 : }
398 : 0 : std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
399 : : {
400 [ # # # # ]: 0 : return MakeSignalHandler(::uiInterface.BannedListChanged_connect(fn));
401 : 0 : }
402 : 0 : std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
403 : : {
404 [ # # # # : 0 : return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
# # ]
405 : 0 : fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
406 : 0 : GuessVerificationProgress(Params().TxData(), block));
407 : 0 : }));
408 : 0 : }
409 : 0 : std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
410 : : {
411 [ # # ]: 0 : return MakeSignalHandler(
412 [ # # # # ]: 0 : ::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, int64_t height, int64_t timestamp, bool presync) {
413 : 0 : fn(sync_state, BlockTip{(int)height, timestamp, uint256{}}, presync);
414 : 0 : }));
415 : 0 : }
416 : 0 : NodeContext* context() override { return m_context; }
417 : 0 : void setContext(NodeContext* context) override
418 : : {
419 : 0 : m_context = context;
420 : 0 : }
421 : 0 : ArgsManager& args() { return *Assert(Assert(m_context)->args); }
422 : 0 : ChainstateManager& chainman() { return *Assert(m_context->chainman); }
423 : 0 : NodeContext* m_context{nullptr};
424 : : };
425 : :
426 : : // NOLINTNEXTLINE(misc-no-recursion)
427 : 196341 : bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active, const BlockManager& blockman)
428 : : {
429 [ - + ]: 196341 : if (!index) return false;
430 [ # # ]: 0 : if (block.m_hash) *block.m_hash = index->GetBlockHash();
431 [ # # ]: 0 : if (block.m_height) *block.m_height = index->nHeight;
432 [ # # ]: 0 : if (block.m_time) *block.m_time = index->GetBlockTime();
433 [ # # ]: 0 : if (block.m_max_time) *block.m_max_time = index->GetBlockTimeMax();
434 [ # # ]: 0 : if (block.m_mtp_time) *block.m_mtp_time = index->GetMedianTimePast();
435 [ # # ]: 0 : if (block.m_in_active_chain) *block.m_in_active_chain = active[index->nHeight] == index;
436 [ # # ]: 0 : if (block.m_locator) { *block.m_locator = GetLocator(index); }
437 [ # # # # ]: 0 : if (block.m_next_block) FillBlock(active[index->nHeight] == index ? active[index->nHeight + 1] : nullptr, *block.m_next_block, lock, active, blockman);
438 [ # # ]: 0 : if (block.m_data) {
439 : 0 : REVERSE_LOCK(lock);
440 [ # # # # : 0 : if (!blockman.ReadBlockFromDisk(*block.m_data, *index)) block.m_data->SetNull();
# # ]
441 : 0 : }
442 : 0 : block.found = true;
443 : 0 : return true;
444 : 196341 : }
445 : :
446 : : class NotificationsProxy : public CValidationInterface
447 : : {
448 : : public:
449 : 0 : explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications)
450 : 0 : : m_notifications(std::move(notifications)) {}
451 : 0 : virtual ~NotificationsProxy() = default;
452 : 0 : void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) override
453 : : {
454 : 0 : m_notifications->transactionAddedToMempool(tx.info.m_tx);
455 : 0 : }
456 : 0 : void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
457 : : {
458 : 0 : m_notifications->transactionRemovedFromMempool(tx, reason);
459 : 0 : }
460 : 0 : void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
461 : : {
462 : 0 : m_notifications->blockConnected(role, kernel::MakeBlockInfo(index, block.get()));
463 : 0 : }
464 : 0 : void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
465 : : {
466 : 0 : m_notifications->blockDisconnected(kernel::MakeBlockInfo(index, block.get()));
467 : 0 : }
468 : 0 : void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
469 : : {
470 : 0 : m_notifications->updatedBlockTip();
471 : 0 : }
472 : 0 : void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override {
473 : 0 : m_notifications->chainStateFlushed(role, locator);
474 : 0 : }
475 : : std::shared_ptr<Chain::Notifications> m_notifications;
476 : : };
477 : :
478 : : class NotificationsHandlerImpl : public Handler
479 : : {
480 : : public:
481 : 0 : explicit NotificationsHandlerImpl(ValidationSignals& signals, std::shared_ptr<Chain::Notifications> notifications)
482 [ # # ]: 0 : : m_signals{signals}, m_proxy{std::make_shared<NotificationsProxy>(std::move(notifications))}
483 : 0 : {
484 [ # # ]: 0 : m_signals.RegisterSharedValidationInterface(m_proxy);
485 : 0 : }
486 [ # # ]: 0 : ~NotificationsHandlerImpl() override { disconnect(); }
487 : 0 : void disconnect() override
488 : : {
489 [ # # ]: 0 : if (m_proxy) {
490 [ # # ]: 0 : m_signals.UnregisterSharedValidationInterface(m_proxy);
491 : 0 : m_proxy.reset();
492 : 0 : }
493 : 0 : }
494 : : ValidationSignals& m_signals;
495 : : std::shared_ptr<NotificationsProxy> m_proxy;
496 : : };
497 : :
498 : : class RpcHandlerImpl : public Handler
499 : : {
500 : : public:
501 [ # # # # ]: 0 : explicit RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
502 : 0 : {
503 : 0 : m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
504 [ # # ]: 0 : if (!m_wrapped_command) return false;
505 : : try {
506 [ # # ]: 0 : return m_wrapped_command->actor(request, result, last_handler);
507 [ # # ]: 0 : } catch (const UniValue& e) {
508 : : // If this is not the last handler and a wallet not found
509 : : // exception was thrown, return false so the next handler can
510 : : // try to handle the request. Otherwise, reraise the exception.
511 [ # # ]: 0 : if (!last_handler) {
512 [ # # # # ]: 0 : const UniValue& code = e["code"];
513 [ # # # # : 0 : if (code.isNum() && code.getInt<int>() == RPC_WALLET_NOT_FOUND) {
# # # # ]
514 : 0 : return false;
515 : : }
516 [ # # ]: 0 : }
517 [ # # ]: 0 : throw;
518 [ # # ]: 0 : }
519 : 0 : };
520 [ # # ]: 0 : ::tableRPC.appendCommand(m_command.name, &m_command);
521 : 0 : }
522 : :
523 : 0 : void disconnect() final
524 : : {
525 [ # # ]: 0 : if (m_wrapped_command) {
526 : 0 : m_wrapped_command = nullptr;
527 : 0 : ::tableRPC.removeCommand(m_command.name, &m_command);
528 : 0 : }
529 : 0 : }
530 : :
531 [ # # ]: 0 : ~RpcHandlerImpl() override { disconnect(); }
532 : :
533 : : CRPCCommand m_command;
534 : : const CRPCCommand* m_wrapped_command;
535 : : };
536 : :
537 : : class ChainImpl : public Chain
538 : : {
539 : : public:
540 : 1826 : explicit ChainImpl(NodeContext& node) : m_node(node) {}
541 : 3228 : std::optional<int> getHeight() override
542 : : {
543 [ + - + - ]: 6456 : const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
544 [ + - ]: 3228 : return height >= 0 ? std::optional{height} : std::nullopt;
545 : 3228 : }
546 : 3228 : uint256 getBlockHash(int height) override
547 : : {
548 : 3228 : LOCK(::cs_main);
549 [ + - + - : 3228 : return Assert(chainman().ActiveChain()[height])->GetBlockHash();
+ - + - ]
550 : 3228 : }
551 : 0 : bool haveBlockOnDisk(int height) override
552 : : {
553 : 0 : LOCK(::cs_main);
554 [ # # # # ]: 0 : const CBlockIndex* block{chainman().ActiveChain()[height]};
555 [ # # # # ]: 0 : return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
556 : 0 : }
557 : 0 : CBlockLocator getTipLocator() override
558 : : {
559 : 0 : LOCK(::cs_main);
560 [ # # # # : 0 : return chainman().ActiveChain().GetLocator();
# # ]
561 : 0 : }
562 : 0 : CBlockLocator getActiveChainLocator(const uint256& block_hash) override
563 : : {
564 : 0 : LOCK(::cs_main);
565 [ # # # # ]: 0 : const CBlockIndex* index = chainman().m_blockman.LookupBlockIndex(block_hash);
566 [ # # ]: 0 : return GetLocator(index);
567 : 0 : }
568 : 0 : std::optional<int> findLocatorFork(const CBlockLocator& locator) override
569 : : {
570 : 0 : LOCK(::cs_main);
571 [ # # # # : 0 : if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) {
# # # # #
# ]
572 : 0 : return fork->nHeight;
573 : : }
574 : 0 : return std::nullopt;
575 : 0 : }
576 : 0 : bool hasBlockFilterIndex(BlockFilterType filter_type) override
577 : : {
578 : 0 : return GetBlockFilterIndex(filter_type) != nullptr;
579 : : }
580 : 0 : std::optional<bool> blockFilterMatchesAny(BlockFilterType filter_type, const uint256& block_hash, const GCSFilter::ElementSet& filter_set) override
581 : : {
582 : 0 : const BlockFilterIndex* block_filter_index{GetBlockFilterIndex(filter_type)};
583 [ # # ]: 0 : if (!block_filter_index) return std::nullopt;
584 : :
585 : 0 : BlockFilter filter;
586 [ # # # # : 0 : const CBlockIndex* index{WITH_LOCK(::cs_main, return chainman().m_blockman.LookupBlockIndex(block_hash))};
# # ]
587 [ # # # # : 0 : if (index == nullptr || !block_filter_index->LookupFilter(index, filter)) return std::nullopt;
# # ]
588 [ # # # # ]: 0 : return filter.GetFilter().MatchAny(filter_set);
589 : 0 : }
590 : 196341 : bool findBlock(const uint256& hash, const FoundBlock& block) override
591 : : {
592 : 196341 : WAIT_LOCK(cs_main, lock);
593 [ + - + - : 196341 : return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain(), chainman().m_blockman);
+ - + - +
- + - ]
594 : 196341 : }
595 : 0 : bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
596 : : {
597 : 0 : WAIT_LOCK(cs_main, lock);
598 [ # # # # ]: 0 : const CChain& active = chainman().ActiveChain();
599 [ # # # # : 0 : return FillBlock(active.FindEarliestAtLeast(min_time, min_height), block, lock, active, chainman().m_blockman);
# # ]
600 : 0 : }
601 : 0 : bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
602 : : {
603 : 0 : WAIT_LOCK(cs_main, lock);
604 [ # # # # ]: 0 : const CChain& active = chainman().ActiveChain();
605 [ # # # # : 0 : if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
# # # # ]
606 [ # # # # : 0 : if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
# # ]
607 [ # # # # ]: 0 : return FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman);
608 : : }
609 : 0 : }
610 [ # # # # ]: 0 : return FillBlock(nullptr, ancestor_out, lock, active, chainman().m_blockman);
611 : 0 : }
612 : 0 : bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
613 : : {
614 : 0 : WAIT_LOCK(cs_main, lock);
615 [ # # # # ]: 0 : const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
616 [ # # # # ]: 0 : const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
617 [ # # # # : 0 : if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
# # # # ]
618 [ # # # # : 0 : return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain(), chainman().m_blockman);
# # # # ]
619 : 0 : }
620 : 0 : bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
621 : : {
622 : 0 : WAIT_LOCK(cs_main, lock);
623 [ # # # # ]: 0 : const CChain& active = chainman().ActiveChain();
624 [ # # # # ]: 0 : const CBlockIndex* block1 = chainman().m_blockman.LookupBlockIndex(block_hash1);
625 [ # # # # ]: 0 : const CBlockIndex* block2 = chainman().m_blockman.LookupBlockIndex(block_hash2);
626 [ # # # # : 0 : const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
# # ]
627 : : // Using & instead of && below to avoid short circuiting and leaving
628 : : // output uninitialized. Cast bool to int to avoid -Wbitwise-instead-of-logical
629 : : // compiler warnings.
630 [ # # # # ]: 0 : return int{FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman)} &
631 [ # # # # ]: 0 : int{FillBlock(block1, block1_out, lock, active, chainman().m_blockman)} &
632 [ # # # # ]: 0 : int{FillBlock(block2, block2_out, lock, active, chainman().m_blockman)};
633 : 0 : }
634 : 272656 : void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
635 : 0 : double guessVerificationProgress(const uint256& block_hash) override
636 : : {
637 : 0 : LOCK(::cs_main);
638 [ # # # # : 0 : return GuessVerificationProgress(chainman().GetParams().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
# # # # ]
639 : 0 : }
640 : 0 : bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
641 : : {
642 : : // hasBlocks returns true if all ancestors of block_hash in specified
643 : : // range have block data (are not pruned), false if any ancestors in
644 : : // specified range are missing data.
645 : : //
646 : : // For simplicity and robustness, min_height and max_height are only
647 : : // used to limit the range, and passing min_height that's too low or
648 : : // max_height that's too high will not crash or change the result.
649 : 0 : LOCK(::cs_main);
650 [ # # # # : 0 : if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
# # # # ]
651 [ # # # # : 0 : if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
# # ]
652 [ # # ]: 0 : for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
653 : : // Check pprev to not segfault if min_height is too low
654 [ # # # # ]: 0 : if (block->nHeight <= min_height || !block->pprev) return true;
655 : 0 : }
656 : 0 : }
657 : 0 : return false;
658 : 0 : }
659 : 0 : RBFTransactionState isRBFOptIn(const CTransaction& tx) override
660 : : {
661 [ # # ]: 0 : if (!m_node.mempool) return IsRBFOptInEmptyMempool(tx);
662 : 0 : LOCK(m_node.mempool->cs);
663 [ # # ]: 0 : return IsRBFOptIn(tx, *m_node.mempool);
664 : 0 : }
665 : 196341 : bool isInMempool(const uint256& txid) override
666 : : {
667 [ - + ]: 196341 : if (!m_node.mempool) return false;
668 : 196341 : LOCK(m_node.mempool->cs);
669 [ + - + - ]: 196341 : return m_node.mempool->exists(GenTxid::Txid(txid));
670 : 196341 : }
671 : 0 : bool hasDescendantsInMempool(const uint256& txid) override
672 : : {
673 [ # # ]: 0 : if (!m_node.mempool) return false;
674 : 0 : LOCK(m_node.mempool->cs);
675 [ # # # # ]: 0 : const auto entry{m_node.mempool->GetEntry(Txid::FromUint256(txid))};
676 [ # # ]: 0 : if (entry == nullptr) return false;
677 [ # # ]: 0 : return entry->GetCountWithDescendants() > 1;
678 : 0 : }
679 : 0 : bool broadcastTransaction(const CTransactionRef& tx,
680 : : const CAmount& max_tx_fee,
681 : : bool relay,
682 : : std::string& err_string) override
683 : : {
684 [ # # ]: 0 : const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay, /*wait_callback=*/false);
685 : : // Chain clients only care about failures to accept the tx to the mempool. Disregard non-mempool related failures.
686 : : // Note: this will need to be updated if BroadcastTransactions() is updated to return other non-mempool failures
687 : : // that Chain clients do not need to know about.
688 : 0 : return TransactionError::OK == err;
689 : 0 : }
690 : 3609287 : void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize, CAmount* ancestorfees) override
691 : : {
692 : 3609287 : ancestors = descendants = 0;
693 [ - + ]: 3609287 : if (!m_node.mempool) return;
694 : 3609287 : m_node.mempool->GetTransactionAncestry(txid, ancestors, descendants, ancestorsize, ancestorfees);
695 : 3609287 : }
696 : :
697 : 191065 : std::map<COutPoint, CAmount> calculateIndividualBumpFees(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
698 : : {
699 [ - + ]: 191065 : if (!m_node.mempool) {
700 : 0 : std::map<COutPoint, CAmount> bump_fees;
701 [ # # ]: 0 : for (const auto& outpoint : outpoints) {
702 [ # # ]: 0 : bump_fees.emplace(outpoint, 0);
703 : 0 : }
704 : 0 : return bump_fees;
705 [ # # ]: 0 : }
706 [ + - ]: 191065 : return MiniMiner(*m_node.mempool, outpoints).CalculateBumpFees(target_feerate);
707 : 191065 : }
708 : :
709 : 339795 : std::optional<CAmount> calculateCombinedBumpFee(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
710 : : {
711 [ - + ]: 339795 : if (!m_node.mempool) {
712 : 0 : return 0;
713 : : }
714 [ + - ]: 339795 : return MiniMiner(*m_node.mempool, outpoints).CalculateTotalBumpFees(target_feerate);
715 : 339795 : }
716 : 58851 : void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) override
717 : : {
718 [ + - ]: 58851 : const CTxMemPool::Limits default_limits{};
719 : :
720 : : const CTxMemPool::Limits& limits{m_node.mempool ? m_node.mempool->m_opts.limits : default_limits};
721 : :
722 : : limit_ancestor_count = limits.ancestor_count;
723 : : limit_descendant_count = limits.descendant_count;
724 : : }
725 : 50843 : util::Result<void> checkChainLimits(const CTransactionRef& tx) override
726 : : {
727 [ - + ]: 50843 : if (!m_node.mempool) return {};
728 : 50843 : LockPoints lp;
729 : 50843 : CTxMemPoolEntry entry(tx, 0, 0, 0, 0, false, 0, lp);
730 [ + - ]: 50843 : LOCK(m_node.mempool->cs);
731 [ + - + - : 50843 : return m_node.mempool->CheckPackageLimits({tx}, entry.GetTxSize());
+ - ]
732 : 50843 : }
733 : 112437 : CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
734 : : {
735 [ + - ]: 112437 : if (!m_node.fee_estimator) return {};
736 : 0 : return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
737 : 112437 : }
738 : 112331 : unsigned int estimateMaxBlocks() override
739 : : {
740 [ + - ]: 112331 : if (!m_node.fee_estimator) return 0;
741 : 0 : return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
742 : 112331 : }
743 : 88 : CFeeRate mempoolMinFee() override
744 : : {
745 [ - + ]: 88 : if (!m_node.mempool) return {};
746 : 88 : return m_node.mempool->GetMinFee();
747 : 88 : }
748 : 249195 : CFeeRate relayMinFee() override
749 : : {
750 [ - + ]: 249195 : if (!m_node.mempool) return CFeeRate{DEFAULT_MIN_RELAY_TX_FEE};
751 : 249195 : return m_node.mempool->m_opts.min_relay_feerate;
752 : 249195 : }
753 : 0 : CFeeRate relayIncrementalFee() override
754 : : {
755 [ # # ]: 0 : if (!m_node.mempool) return CFeeRate{DEFAULT_INCREMENTAL_RELAY_FEE};
756 : 0 : return m_node.mempool->m_opts.incremental_relay_feerate;
757 : 0 : }
758 : 639028 : CFeeRate relayDustFee() override
759 : : {
760 [ - + ]: 639028 : if (!m_node.mempool) return CFeeRate{DUST_RELAY_TX_FEE};
761 : 639028 : return m_node.mempool->m_opts.dust_relay_feerate;
762 : 639028 : }
763 : 0 : bool havePruned() override
764 : : {
765 : 0 : LOCK(::cs_main);
766 [ # # ]: 0 : return chainman().m_blockman.m_have_pruned;
767 : 0 : }
768 [ # # ]: 0 : bool isReadyToBroadcast() override { return !chainman().m_blockman.LoadingBlocks() && !isInitialBlockDownload(); }
769 : 0 : bool isInitialBlockDownload() override
770 : : {
771 : 0 : return chainman().IsInitialBlockDownload();
772 : : }
773 : 0 : bool shutdownRequested() override { return ShutdownRequested(m_node); }
774 : 0 : void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
775 : 0 : void initWarning(const bilingual_str& message) override { InitWarning(message); }
776 : 0 : void initError(const bilingual_str& message) override { InitError(message); }
777 : 0 : void showProgress(const std::string& title, int progress, bool resume_possible) override
778 : : {
779 : 0 : ::uiInterface.ShowProgress(title, progress, resume_possible);
780 : 0 : }
781 : 0 : std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) override
782 : : {
783 : 0 : return std::make_unique<NotificationsHandlerImpl>(validation_signals(), std::move(notifications));
784 : : }
785 : 0 : void waitForNotificationsIfTipChanged(const uint256& old_tip) override
786 : : {
787 [ # # # # : 0 : if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
# # # # ]
788 : 0 : validation_signals().SyncWithValidationInterfaceQueue();
789 : 0 : }
790 : 0 : std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
791 : : {
792 : 0 : return std::make_unique<RpcHandlerImpl>(command);
793 : : }
794 : 0 : bool rpcEnableDeprecated(const std::string& method) override { return IsDeprecatedRPCEnabled(method); }
795 : 0 : void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) override
796 : : {
797 [ # # ]: 0 : RPCRunLater(name, std::move(fn), seconds);
798 : 0 : }
799 : 0 : common::SettingsValue getSetting(const std::string& name) override
800 : : {
801 : 0 : return args().GetSetting(name);
802 : : }
803 : 0 : std::vector<common::SettingsValue> getSettingsList(const std::string& name) override
804 : : {
805 : 0 : return args().GetSettingsList(name);
806 : : }
807 : 0 : common::SettingsValue getRwSetting(const std::string& name) override
808 : : {
809 : 0 : common::SettingsValue result;
810 [ # # # # ]: 0 : args().LockSettings([&](const common::Settings& settings) {
811 [ # # ]: 0 : if (const common::SettingsValue* value = common::FindKey(settings.rw_settings, name)) {
812 : 0 : result = *value;
813 : 0 : }
814 : 0 : });
815 : 0 : return result;
816 [ # # ]: 0 : }
817 : 0 : bool updateRwSetting(const std::string& name,
818 : : const interfaces::SettingsUpdate& update_settings_func) override
819 : : {
820 : 0 : std::optional<interfaces::SettingsAction> action;
821 : 0 : args().LockSettings([&](common::Settings& settings) {
822 : 0 : auto* ptr_value = common::FindKey(settings.rw_settings, name);
823 : : // Create value if it doesn't exist
824 [ # # ]: 0 : auto& value = ptr_value ? *ptr_value : settings.rw_settings[name];
825 : 0 : action = update_settings_func(value);
826 : 0 : });
827 [ # # ]: 0 : if (!action) return false;
828 : : // Now dump value to disk if requested
829 [ # # ]: 0 : return *action == interfaces::SettingsAction::SKIP_WRITE || args().WriteSettingsFile();
830 : 0 : }
831 : 0 : bool overwriteRwSetting(const std::string& name, common::SettingsValue& value, bool write) override
832 : : {
833 [ # # ]: 0 : if (value.isNull()) return deleteRwSettings(name, write);
834 [ # # ]: 0 : return updateRwSetting(name, [&](common::SettingsValue& settings) {
835 : 0 : settings = std::move(value);
836 : 0 : return write ? interfaces::SettingsAction::WRITE : interfaces::SettingsAction::SKIP_WRITE;
837 : : });
838 : 0 : }
839 : 0 : bool deleteRwSettings(const std::string& name, bool write) override
840 : : {
841 : 0 : args().LockSettings([&](common::Settings& settings) {
842 : 0 : settings.rw_settings.erase(name);
843 : 0 : });
844 [ # # ]: 0 : return !write || args().WriteSettingsFile();
845 : : }
846 : 0 : void requestMempoolTransactions(Notifications& notifications) override
847 : : {
848 [ # # ]: 0 : if (!m_node.mempool) return;
849 [ # # ]: 0 : LOCK2(::cs_main, m_node.mempool->cs);
850 [ # # # # ]: 0 : for (const CTxMemPoolEntry& entry : m_node.mempool->entryAll()) {
851 [ # # # # ]: 0 : notifications.transactionAddedToMempool(entry.GetSharedTx());
852 : 0 : }
853 : 0 : }
854 : 0 : bool hasAssumedValidChain() override
855 : : {
856 : 0 : return chainman().IsSnapshotActive();
857 : : }
858 : :
859 : 0 : NodeContext* context() override { return &m_node; }
860 : 0 : ArgsManager& args() { return *Assert(m_node.args); }
861 : 595479 : ChainstateManager& chainman() { return *Assert(m_node.chainman); }
862 : 0 : ValidationSignals& validation_signals() { return *Assert(m_node.validation_signals); }
863 : : NodeContext& m_node;
864 : : };
865 : :
866 : : class MinerImpl : public Mining
867 : : {
868 : : public:
869 : 0 : explicit MinerImpl(NodeContext& node) : m_node(node) {}
870 : :
871 : 0 : bool isTestChain() override
872 : : {
873 : 0 : return chainman().GetParams().IsTestChain();
874 : : }
875 : :
876 : 0 : bool isInitialBlockDownload() override
877 : : {
878 : 0 : return chainman().IsInitialBlockDownload();
879 : : }
880 : :
881 : 0 : std::optional<uint256> getTipHash() override
882 : : {
883 : 0 : LOCK(::cs_main);
884 [ # # # # ]: 0 : CBlockIndex* tip{chainman().ActiveChain().Tip()};
885 [ # # ]: 0 : if (!tip) return {};
886 : 0 : return tip->GetBlockHash();
887 : 0 : }
888 : :
889 : 0 : bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
890 : : {
891 : 0 : return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
892 : : }
893 : :
894 : 0 : unsigned int getTransactionsUpdated() override
895 : : {
896 : 0 : return context()->mempool->GetTransactionsUpdated();
897 : : }
898 : :
899 : 0 : bool testBlockValidity(const CBlock& block, bool check_merkle_root, BlockValidationState& state) override
900 : : {
901 : 0 : LOCK(cs_main);
902 [ # # # # ]: 0 : CBlockIndex* tip{chainman().ActiveChain().Tip()};
903 : : // Fail if the tip updated before the lock was taken
904 [ # # # # ]: 0 : if (block.hashPrevBlock != tip->GetBlockHash()) {
905 [ # # # # ]: 0 : state.Error("Block does not connect to current chain tip.");
906 : 0 : return false;
907 : : }
908 : :
909 [ # # # # : 0 : return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root);
# # # # ]
910 : 0 : }
911 : :
912 : 0 : std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override
913 : : {
914 [ # # # # ]: 0 : BlockAssembler::Options assemble_options{options};
915 : : ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
916 : : return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key);
917 : : }
918 : :
919 : 0 : NodeContext* context() override { return &m_node; }
920 : 0 : ChainstateManager& chainman() { return *Assert(m_node.chainman); }
921 : : NodeContext& m_node;
922 : : };
923 : : } // namespace
924 : : } // namespace node
925 : :
926 : : namespace interfaces {
927 : 0 : std::unique_ptr<Node> MakeNode(node::NodeContext& context) { return std::make_unique<node::NodeImpl>(context); }
928 : 1826 : std::unique_ptr<Chain> MakeChain(node::NodeContext& context) { return std::make_unique<node::ChainImpl>(context); }
929 : 0 : std::unique_ptr<Mining> MakeMining(node::NodeContext& context) { return std::make_unique<node::MinerImpl>(context); }
930 : : } // namespace interfaces
|