Use RPC calls to check bitcoin blocks are valid and confirmed

This commit is contained in:
Matt Corallo 2014-12-06 14:38:47 -08:00 committed by Gregory Sanders
parent 7c9a06de5a
commit c94ee2b4e0
12 changed files with 122 additions and 13 deletions

View file

@ -76,10 +76,11 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
}
#endif
UniValue CallRPC(const std::string& strMethod, const UniValue& params)
UniValue CallRPC(const std::string& strMethod, const UniValue& params, int port)
{
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
int port = GetArg("-rpcport", BaseParams().RPCPort());
if (port < 0)
port = GetArg("-rpcport", BaseParams().RPCPort());
// Obtain event base
raii_event_base base = obtain_event_base();
@ -149,3 +150,37 @@ UniValue CallRPC(const std::string& strMethod, const UniValue& params)
return reply;
}
bool IsConfirmedBitcoinBlock(const uint256& genesishash, const uint256& hash, int nMinConfirmationDepth)
{
try {
UniValue params(UniValue::VARR);
params.push_back(UniValue(0));
UniValue reply = CallRPC("getblockhash", params, GetArg("-rpcconnectport", 18332));
if (!find_value(reply, "error").isNull())
return false;
UniValue result = find_value(reply, "result");
if (!result.isStr())
return false;
if (result.get_str() != genesishash.GetHex())
return false;
params = UniValue(UniValue::VARR);
params.push_back(hash.GetHex());
reply = CallRPC("getblock", params, GetArg("-rpcconnectport", 18332));
if (!find_value(reply, "error").isNull())
return false;
result = find_value(reply, "result");
if (!result.isObject())
return false;
result = find_value(result.get_obj(), "confirmations");
return result.isNum() && result.get_int64() >= nMinConfirmationDepth;
} catch (CConnectionFailed& e) {
LogPrintf("ERROR: Lost connection to bitcoind RPC, you will want to restart after fixing this!\n");
return false;
} catch (...) {
LogPrintf("ERROR: Failure connecting to bitcoind RPC, you will want to restart after fixing this!\n");
return false;
}
return true;
}