Skip to content

Commit

Permalink
have verifytxoutproof check the number of txns in proof structure
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#13452
Rebased-From: ed82f17
  • Loading branch information
instagibbs authored and MarcoFalke committed Jul 13, 2018
1 parent ed82e71 commit 6b9dc8c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/merkleblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class CPartialMerkleTree
* returns the merkle root, or 0 in case of failure
*/
uint256 ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex);

/** Get number of transactions the merkle proof is indicating for cross-reference with
* local blockchain knowledge.
*/
unsigned int GetNumTransactions() const { return nTransactions; };

};


Expand Down
13 changes: 9 additions & 4 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ UniValue verifytxoutproof(const JSONRPCRequest& request)
"\nArguments:\n"
"1. \"proof\" (string, required) The hex-encoded proof generated by gettxoutproof\n"
"\nResult:\n"
"[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
"[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof can not be validated.\n"
);

CDataStream ssMB(ParseHexV(request.params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
Expand All @@ -306,11 +306,16 @@ UniValue verifytxoutproof(const JSONRPCRequest& request)

LOCK(cs_main);

if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()]))
if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()]) || mapBlockIndex[merkleBlock.header.GetHash()]->nTx == 0)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");

for (const uint256& hash : vMatch)
res.push_back(hash.GetHex());
// Check if proof is valid, only add results if so
if (mapBlockIndex[merkleBlock.header.GetHash()]->nTx == merkleBlock.txn.GetNumTransactions()) {
for (const uint256& hash : vMatch) {
res.push_back(hash.GetHex());
}
}

return res;
}

Expand Down

0 comments on commit 6b9dc8c

Please sign in to comment.