Skip to content

Commit e8df529

Browse files
feat(chain): Add initial_changeset to graphs
Add `initial_changeset` to TxGraph and IndexedTxGraph
1 parent c7fff96 commit e8df529

File tree

8 files changed

+50
-10
lines changed

8 files changed

+50
-10
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {
6161

6262
self.graph.apply_changeset(graph);
6363
}
64+
65+
/// Determines the changeset between self and the empty [`IndexedTxGraph`].
66+
pub fn initial_changeset(&self) -> ChangeSet<A, I::ChangeSet> {
67+
let graph = self.graph.initial_changeset();
68+
let indexer = self.index.initial_changeset();
69+
ChangeSet { graph, indexer }
70+
}
6471
}
6572

6673
impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
@@ -234,6 +241,9 @@ pub trait Indexer {
234241
/// Apply changeset to itself.
235242
fn apply_changeset(&mut self, changeset: Self::ChangeSet);
236243

244+
/// Determines the changeset between self and the empty [`Indexer`].
245+
fn initial_changeset(&self) -> Self::ChangeSet;
246+
237247
/// Determines whether the transaction should be included in the index.
238248
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
239249
}

crates/chain/src/keychain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod txout_index;
2020
pub use txout_index::*;
2121

2222
/// Represents updates to the derivation index of a [`KeychainTxOutIndex`].
23+
/// It maps each keychain `K` to its last revealed index.
2324
///
2425
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet] are
2526
/// monotone in that they will never decrease the revealed derivation index.

crates/chain/src/keychain/txout_index.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
9898
self.scan(tx)
9999
}
100100

101+
fn initial_changeset(&self) -> Self::ChangeSet {
102+
super::ChangeSet(self.last_revealed.clone())
103+
}
104+
101105
fn apply_changeset(&mut self, changeset: Self::ChangeSet) {
102106
self.apply_changeset(changeset)
103107
}

crates/chain/src/spk_txout_index.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ impl<I: Clone + Ord> Indexer for SpkTxOutIndex<I> {
6666
Default::default()
6767
}
6868

69+
fn initial_changeset(&self) -> Self::ChangeSet {}
70+
6971
fn apply_changeset(&mut self, _changeset: Self::ChangeSet) {
7072
// This applies nothing.
7173
}

crates/chain/src/tx_graph.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ impl<A: Clone + Ord> TxGraph<A> {
438438
changeset
439439
}
440440

441+
/// Determines the changeset between self and the empty [`TxGraph`].
442+
pub fn initial_changeset(&self) -> ChangeSet<A> {
443+
Self::default().apply_update(self.clone())
444+
}
445+
441446
/// Applies [`ChangeSet`] to [`TxGraph`].
442447
pub fn apply_changeset(&mut self, changeset: ChangeSet<A>) {
443448
for tx in changeset.txs {

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,20 @@ fn insert_relevant_txs() {
6565

6666
let txs = [tx_c, tx_b, tx_a];
6767

68+
let changeset = indexed_tx_graph::ChangeSet {
69+
graph: tx_graph::ChangeSet {
70+
txs: txs.clone().into(),
71+
..Default::default()
72+
},
73+
indexer: keychain::ChangeSet([((), 9_u32)].into()),
74+
};
75+
6876
assert_eq!(
6977
graph.insert_relevant_txs(txs.iter().map(|tx| (tx, None)), None),
70-
indexed_tx_graph::ChangeSet {
71-
graph: tx_graph::ChangeSet {
72-
txs: txs.into(),
73-
..Default::default()
74-
},
75-
indexer: keychain::ChangeSet([((), 9_u32)].into()),
76-
}
77-
)
78+
changeset,
79+
);
80+
81+
assert_eq!(graph.initial_changeset(), changeset,);
7882
}
7983

8084
#[test]

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> Scr
4343

4444
#[test]
4545
fn test_set_all_derivation_indices() {
46+
use bdk_chain::indexed_tx_graph::Indexer;
47+
4648
let (mut txout_index, _, _) = init_txout_index();
4749
let derive_to: BTreeMap<_, _> =
4850
[(TestKeychain::External, 12), (TestKeychain::Internal, 24)].into();
@@ -56,6 +58,7 @@ fn test_set_all_derivation_indices() {
5658
keychain::ChangeSet::default(),
5759
"no changes if we set to the same thing"
5860
);
61+
assert_eq!(txout_index.initial_changeset().as_inner(), &derive_to);
5962
}
6063

6164
#[test]

crates/chain/tests/test_tx_graph.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ fn insert_txouts() {
141141
changeset,
142142
ChangeSet {
143143
txs: [update_txs.clone()].into(),
144-
txouts: update_ops.into(),
144+
txouts: update_ops.clone().into(),
145145
anchors: [(conf_anchor, update_txs.txid()), (unconf_anchor, h!("tx2"))].into(),
146146
last_seen: [(h!("tx2"), 1000000)].into()
147147
}
148148
);
149149

150150
// Apply changeset and check the new graph counts.
151-
graph.apply_changeset(changeset);
151+
graph.apply_changeset(changeset.clone());
152152
assert_eq!(graph.all_txouts().count(), 4);
153153
assert_eq!(graph.full_txs().count(), 1);
154154
assert_eq!(graph.floating_txouts().count(), 3);
@@ -186,6 +186,17 @@ fn insert_txouts() {
186186
)]
187187
.into()
188188
);
189+
190+
// Check that the initial_changeset is correct
191+
assert_eq!(
192+
graph.initial_changeset(),
193+
ChangeSet {
194+
txs: [update_txs.clone()].into(),
195+
txouts: update_ops.into_iter().chain(original_ops).collect(),
196+
anchors: [(conf_anchor, update_txs.txid()), (unconf_anchor, h!("tx2"))].into(),
197+
last_seen: [(h!("tx2"), 1000000)].into()
198+
}
199+
);
189200
}
190201

191202
#[test]

0 commit comments

Comments
 (0)