Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <rpc/blockchain.h> | #include <rpc/blockchain.h> | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <blockfilter.h> | |||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <checkpoints.h> | #include <checkpoints.h> | ||||
#include <coins.h> | #include <coins.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <core_io.h> | #include <core_io.h> | ||||
#include <hash.h> | #include <hash.h> | ||||
#include <index/blockfilterindex.h> | |||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <node/coinstats.h> | #include <node/coinstats.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <primitives/transaction.h> | #include <primitives/transaction.h> | ||||
#include <rpc/server.h> | #include <rpc/server.h> | ||||
#include <rpc/util.h> | #include <rpc/util.h> | ||||
#include <script/descriptor.h> | #include <script/descriptor.h> | ||||
#include <streams.h> | #include <streams.h> | ||||
▲ Show 20 Lines • Show All 2,654 Lines • ▼ Show 20 Lines | if (request.params[0].get_str() == "status") { | ||||
result.pushKV("unspents", unspents); | result.pushKV("unspents", unspents); | ||||
result.pushKV("total_amount", ValueFromAmount(total_in)); | result.pushKV("total_amount", ValueFromAmount(total_in)); | ||||
} else { | } else { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid command"); | throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid command"); | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
static UniValue getblockfilter(const Config &config, | |||||
const JSONRPCRequest &request) { | |||||
if (request.fHelp || request.params.size() < 1 || | |||||
request.params.size() > 2) { | |||||
throw std::runtime_error(RPCHelpMan{ | |||||
"getblockfilter", | |||||
"\nRetrieve a BIP 157 content filter for a particular block.\n", | |||||
{ | |||||
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | |||||
"The hash of the block"}, | |||||
{"filtertype", RPCArg::Type::STR, /*default*/ "basic", | |||||
"The type name of the filter"}, | |||||
}, | |||||
RPCResult{"{\n" | |||||
" \"filter\" : (string) the hex-encoded filter data\n" | |||||
" \"header\" : (string) the hex-encoded filter header\n" | |||||
"}\n"}, | |||||
RPCExamples{HelpExampleCli( | |||||
"getblockfilter", | |||||
"\"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6" | |||||
"ed09\" \"basic\"")}}.ToString()); | |||||
} | |||||
const BlockHash block_hash(ParseHashV(request.params[0], "blockhash")); | |||||
std::string filtertype_name = "basic"; | |||||
if (!request.params[1].isNull()) { | |||||
filtertype_name = request.params[1].get_str(); | |||||
} | |||||
BlockFilterType filtertype; | |||||
if (!BlockFilterTypeByName(filtertype_name, filtertype)) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown filtertype"); | |||||
} | |||||
BlockFilterIndex *index = GetBlockFilterIndex(filtertype); | |||||
if (!index) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, | |||||
"Index is not enabled for filtertype " + | |||||
filtertype_name); | |||||
} | |||||
const CBlockIndex *block_index; | |||||
bool block_was_connected; | |||||
{ | |||||
LOCK(cs_main); | |||||
block_index = LookupBlockIndex(block_hash); | |||||
if (!block_index) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | |||||
} | |||||
block_was_connected = block_index->IsValid(BlockValidity::SCRIPTS); | |||||
} | |||||
bool index_ready = index->BlockUntilSyncedToCurrentChain(); | |||||
BlockFilter filter; | |||||
uint256 filter_header; | |||||
if (!index->LookupFilter(block_index, filter) || | |||||
!index->LookupFilterHeader(block_index, filter_header)) { | |||||
int err_code; | |||||
std::string errmsg = "Filter not found."; | |||||
if (!block_was_connected) { | |||||
err_code = RPC_INVALID_ADDRESS_OR_KEY; | |||||
errmsg += " Block was not connected to active chain."; | |||||
} else if (!index_ready) { | |||||
err_code = RPC_MISC_ERROR; | |||||
errmsg += | |||||
" Block filters are still in the process of being indexed."; | |||||
} else { | |||||
err_code = RPC_INTERNAL_ERROR; | |||||
errmsg += | |||||
" This error is unexpected and indicates index corruption."; | |||||
} | |||||
throw JSONRPCError(err_code, errmsg); | |||||
} | |||||
UniValue ret(UniValue::VOBJ); | |||||
ret.pushKV("filter", HexStr(filter.GetEncodedFilter())); | |||||
ret.pushKV("header", filter_header.GetHex()); | |||||
return ret; | |||||
} | |||||
// clang-format off | // clang-format off | ||||
static const CRPCCommand commands[] = { | static const CRPCCommand commands[] = { | ||||
// category name actor (function) argNames | // category name actor (function) argNames | ||||
// ------------------- ------------------------ ---------------------- ---------- | // ------------------- ------------------------ ---------------------- ---------- | ||||
{ "blockchain", "getbestblockhash", getbestblockhash, {} }, | { "blockchain", "getbestblockhash", getbestblockhash, {} }, | ||||
{ "blockchain", "getblock", getblock, {"blockhash","verbosity|verbose"} }, | { "blockchain", "getblock", getblock, {"blockhash","verbosity|verbose"} }, | ||||
{ "blockchain", "getblockchaininfo", getblockchaininfo, {} }, | { "blockchain", "getblockchaininfo", getblockchaininfo, {} }, | ||||
{ "blockchain", "getblockcount", getblockcount, {} }, | { "blockchain", "getblockcount", getblockcount, {} }, | ||||
Show All 10 Lines | static const CRPCCommand commands[] = { | ||||
{ "blockchain", "getrawmempool", getrawmempool, {"verbose"} }, | { "blockchain", "getrawmempool", getrawmempool, {"verbose"} }, | ||||
{ "blockchain", "gettxout", gettxout, {"txid","n","include_mempool"} }, | { "blockchain", "gettxout", gettxout, {"txid","n","include_mempool"} }, | ||||
{ "blockchain", "gettxoutsetinfo", gettxoutsetinfo, {} }, | { "blockchain", "gettxoutsetinfo", gettxoutsetinfo, {} }, | ||||
{ "blockchain", "pruneblockchain", pruneblockchain, {"height"} }, | { "blockchain", "pruneblockchain", pruneblockchain, {"height"} }, | ||||
{ "blockchain", "savemempool", savemempool, {} }, | { "blockchain", "savemempool", savemempool, {} }, | ||||
{ "blockchain", "verifychain", verifychain, {"checklevel","nblocks"} }, | { "blockchain", "verifychain", verifychain, {"checklevel","nblocks"} }, | ||||
{ "blockchain", "preciousblock", preciousblock, {"blockhash"} }, | { "blockchain", "preciousblock", preciousblock, {"blockhash"} }, | ||||
{ "blockchain", "scantxoutset", scantxoutset, {"action", "scanobjects"} }, | { "blockchain", "scantxoutset", scantxoutset, {"action", "scanobjects"} }, | ||||
{ "blockchain", "getblockfilter", getblockfilter, {"blockhash", "filtertype"} }, | |||||
/* Not shown in help */ | /* Not shown in help */ | ||||
{ "hidden", "getfinalizedblockhash", getfinalizedblockhash, {} }, | { "hidden", "getfinalizedblockhash", getfinalizedblockhash, {} }, | ||||
{ "hidden", "finalizeblock", finalizeblock, {"blockhash"} }, | { "hidden", "finalizeblock", finalizeblock, {"blockhash"} }, | ||||
{ "hidden", "invalidateblock", invalidateblock, {"blockhash"} }, | { "hidden", "invalidateblock", invalidateblock, {"blockhash"} }, | ||||
{ "hidden", "parkblock", parkblock, {"blockhash"} }, | { "hidden", "parkblock", parkblock, {"blockhash"} }, | ||||
{ "hidden", "reconsiderblock", reconsiderblock, {"blockhash"} }, | { "hidden", "reconsiderblock", reconsiderblock, {"blockhash"} }, | ||||
{ "hidden", "syncwithvalidationinterfacequeue", syncwithvalidationinterfacequeue, {} }, | { "hidden", "syncwithvalidationinterfacequeue", syncwithvalidationinterfacequeue, {} }, | ||||
Show All 12 Lines |