diff --git a/src/callrpc.cpp b/src/callrpc.cpp index 43182b23cd..ad8784556e 100644 --- a/src/callrpc.cpp +++ b/src/callrpc.cpp @@ -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"); diff --git a/src/callrpc.h b/src/callrpc.h index 31bbe08365..027e07e3b3 100644 --- a/src/callrpc.h +++ b/src/callrpc.h @@ -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 diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp index fed27dcd0c..db5a39c642 100644 --- a/src/chainparamsbase.cpp +++ b/src/chainparamsbase.cpp @@ -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; } }; diff --git a/src/chainparamsbase.h b/src/chainparamsbase.h index 30f0f7e272..382366a838 100644 --- a/src/chainparamsbase.h +++ b/src/chainparamsbase.h @@ -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; }; diff --git a/src/main.cpp b/src/main.cpp index 3046819c5d..11b6e807ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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;