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");

View file

@ -32,7 +32,7 @@ public:
};
UniValue CallRPC(const std::string& strMethod, const UniValue& params, int port=-1, bool connectToMainchain=false);
UniValue CallRPC(const std::string& strMethod, const UniValue& params, bool connectToMainchain=false);
bool IsConfirmedBitcoinBlock(const uint256& genesishash, const uint256& hash, int nMinConfirmationDepth);
#endif // BITCOIN_CALLRPC_H

View file

@ -45,6 +45,7 @@ public:
CBaseElementsParams()
{
nRPCPort = 9041;
nMainchainRPCPort = 18332;
strDataDir = CHAINPARAMS_ELEMENTS;
}
};
@ -58,6 +59,7 @@ public:
CBaseRegTestParams()
{
nRPCPort = 7041;
nMainchainRPCPort = 18332;
strDataDir = CHAINPARAMS_REGTEST;
}
};

View file

@ -24,6 +24,7 @@ public:
const std::string& DataDir() const { return strDataDir; }
int RPCPort() const { return nRPCPort; }
int MainchainRPCPort() const { return nMainchainRPCPort; }
/**
* Creates and returns a CBaseChainParams* of the chosen chain. The caller has to delete the object.
* @returns A CBaseChainParams* of the chosen chain.
@ -34,6 +35,7 @@ protected:
CBaseChainParams() {}
int nRPCPort;
int nMainchainRPCPort;
std::string strDataDir;
};

View file

@ -2654,7 +2654,7 @@ bool BitcoindRPCCheck(bool init)
try {
UniValue params(UniValue::VARR);
params.push_back(UniValue(0));
UniValue reply = CallRPC("getblockhash", params, GetArg("-mainchainrpcport", 18332), true);
UniValue reply = CallRPC("getblockhash", params, true);
if (!find_value(reply, "error").isNull()) {
LogPrintf("ERROR: Bitcoind RPC check returned 'error' response.\n");
return false;