MCPcopy Create free account
hub / github.com/ElementsProject/elements / MainchainRPCCheck

Function MainchainRPCCheck

src/init.cpp:1188–1238  ·  view source on GitHub ↗

This function checks that the RPC connection to the parent chain node * can be attained, and is returning back reasonable answers. */

Source from the content-addressed store, hash-verified

1186 * can be attained, and is returning back reasonable answers.
1187 */
1188bool MainchainRPCCheck()
1189{
1190 // Check for working and valid rpc
1191 // Retry until a non-RPC_IN_WARMUP result
1192 while (true) {
1193 try {
1194 // The first thing we have to check is the version of the node.
1195 UniValue params(UniValue::VARR);
1196 UniValue reply = CallMainChainRPC("getnetworkinfo", params);
1197 UniValue error = reply["error"];
1198 if (!error.isNull()) {
1199 // On the first call, it's possible to node is still in
1200 // warmup; in that case, just wait and retry.
1201 if (error["code"].get_int() == RPC_IN_WARMUP) {
1202 UninterruptibleSleep(std::chrono::milliseconds{1000});
1203 continue;
1204 }
1205 else {
1206 LogPrintf("ERROR: Mainchain daemon RPC check returned 'error' response.\n");
1207 return false;
1208 }
1209 }
1210 UniValue result = reply["result"];
1211 if (!result.isObject() || !result.get_obj()["version"].isNum() ||
1212 result.get_obj()["version"].get_int() < MIN_MAINCHAIN_NODE_VERSION) {
1213 LogPrintf("ERROR: Parent chain daemon too old; need Bitcoin Core version 0.16.3 or newer.\n");
1214 return false;
1215 }
1216
1217 // Then check the genesis block to correspond to parent chain.
1218 params.push_back(UniValue(0));
1219 reply = CallMainChainRPC("getblockhash", params);
1220 error = reply["error"];
1221 if (!error.isNull()) {
1222 LogPrintf("ERROR: Mainchain daemon RPC check returned 'error' response.\n");
1223 return false;
1224 }
1225 result = reply["result"];
1226 if (!result.isStr() || result.get_str() != Params().ParentGenesisBlockHash().GetHex()) {
1227 LogPrintf("ERROR: Invalid parent genesis block hash response via RPC. Contacting wrong parent daemon?\n");
1228 return false;
1229 }
1230 } catch (const std::runtime_error& re) {
1231 LogPrintf("ERROR: Failure connecting to mainchain daemon RPC: %s\n", std::string(re.what()));
1232 return false;
1233 }
1234
1235 // Success
1236 return true;
1237 }
1238}
1239
1240bool AppInitInterfaces(NodeContext& node)
1241{

Callers 1

AppInitMainFunction · 0.85

Calls 11

CallMainChainRPCFunction · 0.85
UninterruptibleSleepFunction · 0.85
get_intMethod · 0.80
isObjectMethod · 0.80
isNumMethod · 0.80
isStrMethod · 0.80
UniValueClass · 0.70
ParamsClass · 0.70
isNullMethod · 0.45
push_backMethod · 0.45
GetHexMethod · 0.45

Tested by

no test coverage detected