Browse Source

Don't load spending txs of unspendable outputs

liquid_e
Nadav Ivgi 6 years ago
parent
commit
7534bff544
  1. 12
      src/query.rs
  2. 13
      src/rest.rs

12
src/query.rs

@ -387,6 +387,18 @@ impl Query {
})
}
pub fn find_spending_for_funding_tx(&self, tx: Transaction) -> Result<Vec<Option<SpendingInput>>> {
let txid = tx.txid();
let mut spends = vec![];
for (output_index, output) in tx.output.iter().enumerate() {
let spend = if !output.is_fee() && !output.script_pubkey.is_provably_unspendable() {
self.find_spending_by_outpoint((txid, output_index))?
} else { None };
spends.push(spend)
}
Ok(spends)
}
// Internal API for transaction retrieval (uses bitcoind)
fn _load_txn(&self, tx_hash: &Sha256dHash) -> Result<Transaction> {
self.app.daemon().gettransaction(tx_hash)

13
src/rest.rs

@ -456,14 +456,11 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, network: &Network) ->
},
(&Method::GET, Some(&"tx"), Some(hash), Some(&"outspends"), None) => {
let hash = Sha256dHash::from_hex(hash)?;
// optimize: avoid fetching the entire tx just for the vout count
let out_count = query.tx_get(&hash).ok_or(StringError("cannot find tx".to_string()))?.output.len();
let mut spends = vec![];
for output_index in 0..out_count {
let spend = query.find_spending_by_outpoint((hash, output_index))?
.map_or_else(|| SpendingValue::default(), |spend| SpendingValue::from(spend));
spends.push(spend)
}
let tx = query.tx_get(&hash).ok_or(StringError("cannot find tx".to_string()))?;
let spends: Vec<SpendingValue> = query.find_spending_for_funding_tx(tx)?
.into_iter()
.map(|spend| spend.map_or_else(|| SpendingValue::default(), |spend| SpendingValue::from(spend)))
.collect();
json_response(spends)
},
_ => {

Loading…
Cancel
Save