Fix IsConfirmedBitcoinBlock, clean up CallRPC

This commit is contained in:
Gregory Sanders 2016-12-07 09:54:00 -05:00
parent 1150d0e9fc
commit c868ef325e
5 changed files with 23 additions and 11 deletions

View file

@ -43,23 +43,24 @@ static void http_request_done(struct evhttp_request *req, void *ctx)
}
}
UniValue CallRPC(const string& strMethod, const UniValue& params, int port, bool connectToMainchain)
UniValue CallRPC(const string& strMethod, const UniValue& params, bool connectToMainchain)
{
std::string strhost = "-rpcconnect";
std::string strport = "-rpcport";
std::string struser = "-rpcuser";
std::string strpassword = "-rpcpassword";
int port = GetArg(strport, BaseParams().RPCPort());
if (connectToMainchain) {
strhost = "-mainchainhost";
strport = "-mainchainrpcport";
strpassword = "-mainchainrpcpassword";
struser = "-mainchainrpcuser";
port = GetArg(strport, BaseParams().MainchainRPCPort());
}
std::string host = GetArg(strhost, DEFAULT_RPCHOST);
if (port < 0)
port = GetArg(strport, BaseParams().RPCPort());
// Create event base
struct event_base *base = event_base_new(); // TODO RAII
if (!base)
@ -90,12 +91,16 @@ UniValue CallRPC(const string& strMethod, const UniValue& params, int port, bool
// Try fall back to cookie-based authentication if no password is provided
if (connectToMainchain && !GetMainchainAuthCookie(&strRPCUserColonPass)) {
throw runtime_error(strprintf(
_("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"),
_("Could not locate mainchain RPC credentials. No authentication cookie could be found, and no mainchainrpcpassword is set in the configuration file (%s)"),
GetConfigFile().string().c_str()));
}
} else {
strRPCUserColonPass = mapArgs[struser] + ":" + mapArgs[strpassword];
if (struser == "")
throw runtime_error(
_("Could not locate mainchain RPC credentials. No authentication cookie could be found, and no mainchainrpcuser is set in the configuration file"));
else
strRPCUserColonPass = mapArgs[struser] + ":" + mapArgs[strpassword];
}
struct evkeyvalq *output_headers = evhttp_request_get_output_headers(req);
@ -124,7 +129,10 @@ UniValue CallRPC(const string& strMethod, const UniValue& params, int port, bool
if (response.status == 0)
throw CConnectionFailed("couldn't connect to server (make sure daemon is running and you are using the right rpc port)");
else if (response.status == HTTP_UNAUTHORIZED)
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
if (connectToMainchain)
throw runtime_error("incorrect mainchainrpcuser or mainchainrpcpassword (authorization failed)");
else
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR)
throw runtime_error(strprintf("server returned HTTP error %d", response.status));
else if (response.body.empty())
@ -147,7 +155,7 @@ bool IsConfirmedBitcoinBlock(const uint256& genesishash, const uint256& hash, in
try {
UniValue params(UniValue::VARR);
params.push_back(UniValue(0));
UniValue reply = CallRPC("getblockhash", params, GetArg("-mainchainrpcport", 18332));
UniValue reply = CallRPC("getblockhash", params, true);
if (!find_value(reply, "error").isNull())
return false;
UniValue result = find_value(reply, "result");
@ -158,7 +166,7 @@ bool IsConfirmedBitcoinBlock(const uint256& genesishash, const uint256& hash, in
params = UniValue(UniValue::VARR);
params.push_back(hash.GetHex());
reply = CallRPC("getblock", params, GetArg("-mainchainrpcport", 18332));
reply = CallRPC("getblock", params, true);
if (!find_value(reply, "error").isNull())
return false;
result = find_value(reply, "result");