From 7534bff5445ee3cfa676c268dcae5028e3f4f28c Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Sat, 27 Oct 2018 23:55:15 +0300 Subject: [PATCH] Don't load spending txs of unspendable outputs --- src/query.rs | 12 ++++++++++++ src/rest.rs | 13 +++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/query.rs b/src/query.rs index 0644aed..0c211a1 100644 --- a/src/query.rs +++ b/src/query.rs @@ -387,6 +387,18 @@ impl Query { }) } + pub fn find_spending_for_funding_tx(&self, tx: Transaction) -> Result>> { + 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 { self.app.daemon().gettransaction(tx_hash) diff --git a/src/rest.rs b/src/rest.rs index d4cab9a..3125a61 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -456,14 +456,11 @@ fn handle_request(req: Request, query: &Arc, 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 = 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) }, _ => {