Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-06-01 16:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/zip/work/bitcoin/src/node/interfaces.cpp
Line
Count
Source
1
// Copyright (c) 2018-present 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 <bitcoin-build-config.h> // IWYU pragma: keep
6
7
#include <banman.h>
8
#include <blockfilter.h>
9
#include <btcsignals.h>
10
#include <chain.h>
11
#include <chainparams.h>
12
#include <coins.h>
13
#include <common/args.h>
14
#include <common/settings.h>
15
#include <consensus/amount.h>
16
#include <consensus/merkle.h>
17
#include <consensus/validation.h>
18
#include <external_signer.h>
19
#include <httprpc.h>
20
#include <index/blockfilterindex.h>
21
#include <init.h>
22
#include <interfaces/chain.h>
23
#include <interfaces/handler.h>
24
#include <interfaces/mining.h>
25
#include <interfaces/node.h>
26
#include <interfaces/rpc.h>
27
#include <interfaces/types.h>
28
#include <kernel/context.h>
29
#include <key.h>
30
#include <logging.h>
31
#include <mapport.h>
32
#include <net.h>
33
#include <net_processing.h>
34
#include <net_types.h>
35
#include <netaddress.h>
36
#include <netbase.h>
37
#include <node/blockstorage.h>
38
#include <node/coin.h>
39
#include <node/context.h>
40
#include <node/interface_ui.h>
41
#include <node/kernel_notifications.h>
42
#include <node/miner.h>
43
#include <node/mini_miner.h>
44
#include <node/mining_args.h>
45
#include <node/mining_types.h>
46
#include <node/transaction.h>
47
#include <node/types.h>
48
#include <node/warnings.h>
49
#include <policy/feerate.h>
50
#include <policy/fees/block_policy_estimator.h>
51
#include <policy/policy.h>
52
#include <policy/rbf.h>
53
#include <primitives/block.h>
54
#include <primitives/transaction.h>
55
#include <rpc/blockchain.h>
56
#include <rpc/protocol.h>
57
#include <rpc/request.h>
58
#include <rpc/server.h>
59
#include <sync.h>
60
#include <txmempool.h>
61
#include <uint256.h>
62
#include <univalue.h>
63
#include <util/check.h>
64
#include <util/result.h>
65
#include <util/signalinterrupt.h>
66
#include <util/string.h>
67
#include <util/time.h>
68
#include <util/translation.h>
69
#include <validation.h>
70
#include <validationinterface.h>
71
72
#include <any>
73
#include <atomic>
74
#include <condition_variable>
75
#include <cstdint>
76
#include <cstdlib>
77
#include <functional>
78
#include <map>
79
#include <memory>
80
#include <optional>
81
#include <string>
82
#include <tuple>
83
#include <utility>
84
#include <vector>
85
86
using interfaces::BlockRef;
87
using interfaces::BlockTemplate;
88
using interfaces::BlockTip;
89
using interfaces::Chain;
90
using interfaces::FoundBlock;
91
using interfaces::Handler;
92
using interfaces::MakeSignalHandler;
93
using interfaces::Mining;
94
using interfaces::Node;
95
using interfaces::Rpc;
96
using interfaces::WalletLoader;
97
using kernel::ChainstateRole;
98
using node::BlockAssembler;
99
using node::BlockCreateOptions;
100
using node::BlockWaitOptions;
101
using node::CoinbaseTx;
102
using util::Join;
103
104
namespace node {
105
// All members of the classes in this namespace are intentionally public, as the
106
// classes themselves are private.
107
namespace {
108
#ifdef ENABLE_EXTERNAL_SIGNER
109
class ExternalSignerImpl : public interfaces::ExternalSigner
110
{
111
public:
112
    ExternalSignerImpl(::ExternalSigner signer) : m_signer(std::move(signer)) {}
113
    std::string getName() override { return m_signer.m_name; }
114
    ::ExternalSigner m_signer;
115
};
116
#endif
117
118
class NodeImpl : public Node
119
{
120
public:
121
0
    explicit NodeImpl(NodeContext& context) { setContext(&context); }
122
0
    void initLogging() override { InitLogging(args()); }
123
0
    void initParameterInteraction() override { InitParameterInteraction(args()); }
124
0
    bilingual_str getWarnings() override { return Join(Assert(m_context->warnings)->GetMessages(), Untranslated("<hr />")); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
125
0
    int getExitStatus() override { return Assert(m_context)->exit_status.load(); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
126
0
    BCLog::CategoryMask getLogCategories() override { return LogInstance().GetCategoryMask(); }
127
    bool baseInitialize() override
128
0
    {
129
0
        if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false;
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
130
0
        if (!AppInitParameterInteraction(args())) return false;
131
132
0
        m_context->warnings = std::make_unique<node::Warnings>();
133
0
        m_context->kernel = std::make_unique<kernel::Context>();
134
0
        m_context->ecc_context = std::make_unique<ECC_Context>();
135
0
        if (!AppInitSanityChecks(*m_context->kernel)) return false;
136
137
0
        if (!AppInitLockDirectories()) return false;
138
0
        if (!AppInitInterfaces(*m_context)) return false;
139
140
0
        return true;
141
0
    }
142
    bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
143
0
    {
144
0
        if (AppInitMain(*m_context, tip_info)) return true;
145
        // Error during initialization, set exit status before continue
146
0
        m_context->exit_status.store(EXIT_FAILURE);
147
0
        return false;
148
0
    }
149
    void appShutdown() override
150
0
    {
151
0
        Shutdown(*m_context);
152
0
    }
153
    void startShutdown() override
154
0
    {
155
0
        NodeContext& ctx{*Assert(m_context)};
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
156
0
        if (!(Assert(ctx.shutdown_request))()) {
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
157
0
            LogError("Failed to send shutdown signal\n");
Line
Count
Source
127
0
#define LogError(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
158
0
        }
159
0
        Interrupt(*m_context);
160
0
    }
161
0
    bool shutdownRequested() override { return ShutdownRequested(*Assert(m_context)); };
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
162
    bool isSettingIgnored(const std::string& name) override
163
0
    {
164
0
        bool ignored = false;
165
0
        args().LockSettings([&](common::Settings& settings) {
166
0
            if (auto* options = common::FindKey(settings.command_line_options, name)) {
167
0
                ignored = !options->empty();
168
0
            }
169
0
        });
170
0
        return ignored;
171
0
    }
172
0
    common::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); }
173
    void updateRwSetting(const std::string& name, const common::SettingsValue& value) override
174
0
    {
175
0
        args().LockSettings([&](common::Settings& settings) {
176
0
            if (value.isNull()) {
177
0
                settings.rw_settings.erase(name);
178
0
            } else {
179
0
                settings.rw_settings[name] = value;
180
0
            }
181
0
        });
182
0
        args().WriteSettingsFile();
183
0
    }
184
    void forceSetting(const std::string& name, const common::SettingsValue& value) override
185
0
    {
186
0
        args().LockSettings([&](common::Settings& settings) {
187
0
            if (value.isNull()) {
188
0
                settings.forced_settings.erase(name);
189
0
            } else {
190
0
                settings.forced_settings[name] = value;
191
0
            }
192
0
        });
193
0
    }
194
    void resetSettings() override
195
0
    {
196
0
        args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
197
0
        args().LockSettings([&](common::Settings& settings) {
198
0
            settings.rw_settings.clear();
199
0
        });
200
0
        args().WriteSettingsFile();
201
0
    }
202
0
    void mapPort(bool enable) override { StartMapPort(enable); }
203
0
    std::optional<Proxy> getProxy(Network net) override { return GetProxy(net); }
204
    size_t getNodeCount(ConnectionDirection flags) override
205
0
    {
206
0
        return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
207
0
    }
208
    bool getNodesStats(NodesStats& stats) override
209
0
    {
210
0
        stats.clear();
211
212
0
        if (m_context->connman) {
213
0
            std::vector<CNodeStats> stats_temp;
214
0
            m_context->connman->GetNodeStats(stats_temp);
215
216
0
            stats.reserve(stats_temp.size());
217
0
            for (auto& node_stats_temp : stats_temp) {
218
0
                stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());
219
0
            }
220
221
            // Try to retrieve the CNodeStateStats for each node.
222
0
            if (m_context->peerman) {
223
0
                TRY_LOCK(::cs_main, lockMain);
Line
Count
Source
273
0
#define TRY_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs), true)
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
224
0
                if (lockMain) {
225
0
                    for (auto& node_stats : stats) {
226
0
                        std::get<1>(node_stats) =
227
0
                            m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
228
0
                    }
229
0
                }
230
0
            }
231
0
            return true;
232
0
        }
233
0
        return false;
234
0
    }
235
    bool getBanned(banmap_t& banmap) override
236
0
    {
237
0
        if (m_context->banman) {
238
0
            m_context->banman->GetBanned(banmap);
239
0
            return true;
240
0
        }
241
0
        return false;
242
0
    }
243
    bool ban(const CNetAddr& net_addr, int64_t ban_time_offset) override
244
0
    {
245
0
        if (m_context->banman) {
246
0
            m_context->banman->Ban(net_addr, ban_time_offset);
247
0
            return true;
248
0
        }
249
0
        return false;
250
0
    }
251
    bool unban(const CSubNet& ip) override
252
0
    {
253
0
        if (m_context->banman) {
254
0
            m_context->banman->Unban(ip);
255
0
            return true;
256
0
        }
257
0
        return false;
258
0
    }
259
    bool disconnectByAddress(const CNetAddr& net_addr) override
260
0
    {
261
0
        if (m_context->connman) {
262
0
            return m_context->connman->DisconnectNode(net_addr);
263
0
        }
264
0
        return false;
265
0
    }
266
    bool disconnectById(NodeId id) override
267
0
    {
268
0
        if (m_context->connman) {
269
0
            return m_context->connman->DisconnectNode(id);
270
0
        }
271
0
        return false;
272
0
    }
273
    std::vector<std::unique_ptr<interfaces::ExternalSigner>> listExternalSigners() override
274
0
    {
275
#ifdef ENABLE_EXTERNAL_SIGNER
276
        std::vector<ExternalSigner> signers = {};
277
        const std::string command = args().GetArg("-signer", "");
278
        if (command == "") return {};
279
        ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
280
        std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
281
        result.reserve(signers.size());
282
        for (auto& signer : signers) {
283
            result.emplace_back(std::make_unique<ExternalSignerImpl>(std::move(signer)));
284
        }
285
        return result;
286
#else
287
        // This result is indistinguishable from a successful call that returns
288
        // no signers. For the current GUI this doesn't matter, because the wallet
289
        // creation dialog disables the external signer checkbox in both
290
        // cases. The return type could be changed to std::optional<std::vector>
291
        // (or something that also includes error messages) if this distinction
292
        // becomes important.
293
0
        return {};
294
0
#endif // ENABLE_EXTERNAL_SIGNER
295
0
    }
296
0
    int64_t getTotalBytesRecv() override { return m_context->connman ? m_context->connman->GetTotalBytesRecv() : 0; }
297
0
    int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; }
298
0
    size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; }
299
0
    size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; }
300
0
    size_t getMempoolMaxUsage() override { return m_context->mempool ? m_context->mempool->m_opts.max_size_bytes : 0; }
301
    bool getHeaderTip(int& height, int64_t& block_time) override
302
0
    {
303
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
304
0
        auto best_header = chainman().m_best_header;
305
0
        if (best_header) {
306
0
            height = best_header->nHeight;
307
0
            block_time = best_header->GetBlockTime();
308
0
            return true;
309
0
        }
310
0
        return false;
311
0
    }
312
    std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() override
313
0
    {
314
0
        if (m_context->connman)
315
0
            return m_context->connman->getNetLocalAddresses();
316
0
        else
317
0
            return {};
318
0
    }
319
    int getNumBlocks() override
320
0
    {
321
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
322
0
        return chainman().ActiveChain().Height();
323
0
    }
324
    uint256 getBestBlockHash() override
325
0
    {
326
0
        const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
327
0
        return tip ? tip->GetBlockHash() : chainman().GetParams().GenesisBlock().GetHash();
328
0
    }
329
    int64_t getLastBlockTime() override
330
0
    {
331
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
332
0
        if (chainman().ActiveChain().Tip()) {
333
0
            return chainman().ActiveChain().Tip()->GetBlockTime();
334
0
        }
335
0
        return chainman().GetParams().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
336
0
    }
337
    double getVerificationProgress() override
338
0
    {
339
0
        LOCK(chainman().GetMutex());
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
340
0
        return chainman().GuessVerificationProgress(chainman().ActiveTip());
341
0
    }
342
    bool isInitialBlockDownload() override
343
0
    {
344
0
        return chainman().IsInitialBlockDownload();
345
0
    }
346
0
    bool isLoadingBlocks() override { return chainman().m_blockman.LoadingBlocks(); }
347
    void setNetworkActive(bool active) override
348
0
    {
349
0
        if (m_context->connman) {
350
0
            m_context->connman->SetNetworkActive(active);
351
0
        }
352
0
    }
353
0
    bool getNetworkActive() override { return m_context->connman && m_context->connman->GetNetworkActive(); }
354
    CFeeRate getDustRelayFee() override
355
0
    {
356
0
        if (!m_context->mempool) return CFeeRate{DUST_RELAY_TX_FEE};
357
0
        return m_context->mempool->m_opts.dust_relay_feerate;
358
0
    }
359
    UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
360
0
    {
361
0
        JSONRPCRequest req;
362
0
        req.context = m_context;
363
0
        req.params = params;
364
0
        req.strMethod = command;
365
0
        req.URI = uri;
366
0
        return ::tableRPC.execute(req);
367
0
    }
368
0
    std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
369
    std::optional<Coin> getUnspentOutput(const COutPoint& output) override
370
0
    {
371
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
372
0
        return chainman().ActiveChainstate().CoinsTip().GetCoin(output);
373
0
    }
374
    TransactionError broadcastTransaction(CTransactionRef tx, CAmount max_tx_fee, std::string& err_string) override
375
0
    {
376
0
        return BroadcastTransaction(*m_context,
377
0
                                    std::move(tx),
378
0
                                    err_string,
379
0
                                    max_tx_fee,
380
0
                                    TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL,
381
0
                                    /*wait_callback=*/false);
382
0
    }
383
    WalletLoader& walletLoader() override
384
0
    {
385
0
        return *Assert(m_context->wallet_loader);
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
386
0
    }
387
    std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
388
0
    {
389
0
        return MakeSignalHandler(::uiInterface.InitMessage_connect(fn));
390
0
    }
391
    std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override
392
0
    {
393
0
        return MakeSignalHandler(::uiInterface.ThreadSafeMessageBox_connect(fn));
394
0
    }
395
    std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override
396
0
    {
397
0
        return MakeSignalHandler(::uiInterface.ThreadSafeQuestion_connect(fn));
398
0
    }
399
    std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
400
0
    {
401
0
        return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn));
402
0
    }
403
    std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) override
404
0
    {
405
0
        return MakeSignalHandler(::uiInterface.InitWallet_connect(fn));
406
0
    }
407
    std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
408
0
    {
409
0
        return MakeSignalHandler(::uiInterface.NotifyNumConnectionsChanged_connect(fn));
410
0
    }
411
    std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override
412
0
    {
413
0
        return MakeSignalHandler(::uiInterface.NotifyNetworkActiveChanged_connect(fn));
414
0
    }
415
    std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override
416
0
    {
417
0
        return MakeSignalHandler(::uiInterface.NotifyAlertChanged_connect(fn));
418
0
    }
419
    std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override
420
0
    {
421
0
        return MakeSignalHandler(::uiInterface.BannedListChanged_connect(fn));
422
0
    }
423
    std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
424
0
    {
425
0
        return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex& block, double verification_progress) {
426
0
            fn(sync_state, BlockTip{block.nHeight, block.GetBlockTime(), block.GetBlockHash()}, verification_progress);
427
0
        }));
428
0
    }
429
    std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
430
0
    {
431
0
        return MakeSignalHandler(
432
0
            ::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, int64_t height, int64_t timestamp, bool presync) {
433
0
                fn(sync_state, BlockTip{(int)height, timestamp, uint256{}}, presync);
434
0
            }));
435
0
    }
436
0
    NodeContext* context() override { return m_context; }
437
    void setContext(NodeContext* context) override
438
0
    {
439
0
        m_context = context;
440
0
    }
441
0
    ArgsManager& args() { return *Assert(Assert(m_context)->args); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
442
0
    ChainstateManager& chainman() { return *Assert(m_context->chainman); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
443
    NodeContext* m_context{nullptr};
444
};
445
446
// NOLINTNEXTLINE(misc-no-recursion)
447
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active, const BlockManager& blockman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
448
0
{
449
0
    if (!index) return false;
450
0
    if (block.m_hash) *block.m_hash = index->GetBlockHash();
451
0
    if (block.m_height) *block.m_height = index->nHeight;
452
0
    if (block.m_time) *block.m_time = index->GetBlockTime();
453
0
    if (block.m_max_time) *block.m_max_time = index->GetBlockTimeMax();
454
0
    if (block.m_mtp_time) *block.m_mtp_time = index->GetMedianTimePast();
455
0
    if (block.m_in_active_chain) *block.m_in_active_chain = active[index->nHeight] == index;
456
0
    if (block.m_locator) { *block.m_locator = GetLocator(index); }
457
0
    if (block.m_next_block) FillBlock(active[index->nHeight] == index ? active[index->nHeight + 1] : nullptr, *block.m_next_block, lock, active, blockman);
458
0
    if (block.m_data) {
459
0
        REVERSE_LOCK(lock, cs_main);
Line
Count
Source
254
0
#define REVERSE_LOCK(g, cs) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, cs, #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
460
0
        if (!blockman.ReadBlock(*block.m_data, *index)) block.m_data->SetNull();
461
0
    }
462
0
    block.found = true;
463
0
    return true;
464
0
}
465
466
class NotificationsProxy : public CValidationInterface
467
{
468
public:
469
    explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications)
470
0
        : m_notifications(std::move(notifications)) {}
471
0
    virtual ~NotificationsProxy() = default;
472
    void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) override
473
0
    {
474
0
        m_notifications->transactionAddedToMempool(tx.info.m_tx);
475
0
    }
476
    void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
477
0
    {
478
0
        m_notifications->transactionRemovedFromMempool(tx, reason);
479
0
    }
480
    void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
481
0
    {
482
0
        m_notifications->blockConnected(role, kernel::MakeBlockInfo(index, block.get()));
483
0
    }
484
    void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
485
0
    {
486
0
        m_notifications->blockDisconnected(kernel::MakeBlockInfo(index, block.get()));
487
0
    }
488
    void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
489
0
    {
490
0
        m_notifications->updatedBlockTip();
491
0
    }
492
    void ChainStateFlushed(const ChainstateRole& role, const CBlockLocator& locator) override
493
0
    {
494
0
        m_notifications->chainStateFlushed(role, locator);
495
0
    }
496
    std::shared_ptr<Chain::Notifications> m_notifications;
497
};
498
499
class NotificationsHandlerImpl : public Handler
500
{
501
public:
502
    explicit NotificationsHandlerImpl(ValidationSignals& signals, std::shared_ptr<Chain::Notifications> notifications)
503
0
        : m_signals{signals}, m_proxy{std::make_shared<NotificationsProxy>(std::move(notifications))}
504
0
    {
505
0
        m_signals.RegisterSharedValidationInterface(m_proxy);
506
0
    }
507
0
    ~NotificationsHandlerImpl() override { disconnect(); }
508
    void disconnect() override
509
0
    {
510
0
        if (m_proxy) {
511
0
            m_signals.UnregisterSharedValidationInterface(m_proxy);
512
0
            m_proxy.reset();
513
0
        }
514
0
    }
515
    ValidationSignals& m_signals;
516
    std::shared_ptr<NotificationsProxy> m_proxy;
517
};
518
519
class RpcHandlerImpl : public Handler
520
{
521
public:
522
0
    explicit RpcHandlerImpl(const CRPCCommand& command) : m_command(command), m_wrapped_command(&command)
523
0
    {
524
0
        m_command.actor = [this](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
525
0
            if (!m_wrapped_command) return false;
526
0
            try {
527
0
                return m_wrapped_command->actor(request, result, last_handler);
528
0
            } catch (const UniValue& e) {
529
                // If this is not the last handler and a wallet not found
530
                // exception was thrown, return false so the next handler can
531
                // try to handle the request. Otherwise, reraise the exception.
532
0
                if (!last_handler) {
533
0
                    const UniValue& code = e["code"];
534
0
                    if (code.isNum() && code.getInt<int>() == RPC_WALLET_NOT_FOUND) {
535
0
                        return false;
536
0
                    }
537
0
                }
538
0
                throw;
539
0
            }
540
0
        };
541
0
        ::tableRPC.appendCommand(m_command.name, &m_command);
542
0
    }
543
544
    void disconnect() final
545
0
    {
546
0
        if (m_wrapped_command) {
547
0
            m_wrapped_command = nullptr;
548
0
            ::tableRPC.removeCommand(m_command.name, &m_command);
549
0
        }
550
0
    }
551
552
0
    ~RpcHandlerImpl() override { disconnect(); }
553
554
    CRPCCommand m_command;
555
    const CRPCCommand* m_wrapped_command;
556
};
557
558
class ChainImpl : public Chain
559
{
560
public:
561
0
    explicit ChainImpl(NodeContext& node) : m_node(node) {}
562
    std::optional<int> getHeight() override
563
0
    {
564
0
        const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
565
0
        return height >= 0 ? std::optional{height} : std::nullopt;
566
0
    }
567
    uint256 getBlockHash(int height) override
568
0
    {
569
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
570
0
        return Assert(chainman().ActiveChain()[height])->GetBlockHash();
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
571
0
    }
572
    bool haveBlockOnDisk(int height) override
573
0
    {
574
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
575
0
        const CBlockIndex* block{chainman().ActiveChain()[height]};
576
0
        return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
577
0
    }
578
    std::optional<int> findLocatorFork(const CBlockLocator& locator) override
579
0
    {
580
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
581
0
        if (const CBlockIndex* fork = chainman().ActiveChainstate().FindForkInGlobalIndex(locator)) {
582
0
            return fork->nHeight;
583
0
        }
584
0
        return std::nullopt;
585
0
    }
586
    bool hasBlockFilterIndex(BlockFilterType filter_type) override
587
0
    {
588
0
        return GetBlockFilterIndex(filter_type) != nullptr;
589
0
    }
590
    std::optional<bool> blockFilterMatchesAny(BlockFilterType filter_type, const uint256& block_hash, const GCSFilter::ElementSet& filter_set) override
591
0
    {
592
0
        const BlockFilterIndex* block_filter_index{GetBlockFilterIndex(filter_type)};
593
0
        if (!block_filter_index) return std::nullopt;
594
595
0
        BlockFilter filter;
596
0
        const CBlockIndex* index{WITH_LOCK(::cs_main, return chainman().m_blockman.LookupBlockIndex(block_hash))};
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
597
0
        if (index == nullptr || !block_filter_index->LookupFilter(index, filter)) return std::nullopt;
598
0
        return filter.GetFilter().MatchAny(filter_set);
599
0
    }
600
    bool findBlock(const uint256& hash, const FoundBlock& block) override
601
0
    {
602
0
        WAIT_LOCK(cs_main, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
603
0
        return FillBlock(chainman().m_blockman.LookupBlockIndex(hash), block, lock, chainman().ActiveChain(), chainman().m_blockman);
604
0
    }
605
    bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
606
0
    {
607
0
        WAIT_LOCK(cs_main, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
608
0
        const CChain& active = chainman().ActiveChain();
609
0
        return FillBlock(active.FindEarliestAtLeast(min_time, min_height), block, lock, active, chainman().m_blockman);
610
0
    }
611
    bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
612
0
    {
613
0
        WAIT_LOCK(cs_main, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
614
0
        const CChain& active = chainman().ActiveChain();
615
0
        if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
616
0
            if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
617
0
                return FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman);
618
0
            }
619
0
        }
620
0
        return FillBlock(nullptr, ancestor_out, lock, active, chainman().m_blockman);
621
0
    }
622
    bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
623
0
    {
624
0
        WAIT_LOCK(cs_main, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
625
0
        const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash);
626
0
        const CBlockIndex* ancestor = chainman().m_blockman.LookupBlockIndex(ancestor_hash);
627
0
        if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
628
0
        return FillBlock(ancestor, ancestor_out, lock, chainman().ActiveChain(), chainman().m_blockman);
629
0
    }
630
    bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
631
0
    {
632
0
        WAIT_LOCK(cs_main, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
633
0
        const CChain& active = chainman().ActiveChain();
634
0
        const CBlockIndex* block1 = chainman().m_blockman.LookupBlockIndex(block_hash1);
635
0
        const CBlockIndex* block2 = chainman().m_blockman.LookupBlockIndex(block_hash2);
636
0
        const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
637
        // Using & instead of && below to avoid short circuiting and leaving
638
        // output uninitialized. Cast bool to int to avoid -Wbitwise-instead-of-logical
639
        // compiler warnings.
640
0
        return int{FillBlock(ancestor, ancestor_out, lock, active, chainman().m_blockman)} &
641
0
               int{FillBlock(block1, block1_out, lock, active, chainman().m_blockman)} &
642
0
               int{FillBlock(block2, block2_out, lock, active, chainman().m_blockman)};
643
0
    }
644
0
    void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
645
    double guessVerificationProgress(const uint256& block_hash) override
646
0
    {
647
0
        LOCK(chainman().GetMutex());
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
648
0
        return chainman().GuessVerificationProgress(chainman().m_blockman.LookupBlockIndex(block_hash));
649
0
    }
650
    bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
651
0
    {
652
        // hasBlocks returns true if all ancestors of block_hash in specified
653
        // range have block data (are not pruned), false if any ancestors in
654
        // specified range are missing data.
655
        //
656
        // For simplicity and robustness, min_height and max_height are only
657
        // used to limit the range, and passing min_height that's too low or
658
        // max_height that's too high will not crash or change the result.
659
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
660
0
        if (const CBlockIndex* block = chainman().m_blockman.LookupBlockIndex(block_hash)) {
661
0
            if (max_height && block->nHeight >= *max_height) block = block->GetAncestor(*max_height);
662
0
            for (; block->nStatus & BLOCK_HAVE_DATA; block = block->pprev) {
663
                // Check pprev to not segfault if min_height is too low
664
0
                if (block->nHeight <= min_height || !block->pprev) return true;
665
0
            }
666
0
        }
667
0
        return false;
668
0
    }
669
    RBFTransactionState isRBFOptIn(const CTransaction& tx) override
670
0
    {
671
0
        if (!m_node.mempool) return IsRBFOptInEmptyMempool(tx);
672
0
        LOCK(m_node.mempool->cs);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
673
0
        return IsRBFOptIn(tx, *m_node.mempool);
674
0
    }
675
    bool isInMempool(const Txid& txid) override
676
0
    {
677
0
        if (!m_node.mempool) return false;
678
0
        return m_node.mempool->exists(txid);
679
0
    }
680
    bool hasDescendantsInMempool(const Txid& txid) override
681
0
    {
682
0
        if (!m_node.mempool) return false;
683
0
        return m_node.mempool->HasDescendants(txid);
684
0
    }
685
    bool broadcastTransaction(const CTransactionRef& tx,
686
        const CAmount& max_tx_fee,
687
        TxBroadcast broadcast_method,
688
        std::string& err_string) override
689
0
    {
690
0
        const TransactionError err = BroadcastTransaction(m_node, tx, err_string, max_tx_fee, broadcast_method, /*wait_callback=*/false);
691
        // Chain clients only care about failures to accept the tx to the mempool. Disregard non-mempool related failures.
692
        // Note: this will need to be updated if BroadcastTransactions() is updated to return other non-mempool failures
693
        // that Chain clients do not need to know about.
694
0
        return TransactionError::OK == err;
695
0
    }
696
    void getTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* ancestorsize, CAmount* ancestorfees) override
697
0
    {
698
0
        ancestors = cluster_count = 0;
699
0
        if (!m_node.mempool) return;
700
0
        m_node.mempool->GetTransactionAncestry(txid, ancestors, cluster_count, ancestorsize, ancestorfees);
701
0
    }
702
703
    std::map<COutPoint, CAmount> calculateIndividualBumpFees(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
704
0
    {
705
0
        if (!m_node.mempool) {
706
0
            std::map<COutPoint, CAmount> bump_fees;
707
0
            for (const auto& outpoint : outpoints) {
708
0
                bump_fees.emplace(outpoint, 0);
709
0
            }
710
0
            return bump_fees;
711
0
        }
712
0
        return MiniMiner(*m_node.mempool, outpoints).CalculateBumpFees(target_feerate);
713
0
    }
714
715
    std::optional<CAmount> calculateCombinedBumpFee(const std::vector<COutPoint>& outpoints, const CFeeRate& target_feerate) override
716
0
    {
717
0
        if (!m_node.mempool) {
718
0
            return 0;
719
0
        }
720
0
        return MiniMiner(*m_node.mempool, outpoints).CalculateTotalBumpFees(target_feerate);
721
0
    }
722
    void getPackageLimits(unsigned int& limit_ancestor_count, unsigned int& limit_descendant_count) override
723
0
    {
724
0
        const CTxMemPool::Limits default_limits{};
725
726
0
        const CTxMemPool::Limits& limits{m_node.mempool ? m_node.mempool->m_opts.limits : default_limits};
727
728
0
        limit_ancestor_count = limits.ancestor_count;
729
0
        limit_descendant_count = limits.descendant_count;
730
0
    }
731
    util::Result<void> checkChainLimits(const CTransactionRef& tx) override
732
0
    {
733
0
        if (!m_node.mempool) return {};
734
0
        if (!m_node.mempool->CheckPolicyLimits(tx)) {
735
0
            return util::Error{Untranslated("too many unconfirmed transactions in cluster")};
736
0
        }
737
0
        return {};
738
0
    }
739
    CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
740
0
    {
741
0
        if (!m_node.fee_estimator) return {};
742
0
        return m_node.fee_estimator->estimateSmartFee(num_blocks, calc, conservative);
743
0
    }
744
    unsigned int estimateMaxBlocks() override
745
0
    {
746
0
        if (!m_node.fee_estimator) return 0;
747
0
        return m_node.fee_estimator->HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
748
0
    }
749
    CFeeRate mempoolMinFee() override
750
0
    {
751
0
        if (!m_node.mempool) return {};
752
0
        return m_node.mempool->GetMinFee();
753
0
    }
754
    CFeeRate relayMinFee() override
755
0
    {
756
0
        if (!m_node.mempool) return CFeeRate{DEFAULT_MIN_RELAY_TX_FEE};
757
0
        return m_node.mempool->m_opts.min_relay_feerate;
758
0
    }
759
    CFeeRate relayIncrementalFee() override
760
0
    {
761
0
        if (!m_node.mempool) return CFeeRate{DEFAULT_INCREMENTAL_RELAY_FEE};
762
0
        return m_node.mempool->m_opts.incremental_relay_feerate;
763
0
    }
764
    CFeeRate relayDustFee() override
765
0
    {
766
0
        if (!m_node.mempool) return CFeeRate{DUST_RELAY_TX_FEE};
767
0
        return m_node.mempool->m_opts.dust_relay_feerate;
768
0
    }
769
    bool havePruned() override
770
0
    {
771
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
772
0
        return chainman().m_blockman.m_have_pruned;
773
0
    }
774
    std::optional<int> getPruneHeight() override
775
0
    {
776
0
        LOCK(chainman().GetMutex());
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
777
0
        return GetPruneHeight(chainman().m_blockman, chainman().ActiveChain());
778
0
    }
779
0
    bool isReadyToBroadcast() override { return !chainman().m_blockman.LoadingBlocks() && !isInitialBlockDownload(); }
780
    bool isInitialBlockDownload() override
781
0
    {
782
0
        return chainman().IsInitialBlockDownload();
783
0
    }
784
0
    bool shutdownRequested() override { return ShutdownRequested(m_node); }
785
0
    void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
786
0
    void initWarning(const bilingual_str& message) override { InitWarning(message); }
787
0
    void initError(const bilingual_str& message) override { InitError(message); }
788
    void showProgress(const std::string& title, int progress, bool resume_possible) override
789
0
    {
790
0
        ::uiInterface.ShowProgress(title, progress, resume_possible);
791
0
    }
792
    std::unique_ptr<Handler> handleNotifications(std::shared_ptr<Notifications> notifications) override
793
0
    {
794
0
        return std::make_unique<NotificationsHandlerImpl>(validation_signals(), std::move(notifications));
795
0
    }
796
    void waitForNotificationsIfTipChanged(const uint256& old_tip) override
797
0
    {
798
0
        if (!old_tip.IsNull() && old_tip == WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip()->GetBlockHash())) return;
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
799
0
        validation_signals().SyncWithValidationInterfaceQueue();
800
0
    }
801
    void waitForNotifications() override
802
0
    {
803
0
        validation_signals().SyncWithValidationInterfaceQueue();
804
0
    }
805
    std::unique_ptr<Handler> handleRpc(const CRPCCommand& command) override
806
0
    {
807
0
        return std::make_unique<RpcHandlerImpl>(command);
808
0
    }
809
0
    bool rpcEnableDeprecated(const std::string& method) override { return IsDeprecatedRPCEnabled(method); }
810
    common::SettingsValue getSetting(const std::string& name) override
811
0
    {
812
0
        return args().GetSetting(name);
813
0
    }
814
    std::vector<common::SettingsValue> getSettingsList(const std::string& name) override
815
0
    {
816
0
        return args().GetSettingsList(name);
817
0
    }
818
    common::SettingsValue getRwSetting(const std::string& name) override
819
0
    {
820
0
        common::SettingsValue result;
821
0
        args().LockSettings([&](const common::Settings& settings) {
822
0
            if (const common::SettingsValue* value = common::FindKey(settings.rw_settings, name)) {
823
0
                result = *value;
824
0
            }
825
0
        });
826
0
        return result;
827
0
    }
828
    bool updateRwSetting(const std::string& name,
829
                         const interfaces::SettingsUpdate& update_settings_func) override
830
0
    {
831
0
        std::optional<interfaces::SettingsAction> action;
832
0
        args().LockSettings([&](common::Settings& settings) {
833
0
            if (auto* value = common::FindKey(settings.rw_settings, name)) {
834
0
                action = update_settings_func(*value);
835
0
                if (value->isNull()) settings.rw_settings.erase(name);
836
0
            } else {
837
0
                UniValue new_value;
838
0
                action = update_settings_func(new_value);
839
0
                if (!new_value.isNull()) settings.rw_settings[name] = std::move(new_value);
840
0
            }
841
0
        });
842
0
        if (!action) return false;
843
        // Now dump value to disk if requested
844
0
        return *action != interfaces::SettingsAction::WRITE || args().WriteSettingsFile();
845
0
    }
846
    bool overwriteRwSetting(const std::string& name, common::SettingsValue value, interfaces::SettingsAction action) override
847
0
    {
848
0
        return updateRwSetting(name, [&](common::SettingsValue& settings) {
849
0
            settings = std::move(value);
850
0
            return action;
851
0
        });
852
0
    }
853
    bool deleteRwSettings(const std::string& name, interfaces::SettingsAction action) override
854
0
    {
855
0
        return overwriteRwSetting(name, {}, action);
856
0
    }
857
    void requestMempoolTransactions(Notifications& notifications) override
858
0
    {
859
0
        if (!m_node.mempool) return;
860
0
        LOCK2(::cs_main, m_node.mempool->cs);
Line
Count
Source
270
0
    UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
271
0
    UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
861
0
        for (const CTxMemPoolEntry& entry : m_node.mempool->entryAll()) {
862
0
            notifications.transactionAddedToMempool(entry.GetSharedTx());
863
0
        }
864
0
    }
865
    bool hasAssumedValidChain() override
866
0
    {
867
0
        LOCK(::cs_main);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
868
0
        return bool{chainman().CurrentChainstate().m_from_snapshot_blockhash};
869
0
    }
870
871
0
    NodeContext* context() override { return &m_node; }
872
0
    ArgsManager& args() { return *Assert(m_node.args); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
873
0
    ChainstateManager& chainman() { return *Assert(m_node.chainman); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
874
0
    ValidationSignals& validation_signals() { return *Assert(m_node.validation_signals); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
875
    NodeContext& m_node;
876
};
877
878
class BlockTemplateImpl : public BlockTemplate
879
{
880
public:
881
    explicit BlockTemplateImpl(BlockCreateOptions create_options,
882
                               std::unique_ptr<CBlockTemplate> block_template,
883
0
                               const NodeContext& node) : m_create_options(std::move(create_options)),
884
0
                                                          m_block_template(std::move(block_template)),
885
0
                                                          m_node(node)
886
0
    {
887
0
        assert(m_block_template);
888
0
    }
889
890
    CBlockHeader getBlockHeader() override
891
0
    {
892
0
        return m_block_template->block;
893
0
    }
894
895
    CBlock getBlock() override
896
0
    {
897
0
        return m_block_template->block;
898
0
    }
899
900
    std::vector<CAmount> getTxFees() override
901
0
    {
902
0
        return m_block_template->vTxFees;
903
0
    }
904
905
    std::vector<int64_t> getTxSigops() override
906
0
    {
907
0
        return m_block_template->vTxSigOpsCost;
908
0
    }
909
910
    CoinbaseTx getCoinbaseTx() override
911
0
    {
912
0
        return m_block_template->m_coinbase_tx;
913
0
    }
914
915
    std::vector<uint256> getCoinbaseMerklePath() override
916
0
    {
917
0
        return TransactionMerklePath(m_block_template->block, 0);
918
0
    }
919
920
    bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) override
921
0
    {
922
0
        AddMerkleRootAndCoinbase(m_block_template->block, std::move(coinbase), version, timestamp, nonce);
923
0
        return chainman().ProcessNewBlock(std::make_shared<const CBlock>(m_block_template->block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
924
0
    }
925
926
    std::unique_ptr<BlockTemplate> waitNext(BlockWaitOptions options) override
927
0
    {
928
0
        auto new_template = WaitAndCreateNewBlock(chainman(),
929
0
                                                  notifications(),
930
0
                                                  m_node.mempool.get(),
931
0
                                                  m_block_template,
932
0
                                                  /*wait_options=*/options,
933
0
                                                  /*create_options=*/m_create_options,
934
0
                                                  /*interrupt_wait=*/m_interrupt_wait);
935
0
        if (new_template) return std::make_unique<BlockTemplateImpl>(m_create_options, std::move(new_template), m_node);
936
0
        return nullptr;
937
0
    }
938
939
    void interruptWait() override
940
0
    {
941
0
        InterruptWait(notifications(), m_interrupt_wait);
942
0
    }
943
944
    const BlockCreateOptions m_create_options;
945
946
    const std::unique_ptr<CBlockTemplate> m_block_template;
947
948
    bool m_interrupt_wait{false};
949
0
    ChainstateManager& chainman() { return *Assert(m_node.chainman); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
950
0
    KernelNotifications& notifications() { return *Assert(m_node.notifications); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
951
    const NodeContext& m_node;
952
};
953
954
class MinerImpl : public Mining
955
{
956
public:
957
0
    explicit MinerImpl(const NodeContext& node) : m_node(node) {}
958
959
    bool isTestChain() override
960
0
    {
961
0
        return chainman().GetParams().IsTestChain();
962
0
    }
963
964
    bool isInitialBlockDownload() override
965
0
    {
966
0
        return chainman().IsInitialBlockDownload();
967
0
    }
968
969
    std::optional<BlockRef> getTip() override
970
0
    {
971
0
        return GetTip(chainman());
972
0
    }
973
974
    std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override
975
0
    {
976
0
        return WaitTipChanged(chainman(), notifications(), current_tip, timeout, m_interrupt_mining);
977
0
    }
978
979
    std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options, bool cooldown) override
980
0
    {
981
        // Ensure m_tip_block is set so consumers of BlockTemplate can rely on that.
982
0
        std::optional<BlockRef> maybe_tip{waitTipChanged(uint256::ZERO, MillisecondsDouble::max())};
983
984
0
        if (!maybe_tip) return {};
985
986
0
        if (cooldown) {
987
            // Do not return a template during IBD, because it can have long
988
            // pauses and sometimes takes a while to get started. Although this
989
            // is useful in general, it's gated behind the cooldown argument,
990
            // because on regtest and single miner signets this would wait
991
            // forever if no block was mined in the past day.
992
0
            while (chainman().IsInitialBlockDownload()) {
993
0
                maybe_tip = waitTipChanged(maybe_tip->hash, MillisecondsDouble{1000});
994
0
                if (!maybe_tip || chainman().m_interrupt || WITH_LOCK(notifications().m_tip_block_mutex, return m_interrupt_mining)) return {};
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
995
0
            }
996
997
            // Also wait during the final catch-up moments after IBD.
998
0
            if (!CooldownIfHeadersAhead(chainman(), notifications(), *maybe_tip, m_interrupt_mining)) return {};
999
0
        }
1000
0
        const BlockCreateOptions create_options{MergeMiningOptions(options, m_node.mining_args)};
1001
0
        return std::make_unique<BlockTemplateImpl>(create_options,
1002
0
                                                   BlockAssembler{
1003
0
                                                       chainman().ActiveChainstate(),
1004
0
                                                       m_node.mempool.get(),
1005
0
                                                       create_options,
1006
0
                                                   }.CreateNewBlock(),
1007
0
                                                   m_node);
1008
0
    }
1009
1010
    void interrupt() override
1011
0
    {
1012
0
        InterruptWait(notifications(), m_interrupt_mining);
1013
0
    }
1014
1015
    bool checkBlock(const CBlock& block, const node::BlockCheckOptions& options, std::string& reason, std::string& debug) override
1016
0
    {
1017
0
        LOCK(chainman().GetMutex());
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1018
0
        BlockValidationState state{TestBlockValidity(chainman().ActiveChainstate(), block, /*check_pow=*/options.check_pow, /*check_merkle_root=*/options.check_merkle_root)};
1019
0
        reason = state.GetRejectReason();
1020
0
        debug = state.GetDebugMessage();
1021
0
        return state.IsValid();
1022
0
    }
1023
1024
0
    const NodeContext* context() override { return &m_node; }
1025
0
    ChainstateManager& chainman() { return *Assert(m_node.chainman); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
1026
0
    KernelNotifications& notifications() { return *Assert(m_node.notifications); }
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
1027
    // Treat as if guarded by notifications().m_tip_block_mutex
1028
    bool m_interrupt_mining{false};
1029
    const NodeContext& m_node;
1030
};
1031
1032
class RpcImpl : public Rpc
1033
{
1034
public:
1035
0
    explicit RpcImpl(NodeContext& node) : m_node(node) {}
1036
1037
    UniValue executeRpc(UniValue request, std::string uri, std::string user) override
1038
0
    {
1039
0
        JSONRPCRequest req;
1040
0
        req.context = &m_node;
1041
0
        req.URI = std::move(uri);
1042
0
        req.authUser = std::move(user);
1043
0
        HTTPStatusCode status;
1044
0
        return ExecuteHTTPRPC(request, req, status);
1045
0
    }
1046
1047
    NodeContext& m_node;
1048
};
1049
} // namespace
1050
} // namespace node
1051
1052
namespace interfaces {
1053
0
std::unique_ptr<Node> MakeNode(node::NodeContext& context) { return std::make_unique<node::NodeImpl>(context); }
1054
0
std::unique_ptr<Chain> MakeChain(node::NodeContext& context) { return std::make_unique<node::ChainImpl>(context); }
1055
std::unique_ptr<Mining> MakeMining(const node::NodeContext& context, bool wait_loaded)
1056
0
{
1057
0
    if (wait_loaded) {
1058
0
        node::KernelNotifications& kernel_notifications(*Assert(context.notifications));
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
1059
0
        util::SignalInterrupt& interrupt(*Assert(context.shutdown_signal));
Line
Count
Source
116
0
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
1060
0
        WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
Line
Count
Source
274
0
#define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs))
Line
Count
Source
272
0
#define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__
1061
0
        kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
1062
0
            return kernel_notifications.m_state.chainstate_loaded || interrupt;
1063
0
        });
1064
0
        if (interrupt) return nullptr;
1065
0
    }
1066
0
    return std::make_unique<node::MinerImpl>(context);
1067
0
}
1068
0
std::unique_ptr<Rpc> MakeRpc(node::NodeContext& context) { return std::make_unique<node::RpcImpl>(context); }
1069
} // namespace interfaces