Skip to content

Commit eab63b9

Browse files
committed
Merge #19847: rpc, refactor: Avoid duplicate set lookup in gettxoutproof
52fc399 rpc: Reject empty txids in gettxoutproof (João Barbosa) 73dc19a rpc, refactor: Avoid duplicate set lookup in gettxoutproof (João Barbosa) Pull request description: ACKs for top commit: jonasschnelli: code review ACK 52fc399 Tree-SHA512: 76b18e5235e8b2d394685515a4a60335666eeb0f6b31c1d397f7db2fbe681bc817b8cd3e8f6708b9dacd6113e4e1d94837072cae27834b8a1a22d2717db8191e
2 parents 1a04f45 + 52fc399 commit eab63b9

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,15 @@ static RPCHelpMan gettxoutproof()
244244
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
245245
{
246246
std::set<uint256> setTxids;
247-
uint256 oneTxid;
248247
UniValue txids = request.params[0].get_array();
248+
if (txids.empty()) {
249+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty");
250+
}
249251
for (unsigned int idx = 0; idx < txids.size(); idx++) {
250-
const UniValue& txid = txids[idx];
251-
uint256 hash(ParseHashV(txid, "txid"));
252-
if (setTxids.count(hash)) {
253-
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txid.get_str());
252+
auto ret = setTxids.insert(ParseHashV(txids[idx], "txid"));
253+
if (!ret.second) {
254+
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str());
254255
}
255-
setTxids.insert(hash);
256-
oneTxid = hash;
257256
}
258257

259258
CBlockIndex* pblockindex = nullptr;
@@ -287,7 +286,7 @@ static RPCHelpMan gettxoutproof()
287286
LOCK(cs_main);
288287

289288
if (pblockindex == nullptr) {
290-
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, oneTxid, Params().GetConsensus(), hashBlock);
289+
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, *setTxids.begin(), Params().GetConsensus(), hashBlock);
291290
if (!tx || hashBlock.IsNull()) {
292291
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
293292
}

test/functional/rpc_txoutproof.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def run_test(self):
7878
# We can't get a proof if we specify transactions from different blocks
7979
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
8080
# Test empty list
81-
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [])
81+
assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, [])
8282
# Test duplicate txid
8383
assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1])
8484

0 commit comments

Comments
 (0)