Remove unreachable or otherwise redundant code
[bitcoinplatinum.git] / src / bitcoin-cli.cpp
blob885b787b4da09815fd6ba79038608f0b9e8c0281
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
10 #include "chainparamsbase.h"
11 #include "clientversion.h"
12 #include "fs.h"
13 #include "rpc/client.h"
14 #include "rpc/protocol.h"
15 #include "util.h"
16 #include "utilstrencodings.h"
18 #include <stdio.h>
20 #include <event2/buffer.h>
21 #include <event2/keyvalq_struct.h>
22 #include "support/events.h"
24 #include <univalue.h>
26 static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
27 static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
28 static const bool DEFAULT_NAMED=false;
29 static const int CONTINUE_EXECUTION=-1;
31 std::string HelpMessageCli()
33 const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
34 const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
35 std::string strUsage;
36 strUsage += HelpMessageGroup(_("Options:"));
37 strUsage += HelpMessageOpt("-?", _("This help message"));
38 strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
39 strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
40 AppendParamsHelpMessages(strUsage);
41 strUsage += HelpMessageOpt("-named", strprintf(_("Pass named instead of positional arguments (default: %s)"), DEFAULT_NAMED));
42 strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), DEFAULT_RPCCONNECT));
43 strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
44 strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start"));
45 strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
46 strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
47 strUsage += HelpMessageOpt("-rpcclienttimeout=<n>", strprintf(_("Timeout in seconds during HTTP requests, or 0 for no timeout. (default: %d)"), DEFAULT_HTTP_CLIENT_TIMEOUT));
48 strUsage += HelpMessageOpt("-stdin", _("Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases)"));
50 return strUsage;
53 //////////////////////////////////////////////////////////////////////////////
55 // Start
59 // Exception thrown on connection error. This error is used to determine
60 // when to wait if -rpcwait is given.
62 class CConnectionFailed : public std::runtime_error
64 public:
66 explicit inline CConnectionFailed(const std::string& msg) :
67 std::runtime_error(msg)
73 // This function returns either one of EXIT_ codes when it's expected to stop the process or
74 // CONTINUE_EXECUTION when it's expected to continue further.
76 static int AppInitRPC(int argc, char* argv[])
79 // Parameters
81 ParseParameters(argc, argv);
82 if (argc<2 || IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version")) {
83 std::string strUsage = strprintf(_("%s RPC client version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n";
84 if (!IsArgSet("-version")) {
85 strUsage += "\n" + _("Usage:") + "\n" +
86 " bitcoin-cli [options] <command> [params] " + strprintf(_("Send command to %s"), _(PACKAGE_NAME)) + "\n" +
87 " bitcoin-cli [options] -named <command> [name=value] ... " + strprintf(_("Send command to %s (with named arguments)"), _(PACKAGE_NAME)) + "\n" +
88 " bitcoin-cli [options] help " + _("List commands") + "\n" +
89 " bitcoin-cli [options] help <command> " + _("Get help for a command") + "\n";
91 strUsage += "\n" + HelpMessageCli();
94 fprintf(stdout, "%s", strUsage.c_str());
95 if (argc < 2) {
96 fprintf(stderr, "Error: too few parameters\n");
97 return EXIT_FAILURE;
99 return EXIT_SUCCESS;
101 if (!fs::is_directory(GetDataDir(false))) {
102 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", GetArg("-datadir", "").c_str());
103 return EXIT_FAILURE;
105 try {
106 ReadConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME));
107 } catch (const std::exception& e) {
108 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
109 return EXIT_FAILURE;
111 // Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
112 try {
113 SelectBaseParams(ChainNameFromCommandLine());
114 } catch (const std::exception& e) {
115 fprintf(stderr, "Error: %s\n", e.what());
116 return EXIT_FAILURE;
118 if (GetBoolArg("-rpcssl", false))
120 fprintf(stderr, "Error: SSL mode for RPC (-rpcssl) is no longer supported.\n");
121 return EXIT_FAILURE;
123 return CONTINUE_EXECUTION;
127 /** Reply structure for request_done to fill in */
128 struct HTTPReply
130 HTTPReply(): status(0), error(-1) {}
132 int status;
133 int error;
134 std::string body;
137 const char *http_errorstring(int code)
139 switch(code) {
140 #if LIBEVENT_VERSION_NUMBER >= 0x02010300
141 case EVREQ_HTTP_TIMEOUT:
142 return "timeout reached";
143 case EVREQ_HTTP_EOF:
144 return "EOF reached";
145 case EVREQ_HTTP_INVALID_HEADER:
146 return "error while reading header, or invalid header";
147 case EVREQ_HTTP_BUFFER_ERROR:
148 return "error encountered while reading or writing";
149 case EVREQ_HTTP_REQUEST_CANCEL:
150 return "request was canceled";
151 case EVREQ_HTTP_DATA_TOO_LONG:
152 return "response body is larger than allowed";
153 #endif
154 default:
155 return "unknown";
159 static void http_request_done(struct evhttp_request *req, void *ctx)
161 HTTPReply *reply = static_cast<HTTPReply*>(ctx);
163 if (req == NULL) {
164 /* If req is NULL, it means an error occurred while connecting: the
165 * error code will have been passed to http_error_cb.
167 reply->status = 0;
168 return;
171 reply->status = evhttp_request_get_response_code(req);
173 struct evbuffer *buf = evhttp_request_get_input_buffer(req);
174 if (buf)
176 size_t size = evbuffer_get_length(buf);
177 const char *data = (const char*)evbuffer_pullup(buf, size);
178 if (data)
179 reply->body = std::string(data, size);
180 evbuffer_drain(buf, size);
184 #if LIBEVENT_VERSION_NUMBER >= 0x02010300
185 static void http_error_cb(enum evhttp_request_error err, void *ctx)
187 HTTPReply *reply = static_cast<HTTPReply*>(ctx);
188 reply->error = err;
190 #endif
192 UniValue CallRPC(const std::string& strMethod, const UniValue& params)
194 std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
195 int port = GetArg("-rpcport", BaseParams().RPCPort());
197 // Obtain event base
198 raii_event_base base = obtain_event_base();
200 // Synchronously look up hostname
201 raii_evhttp_connection evcon = obtain_evhttp_connection_base(base.get(), host, port);
202 evhttp_connection_set_timeout(evcon.get(), GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT));
204 HTTPReply response;
205 raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response);
206 if (req == NULL)
207 throw std::runtime_error("create http request failed");
208 #if LIBEVENT_VERSION_NUMBER >= 0x02010300
209 evhttp_request_set_error_cb(req.get(), http_error_cb);
210 #endif
212 // Get credentials
213 std::string strRPCUserColonPass;
214 if (GetArg("-rpcpassword", "") == "") {
215 // Try fall back to cookie-based authentication if no password is provided
216 if (!GetAuthCookie(&strRPCUserColonPass)) {
217 throw std::runtime_error(strprintf(
218 _("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"),
219 GetConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str()));
222 } else {
223 strRPCUserColonPass = GetArg("-rpcuser", "") + ":" + GetArg("-rpcpassword", "");
226 struct evkeyvalq* output_headers = evhttp_request_get_output_headers(req.get());
227 assert(output_headers);
228 evhttp_add_header(output_headers, "Host", host.c_str());
229 evhttp_add_header(output_headers, "Connection", "close");
230 evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str());
232 // Attach request data
233 std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n";
234 struct evbuffer* output_buffer = evhttp_request_get_output_buffer(req.get());
235 assert(output_buffer);
236 evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
238 int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, "/");
239 req.release(); // ownership moved to evcon in above call
240 if (r != 0) {
241 throw CConnectionFailed("send http request failed");
244 event_base_dispatch(base.get());
246 if (response.status == 0)
247 throw CConnectionFailed(strprintf("couldn't connect to server: %s (code %d)\n(make sure server is running and you are connecting to the correct RPC port)", http_errorstring(response.error), response.error));
248 else if (response.status == HTTP_UNAUTHORIZED)
249 throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
250 else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR)
251 throw std::runtime_error(strprintf("server returned HTTP error %d", response.status));
252 else if (response.body.empty())
253 throw std::runtime_error("no response from server");
255 // Parse reply
256 UniValue valReply(UniValue::VSTR);
257 if (!valReply.read(response.body))
258 throw std::runtime_error("couldn't parse reply from server");
259 const UniValue& reply = valReply.get_obj();
260 if (reply.empty())
261 throw std::runtime_error("expected reply to have result, error and id properties");
263 return reply;
266 int CommandLineRPC(int argc, char *argv[])
268 std::string strPrint;
269 int nRet = 0;
270 try {
271 // Skip switches
272 while (argc > 1 && IsSwitchChar(argv[1][0])) {
273 argc--;
274 argv++;
276 std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
277 if (GetBoolArg("-stdin", false)) {
278 // Read one arg per line from stdin and append
279 std::string line;
280 while (std::getline(std::cin,line))
281 args.push_back(line);
283 if (args.size() < 1)
284 throw std::runtime_error("too few parameters (need at least command)");
285 std::string strMethod = args[0];
286 args.erase(args.begin()); // Remove trailing method name from arguments vector
288 UniValue params;
289 if(GetBoolArg("-named", DEFAULT_NAMED)) {
290 params = RPCConvertNamedValues(strMethod, args);
291 } else {
292 params = RPCConvertValues(strMethod, args);
295 // Execute and handle connection failures with -rpcwait
296 const bool fWait = GetBoolArg("-rpcwait", false);
297 do {
298 try {
299 const UniValue reply = CallRPC(strMethod, params);
301 // Parse reply
302 const UniValue& result = find_value(reply, "result");
303 const UniValue& error = find_value(reply, "error");
305 if (!error.isNull()) {
306 // Error
307 int code = error["code"].get_int();
308 if (fWait && code == RPC_IN_WARMUP)
309 throw CConnectionFailed("server in warmup");
310 strPrint = "error: " + error.write();
311 nRet = abs(code);
312 if (error.isObject())
314 UniValue errCode = find_value(error, "code");
315 UniValue errMsg = find_value(error, "message");
316 strPrint = errCode.isNull() ? "" : "error code: "+errCode.getValStr()+"\n";
318 if (errMsg.isStr())
319 strPrint += "error message:\n"+errMsg.get_str();
321 } else {
322 // Result
323 if (result.isNull())
324 strPrint = "";
325 else if (result.isStr())
326 strPrint = result.get_str();
327 else
328 strPrint = result.write(2);
330 // Connection succeeded, no need to retry.
331 break;
333 catch (const CConnectionFailed&) {
334 if (fWait)
335 MilliSleep(1000);
336 else
337 throw;
339 } while (fWait);
341 catch (const boost::thread_interrupted&) {
342 throw;
344 catch (const std::exception& e) {
345 strPrint = std::string("error: ") + e.what();
346 nRet = EXIT_FAILURE;
348 catch (...) {
349 PrintExceptionContinue(NULL, "CommandLineRPC()");
350 throw;
353 if (strPrint != "") {
354 fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
356 return nRet;
359 int main(int argc, char* argv[])
361 SetupEnvironment();
362 if (!SetupNetworking()) {
363 fprintf(stderr, "Error: Initializing networking failed\n");
364 return EXIT_FAILURE;
367 try {
368 int ret = AppInitRPC(argc, argv);
369 if (ret != CONTINUE_EXECUTION)
370 return ret;
372 catch (const std::exception& e) {
373 PrintExceptionContinue(&e, "AppInitRPC()");
374 return EXIT_FAILURE;
375 } catch (...) {
376 PrintExceptionContinue(NULL, "AppInitRPC()");
377 return EXIT_FAILURE;
380 int ret = EXIT_FAILURE;
381 try {
382 ret = CommandLineRPC(argc, argv);
384 catch (const std::exception& e) {
385 PrintExceptionContinue(&e, "CommandLineRPC()");
386 } catch (...) {
387 PrintExceptionContinue(NULL, "CommandLineRPC()");
389 return ret;