Skip to content

Commit 9e5d612

Browse files
committed
Check parent RPC version on startup
1 parent 9259ae4 commit 9e5d612

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/validation.cpp

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

2231-
//Next, check for working rpc
2231+
// Next, check for working and valid rpc
22322232
if (GetBoolArg("-validatepegin", DEFAULT_VALIDATE_PEGIN)) {
22332233
// During init try until a non-RPC_IN_WARMUP result
22342234
while (true) {
22352235
try {
2236+
// The first thing we have to check is the version of the node.
22362237
UniValue params(UniValue::VARR);
2237-
params.push_back(UniValue(0));
2238-
UniValue reply = CallRPC("getblockhash", params, true);
2239-
UniValue error = find_value(reply, "error");
2238+
UniValue reply = CallRPC("getnetworkinfo", params, true);
2239+
UniValue error = reply["error"];
22402240
if (!error.isNull()) {
2241+
// On the first call, it's possible to node is still in
2242+
// warmup; in that case, just wait and retry.
22412243
if (error["code"].get_int() == RPC_IN_WARMUP) {
22422244
MilliSleep(1000);
22432245
continue;
@@ -2248,6 +2250,21 @@ bool BitcoindRPCCheck(const bool init)
22482250
}
22492251
}
22502252
UniValue result = reply["result"];
2253+
if (!result.isObject() || !result.get_obj()["version"].isNum() ||
2254+
result.get_obj()["version"].get_int() < MIN_PARENT_RPC_VERSION) {
2255+
LogPrintf("ERROR: Parent chain daemon too old; need version 0.16.2.\n");
2256+
return false;
2257+
}
2258+
2259+
// Then check the genesis block to correspond to parent chain.
2260+
params.push_back(UniValue(0));
2261+
reply = CallRPC("getblockhash", params, true);
2262+
error = reply["error"];
2263+
if (!error.isNull()) {
2264+
LogPrintf("ERROR: Bitcoind RPC check returned 'error' response.\n");
2265+
return false;
2266+
}
2267+
result = reply["result"];
22512268
if (!result.isStr() || result.get_str() != Params().ParentGenesisBlockHash().GetHex()) {
22522269
LogPrintf("ERROR: Invalid parent genesis block hash response via RPC. Contacting wrong parent daemon?\n");
22532270
return false;
@@ -2303,7 +2320,7 @@ bool BitcoindRPCCheck(const bool init)
23032320

23042321
//Write back remaining blocks
23052322
pblocktree->WriteInvalidBlockQueue(vblocksToReconsiderAgain);
2306-
}
2323+
}
23072324
return true;
23082325
}
23092326

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 RPC version for the parent chain.
153+
* We need v0.16.2 to get the nTx field in getblockheader. */
154+
static const int MIN_PARENT_RPC_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)