Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <base58.h>6#include <key.h>7#include <key_io.h>8#include <primitives/block.h>9#include <primitives/transaction.h>10#include <psbt.h>11#include <rpc/request.h>12#include <rpc/server.h>13#include <span.h>14#include <streams.h>15#include <test/fuzz/FuzzedDataProvider.h>16#include <test/fuzz/fuzz.h>17#include <test/fuzz/util.h>18#include <test/util/setup_common.h>19#include <test/util/time.h>20#include <tinyformat.h>21#include <uint256.h>22#include <univalue.h>23#include <util/strencodings.h>24#include <util/string.h>25#include <util/time.h>27#include <algorithm>28#include <cassert>29#include <cstdint>30#include <cstdlib>31#include <exception>32#include <iostream>33#include <memory>34#include <optional>35#include <stdexcept>36#include <utility>37#include <vector>39enum class ChainType;41namespace {42struct RPCFuzzTestingSetup : public TestingSetup {43RPCFuzzTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}44{45}47void CallRPC(const std::string& rpc_method, UniValue&& params)48{49JSONRPCRequest request;50request.context = &m_node;51request.strMethod = rpc_method;52request.params = std::move(params);53tableRPC.execute(request);54}56std::vector<std::string> GetRPCCommands() const57{58return tableRPC.listCommands();59}60};62RPCFuzzTestingSetup* rpc_testing_setup = nullptr;63std::string g_limit_to_rpc_command;65// RPC commands which are not appropriate for fuzzing: such as RPC commands66// reading or writing to a filename passed as an RPC parameter, RPC commands67// resulting in network activity, etc.68const std::vector<std::string> RPC_COMMANDS_NOT_SAFE_FOR_FUZZING{69"addconnection", // avoid DNS lookups70"addnode", // avoid DNS lookups71"addpeeraddress", // avoid DNS lookups72"dumptxoutset", // avoid writing to disk73"enumeratesigners",74"echoipc", // avoid assertion failure (Assertion `"EnsureAnyNodeContext(request.context).init" && check' failed.)75"exportasmap", // avoid writing to disk76"generatetoaddress", // avoid prohibitively slow execution (when `num_blocks` is large)77"generatetodescriptor", // avoid prohibitively slow execution (when `nblocks` is large)78"gettxoutproof", // avoid prohibitively slow execution79"importmempool", // avoid reading from disk80"loadtxoutset", // avoid reading from disk81"loadwallet", // avoid reading from disk82"savemempool", // disabled as a precautionary measure: may take a file path argument in the future83"setban", // avoid DNS lookups84"stop", // avoid shutdown state85};87// RPC commands which are safe for fuzzing.88const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{89"abortprivatebroadcast",90"analyzepsbt",91"clearbanned",92"combinepsbt",93"combinerawtransaction",94"converttopsbt",95"createmultisig",96"createpsbt",97"createrawtransaction",98"decodepsbt",99"decoderawtransaction",100"decodescript",101"deriveaddresses",102"descriptorprocesspsbt",103"disconnectnode",104"echo",105"echojson",106"estimaterawfee",107"estimatesmartfee",108"finalizepsbt",109"generate",110"generateblock",111"getaddednodeinfo",112"getaddrmaninfo",113"getbestblockhash",114"getblock",115"getblockchaininfo",116"getblockcount",117"getblockfilter",118"getblockfrompeer", // when no peers are connected, no p2p message is sent119"getblockhash",120"getblockheader",121"getblockstats",122"getblocktemplate",123"getchaintips",124"getchainstates",125"getchaintxstats",126"getconnectioncount",127"getdeploymentinfo",128"getdescriptoractivity",129"getdescriptorinfo",130"getdifficulty",131"getindexinfo",132"getmemoryinfo",133"getmempoolancestors",134"getmempooldescendants",135"getmempoolentry",136"getmempoolfeeratediagram",137"getmempoolcluster",138"getmempoolinfo",139"getmininginfo",140"getnettotals",141"getnetworkhashps",142"getnetworkinfo",143"getnodeaddresses",144"getopenrpcinfo",145"getorphantxs",146"getpeerinfo",147"getprioritisedtransactions",148"getprivatebroadcastinfo",149"getrawaddrman",150"getrawmempool",151"getrawtransaction",152"getrpcinfo",153"gettxout",154"gettxoutsetinfo",155"gettxspendingprevout",156"help",157"invalidateblock",158"joinpsbts",159"listbanned",160"logging",161"mockscheduler",162"ping",163"preciousblock",164"prioritisetransaction",165"pruneblockchain",166"reconsiderblock",167"rpc.discover",168"scanblocks",169"scantxoutset",170"sendmsgtopeer", // when no peers are connected, no p2p message is sent171"sendrawtransaction",172"setmocktime",173"setnetworkactive",174"signmessagewithprivkey",175"signrawtransactionwithkey",176"submitblock",177"submitheader",178"submitpackage",179"syncwithvalidationinterfacequeue",180"testmempoolaccept",181"uptime",182"utxoupdatepsbt",183"validateaddress",184"verifychain",185"verifymessage",186"verifytxoutproof",187"waitforblock",188"waitforblockheight",189"waitfornewblock",190};192UniValue ConsumeBasicRPCArgument(FuzzedDataProvider& fuzzed_data_provider, bool& good_data)193{194const size_t max_string_length = 4096;195const size_t max_base58_bytes_length{64};196UniValue r{};197CallOneOf(198fuzzed_data_provider,199[&] {200// arbitrary JSON argument201if (!r.read(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))) {202good_data = false;203}204},205[&] {206// null argument207r = UniValue{UniValue::VNULL};208},209[&] {210// string argument211r = UniValue{fuzzed_data_provider.ConsumeRandomLengthString(max_string_length)};212},213[&] {214// base64 argument215r = UniValue{EncodeBase64(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))};216},217[&] {218// hex argument219r = UniValue{HexStr(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length))};220},221[&] {222// bool argument223r = UniValue{fuzzed_data_provider.ConsumeBool()};224},225[&] {226// integral argument (int64_t)227r = UniValue{fuzzed_data_provider.ConsumeIntegral<int64_t>()};228},229[&] {230// integral argument (uint64_t)231r = UniValue{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};232},233[&] {234// floating point argument235r = UniValue{fuzzed_data_provider.ConsumeFloatingPoint<double>()};236},237[&] {238// tx destination argument239r = UniValue{EncodeDestination(ConsumeTxDestination(fuzzed_data_provider))};240},241[&] {242// uint160 argument243r = UniValue{ConsumeUInt160(fuzzed_data_provider).ToString()};244},245[&] {246// uint256 argument247r = UniValue{ConsumeUInt256(fuzzed_data_provider).ToString()};248},249[&] {250// base32 argument
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.