1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

Cache amounts in status::TxEntry

This commit is contained in:
Roman Zeyde 2021-09-15 11:29:03 +03:00 committed by Roman Zeyde
parent 66b7b8bad8
commit fb7e9c576c

View File

@ -22,8 +22,13 @@ use crate::{
/// Given a scripthash, store relevant inputs and outputs of a specific transaction
struct TxEntry {
txid: Txid,
outputs: Vec<u32>, // relevant funded output indices
spent: Vec<OutPoint>, // relevant spent outpoints
outputs: Vec<TxOutput>, // relevant funded outputs and their amounts
spent: Vec<OutPoint>, // 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<Item = OutPoint> + 'a {
outputs.iter().map(move |vout| OutPoint::new(*txid, *vout))
fn make_outpoints<'a>(
txid: &'a Txid,
outputs: &'a [TxOutput],
) -> impl Iterator<Item = OutPoint> + 'a {
outputs
.iter()
.map(move |out| OutPoint::new(*txid, out.index))
}
fn filter_outputs(tx: &Transaction, scripthash: &ScriptHash) -> Vec<u32> {
fn filter_outputs(tx: &Transaction, scripthash: &ScriptHash) -> Vec<TxOutput> {
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
}