use crate::collections::{BTreeMap, BTreeSet, HashSet};
use alloc::{sync::Arc, vec::Vec};
use bitcoin::{OutPoint, Transaction, TxOut, Txid};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TxUpdate<A = ()> {
pub txs: Vec<Arc<Transaction>>,
pub txouts: BTreeMap<OutPoint, TxOut>,
pub anchors: BTreeSet<(A, Txid)>,
pub seen_ats: HashSet<(Txid, u64)>,
pub evicted_ats: HashSet<(Txid, u64)>,
}
impl<A> Default for TxUpdate<A> {
fn default() -> Self {
Self {
txs: Default::default(),
txouts: Default::default(),
anchors: Default::default(),
seen_ats: Default::default(),
evicted_ats: Default::default(),
}
}
}
impl<A> TxUpdate<A> {
pub fn is_empty(&self) -> bool {
self.txs.is_empty()
&& self.txouts.is_empty()
&& self.anchors.is_empty()
&& self.seen_ats.is_empty()
&& self.evicted_ats.is_empty()
}
}
impl<A: Ord> TxUpdate<A> {
pub fn map_anchors<A2: Ord, F: FnMut(A) -> A2>(self, mut map: F) -> TxUpdate<A2> {
TxUpdate {
txs: self.txs,
txouts: self.txouts,
anchors: self
.anchors
.into_iter()
.map(|(a, txid)| (map(a), txid))
.collect(),
seen_ats: self.seen_ats,
evicted_ats: self.evicted_ats,
}
}
pub fn extend(&mut self, other: TxUpdate<A>) {
self.txs.extend(other.txs);
self.txouts.extend(other.txouts);
self.anchors.extend(other.anchors);
self.seen_ats.extend(other.seen_ats);
self.evicted_ats.extend(other.evicted_ats);
}
}