Skip to content

Commit

Permalink
[bdk_chain_redesign] IndexedTxGraph keeps track of the last synced …
Browse files Browse the repository at this point in the history
…height

This is important as a `ChainOracle` implementation is updated
separately to an `IndexedTxGraph`.
  • Loading branch information
evanlinjin committed Mar 27, 2023
1 parent e902c10 commit 236c50f
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::convert::Infallible;

use alloc::collections::BTreeSet;
use bitcoin::{OutPoint, Transaction, TxOut};

use crate::{
Expand All @@ -26,13 +25,15 @@ pub struct TxOutInChain<'a, I, A> {
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> {
fn default() -> Self {
Self {
graph_additions: Default::default(),
index_delta: Default::default(),
last_height: None,
}
}
}
Expand All @@ -42,22 +43,30 @@ impl<A: BlockAnchor, D: TxIndexAdditions> 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<A, I> {
graph: TxGraph<A>,
index: I,
last_height: u32,
}

impl<A, I: Default> Default for IndexedTxGraph<A, I> {
fn default() -> Self {
Self {
graph: Default::default(),
index: Default::default(),
last_height: u32::MIN,
}
}
}
Expand All @@ -83,6 +92,7 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
let IndexedAdditions {
graph_additions,
index_delta,
last_height,
} = additions;

self.index.apply_additions(index_delta);
Expand All @@ -95,6 +105,23 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
}

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`.
Expand All @@ -104,7 +131,12 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
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 {
Expand All @@ -116,7 +148,10 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
graph_additions
},
index_delta: <I as TxIndex>::index_txout(&mut self.index, outpoint, txout),
}
last_height: None,
});

additions
}

pub fn insert_tx(
Expand All @@ -125,7 +160,13 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
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 {
Expand All @@ -135,7 +176,10 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
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>(
Expand All @@ -159,8 +203,9 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
})
}

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>(
Expand Down

0 comments on commit 236c50f

Please sign in to comment.