Skip to content

Commit ed82f17

Browse files
committed
have verifytxoutproof check the number of txns in proof structure
1 parent a607d23 commit ed82f17

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/merkleblock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ class CPartialMerkleTree
115115
* returns the merkle root, or 0 in case of failure
116116
*/
117117
uint256 ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);
118+
119+
/** Get number of transactions the merkle proof is indicating for cross-reference with
120+
* local blockchain knowledge.
121+
*/
122+
unsigned int GetNumTransactions() const { return nTransactions; };
123+
118124
};
119125

120126

src/rpc/rawtransaction.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static UniValue verifytxoutproof(const JSONRPCRequest& request)
307307
"\nArguments:\n"
308308
"1. \"proof\" (string, required) The hex-encoded proof generated by gettxoutproof\n"
309309
"\nResult:\n"
310-
"[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
310+
"[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof can not be validated.\n"
311311
);
312312

313313
CDataStream ssMB(ParseHexV(request.params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
@@ -324,12 +324,17 @@ static UniValue verifytxoutproof(const JSONRPCRequest& request)
324324
LOCK(cs_main);
325325

326326
const CBlockIndex* pindex = LookupBlockIndex(merkleBlock.header.GetHash());
327-
if (!pindex || !chainActive.Contains(pindex)) {
327+
if (!pindex || !chainActive.Contains(pindex) || pindex->nTx == 0) {
328328
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
329329
}
330330

331-
for (const uint256& hash : vMatch)
332-
res.push_back(hash.GetHex());
331+
// Check if proof is valid, only add results if so
332+
if (pindex->nTx == merkleBlock.txn.GetNumTransactions()) {
333+
for (const uint256& hash : vMatch) {
334+
res.push_back(hash.GetHex());
335+
}
336+
}
337+
333338
return res;
334339
}
335340

0 commit comments

Comments
 (0)