use core::convert::Infallible;
-use alloc::collections::BTreeSet;
use bitcoin::{OutPoint, Transaction, TxOut};
use crate::{
pub struct IndexedAdditions<A, D> {
pub graph_additions: Additions<A>,
pub index_delta: D,
+ pub last_height: Option<u32>,
}
impl<A, D: Default> Default for IndexedAdditions<A, D> {
Self {
graph_additions: Default::default(),
index_delta: Default::default(),
+ last_height: None,
}
}
}
let Self {
graph_additions,
index_delta,
+ last_height,
} = other;
self.graph_additions.append(graph_additions);
self.index_delta.append_additions(index_delta);
+ if self.last_height < last_height {
+ let last_height =
+ last_height.expect("must exist as it is larger than self.last_height");
+ self.last_height.replace(last_height);
+ }
}
}
pub struct IndexedTxGraph<A, I> {
graph: TxGraph<A>,
index: I,
+ last_height: u32,
}
impl<A, I: Default> Default for IndexedTxGraph<A, I> {
Self {
graph: Default::default(),
index: Default::default(),
+ last_height: u32::MIN,
}
}
}
let IndexedAdditions {
graph_additions,
index_delta,
+ last_height,
} = additions;
self.index.apply_additions(index_delta);
}
self.graph.apply_additions(graph_additions);
+
+ if let Some(height) = last_height {
+ self.last_height = height;
+ }
+ }
+
+ /// Insert a block height that the chain source has scanned up to.
+ pub fn insert_height(&mut self, tip: u32) -> IndexedAdditions<A, I::Additions> {
+ if self.last_height < tip {
+ self.last_height = tip;
+ IndexedAdditions {
+ last_height: Some(tip),
+ ..Default::default()
+ }
+ } else {
+ IndexedAdditions::default()
+ }
}
/// Insert a `txout` that exists in `outpoint` with the given `observation`.
txout: &TxOut,
observation: ObservedIn<A>,
) -> IndexedAdditions<A, I::Additions> {
- IndexedAdditions {
+ let mut additions = match &observation {
+ ObservedIn::Block(anchor) => self.insert_height(anchor.anchor_block().height),
+ ObservedIn::Mempool(_) => IndexedAdditions::default(),
+ };
+
+ additions.append_additions(IndexedAdditions {
graph_additions: {
let mut graph_additions = self.graph.insert_txout(outpoint, txout.clone());
graph_additions.append(match observation {
graph_additions
},
index_delta: <I as TxIndex>::index_txout(&mut self.index, outpoint, txout),
- }
+ last_height: None,
+ });
+
+ additions
}
pub fn insert_tx(
observation: ObservedIn<A>,
) -> IndexedAdditions<A, I::Additions> {
let txid = tx.txid();
- IndexedAdditions {
+
+ let mut additions = match &observation {
+ ObservedIn::Block(anchor) => self.insert_height(anchor.anchor_block().height),
+ ObservedIn::Mempool(_) => IndexedAdditions::default(),
+ };
+
+ additions.append_additions(IndexedAdditions {
graph_additions: {
let mut graph_additions = self.graph.insert_tx(tx.clone());
graph_additions.append(match observation {
graph_additions
},
index_delta: <I as TxIndex>::index_tx(&mut self.index, tx),
- }
+ last_height: None,
+ });
+
+ additions
}
pub fn filter_and_insert_txs<'t, T>(
})
}
- pub fn relevant_heights(&self) -> BTreeSet<u32> {
- self.graph.relevant_heights()
+ /// Get the last block height that we are synced up to.
+ pub fn last_height(&self) -> u32 {
+ self.last_height
}
pub fn try_list_chain_txs<'a, C>(