Skip to content

Commit 5154fa0

Browse files
jonasschnelliPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent ea82816 commit 5154fa0

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
@@ -591,16 +591,15 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
591591
}.Check(request);
592592

593593
std::set<uint256> setTxids;
594-
uint256 oneTxid;
595594
UniValue txids = request.params[0].get_array();
595+
if (txids.empty()) {
596+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Parameter 'txids' cannot be empty");
597+
}
596598
for (unsigned int idx = 0; idx < txids.size(); idx++) {
597-
const UniValue& txid = txids[idx];
598-
uint256 hash(ParseHashV(txid, "txid"));
599-
if (setTxids.count(hash)) {
600-
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ")+txid.get_str());
599+
auto ret = setTxids.insert(ParseHashV(txids[idx], "txid"));
600+
if (!ret.second) {
601+
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ") + txids[idx].get_str());
601602
}
602-
setTxids.insert(hash);
603-
oneTxid = hash;
604603
}
605604

606605
CBlockIndex* pblockindex = nullptr;
@@ -636,7 +635,7 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
636635
LOCK(cs_main);
637636

638637
if (pblockindex == nullptr) {
639-
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, oneTxid, Params().GetConsensus(), hashBlock);
638+
const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, /* mempool */ nullptr, *setTxids.begin(), Params().GetConsensus(), hashBlock);
640639
if (!tx || hashBlock.IsNull()) {
641640
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
642641
}

test/functional/rpc_txoutproof.py

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

0 commit comments

Comments
 (0)