Skip to content

Commit 621b52f

Browse files
committed
Check parent RPC version on startup
1 parent 7bfb100 commit 621b52f

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/validation.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,16 +2229,18 @@ bool BitcoindRPCCheck(const bool init)
22292229
vblocksToReconsiderAgain.clear();
22302230
pblocktree->WriteInvalidBlockQueue(vblocksToReconsider);
22312231

2232-
//Next, check for working rpc
2232+
// Next, check for working and valid rpc
22332233
if (GetBoolArg("-validatepegin", DEFAULT_VALIDATE_PEGIN)) {
22342234
// During init try until a non-RPC_IN_WARMUP result
22352235
while (true) {
22362236
try {
2237+
// The first thing we have to check is the version of the node.
22372238
UniValue params(UniValue::VARR);
2238-
params.push_back(UniValue(0));
2239-
UniValue reply = CallRPC("getblockhash", params, true);
2240-
UniValue error = find_value(reply, "error");
2239+
UniValue reply = CallRPC("getnetworkinfo", params, true);
2240+
UniValue error = reply["error"];
22412241
if (!error.isNull()) {
2242+
// On the first call, it's possible to node is still in
2243+
// warmup; in that case, just wait and retry.
22422244
if (error["code"].get_int() == RPC_IN_WARMUP) {
22432245
MilliSleep(1000);
22442246
continue;
@@ -2249,6 +2251,22 @@ bool BitcoindRPCCheck(const bool init)
22492251
}
22502252
}
22512253
UniValue result = reply["result"];
2254+
if (!result.isObject() || !result.get_obj()["version"].isNum() ||
2255+
result.get_obj()["version"].get_int() < MIN_PARENT_NODE_VERSION) {
2256+
LogPrintf("ERROR: Parent chain daemon too old; "
2257+
"need Bitcoin Core version 0.16.2 or newer.\n");
2258+
return false;
2259+
}
2260+
2261+
// Then check the genesis block to correspond to parent chain.
2262+
params.push_back(UniValue(0));
2263+
reply = CallRPC("getblockhash", params, true);
2264+
error = reply["error"];
2265+
if (!error.isNull()) {
2266+
LogPrintf("ERROR: Bitcoind RPC check returned 'error' response.\n");
2267+
return false;
2268+
}
2269+
result = reply["result"];
22522270
if (!result.isStr() || result.get_str() != Params().ParentGenesisBlockHash().GetHex()) {
22532271
LogPrintf("ERROR: Invalid parent genesis block hash response via RPC. Contacting wrong parent daemon?\n");
22542272
return false;
@@ -2304,7 +2322,7 @@ bool BitcoindRPCCheck(const bool init)
23042322

23052323
//Write back remaining blocks
23062324
pblocktree->WriteInvalidBlockQueue(vblocksToReconsiderAgain);
2307-
}
2325+
}
23082326
return true;
23092327
}
23102328

src/validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ static const int MAX_UNCONNECTING_HEADERS = 10;
149149

150150
static const bool DEFAULT_PEERBLOOMFILTERS = false;
151151

152+
/** The minimum version for the parent chain node.
153+
* We need v0.16.2 to get the nTx field in getblockheader. */
154+
static const int MIN_PARENT_NODE_VERSION = 160200; // 0.16.2
155+
152156
struct BlockHasher
153157
{
154158
size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }

0 commit comments

Comments
 (0)