Skip to content

Commit

Permalink
Filter out transactions with missing prevouts from instead of returni…
Browse files Browse the repository at this point in the history
…ng bad data
  • Loading branch information
mononaut committed Aug 4, 2023
1 parent a21a958 commit 173d613
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,15 @@ impl TransactionValue {
blockid: Option<BlockId>,
txos: &HashMap<OutPoint, TxOut>,
config: &Config,
) -> Self {
let prevouts =
extract_tx_prevouts(&tx, txos, true).expect("Cannot Err when allow_missing is true");
allow_missing_prevouts: bool,
) -> Result<TransactionValue, errors::Error> {
let prevouts_result = extract_tx_prevouts(&tx, txos, allow_missing_prevouts);
let prevouts = if allow_missing_prevouts {
prevouts_result.expect("Cannot Err when allow_missing is true")
} else {
prevouts_result?
};

let vins: Vec<TxInValue> = tx
.input
.iter()
Expand All @@ -174,7 +180,7 @@ impl TransactionValue {

let fee = get_tx_fee(&tx, &prevouts, config.network_type);

TransactionValue {
Ok(TransactionValue {
txid: tx.txid(),
#[allow(clippy::unnecessary_cast)]
version: tx.version as u32,
Expand All @@ -185,7 +191,7 @@ impl TransactionValue {
weight: tx.weight() as u32,
fee,
status: Some(TransactionStatus::from(blockid)),
}
})
}
}

Expand Down Expand Up @@ -523,6 +529,7 @@ fn prepare_txs(
txs: Vec<(Transaction, Option<BlockId>)>,
query: &Query,
config: &Config,
filter_missing_prevouts: bool,
) -> Vec<TransactionValue> {
let outpoints = txs
.iter()
Expand All @@ -537,7 +544,9 @@ fn prepare_txs(
let prevouts = query.lookup_txos(&outpoints);

txs.into_iter()
.map(|(tx, blockid)| TransactionValue::new(tx, blockid, &prevouts, config))
.filter_map(|(tx, blockid)| {
TransactionValue::new(tx, blockid, &prevouts, config, !filter_missing_prevouts).ok()
})
.collect()
}

Expand Down Expand Up @@ -729,7 +738,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, false), TTL_SHORT)
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"header"), None, None) => {
let hash = BlockHash::from_hex(hash)?;
Expand Down Expand Up @@ -806,7 +815,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, false), 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 @@ -894,7 +903,7 @@ fn handle_request(
);
}

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

(
Expand Down Expand Up @@ -927,7 +936,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, false), TTL_SHORT)
}
(
&Method::GET,
Expand Down Expand Up @@ -958,7 +967,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, false), TTL_SHORT)
}

(
Expand Down Expand Up @@ -1001,9 +1010,17 @@ 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 mut tx = prepare_txs(vec![(tx, blockid)], query, config);
let mut tx = prepare_txs(vec![(tx, blockid)], query, config, true);

json_response(tx.remove(0), ttl)
if tx.is_empty() {
http_message(
StatusCode::INTERNAL_SERVER_ERROR,
"Transaction missing prevouts",
0,
)
} else {
json_response(tx.remove(0), ttl)
}
}
(&Method::GET, Some(&"tx"), Some(hash), Some(out_type @ &"hex"), None, None)
| (&Method::GET, Some(&"tx"), Some(hash), Some(out_type @ &"raw"), None, None) => {
Expand Down Expand Up @@ -1126,7 +1143,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, true), TTL_SHORT)
}
(&Method::GET, Some(&"mempool"), Some(&"txs"), last_seen_txid, None, None) => {
let last_seen_txid = last_seen_txid.and_then(|txid| Txid::from_hex(txid).ok());
Expand All @@ -1137,7 +1154,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, true), TTL_SHORT)
}
(&Method::GET, Some(&"mempool"), Some(&"recent"), None, None, None) => {
let mempool = query.mempool();
Expand Down Expand Up @@ -1208,7 +1225,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, false), TTL_SHORT)
}

#[cfg(feature = "liquid")]
Expand All @@ -1234,7 +1251,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, false), TTL_SHORT)
}

#[cfg(feature = "liquid")]
Expand All @@ -1248,7 +1265,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, false), TTL_SHORT)
}

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

0 comments on commit 173d613

Please sign in to comment.