Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lookup_txos error instead of panic #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading