Skip to content

Commit

Permalink
feat(chain): Add initial_changeset to graphs
Browse files Browse the repository at this point in the history
Add `initial_changeset` to TxGraph and IndexedTxGraph
  • Loading branch information
danielabrozzoni committed Aug 18, 2023
1 parent ea50b6a commit 80f5ecf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
10 changes: 10 additions & 0 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {

self.graph.apply_changeset(changeset.graph);
}

/// Determines the [`ChangeSet`] between `self` and an empty [`IndexedTxGraph`].
pub fn initial_changeset(&self) -> ChangeSet<A, I::ChangeSet> {
let graph = self.graph.initial_changeset();
let indexer = self.index.initial_changeset();
ChangeSet { graph, indexer }
}
}

impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
Expand Down Expand Up @@ -232,6 +239,9 @@ pub trait Indexer {
/// Apply changeset to itself.
fn apply_changeset(&mut self, changeset: Self::ChangeSet);

/// Determines the [`ChangeSet`] between `self` and an empty [`Indexer`].
fn initial_changeset(&self) -> Self::ChangeSet;

/// Determines whether the transaction should be included in the index.
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
}
1 change: 1 addition & 0 deletions crates/chain/src/keychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod txout_index;
pub use txout_index::*;

/// Represents updates to the derivation index of a [`KeychainTxOutIndex`].
/// It maps each keychain `K` to its last revealed index.
///
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet] are
/// monotone in that they will never decrease the revealed derivation index.
Expand Down
4 changes: 4 additions & 0 deletions crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
self.scan(tx)
}

fn initial_changeset(&self) -> Self::ChangeSet {
super::ChangeSet(self.last_revealed.clone())
}

fn apply_changeset(&mut self, changeset: Self::ChangeSet) {
self.apply_changeset(changeset)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/chain/src/spk_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl<I: Clone + Ord> Indexer for SpkTxOutIndex<I> {
Default::default()
}

fn initial_changeset(&self) -> Self::ChangeSet {}

fn apply_changeset(&mut self, _changeset: Self::ChangeSet) {
// This applies nothing.
}
Expand Down
5 changes: 5 additions & 0 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ impl<A: Clone + Ord> TxGraph<A> {
changeset
}

/// Determines the [`ChangeSet`] between `self` and an empty [`TxGraph`].
pub fn initial_changeset(&self) -> ChangeSet<A> {
Self::default().determine_changeset(self.clone())
}

/// Applies [`ChangeSet`] to [`TxGraph`].
pub fn apply_changeset(&mut self, changeset: ChangeSet<A>) {
for tx in changeset.txs {
Expand Down
20 changes: 12 additions & 8 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ fn insert_relevant_txs() {

let txs = [tx_c, tx_b, tx_a];

let changeset = indexed_tx_graph::ChangeSet {
graph: tx_graph::ChangeSet {
txs: txs.clone().into(),
..Default::default()
},
indexer: keychain::ChangeSet([((), 9_u32)].into()),
};

assert_eq!(
graph.insert_relevant_txs(txs.iter().map(|tx| (tx, None)), None),
indexed_tx_graph::ChangeSet {
graph: tx_graph::ChangeSet {
txs: txs.into(),
..Default::default()
},
indexer: keychain::ChangeSet([((), 9_u32)].into()),
}
)
changeset,
);

assert_eq!(graph.initial_changeset(), changeset,);
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> Scr

#[test]
fn test_set_all_derivation_indices() {
use bdk_chain::indexed_tx_graph::Indexer;

let (mut txout_index, _, _) = init_txout_index();
let derive_to: BTreeMap<_, _> =
[(TestKeychain::External, 12), (TestKeychain::Internal, 24)].into();
Expand All @@ -56,6 +58,7 @@ fn test_set_all_derivation_indices() {
keychain::ChangeSet::default(),
"no changes if we set to the same thing"
);
assert_eq!(txout_index.initial_changeset().as_inner(), &derive_to);
}

#[test]
Expand Down
13 changes: 12 additions & 1 deletion crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn insert_txouts() {
changeset,
ChangeSet {
txs: [update_txs.clone()].into(),
txouts: update_ops.into(),
txouts: update_ops.clone().into(),
anchors: [(conf_anchor, update_txs.txid()), (unconf_anchor, h!("tx2"))].into(),
last_seen: [(h!("tx2"), 1000000)].into()
}
Expand Down Expand Up @@ -186,6 +186,17 @@ fn insert_txouts() {
)]
.into()
);

// Check that the initial_changeset is correct
assert_eq!(
graph.initial_changeset(),
ChangeSet {
txs: [update_txs.clone()].into(),
txouts: update_ops.into_iter().chain(original_ops).collect(),
anchors: [(conf_anchor, update_txs.txid()), (unconf_anchor, h!("tx2"))].into(),
last_seen: [(h!("tx2"), 1000000)].into()
}
);
}

#[test]
Expand Down

0 comments on commit 80f5ecf

Please sign in to comment.