Skip to content

Commit

Permalink
make lookup_txos error instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Sep 25, 2024
1 parent 6d182d8 commit 1e772a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/new_index/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ impl Query {
.or_else(|| self.mempool().lookup_raw_txn(txid))
}

pub fn lookup_txos(&self, outpoints: BTreeSet<OutPoint>) -> HashMap<OutPoint, TxOut> {
pub fn lookup_txos(&self, outpoints: BTreeSet<OutPoint>) -> Result<HashMap<OutPoint, TxOut>> {
// the mempool lookup_txos() internally looks up confirmed txos as well
self.mempool()
.lookup_txos(outpoints)
.expect("failed loading txos")
self.mempool().lookup_txos(outpoints)
}

pub fn lookup_spend(&self, outpoint: &OutPoint) -> Option<SpendingInput> {
Expand Down
25 changes: 13 additions & 12 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ fn prepare_txs(
txs: Vec<(Transaction, Option<BlockId>)>,
query: &Query,
config: &Config,
) -> Vec<TransactionValue> {
) -> Result<Vec<TransactionValue>, HttpError> {
let outpoints = txs
.iter()
.flat_map(|(tx, _)| {
Expand All @@ -468,11 +468,12 @@ fn prepare_txs(
})
.collect();

let prevouts = query.lookup_txos(outpoints);
let prevouts = query.lookup_txos(outpoints)?;

txs.into_iter()
Ok(txs
.into_iter()
.map(|(tx, blockid)| TransactionValue::new(tx, blockid, &prevouts, config))
.collect()
.collect())
}

#[tokio::main]
Expand Down Expand Up @@ -726,7 +727,7 @@ fn handle_request(
// XXX orphraned blocks alway get TTL_SHORT
let ttl = ttl_by_depth(confirmed_blockid.map(|b| b.height), query);

json_response(prepare_txs(txs, query, config), ttl)
json_response(prepare_txs(txs, query, config)?, ttl)
}
(&Method::GET, Some(script_type @ &"address"), Some(script_str), None, None, None)
| (&Method::GET, Some(script_type @ &"scripthash"), Some(script_str), None, None, None) => {
Expand Down Expand Up @@ -777,7 +778,7 @@ fn handle_request(
.map(|(tx, blockid)| (tx, Some(blockid))),
);

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}

(
Expand Down Expand Up @@ -810,7 +811,7 @@ fn handle_request(
.map(|(tx, blockid)| (tx, Some(blockid)))
.collect();

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}
(
&Method::GET,
Expand All @@ -837,7 +838,7 @@ fn handle_request(
.map(|tx| (tx, None))
.collect();

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}

(
Expand Down Expand Up @@ -880,7 +881,7 @@ fn handle_request(
let blockid = query.chain().tx_confirming_block(&hash);
let ttl = ttl_by_depth(blockid.as_ref().map(|b| b.height), query);

let tx = prepare_txs(vec![(tx, blockid)], query, config).remove(0);
let tx = prepare_txs(vec![(tx, blockid)], query, config)?.remove(0);

json_response(tx, ttl)
}
Expand Down Expand Up @@ -1067,7 +1068,7 @@ fn handle_request(
.map(|(tx, blockid)| (tx, Some(blockid))),
);

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}

#[cfg(feature = "liquid")]
Expand All @@ -1089,7 +1090,7 @@ fn handle_request(
.map(|(tx, blockid)| (tx, Some(blockid)))
.collect();

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}

#[cfg(feature = "liquid")]
Expand All @@ -1103,7 +1104,7 @@ fn handle_request(
.map(|tx| (tx, None))
.collect();

json_response(prepare_txs(txs, query, config), TTL_SHORT)
json_response(prepare_txs(txs, query, config)?, TTL_SHORT)
}

#[cfg(feature = "liquid")]
Expand Down

0 comments on commit 1e772a7

Please sign in to comment.