@@ -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
@@ -2513,6 +2531,7 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
25132531
25142532 uint256 block_hash;
25152533 uint256 tx_hash;
2534+ int num_txs;
25162535 // Get txout proof
25172536 if (Params ().GetConsensus ().ParentChainHasPow ()) {
25182537
@@ -2528,6 +2547,8 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
25282547 if (!CheckPeginTx (stack[4 ], pegtx, prevout, value, claim_script)) {
25292548 return false ;
25302549 }
2550+
2551+ num_txs = merkle_block_pow.txn .GetNumTransactions ();
25312552 } else {
25322553
25332554 CMerkleBlock merkle_block;
@@ -2543,6 +2564,8 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
25432564 if (!CheckPeginTx (stack[4 ], pegtx, prevout, value, claim_script)) {
25442565 return false ;
25452566 }
2567+
2568+ num_txs = merkle_block.txn .GetNumTransactions ();
25462569 }
25472570
25482571 // Check that the merkle proof corresponds to the txid
@@ -2562,7 +2585,9 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
25622585
25632586 // Finally, validate peg-in via rpc call
25642587 if (check_depth && GetBoolArg (" -validatepegin" , DEFAULT_VALIDATE_PEGIN)) {
2565- return IsConfirmedBitcoinBlock (block_hash, Params ().GetConsensus ().pegin_min_depth );
2588+ if (!IsConfirmedBitcoinBlock (block_hash, Params ().GetConsensus ().pegin_min_depth , num_txs)) {
2589+ return false ;
2590+ }
25662591 }
25672592 return true ;
25682593}
0 commit comments