Merge #19847: rpc, refactor: Avoid duplicate set lookup in gettxoutproof

52fc39917fc52c2ff279fe434431e18900b347bd rpc: Reject empty txids in gettxoutproof (João Barbosa)
73dc19a330f8cb063af46e6c4246f2e64a04bdc1 rpc, refactor: Avoid duplicate set lookup in gettxoutproof (João Barbosa)

Pull request description:

ACKs for top commit:
  jonasschnelli:
    code review ACK 52fc39917fc52c2ff279fe434431e18900b347bd

Tree-SHA512: 76b18e5235e8b2d394685515a4a60335666eeb0f6b31c1d397f7db2fbe681bc817b8cd3e8f6708b9dacd6113e4e1d94837072cae27834b8a1a22d2717db8191e
This commit is contained in:
Jonas Schnelli 2020-12-07 09:17:02 +01:00 committed by pasta
parent ea828164e6
commit 5154fa0ebf
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
2 changed files with 8 additions and 9 deletions

View File

@ -591,16 +591,15 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
}.Check(request); }.Check(request);
std::set<uint256> setTxids; std::set<uint256> setTxids;
uint256 oneTxid;
UniValue txids = request.params[0].get_array(); UniValue txids = request.params[0].get_array();
if (txids.empty()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty");
}
for (unsigned int idx = 0; idx < txids.size(); idx++) { for (unsigned int idx = 0; idx < txids.size(); idx++) {
const UniValue& txid = txids[idx]; auto ret = setTxids.insert(ParseHashV(txids[idx], "txid"));
uint256 hash(ParseHashV(txid, "txid")); if (!ret.second) {
if (setTxids.count(hash)) { throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str());
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ")+txid.get_str());
} }
setTxids.insert(hash);
oneTxid = hash;
} }
CBlockIndex* pblockindex = nullptr; CBlockIndex* pblockindex = nullptr;
@ -636,7 +635,7 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
LOCK(cs_main); LOCK(cs_main);
if (pblockindex == nullptr) { if (pblockindex == nullptr) {
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, oneTxid, Params().GetConsensus(), hashBlock); const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, *setTxids.begin(), Params().GetConsensus(), hashBlock);
if (!tx || hashBlock.IsNull()) { if (!tx || hashBlock.IsNull()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
} }

View File

@ -82,7 +82,7 @@ class MerkleBlockTest(BitcoinTestFramework):
# We can't get a proof if we specify transactions from different blocks # We can't get a proof if we specify transactions from different blocks
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3]) assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
# Test empty list # Test empty list
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, []) assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, [])
# Test duplicate txid # Test duplicate txid
assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1]) assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1])