|
| 1 | +use alloc::sync::Arc; |
| 2 | + |
| 3 | +use bitcoin::{psbt, OutPoint, Sequence, Transaction, Txid}; |
| 4 | +use miniscript::{bitcoin, plan::Plan}; |
| 5 | + |
| 6 | +use crate::{collections::HashMap, Input, InputStatus}; |
| 7 | + |
| 8 | +/// Tx with confirmation status. |
| 9 | +pub type TxWithStatus<T> = (T, Option<InputStatus>); |
| 10 | + |
| 11 | +/// Our canonical view. |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct CanonicalLeaves { |
| 14 | + txs: HashMap<Txid, Arc<Transaction>>, |
| 15 | + statuses: HashMap<Txid, InputStatus>, |
| 16 | + spends: HashMap<OutPoint, Txid>, |
| 17 | +} |
| 18 | + |
| 19 | +impl CanonicalLeaves { |
| 20 | + /// Construct. |
| 21 | + pub fn new<T>(canonical_txs: impl IntoIterator<Item = TxWithStatus<T>>) -> Self |
| 22 | + where |
| 23 | + T: Into<Arc<Transaction>>, |
| 24 | + { |
| 25 | + let mut txs = HashMap::new(); |
| 26 | + let mut statuses = HashMap::new(); |
| 27 | + let mut spends = HashMap::new(); |
| 28 | + for (tx, status) in canonical_txs { |
| 29 | + let tx: Arc<Transaction> = tx.into(); |
| 30 | + let txid = tx.compute_txid(); |
| 31 | + spends.extend(tx.input.iter().map(|txin| (txin.previous_output, txid))); |
| 32 | + txs.insert(txid, tx); |
| 33 | + if let Some(status) = status { |
| 34 | + statuses.insert(txid, status); |
| 35 | + } |
| 36 | + } |
| 37 | + Self { |
| 38 | + txs, |
| 39 | + statuses, |
| 40 | + spends, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Whether outpoint is a leaf (unspent). |
| 45 | + pub fn is_leaf(&self, outpoint: OutPoint) -> bool { |
| 46 | + if self.spends.contains_key(&outpoint) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + match self.txs.get(&outpoint.txid) { |
| 50 | + Some(tx) => { |
| 51 | + let vout: usize = outpoint.vout.try_into().expect("vout must fit into usize"); |
| 52 | + vout < tx.output.len() |
| 53 | + } |
| 54 | + None => false, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Try get leaf (unspent) of given `outpoint`. |
| 59 | + pub fn try_get_leaf(&self, outpoint: OutPoint, plan: Plan) -> Option<Input> { |
| 60 | + if self.spends.contains_key(&outpoint) { |
| 61 | + return None; |
| 62 | + } |
| 63 | + let prev_tx = Arc::clone(self.txs.get(&outpoint.txid)?); |
| 64 | + Input::from_prev_tx( |
| 65 | + plan, |
| 66 | + prev_tx, |
| 67 | + outpoint.vout.try_into().expect("vout must fit into usize"), |
| 68 | + self.statuses.get(&outpoint.txid).cloned(), |
| 69 | + ) |
| 70 | + .ok() |
| 71 | + } |
| 72 | + |
| 73 | + /// Try get leaves of given `outpoints`. |
| 74 | + pub fn try_get_leaves<'a, O>(&'a self, outpoints: O) -> impl Iterator<Item = Input> + 'a |
| 75 | + where |
| 76 | + O: IntoIterator<Item = (OutPoint, Plan)>, |
| 77 | + O::IntoIter: 'a, |
| 78 | + { |
| 79 | + outpoints |
| 80 | + .into_iter() |
| 81 | + .filter_map(|(op, plan)| self.try_get_leaf(op, plan)) |
| 82 | + } |
| 83 | + |
| 84 | + /// Try get foreign leaf. |
| 85 | + /// TODO: Check psbt_input data with our own prev tx data. |
| 86 | + /// TODO: Create `try_get_foreign_leaves` method. |
| 87 | + pub fn try_get_foreign_leaf( |
| 88 | + &self, |
| 89 | + outpoint: OutPoint, |
| 90 | + sequence: Sequence, |
| 91 | + psbt_input: psbt::Input, |
| 92 | + satisfaction_weight: usize, |
| 93 | + ) -> Option<Input> { |
| 94 | + if self.spends.contains_key(&outpoint) { |
| 95 | + return None; |
| 96 | + } |
| 97 | + let prev_tx = Arc::clone(self.txs.get(&outpoint.txid)?); |
| 98 | + let output_index: usize = outpoint.vout.try_into().expect("vout must fit into usize"); |
| 99 | + let _txout = prev_tx.output.get(output_index)?; |
| 100 | + let status = self.statuses.get(&outpoint.txid).cloned(); |
| 101 | + Input::from_psbt_input(outpoint, sequence, psbt_input, satisfaction_weight, status) |
| 102 | + } |
| 103 | +} |
0 commit comments