From fb7e9c576c5b7e8f464df90160d6b38d8f1392fb Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 15 Sep 2021 11:29:03 +0300 Subject: [PATCH] Cache amounts in status::TxEntry --- src/status.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/status.rs b/src/status.rs index 9540e8b..1d48482 100644 --- a/src/status.rs +++ b/src/status.rs @@ -22,8 +22,13 @@ use crate::{ /// Given a scripthash, store relevant inputs and outputs of a specific transaction struct TxEntry { txid: Txid, - outputs: Vec, // relevant funded output indices - spent: Vec, // relevant spent outpoints + outputs: Vec, // relevant funded outputs and their amounts + spent: Vec, // relevant spent outpoints +} + +struct TxOutput { + index: u32, + value: Amount, } impl TxEntry { @@ -426,16 +431,24 @@ impl ScriptHashStatus { } } -fn make_outpoints<'a>(txid: &'a Txid, outputs: &'a [u32]) -> impl Iterator + 'a { - outputs.iter().map(move |vout| OutPoint::new(*txid, *vout)) +fn make_outpoints<'a>( + txid: &'a Txid, + outputs: &'a [TxOutput], +) -> impl Iterator + 'a { + outputs + .iter() + .map(move |out| OutPoint::new(*txid, out.index)) } -fn filter_outputs(tx: &Transaction, scripthash: &ScriptHash) -> Vec { +fn filter_outputs(tx: &Transaction, scripthash: &ScriptHash) -> Vec { let outputs = tx.output.iter().zip(0u32..); outputs .filter_map(move |(txo, vout)| { if ScriptHash::new(&txo.script_pubkey) == *scripthash { - Some(vout) + Some(TxOutput { + index: vout, + value: Amount::from_sat(txo.value), + }) } else { None }