Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 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. | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <chainparamsbase.h> | #include <chainparamsbase.h> | ||||
#include <clientversion.h> | #include <clientversion.h> | ||||
#include <currencyunit.h> | #include <currencyunit.h> | ||||
#include <rpc/client.h> | #include <rpc/client.h> | ||||
#include <rpc/mining.h> | |||||
#include <rpc/protocol.h> | #include <rpc/protocol.h> | ||||
#include <rpc/request.h> | #include <rpc/request.h> | ||||
#include <support/events.h> | #include <support/events.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/translation.h> | #include <util/translation.h> | ||||
#include <event2/buffer.h> | #include <event2/buffer.h> | ||||
Show All 12 Lines | |||||
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1"; | static const char DEFAULT_RPCCONNECT[] = "127.0.0.1"; | ||||
static const int DEFAULT_HTTP_CLIENT_TIMEOUT = 900; | static const int DEFAULT_HTTP_CLIENT_TIMEOUT = 900; | ||||
static const bool DEFAULT_NAMED = false; | static const bool DEFAULT_NAMED = false; | ||||
static const int CONTINUE_EXECUTION = -1; | static const int CONTINUE_EXECUTION = -1; | ||||
static const std::string ONION{".onion"}; | static const std::string ONION{".onion"}; | ||||
static const size_t ONION_LEN{ONION.size()}; | static const size_t ONION_LEN{ONION.size()}; | ||||
/** Default number of blocks to generate for RPC generatetoaddress. */ | |||||
static const std::string DEFAULT_NBLOCKS = "1"; | |||||
static void SetupCliArgs(ArgsManager &argsman) { | static void SetupCliArgs(ArgsManager &argsman) { | ||||
SetupHelpOptions(argsman); | SetupHelpOptions(argsman); | ||||
const auto defaultBaseParams = | const auto defaultBaseParams = | ||||
CreateBaseChainParams(CBaseChainParams::MAIN); | CreateBaseChainParams(CBaseChainParams::MAIN); | ||||
const auto testnetBaseParams = | const auto testnetBaseParams = | ||||
CreateBaseChainParams(CBaseChainParams::TESTNET); | CreateBaseChainParams(CBaseChainParams::TESTNET); | ||||
const auto regtestBaseParams = | const auto regtestBaseParams = | ||||
CreateBaseChainParams(CBaseChainParams::REGTEST); | CreateBaseChainParams(CBaseChainParams::REGTEST); | ||||
SetupCurrencyUnitOptions(argsman); | SetupCurrencyUnitOptions(argsman); | ||||
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, | argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, | ||||
OptionsCategory::OPTIONS); | OptionsCategory::OPTIONS); | ||||
argsman.AddArg( | argsman.AddArg( | ||||
"-conf=<file>", | "-conf=<file>", | ||||
strprintf("Specify configuration file. Relative paths will be " | strprintf("Specify configuration file. Relative paths will be " | ||||
"prefixed by datadir location. (default: %s)", | "prefixed by datadir location. (default: %s)", | ||||
BITCOIN_CONF_FILENAME), | BITCOIN_CONF_FILENAME), | ||||
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ||||
argsman.AddArg("-datadir=<dir>", "Specify data directory", | argsman.AddArg("-datadir=<dir>", "Specify data directory", | ||||
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ||||
argsman.AddArg( | argsman.AddArg( | ||||
"-generate", | |||||
strprintf( | |||||
"Generate blocks immediately, equivalent to RPC generatenewaddress " | |||||
"followed by RPC generatetoaddress. Optional positional integer " | |||||
"arguments are number of blocks to generate (default: %s) and " | |||||
"maximum iterations to try (default: %s), equivalent to RPC " | |||||
"generatetoaddress nblocks and maxtries arguments. Example: " | |||||
"bitcoin-cli -generate 4 1000", | |||||
DEFAULT_NBLOCKS, DEFAULT_MAX_TRIES), | |||||
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | |||||
argsman.AddArg( | |||||
"-getinfo", | "-getinfo", | ||||
"Get general information from the remote server. Note that unlike " | "Get general information from the remote server. Note that unlike " | ||||
"server-side RPC calls, the results of -getinfo is the result of " | "server-side RPC calls, the results of -getinfo is the result of " | ||||
"multiple non-atomic requests. Some entries in the result may " | "multiple non-atomic requests. Some entries in the result may " | ||||
"represent results from different states (e.g. wallet balance may be " | "represent results from different states (e.g. wallet balance may be " | ||||
"as of a different block from the chain state reported)", | "as of a different block from the chain state reported)", | ||||
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); | ||||
argsman.AddArg("-netinfo", | argsman.AddArg("-netinfo", | ||||
▲ Show 20 Lines • Show All 866 Lines • ▼ Show 20 Lines | static void GetWalletBalances(UniValue &result) { | ||||
} | } | ||||
result.pushKV("balances", balances); | result.pushKV("balances", balances); | ||||
} | } | ||||
/** | /** | ||||
* Call RPC getnewaddress. | * Call RPC getnewaddress. | ||||
* @returns getnewaddress response as a UniValue object. | * @returns getnewaddress response as a UniValue object. | ||||
*/ | */ | ||||
[[maybe_unused]] static UniValue GetNewAddress() { | static UniValue GetNewAddress() { | ||||
std::unique_ptr<BaseRequestHandler> rh{ | std::unique_ptr<BaseRequestHandler> rh{ | ||||
std::make_unique<DefaultRequestHandler>()}; | std::make_unique<DefaultRequestHandler>()}; | ||||
return ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{}); | return ConnectAndCallRPC(rh.get(), "getnewaddress", /* args=*/{}); | ||||
} | } | ||||
/** | |||||
* Check bounds and set up args for RPC generatetoaddress params: nblocks, | |||||
* address, maxtries. | |||||
* @param[in] address Reference to const string address to insert into the | |||||
* args. | |||||
* @param args Reference to vector of string args to modify. | |||||
*/ | |||||
static void SetGenerateToAddressArgs(const std::string &address, | |||||
std::vector<std::string> &args) { | |||||
if (args.size() > 2) { | |||||
throw std::runtime_error( | |||||
"too many arguments (maximum 2 for nblocks and maxtries)"); | |||||
} | |||||
if (args.size() == 0) { | |||||
args.emplace_back(DEFAULT_NBLOCKS); | |||||
} else if (args.at(0) == "0") { | |||||
throw std::runtime_error( | |||||
"the first argument (number of blocks to generate, default: " + | |||||
DEFAULT_NBLOCKS + ") must be an integer value greater than zero"); | |||||
} | |||||
args.emplace(args.begin() + 1, address); | |||||
} | |||||
static int CommandLineRPC(int argc, char *argv[]) { | static int CommandLineRPC(int argc, char *argv[]) { | ||||
std::string strPrint; | std::string strPrint; | ||||
int nRet = 0; | int nRet = 0; | ||||
try { | try { | ||||
// Skip switches | // Skip switches | ||||
while (argc > 1 && IsSwitchChar(argv[1][0])) { | while (argc > 1 && IsSwitchChar(argv[1][0])) { | ||||
argc--; | argc--; | ||||
argv++; | argv++; | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | try { | ||||
if (StdinTerminal()) { | if (StdinTerminal()) { | ||||
fputc('\n', stdout); | fputc('\n', stdout); | ||||
} | } | ||||
} | } | ||||
std::unique_ptr<BaseRequestHandler> rh; | std::unique_ptr<BaseRequestHandler> rh; | ||||
std::string method; | std::string method; | ||||
if (gArgs.IsArgSet("-getinfo")) { | if (gArgs.IsArgSet("-getinfo")) { | ||||
rh.reset(new GetinfoRequestHandler()); | rh.reset(new GetinfoRequestHandler()); | ||||
} else if (gArgs.GetBoolArg("-generate", false)) { | |||||
const UniValue getnewaddress{GetNewAddress()}; | |||||
const UniValue &error{find_value(getnewaddress, "error")}; | |||||
if (error.isNull()) { | |||||
SetGenerateToAddressArgs( | |||||
find_value(getnewaddress, "result").get_str(), args); | |||||
rh.reset(new GenerateToAddressRequestHandler()); | |||||
} else { | |||||
ParseError(error, strPrint, nRet); | |||||
} | |||||
} else if (gArgs.GetBoolArg("-netinfo", false)) { | } else if (gArgs.GetBoolArg("-netinfo", false)) { | ||||
rh.reset(new NetinfoRequestHandler()); | rh.reset(new NetinfoRequestHandler()); | ||||
} else { | } else { | ||||
rh.reset(new DefaultRequestHandler()); | rh.reset(new DefaultRequestHandler()); | ||||
if (args.size() < 1) { | if (args.size() < 1) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"too few parameters (need at least command)"); | "too few parameters (need at least command)"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |