Bitcoin ABC 0.33.6
P2P Digital Currency
interfaces.cpp
Go to the documentation of this file.
1// Copyright (c) 2018 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 <chain.h>
8#include <chainparams.h>
9#include <common/args.h>
10#include <config.h>
11#include <init.h>
12#include <interfaces/chain.h>
13#include <interfaces/handler.h>
14#include <interfaces/node.h>
15#include <interfaces/wallet.h>
17#include <mapport.h>
18#include <net.h>
19#include <net_processing.h>
20#include <netaddress.h>
21#include <netbase.h>
22#include <node/blockstorage.h>
23#include <node/coin.h>
24#include <node/context.h>
25#include <node/transaction.h>
26#include <node/types.h>
27#include <node/ui_interface.h>
28#include <policy/settings.h>
29#include <primitives/block.h>
31#include <rpc/blockchain.h>
32#include <rpc/protocol.h>
33#include <rpc/server.h>
34#include <shutdown.h>
35#include <sync.h>
36#include <txmempool.h>
37#include <uint256.h>
38#include <util/check.h>
39#include <util/translation.h>
40#include <validation.h>
41#include <validationinterface.h>
42#include <warnings.h>
43
44#if defined(HAVE_CONFIG_H)
45#include <config/bitcoin-config.h>
46#endif
47
48#include <univalue.h>
49
50#include <boost/signals2/signal.hpp>
51
52#include <memory>
53#include <utility>
54
56
64
65namespace node {
66namespace {
67
68 class NodeImpl : public Node {
69 private:
70 ChainstateManager &chainman() { return *Assert(m_context->chainman); }
71
72 public:
73 explicit NodeImpl(NodeContext *context) { setContext(context); }
74 void initLogging() override { InitLogging(*Assert(m_context->args)); }
75 void initParameterInteraction() override {
77 }
78 bilingual_str getWarnings() override { return GetWarnings(true); }
79 int getExitStatus() override {
80 return Assert(m_context)->exit_status.load();
81 }
82 bool baseInitialize(Config &config) override {
83 if (!AppInitBasicSetup(gArgs, Assert(context())->exit_status)) {
84 return false;
85 }
86 if (!AppInitParameterInteraction(config, gArgs)) {
87 return false;
88 }
89
90 m_context->kernel = std::make_unique<kernel::Context>();
91 if (!AppInitSanityChecks(*m_context->kernel)) {
92 return false;
93 }
94
96 return false;
97 }
99 return false;
100 }
101
102 return true;
103 }
104 bool appInitMain(Config &config, RPCServer &rpcServer,
105 HTTPRPCRequestProcessor &httpRPCRequestProcessor,
106 interfaces::BlockAndHeaderTipInfo *tip_info) override {
107 if (AppInitMain(config, rpcServer, httpRPCRequestProcessor,
108 *m_context, tip_info)) {
109 return true;
110 }
111 // Error during initialization, set exit status before continue
112 m_context->exit_status.store(EXIT_FAILURE);
113 return false;
114 }
115 void appShutdown() override {
118 }
119 void startShutdown() override {
121 // Stop RPC for clean shutdown if any of waitfor* commands is
122 // executed.
123 if (gArgs.GetBoolArg("-server", false)) {
124 InterruptRPC();
125 StopRPC();
126 }
127 }
128 bool shutdownRequested() override { return ShutdownRequested(); }
129 bool isPersistentSettingIgnored(const std::string &name) override {
130 bool ignored = false;
131 gArgs.LockSettings([&](util::Settings &settings) {
132 if (auto *options =
134 ignored = !options->empty();
135 }
136 });
137 return ignored;
138 }
140 getPersistentSetting(const std::string &name) override {
142 }
143 void updateRwSetting(const std::string &name,
144 const util::SettingsValue &value) override {
145 gArgs.LockSettings([&](util::Settings &settings) {
146 if (value.isNull()) {
147 settings.rw_settings.erase(name);
148 } else {
149 settings.rw_settings[name] = value;
150 }
151 });
153 }
154 void forceSetting(const std::string &name,
155 const util::SettingsValue &value) override {
156 gArgs.LockSettings([&](util::Settings &settings) {
157 if (value.isNull()) {
158 settings.forced_settings.erase(name);
159 } else {
160 settings.forced_settings[name] = value;
161 }
162 });
163 }
164 void resetSettings() override {
165 gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
166 gArgs.LockSettings([&](util::Settings &settings) {
167 settings.rw_settings.clear();
168 });
170 }
171 void mapPort(bool enable) override { StartMapPort(enable); }
172 bool getProxy(Network net, Proxy &proxy_info) override {
173 return GetProxy(net, proxy_info);
174 }
175 size_t getNodeCount(ConnectionDirection flags) override {
176 return m_context->connman ? m_context->connman->GetNodeCount(flags)
177 : 0;
178 }
179 bool getNodesStats(NodesStats &stats) override {
180 stats.clear();
181
182 if (m_context->connman) {
183 std::vector<CNodeStats> stats_temp;
184 m_context->connman->GetNodeStats(stats_temp);
185
186 stats.reserve(stats_temp.size());
187 for (auto &node_stats_temp : stats_temp) {
188 stats.emplace_back(std::move(node_stats_temp), false,
190 }
191
192 // Try to retrieve the CNodeStateStats for each node.
193 if (m_context->peerman) {
194 TRY_LOCK(::cs_main, lockMain);
195 if (lockMain) {
196 for (auto &node_stats : stats) {
197 std::get<1>(node_stats) =
198 m_context->peerman->GetNodeStateStats(
199 std::get<0>(node_stats).nodeid,
200 std::get<2>(node_stats));
201 }
202 }
203 }
204 return true;
205 }
206 return false;
207 }
208 bool getBanned(banmap_t &banmap) override {
209 if (m_context->banman) {
210 m_context->banman->GetBanned(banmap);
211 return true;
212 }
213 return false;
214 }
215 bool ban(const CNetAddr &net_addr, int64_t ban_time_offset) override {
216 if (m_context->banman) {
217 m_context->banman->Ban(net_addr, ban_time_offset);
218 return true;
219 }
220 return false;
221 }
222 bool unban(const CSubNet &ip) override {
223 if (m_context->banman) {
224 m_context->banman->Unban(ip);
225 return true;
226 }
227 return false;
228 }
229 bool disconnectByAddress(const CNetAddr &net_addr) override {
230 if (m_context->connman) {
231 return m_context->connman->DisconnectNode(net_addr);
232 }
233 return false;
234 }
235 bool disconnectById(NodeId id) override {
236 if (m_context->connman) {
237 return m_context->connman->DisconnectNode(id);
238 }
239 return false;
240 }
241 int64_t getTotalBytesRecv() override {
242 return m_context->connman ? m_context->connman->GetTotalBytesRecv()
243 : 0;
244 }
245 int64_t getTotalBytesSent() override {
246 return m_context->connman ? m_context->connman->GetTotalBytesSent()
247 : 0;
248 }
249 size_t getMempoolSize() override {
250 return m_context->mempool ? m_context->mempool->size() : 0;
251 }
252 size_t getMempoolDynamicUsage() override {
253 return m_context->mempool ? m_context->mempool->DynamicMemoryUsage()
254 : 0;
255 }
256 bool getHeaderTip(int &height, int64_t &block_time) override {
258 auto best_header = chainman().m_best_header;
259 if (best_header) {
260 height = best_header->nHeight;
261 block_time = best_header->GetBlockTime();
262 return true;
263 }
264 return false;
265 }
266 int getNumBlocks() override {
268 return chainman().ActiveChain().Height();
269 }
270 BlockHash getBestBlockHash() override {
271 const CBlockIndex *tip =
272 WITH_LOCK(::cs_main, return chainman().ActiveTip());
273 return tip ? tip->GetBlockHash()
274 : chainman().GetParams().GenesisBlock().GetHash();
275 }
276 int64_t getLastBlockTime() override {
278 if (chainman().ActiveChain().Tip()) {
279 return chainman().ActiveChain().Tip()->GetBlockTime();
280 }
281 // Genesis block's time of current network
282 return chainman().GetParams().GenesisBlock().GetBlockTime();
283 }
284 double getVerificationProgress() override {
285 const CBlockIndex *tip;
286 {
288 tip = chainman().ActiveChain().Tip();
289 }
290 return GuessVerificationProgress(chainman().GetParams().TxData(),
291 tip);
292 }
293 bool isInitialBlockDownload() override {
294 return chainman().IsInitialBlockDownload();
295 }
296 bool isLoadingBlocks() override {
297 return chainman().m_blockman.LoadingBlocks();
298 }
299 void setNetworkActive(bool active) override {
300 if (m_context->connman) {
301 m_context->connman->SetNetworkActive(active);
302 }
303 }
304 bool getNetworkActive() override {
305 return m_context->connman && m_context->connman->GetNetworkActive();
306 }
307 CFeeRate getDustRelayFee() override {
308 if (!m_context->mempool) {
310 }
311 return m_context->mempool->m_dust_relay_feerate;
312 }
313 UniValue executeRpc(const Config &config, const std::string &command,
314 const UniValue &params,
315 const std::string &uri) override {
316 JSONRPCRequest req;
317 req.context = m_context;
318 req.params = params;
319 req.strMethod = command;
320 req.URI = uri;
321 return ::tableRPC.execute(config, req);
322 }
323 std::vector<std::string> listRpcCommands() override {
325 }
326 void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface) override {
328 }
329 void rpcUnsetTimerInterface(RPCTimerInterface *iface) override {
331 }
332 std::optional<Coin> getUnspentOutput(const COutPoint &output) override {
334 return chainman().ActiveChainstate().CoinsTip().GetCoin(output);
335 }
336 WalletClient &walletClient() override {
337 return *Assert(m_context->wallet_client);
338 }
339 std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override {
340 return MakeHandler(::uiInterface.InitMessage_connect(fn));
341 }
342 std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override {
343 return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
344 }
345 std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override {
346 return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
347 }
348 std::unique_ptr<Handler>
349 handleShowProgress(ShowProgressFn fn) override {
350 return MakeHandler(::uiInterface.ShowProgress_connect(fn));
351 }
352 std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(
353 NotifyNumConnectionsChangedFn fn) override {
354 return MakeHandler(
355 ::uiInterface.NotifyNumConnectionsChanged_connect(fn));
356 }
357 std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(
358 NotifyNetworkActiveChangedFn fn) override {
359 return MakeHandler(
360 ::uiInterface.NotifyNetworkActiveChanged_connect(fn));
361 }
362 std::unique_ptr<Handler>
363 handleNotifyAlertChanged(NotifyAlertChangedFn fn) override {
364 return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn));
365 }
366 std::unique_ptr<Handler>
367 handleBannedListChanged(BannedListChangedFn fn) override {
368 return MakeHandler(::uiInterface.BannedListChanged_connect(fn));
369 }
370 std::unique_ptr<Handler>
371 handleNotifyBlockTip(NotifyBlockTipFn fn) override {
372 return MakeHandler(::uiInterface.NotifyBlockTip_connect(
373 [fn](SynchronizationState sync_state,
374 const CBlockIndex *block) {
375 fn(sync_state,
376 BlockTip{block->nHeight, block->GetBlockTime(),
377 block->GetBlockHash()},
378 GuessVerificationProgress(Params().TxData(), block));
379 }));
380 }
381 std::unique_ptr<Handler>
382 handleNotifyHeaderTip(NotifyHeaderTipFn fn) override {
383 /* verification progress is unused when a header was received */
384 return MakeHandler(::uiInterface.NotifyHeaderTip_connect(
385 [fn](SynchronizationState sync_state, int64_t height,
386 int64_t timestamp, bool presync) {
387 fn(sync_state,
388 BlockTip{int(height), timestamp, BlockHash{}}, presync);
389 }));
390 }
391 NodeContext *context() override { return m_context; }
392 void setContext(NodeContext *context) override { m_context = context; }
393 NodeContext *m_context{nullptr};
394 };
395
396 bool FillBlock(const CBlockIndex *index, const FoundBlock &block,
397 UniqueLock<RecursiveMutex> &lock, const CChain &active,
398 const BlockManager &blockman) {
399 if (!index) {
400 return false;
401 }
402 if (block.m_hash) {
403 *block.m_hash = index->GetBlockHash();
404 }
405 if (block.m_height) {
406 *block.m_height = index->nHeight;
407 }
408 if (block.m_time) {
409 *block.m_time = index->GetBlockTime();
410 }
411 if (block.m_max_time) {
412 *block.m_max_time = index->GetBlockTimeMax();
413 }
414 if (block.m_mtp_time) {
415 *block.m_mtp_time = index->GetMedianTimePast();
416 }
417 if (block.m_in_active_chain) {
418 *block.m_in_active_chain = active[index->nHeight] == index;
419 }
420 if (block.m_locator) {
421 *block.m_locator = GetLocator(index);
422 }
423 if (block.m_next_block) {
424 FillBlock(active[index->nHeight] == index
425 ? active[index->nHeight + 1]
426 : nullptr,
427 *block.m_next_block, lock, active, blockman);
428 }
429 if (block.m_data) {
430 REVERSE_LOCK(lock);
431 if (!blockman.ReadBlock(*block.m_data, *index)) {
432 block.m_data->SetNull();
433 }
434 }
435 return true;
436 }
437
438 class NotificationsProxy : public CValidationInterface {
439 public:
440 explicit NotificationsProxy(
441 std::shared_ptr<Chain::Notifications> notifications)
442 : m_notifications(std::move(notifications)) {}
443 virtual ~NotificationsProxy() = default;
444 void TransactionAddedToMempool(const CTransactionRef &tx,
445 std::shared_ptr<const std::vector<Coin>>,
446 uint64_t mempool_sequence) override {
447 m_notifications->transactionAddedToMempool(tx, mempool_sequence);
448 }
449 void TransactionRemovedFromMempool(const CTransactionRef &tx,
451 uint64_t mempool_sequence) override {
452 m_notifications->transactionRemovedFromMempool(tx, reason,
453 mempool_sequence);
454 }
455 void BlockConnected(ChainstateRole role,
456 const std::shared_ptr<const CBlock> &block,
457 const CBlockIndex *index) override {
458 m_notifications->blockConnected(role, *block, index->nHeight);
459 }
460 void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
461 const CBlockIndex *index) override {
462 m_notifications->blockDisconnected(*block, index->nHeight);
463 }
464 void UpdatedBlockTip(const CBlockIndex *index,
465 const CBlockIndex *fork_index,
466 bool is_ibd) override {
467 m_notifications->updatedBlockTip();
468 }
469 void ChainStateFlushed(ChainstateRole role,
470 const CBlockLocator &locator) override {
471 m_notifications->chainStateFlushed(role, locator);
472 }
473 std::shared_ptr<Chain::Notifications> m_notifications;
474 };
475
476 class NotificationsHandlerImpl : public Handler {
477 public:
478 explicit NotificationsHandlerImpl(
479 std::shared_ptr<Chain::Notifications> notifications)
480 : m_proxy(std::make_shared<NotificationsProxy>(
481 std::move(notifications))) {
483 }
484 ~NotificationsHandlerImpl() override { disconnect(); }
485 void disconnect() override {
486 if (m_proxy) {
488 m_proxy.reset();
489 }
490 }
491 std::shared_ptr<NotificationsProxy> m_proxy;
492 };
493
494 class RpcHandlerImpl : public Handler {
495 public:
496 explicit RpcHandlerImpl(const CRPCCommand &command)
497 : m_command(command), m_wrapped_command(&command) {
498 m_command.actor = [this](const Config &config,
499 const JSONRPCRequest &request,
500 UniValue &result, bool last_handler) {
501 if (!m_wrapped_command) {
502 return false;
503 }
504 try {
505 return m_wrapped_command->actor(config, request, result,
506 last_handler);
507 } 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
510 // can try to handle the request. Otherwise, reraise the
511 // exception.
512 if (!last_handler) {
513 const UniValue &code = e["code"];
514 if (code.isNum() &&
515 code.getInt<int>() == RPC_WALLET_NOT_FOUND) {
516 return false;
517 }
518 }
519 throw;
520 }
521 };
523 }
524
525 void disconnect() final {
526 if (m_wrapped_command) {
527 m_wrapped_command = nullptr;
529 }
530 }
531
532 ~RpcHandlerImpl() override { disconnect(); }
533
536 };
537
538 class ChainImpl : public Chain {
539 private:
540 ChainstateManager &chainman() { return *Assert(m_node.chainman); }
541
542 public:
543 explicit ChainImpl(NodeContext &node, const CChainParams &params)
544 : m_node(node), m_params(params) {}
545 std::optional<int> getHeight() override {
547 const CChain &active = Assert(m_node.chainman)->ActiveChain();
548 int height = active.Height();
549 if (height >= 0) {
550 return height;
551 }
552 return std::nullopt;
553 }
554 BlockHash getBlockHash(int height) override {
556 const CChain &active = Assert(m_node.chainman)->ActiveChain();
557 CBlockIndex *block = active[height];
558 assert(block);
559 return block->GetBlockHash();
560 }
561 bool haveBlockOnDisk(int height) override {
562 LOCK(cs_main);
563 const CChain &active = Assert(m_node.chainman)->ActiveChain();
564 CBlockIndex *block = active[height];
565 return block && (block->nStatus.hasData() != 0) && block->nTx > 0;
566 }
567 CBlockLocator getTipLocator() override {
568 LOCK(cs_main);
569 const CChain &active = Assert(m_node.chainman)->ActiveChain();
570 return active.GetLocator();
571 }
572 // TODO: backport core#25036 with changes from core#25717
573 std::optional<int>
574 findLocatorFork(const CBlockLocator &locator) override {
575 LOCK(cs_main);
576 const Chainstate &active =
577 Assert(m_node.chainman)->ActiveChainstate();
578 if (const CBlockIndex *fork =
579 active.FindForkInGlobalIndex(locator)) {
580 return fork->nHeight;
581 }
582 return std::nullopt;
583 }
584 bool findBlock(const BlockHash &hash,
585 const FoundBlock &block) override {
586 WAIT_LOCK(cs_main, lock);
587 const CChain &active = Assert(m_node.chainman)->ActiveChain();
588 return FillBlock(m_node.chainman->m_blockman.LookupBlockIndex(hash),
589 block, lock, active, chainman().m_blockman);
590 }
591 bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height,
592 const FoundBlock &block) override {
593 WAIT_LOCK(cs_main, lock);
594 const CChain &active = Assert(m_node.chainman)->ActiveChain();
595 return FillBlock(active.FindEarliestAtLeast(min_time, min_height),
596 block, lock, active, chainman().m_blockman);
597 }
598 bool findAncestorByHeight(const BlockHash &block_hash,
599 int ancestor_height,
600 const FoundBlock &ancestor_out) override {
601 WAIT_LOCK(cs_main, lock);
602 const CChain &active = Assert(m_node.chainman)->ActiveChain();
603 if (const CBlockIndex *block =
604 m_node.chainman->m_blockman.LookupBlockIndex(block_hash)) {
605 if (const CBlockIndex *ancestor =
606 block->GetAncestor(ancestor_height)) {
607 return FillBlock(ancestor, ancestor_out, lock, active,
608 chainman().m_blockman);
609 }
610 }
611 return FillBlock(nullptr, ancestor_out, lock, active,
612 chainman().m_blockman);
613 }
614 bool findAncestorByHash(const BlockHash &block_hash,
615 const BlockHash &ancestor_hash,
616 const FoundBlock &ancestor_out) override {
617 WAIT_LOCK(cs_main, lock);
618 const CChain &active = Assert(m_node.chainman)->ActiveChain();
619 const CBlockIndex *block =
620 m_node.chainman->m_blockman.LookupBlockIndex(block_hash);
621 const CBlockIndex *ancestor =
622 m_node.chainman->m_blockman.LookupBlockIndex(ancestor_hash);
623 if (block && ancestor &&
624 block->GetAncestor(ancestor->nHeight) != ancestor) {
625 ancestor = nullptr;
626 }
627 return FillBlock(ancestor, ancestor_out, lock, active,
628 chainman().m_blockman);
629 }
630 bool findCommonAncestor(const BlockHash &block_hash1,
631 const BlockHash &block_hash2,
632 const FoundBlock &ancestor_out,
633 const FoundBlock &block1_out,
634 const FoundBlock &block2_out) override {
635 WAIT_LOCK(cs_main, lock);
636 const CChain &active = Assert(m_node.chainman)->ActiveChain();
637 const CBlockIndex *block1 =
638 m_node.chainman->m_blockman.LookupBlockIndex(block_hash1);
639 const CBlockIndex *block2 =
640 m_node.chainman->m_blockman.LookupBlockIndex(block_hash2);
641 const CBlockIndex *ancestor =
642 block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
643 // Using & instead of && below to avoid short circuiting and leaving
644 // output uninitialized. Cast bool to int to avoid
645 // -Wbitwise-instead-of-logical compiler warnings.
646 return int{FillBlock(ancestor, ancestor_out, lock, active,
647 chainman().m_blockman)} &
648 int{FillBlock(block1, block1_out, lock, active,
649 chainman().m_blockman)} &
650 int{FillBlock(block2, block2_out, lock, active,
651 chainman().m_blockman)};
652 }
653 void findCoins(std::map<COutPoint, Coin> &coins) override {
654 return FindCoins(m_node, coins);
655 }
656 double guessVerificationProgress(const BlockHash &block_hash) override {
657 LOCK(cs_main);
659 chainman().GetParams().TxData(),
660 chainman().m_blockman.LookupBlockIndex(block_hash));
661 }
662 bool hasBlocks(const BlockHash &block_hash, int min_height,
663 std::optional<int> max_height) override {
664 // hasBlocks returns true if all ancestors of block_hash in
665 // specified range have block data (are not pruned), false if any
666 // ancestors in specified range are missing data.
667 //
668 // For simplicity and robustness, min_height and max_height are only
669 // used to limit the range, and passing min_height that's too low or
670 // max_height that's too high will not crash or change the result.
672 if (const CBlockIndex *block =
673 chainman().m_blockman.LookupBlockIndex(block_hash)) {
674 if (max_height && block->nHeight >= *max_height) {
675 block = block->GetAncestor(*max_height);
676 }
677 for (; block->nStatus.hasData(); block = block->pprev) {
678 // Check pprev to not segfault if min_height is too low
679 if (block->nHeight <= min_height || !block->pprev) {
680 return true;
681 }
682 }
683 }
684 return false;
685 }
686 bool broadcastTransaction(const Config &config,
687 const CTransactionRef &tx,
688 const Amount &max_tx_fee, bool relay,
689 std::string &err_string) override {
690 const TransactionError err =
691 BroadcastTransaction(m_node, tx, err_string, max_tx_fee, relay,
692 /*wait_callback=*/false);
693 // Chain clients only care about failures to accept the tx to the
694 // mempool. Disregard non-mempool related failures. Note: this will
695 // need to be updated if BroadcastTransactions() is updated to
696 // return other non-mempool failures that Chain clients do not need
697 // to know about.
698 return err == TransactionError::OK;
699 }
700 CFeeRate estimateFee() const override {
701 if (!m_node.mempool) {
702 return {};
703 }
704 return m_node.mempool->estimateFee();
705 }
706 CFeeRate relayMinFee() override {
707 if (!m_node.mempool) {
709 }
710 return m_node.mempool->m_min_relay_feerate;
711 }
712 CFeeRate relayDustFee() override {
713 if (!m_node.mempool) {
715 }
716 return m_node.mempool->m_dust_relay_feerate;
717 }
718 bool havePruned() override {
719 LOCK(cs_main);
720 return m_node.chainman->m_blockman.m_have_pruned;
721 }
722 bool isReadyToBroadcast() override {
723 return !chainman().m_blockman.LoadingBlocks() &&
724 !isInitialBlockDownload();
725 }
726 std::optional<int> getPruneHeight() override {
727 LOCK(chainman().GetMutex());
728 return GetPruneHeight(chainman().m_blockman,
729 chainman().ActiveChain());
730 }
731 bool isInitialBlockDownload() override {
732 return chainman().IsInitialBlockDownload();
733 }
734 bool shutdownRequested() override { return ShutdownRequested(); }
735 void initMessage(const std::string &message) override {
736 ::uiInterface.InitMessage(message);
737 }
738 void initWarning(const bilingual_str &message) override {
739 InitWarning(message);
740 }
741 void initError(const bilingual_str &message) override {
742 InitError(message);
743 }
744 void showProgress(const std::string &title, int progress,
745 bool resume_possible) override {
746 ::uiInterface.ShowProgress(title, progress, resume_possible);
747 }
748 std::unique_ptr<Handler> handleNotifications(
749 std::shared_ptr<Notifications> notifications) override {
750 return std::make_unique<NotificationsHandlerImpl>(
751 std::move(notifications));
752 }
753 void
754 waitForNotificationsIfTipChanged(const BlockHash &old_tip) override {
755 if (!old_tip.IsNull()) {
757 const CChain &active = Assert(m_node.chainman)->ActiveChain();
758 if (old_tip == active.Tip()->GetBlockHash()) {
759 return;
760 }
761 }
763 }
764
765 std::unique_ptr<Handler>
766 handleRpc(const CRPCCommand &command) override {
767 return std::make_unique<RpcHandlerImpl>(command);
768 }
769 bool rpcEnableDeprecated(const std::string &method) override {
770 return IsDeprecatedRPCEnabled(gArgs, method);
771 }
772 void rpcRunLater(const std::string &name, std::function<void()> fn,
773 int64_t seconds) override {
774 RPCRunLater(name, std::move(fn), seconds);
775 }
776 util::SettingsValue getSetting(const std::string &name) override {
777 return gArgs.GetSetting(name);
778 }
779 std::vector<util::SettingsValue>
780 getSettingsList(const std::string &name) override {
781 return gArgs.GetSettingsList(name);
782 }
783 util::SettingsValue getRwSetting(const std::string &name) override {
784 util::SettingsValue result;
785 gArgs.LockSettings([&](const util::Settings &settings) {
786 if (const util::SettingsValue *value =
787 util::FindKey(settings.rw_settings, name)) {
788 result = *value;
789 }
790 });
791 return result;
792 }
793 bool updateRwSetting(const std::string &name,
794 const util::SettingsValue &value,
795 bool write) override {
796 gArgs.LockSettings([&](util::Settings &settings) {
797 if (value.isNull()) {
798 settings.rw_settings.erase(name);
799 } else {
800 settings.rw_settings[name] = value;
801 }
802 });
803 return !write || gArgs.WriteSettingsFile();
804 }
805 void requestMempoolTransactions(Notifications &notifications) override {
806 if (!m_node.mempool) {
807 return;
808 }
809 LOCK2(::cs_main, m_node.mempool->cs);
810 for (const CTxMemPoolEntryRef &entry : m_node.mempool->mapTx) {
811 notifications.transactionAddedToMempool(entry->GetSharedTx(),
812 /*mempool_sequence=*/0);
813 }
814 }
815 bool hasAssumedValidChain() override {
816 return Assert(m_node.chainman)->IsSnapshotActive();
817 }
818 const CChainParams &params() const override { return m_params; }
819 NodeContext *context() override { return &m_node; }
820 NodeContext &m_node;
822 };
823} // namespace
824} // namespace node
825
826namespace interfaces {
827std::unique_ptr<Node> MakeNode(node::NodeContext *context) {
828 return std::make_unique<node::NodeImpl>(context);
829}
830std::unique_ptr<Chain> MakeChain(node::NodeContext &node,
831 const CChainParams &params) {
832 return std::make_unique<node::ChainImpl>(node, params);
833}
834} // namespace interfaces
ArgsManager gArgs
Definition: args.cpp:39
int flags
Definition: bitcoin-tx.cpp:546
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
Definition: blockchain.cpp:852
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
Definition: chain.cpp:41
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:112
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
#define Assert(val)
Identity function.
Definition: check.h:84
util::SettingsValue GetPersistentSetting(const std::string &name) const
Get current setting from config file or read/write settings file, ignoring nonpersistent command line...
Definition: args.cpp:451
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: args.h:422
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:425
std::vector< util::SettingsValue > GetSettingsList(const std::string &arg) const
Get list of setting values.
Definition: args.cpp:829
util::SettingsValue GetSetting(const std::string &arg) const
Get setting value.
Definition: args.cpp:820
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:524
void SetNull()
Definition: block.h:82
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
int64_t GetBlockTime() const
Definition: blockindex.h:160
int64_t GetMedianTimePast() const
Definition: blockindex.h:172
int64_t GetBlockTimeMax() const
Definition: blockindex.h:162
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:55
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:62
BlockHash GetBlockHash() const
Definition: blockindex.h:130
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
An in-memory indexed chain of blocks.
Definition: chain.h:138
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:154
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
Definition: chain.cpp:62
int Height() const
Return the maximal height in the chain.
Definition: chain.h:190
CBlockLocator GetLocator() const
Return a CBlockLocator that refers to the tip of this chain.
Definition: chain.cpp:45
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Network address.
Definition: netaddress.h:114
std::string name
Definition: server.h:176
Actor actor
Definition: server.h:177
bool removeCommand(const std::string &name, const CRPCCommand *pcmd)
Definition: server.cpp:338
std::vector< std::string > listCommands() const
Returns a list of registered commands.
Definition: server.cpp:626
UniValue execute(const Config &config, const JSONRPCRequest &request) const
Execute a method.
Definition: server.cpp:585
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:330
Implement this to subscribe to events generated in validation.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:739
void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * FindForkInGlobalIndex(const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the last common block of this chain and a locator.
Definition: validation.cpp:126
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1191
Definition: config.h:19
UniValue params
Definition: request.h:34
std::string strMethod
Definition: request.h:33
std::string URI
Definition: request.h:36
std::any context
Definition: request.h:39
Definition: netbase.h:67
Definition: rcu.h:85
Class for registering and managing all RPC calls.
Definition: server.h:40
RPC timer "driver".
Definition: server.h:100
bool isNull() const
Definition: univalue.h:104
Int getInt() const
Definition: univalue.h:157
bool isNum() const
Definition: univalue.h:109
Wrapper around std::unique_lock style lock for Mutex.
Definition: sync.h:168
bool IsNull() const
Definition: uint256.h:32
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:136
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:55
const FoundBlock * m_next_block
Definition: chain.h:107
BlockHash * m_hash
Definition: chain.h:100
int64_t * m_max_time
Definition: chain.h:103
int64_t * m_time
Definition: chain.h:102
bool * m_in_active_chain
Definition: chain.h:105
int64_t * m_mtp_time
Definition: chain.h:104
CBlockLocator * m_locator
Definition: chain.h:106
Generic interface for managing an event handler or callback function registered with another interfac...
Definition: handler.h:22
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:59
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:304
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
int64_t NodeId
Definition: eviction.h:16
void Interrupt(NodeContext &node)
Interrupt threads.
Definition: init.cpp:209
void InitLogging(const ArgsManager &args)
Initialize global loggers.
Definition: init.cpp:1768
bool AppInitLockDataDirectory()
Lock bitcoin data directory.
Definition: init.cpp:2159
void Shutdown(NodeContext &node)
Definition: init.cpp:233
bool AppInitBasicSetup(const ArgsManager &args, std::atomic< int > &exit_status)
Initialize bitcoin: Basic context setup.
Definition: init.cpp:1795
bool AppInitMain(Config &config, RPCServer &rpcServer, HTTPRPCRequestProcessor &httpRPCRequestProcessor, NodeContext &node, interfaces::BlockAndHeaderTipInfo *tip_info)
Bitcoin main initialization.
Definition: init.cpp:2181
bool AppInitInterfaces(NodeContext &node)
Initialize node and wallet interface pointers.
Definition: init.cpp:2171
void InitParameterInteraction(ArgsManager &args)
Parameter interaction: change current parameters depending on various rules.
Definition: init.cpp:1644
bool AppInitParameterInteraction(Config &config, const ArgsManager &args)
Initialization: parameter interaction.
Definition: init.cpp:1837
bool AppInitSanityChecks(const kernel::Context &kernel)
Initialization sanity checks.
Definition: init.cpp:2143
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:14
void StartMapPort(bool enable)
Definition: mapport.cpp:176
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
Definition: handler.cpp:48
std::unique_ptr< Node > MakeNode(node::NodeContext *context)
Return implementation of Node interface.
Definition: interfaces.cpp:827
std::unique_ptr< Chain > MakeChain(node::NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: interfaces.cpp:830
Definition: messages.h:12
TransactionError BroadcastTransaction(const NodeContext &node, const CTransactionRef tx, std::string &err_string, const Amount max_tx_fee, bool relay, bool wait_callback)
Submit a transaction to the mempool and (optionally) relay it to all P2P peers.
Definition: transaction.cpp:38
TransactionError
Definition: types.h:17
void FindCoins(const NodeContext &node, std::map< COutPoint, Coin > &coins)
Look up unspent output information.
Definition: coin.cpp:12
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:115
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
Network
A network type.
Definition: netaddress.h:37
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:809
ConnectionDirection
Definition: netbase.h:37
NodeContext & m_node
Definition: interfaces.cpp:820
NodeContext * m_context
Definition: interfaces.cpp:393
std::shared_ptr< Chain::Notifications > m_notifications
Definition: interfaces.cpp:473
CRPCCommand m_command
Definition: interfaces.cpp:534
const CChainParams & m_params
Definition: interfaces.cpp:821
const CRPCCommand * m_wrapped_command
Definition: interfaces.cpp:535
std::shared_ptr< NotificationsProxy > m_proxy
Definition: interfaces.cpp:491
is a home for public enum and struct type definitions that are used by internally by node code,...
static constexpr Amount DUST_RELAY_TX_FEE(1000 *SATOSHI)
Min feerate for defining dust.
static constexpr Amount DEFAULT_MIN_RELAY_TX_FEE_PER_KB(1000 *SATOSHI)
Default for -minrelaytxfee, minimum relay fee for transactions.
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
const char * name
Definition: rest.cpp:47
@ RPC_WALLET_NOT_FOUND
Invalid wallet specified.
Definition: protocol.h:109
void RPCSetTimerInterfaceIfUnset(RPCTimerInterface *iface)
Set the factory function for timer, but only, if unset.
Definition: server.cpp:652
bool IsDeprecatedRPCEnabled(const ArgsManager &args, const std::string &method)
Definition: server.cpp:410
void RPCUnsetTimerInterface(RPCTimerInterface *iface)
Unset factory function for timers.
Definition: server.cpp:662
void RPCRunLater(const std::string &name, std::function< void()> func, int64_t nSeconds)
Run func nSeconds from now.
Definition: server.cpp:668
void StopRPC()
Definition: server.cpp:368
void InterruptRPC()
Definition: server.cpp:357
CRPCTable tableRPC
Definition: server.cpp:683
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:29
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:16
Definition: amount.h:21
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:108
Bilingual messages:
Definition: translation.h:17
Block and header tip information.
Definition: node.h:50
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:273
NodeContext struct containing references to chain state and connection state.
Definition: context.h:48
Stored settings.
Definition: settings.h:31
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:35
#define WAIT_LOCK(cs, name)
Definition: sync.h:317
#define LOCK2(cs1, cs2)
Definition: sync.h:309
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
#define TRY_LOCK(cs, name)
Definition: sync.h:314
#define REVERSE_LOCK(g)
Definition: sync.h:265
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:158
CClientUIInterface uiInterface
void InitWarning(const bilingual_str &str)
Show warning message.
bool InitError(const bilingual_str &str)
Show error message.
double GuessVerificationProgress(const ChainTxData &data, const CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index require cs_main if pindex h...
assert(!tx.IsCoinBase())
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:118
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.
Definition: warnings.cpp:43