rpc: honour importprivkey's height with a secret_key, and lock in three block RPCs

From the general audit, all still on dev:

- importprivkey used its height argument only when exactly 4 arguments were given, so supplying the
  optional 5th (secret_key) silently rescanned from genesis. Regtest under gdb: with
  `true 120 <prefix>` the old binary started ScanForWalletTransactions at height 0, this one at 120.
- getblockhashes: `if (fActiveOnly) LOCK(cs_main);` was unbraced, so the lock was released on the
  same line and, with noOrphans, GetTimestampIndex checked rows against chainActive unlocked. The
  lock now spans that call when noOrphans is set; without it the call only walks the LevelDB index
  and takes no lock, as before.
- getblockdeltas and getblockmerkletree read mapBlockIndex, chainActive and (getblockmerkletree)
  pcoinsTip's anchor cache, which inserts on a miss, with no lock; every sibling RPC locks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-20 20:10:01 -06:00
co-authored by Claude Opus 5.5
parent dbe154c2dc
commit 179429ef7e
2 changed files with 23 additions and 6 deletions
+20 -5
View File
@@ -534,6 +534,9 @@ UniValue getblockdeltas(const UniValue& params, bool fHelp, const CPubKey& mypk)
if (fHelp || params.size() != 1)
throw runtime_error("");
// mapBlockIndex and chainActive (read here and in blockToDeltasJSON) are guarded by cs_main.
LOCK(cs_main);
std::string strHash = params[0].get_str();
uint256 hash(uint256S(strHash));
@@ -602,11 +605,20 @@ UniValue getblockhashes(const UniValue& params, bool fHelp, const CPubKey& mypk)
std::vector<std::pair<uint256, unsigned int> > blockHashes;
if (fActiveOnly)
LOCK(cs_main);
if (!GetTimestampIndex(high, low, fActiveOnly, blockHashes)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for block hashes");
{
// With noOrphans, GetTimestampIndex checks every row against mapBlockIndex/chainActive, so
// cs_main must span the call (the old unbraced "if (fActiveOnly) LOCK(cs_main);" released it
// on the same line). Without noOrphans it only walks the LevelDB index, so no lock is taken:
// holding cs_main over a wide range would stall block validation for the whole scan.
bool fFound;
if (fActiveOnly) {
LOCK(cs_main);
fFound = GetTimestampIndex(high, low, fActiveOnly, blockHashes);
} else {
fFound = GetTimestampIndex(high, low, fActiveOnly, blockHashes);
}
if (!fFound)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for block hashes");
}
UniValue result(UniValue::VARR);
@@ -877,6 +889,9 @@ UniValue getblockmerkletree(const UniValue& params, bool fHelp, const CPubKey& m
+ HelpExampleRpc("getblockmerkletree", "290000")
);
// chainActive and pcoinsTip (whose anchor cache inserts on a miss) are guarded by cs_main.
LOCK(cs_main);
CBlockIndex* phushblockindex;
uint256 blockRoot;
SaplingMerkleTree tree;
+3 -1
View File
@@ -299,7 +299,9 @@ UniValue importprivkey(const UniValue& params, bool fHelp, const CPubKey& mypk)
bool fRescan = true;
if (params.size() > 2)
fRescan = params[2].get_bool();
if ( fRescan && params.size() == 4 )
// '> 3', not '== 4': with the optional 5th (secret_key) argument the height was silently
// dropped and the rescan started from genesis.
if ( fRescan && params.size() > 3 )
height = params[3].get_int();