diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs
index 4245e57d..08926f56 100644
--- a/crates/chain/src/indexed_tx_graph.rs
+++ b/crates/chain/src/indexed_tx_graph.rs
@@ -1,6 +1,5 @@
use core::convert::Infallible;
-use alloc::collections::BTreeSet;
use bitcoin::{OutPoint, Transaction, TxOut};
use crate::{
@@ -26,6 +25,7 @@ pub struct TxOutInChain<'a, I, A> {
pub struct IndexedAdditions {
pub graph_additions: Additions,
pub index_delta: D,
+ pub last_height: Option,
}
impl Default for IndexedAdditions {
@@ -33,6 +33,7 @@ impl Default for IndexedAdditions {
Self {
graph_additions: Default::default(),
index_delta: Default::default(),
+ last_height: None,
}
}
}
@@ -42,15 +43,22 @@ impl TxIndexAdditions for IndexedAdditions<
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 {
graph: TxGraph,
index: I,
+ last_height: u32,
}
impl Default for IndexedTxGraph {
@@ -58,6 +66,7 @@ impl Default for IndexedTxGraph {
Self {
graph: Default::default(),
index: Default::default(),
+ last_height: u32::MIN,
}
}
}
@@ -83,6 +92,7 @@ impl IndexedTxGraph {
let IndexedAdditions {
graph_additions,
index_delta,
+ last_height,
} = additions;
self.index.apply_additions(index_delta);
@@ -95,6 +105,23 @@ impl IndexedTxGraph {
}
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 {
+ 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`.
@@ -104,7 +131,12 @@ impl IndexedTxGraph {
txout: &TxOut,
observation: ObservedIn,
) -> IndexedAdditions {
- 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 {
@@ -116,7 +148,10 @@ impl IndexedTxGraph {
graph_additions
},
index_delta: ::index_txout(&mut self.index, outpoint, txout),
- }
+ last_height: None,
+ });
+
+ additions
}
pub fn insert_tx(
@@ -125,7 +160,13 @@ impl IndexedTxGraph {
observation: ObservedIn,
) -> IndexedAdditions {
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 {
@@ -135,7 +176,10 @@ impl IndexedTxGraph {
graph_additions
},
index_delta: ::index_tx(&mut self.index, tx),
- }
+ last_height: None,
+ });
+
+ additions
}
pub fn filter_and_insert_txs<'t, T>(
@@ -159,8 +203,9 @@ impl IndexedTxGraph {
})
}
- pub fn relevant_heights(&self) -> BTreeSet {
- 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>(