Skip to content

Commit de25407

Browse files
refactor!(chain): Unify ChangeSet nomenclature
This commit renames: - indexed_tx_graph::IndexedAdditions -> indexed_tx_graph::ChangeSet - tx_graph::Additions -> tx_graph::ChangeSet - keychain::DerivationAdditions -> keychain::ChangeSet - CanonicalTx::node -> CanonicalTx::tx_node - CanonicalTx::observed_as -> CanonicalTx::chain_position This commit also changes the visibility of TxGraph::determine_changeset to be pub(crate) This commit removes: - `TxGraph::insert_txout_preview` - `TxGraph::insert_tx_preview` - `insert_anchor_preview` - `insert_seen_at_preview` Solves #1022
1 parent 1da3b30 commit de25407

File tree

14 files changed

+300
-342
lines changed

14 files changed

+300
-342
lines changed

crates/bdk/src/wallet/export.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ impl FullyNodedExport {
126126
Self::is_compatible_with_core(&descriptor)?;
127127

128128
let blockheight = if include_blockheight {
129-
wallet
130-
.transactions()
131-
.next()
132-
.map_or(0, |canonical_tx| match canonical_tx.observed_as {
129+
wallet.transactions().next().map_or(0, |canonical_tx| {
130+
match canonical_tx.chain_position {
133131
bdk_chain::ChainPosition::Confirmed(a) => a.confirmation_height,
134132
bdk_chain::ChainPosition::Unconfirmed(_) => 0,
135-
})
133+
}
134+
})
136135
} else {
137136
0
138137
};

crates/bdk/src/wallet/mod.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use alloc::{
2121
};
2222
pub use bdk_chain::keychain::Balance;
2323
use bdk_chain::{
24-
indexed_tx_graph::IndexedAdditions,
24+
indexed_tx_graph,
2525
keychain::{KeychainTxOutIndex, LocalChangeSet, LocalUpdate},
2626
local_chain::{self, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
2727
tx_graph::{CanonicalTx, TxGraph},
@@ -247,7 +247,7 @@ impl<D> Wallet<D> {
247247

248248
let changeset = db.load_from_persistence().map_err(NewError::Persist)?;
249249
chain.apply_changeset(&changeset.chain_changeset);
250-
indexed_graph.apply_additions(changeset.indexed_additions);
250+
indexed_graph.apply_changeset(changeset.indexed_changeset);
251251

252252
let persist = Persist::new(db);
253253

@@ -320,14 +320,14 @@ impl<D> Wallet<D> {
320320
{
321321
let keychain = self.map_keychain(keychain);
322322
let txout_index = &mut self.indexed_graph.index;
323-
let (index, spk, additions) = match address_index {
323+
let (index, spk, changeset) = match address_index {
324324
AddressIndex::New => {
325-
let ((index, spk), index_additions) = txout_index.reveal_next_spk(&keychain);
326-
(index, spk.into(), Some(index_additions))
325+
let ((index, spk), index_changeset) = txout_index.reveal_next_spk(&keychain);
326+
(index, spk.into(), Some(index_changeset))
327327
}
328328
AddressIndex::LastUnused => {
329-
let ((index, spk), index_additions) = txout_index.next_unused_spk(&keychain);
330-
(index, spk.into(), Some(index_additions))
329+
let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
330+
(index, spk.into(), Some(index_changeset))
331331
}
332332
AddressIndex::Peek(index) => {
333333
let (index, spk) = txout_index
@@ -339,9 +339,11 @@ impl<D> Wallet<D> {
339339
}
340340
};
341341

342-
if let Some(additions) = additions {
342+
if let Some(changeset) = changeset {
343343
self.persist
344-
.stage(ChangeSet::from(IndexedAdditions::from(additions)));
344+
.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
345+
changeset,
346+
)));
345347
self.persist.commit()?;
346348
}
347349

@@ -436,12 +438,12 @@ impl<D> Wallet<D> {
436438
let graph = self.indexed_graph.graph();
437439

438440
let canonical_tx = CanonicalTx {
439-
observed_as: graph.get_chain_position(
441+
chain_position: graph.get_chain_position(
440442
&self.chain,
441443
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
442444
txid,
443445
)?,
444-
node: graph.get_tx_node(txid)?,
446+
tx_node: graph.get_tx_node(txid)?,
445447
};
446448

447449
Some(new_tx_details(
@@ -889,12 +891,14 @@ impl<D> Wallet<D> {
889891
Some(ref drain_recipient) => drain_recipient.clone(),
890892
None => {
891893
let change_keychain = self.map_keychain(KeychainKind::Internal);
892-
let ((index, spk), index_additions) =
894+
let ((index, spk), index_changeset) =
893895
self.indexed_graph.index.next_unused_spk(&change_keychain);
894896
let spk = spk.into();
895897
self.indexed_graph.index.mark_used(&change_keychain, index);
896898
self.persist
897-
.stage(ChangeSet::from(IndexedAdditions::from(index_additions)));
899+
.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
900+
index_changeset,
901+
)));
898902
self.persist.commit().expect("TODO");
899903
spk
900904
}
@@ -1286,7 +1290,7 @@ impl<D> Wallet<D> {
12861290
.indexed_graph
12871291
.graph()
12881292
.get_chain_position(&self.chain, chain_tip, input.previous_output.txid)
1289-
.map(|observed_as| match observed_as {
1293+
.map(|chain_position| match chain_position {
12901294
ChainPosition::Confirmed(a) => a.confirmation_height,
12911295
ChainPosition::Unconfirmed(_) => u32::MAX,
12921296
});
@@ -1469,7 +1473,7 @@ impl<D> Wallet<D> {
14691473
.graph()
14701474
.get_chain_position(&self.chain, chain_tip, txid)
14711475
{
1472-
Some(observed_as) => observed_as.cloned().into(),
1476+
Some(chain_position) => chain_position.cloned().into(),
14731477
None => return false,
14741478
};
14751479

@@ -1716,11 +1720,13 @@ impl<D> Wallet<D> {
17161720
D: PersistBackend<ChangeSet>,
17171721
{
17181722
let mut changeset = ChangeSet::from(self.chain.apply_update(update.chain)?);
1719-
let (_, index_additions) = self
1723+
let (_, index_changeset) = self
17201724
.indexed_graph
17211725
.index
17221726
.reveal_to_target_multi(&update.last_active_indices);
1723-
changeset.append(ChangeSet::from(IndexedAdditions::from(index_additions)));
1727+
changeset.append(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
1728+
index_changeset,
1729+
)));
17241730
changeset.append(ChangeSet::from(
17251731
self.indexed_graph.apply_update(update.graph),
17261732
));
@@ -1827,7 +1833,7 @@ fn new_tx_details(
18271833
) -> TransactionDetails {
18281834
let graph = indexed_graph.graph();
18291835
let index = &indexed_graph.index;
1830-
let tx = canonical_tx.node.tx;
1836+
let tx = canonical_tx.tx_node.tx;
18311837

18321838
let received = tx
18331839
.output
@@ -1867,11 +1873,11 @@ fn new_tx_details(
18671873

18681874
TransactionDetails {
18691875
transaction: if include_raw { Some(tx.clone()) } else { None },
1870-
txid: canonical_tx.node.txid,
1876+
txid: canonical_tx.tx_node.txid,
18711877
received,
18721878
sent,
18731879
fee,
1874-
confirmation_time: canonical_tx.observed_as.cloned().into(),
1880+
confirmation_time: canonical_tx.chain_position.cloned().into(),
18751881
}
18761882
}
18771883

crates/bdk/tests/wallet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ fn test_bump_fee_remove_output_manually_selected_only() {
15711571
.transactions()
15721572
.last()
15731573
.unwrap()
1574-
.observed_as
1574+
.chain_position
15751575
.cloned()
15761576
.into(),
15771577
)
@@ -1621,7 +1621,7 @@ fn test_bump_fee_add_input() {
16211621
.transactions()
16221622
.last()
16231623
.unwrap()
1624-
.observed_as
1624+
.chain_position
16251625
.cloned()
16261626
.into();
16271627
wallet.insert_tx(init_tx, pos).unwrap();

crates/chain/src/indexed_tx_graph.rs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use alloc::vec::Vec;
66
use bitcoin::{OutPoint, Transaction, TxOut};
77

88
use crate::{
9-
keychain::DerivationAdditions,
10-
tx_graph::{Additions, TxGraph},
9+
keychain,
10+
tx_graph::{self, TxGraph},
1111
Anchor, Append,
1212
};
1313

@@ -46,47 +46,47 @@ impl<A, I> IndexedTxGraph<A, I> {
4646
}
4747

4848
impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {
49-
/// Applies the [`IndexedAdditions`] to the [`IndexedTxGraph`].
50-
pub fn apply_additions(&mut self, additions: IndexedAdditions<A, I::Additions>) {
51-
let IndexedAdditions {
52-
graph_additions,
53-
index_additions,
54-
} = additions;
49+
/// Applies the [`ChangeSet`] to the [`IndexedTxGraph`].
50+
pub fn apply_changeset(&mut self, changeset: ChangeSet<A, I::ChangeSet>) {
51+
let ChangeSet {
52+
graph_changeset,
53+
index_changeset,
54+
} = changeset;
5555

56-
self.index.apply_additions(index_additions);
56+
self.index.apply_changeset(index_changeset);
5757

58-
for tx in &graph_additions.txs {
58+
for tx in &graph_changeset.txs {
5959
self.index.index_tx(tx);
6060
}
61-
for (&outpoint, txout) in &graph_additions.txouts {
61+
for (&outpoint, txout) in &graph_changeset.txouts {
6262
self.index.index_txout(outpoint, txout);
6363
}
6464

65-
self.graph.apply_additions(graph_additions);
65+
self.graph.apply_changeset(graph_changeset);
6666
}
6767
}
6868

6969
impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
7070
where
71-
I::Additions: Default + Append,
71+
I::ChangeSet: Default + Append,
7272
{
7373
/// Apply an `update` directly.
7474
///
75-
/// `update` is a [`TxGraph<A>`] and the resultant changes is returned as [`IndexedAdditions`].
76-
pub fn apply_update(&mut self, update: TxGraph<A>) -> IndexedAdditions<A, I::Additions> {
77-
let graph_additions = self.graph.apply_update(update);
75+
/// `update` is a [`TxGraph<A>`] and the resultant changes is returned as [`ChangeSet`].
76+
pub fn apply_update(&mut self, update: TxGraph<A>) -> ChangeSet<A, I::ChangeSet> {
77+
let graph_changeset = self.graph.apply_update(update);
7878

79-
let mut index_additions = I::Additions::default();
80-
for added_tx in &graph_additions.txs {
81-
index_additions.append(self.index.index_tx(added_tx));
79+
let mut index_changeset = I::ChangeSet::default();
80+
for added_tx in &graph_changeset.txs {
81+
index_changeset.append(self.index.index_tx(added_tx));
8282
}
83-
for (&added_outpoint, added_txout) in &graph_additions.txouts {
84-
index_additions.append(self.index.index_txout(added_outpoint, added_txout));
83+
for (&added_outpoint, added_txout) in &graph_changeset.txouts {
84+
index_changeset.append(self.index.index_txout(added_outpoint, added_txout));
8585
}
8686

87-
IndexedAdditions {
88-
graph_additions,
89-
index_additions,
87+
ChangeSet {
88+
graph_changeset,
89+
index_changeset,
9090
}
9191
}
9292

@@ -95,7 +95,7 @@ where
9595
&mut self,
9696
outpoint: OutPoint,
9797
txout: &TxOut,
98-
) -> IndexedAdditions<A, I::Additions> {
98+
) -> ChangeSet<A, I::ChangeSet> {
9999
let mut update = TxGraph::<A>::default();
100100
let _ = update.insert_txout(outpoint, txout.clone());
101101
self.apply_update(update)
@@ -110,7 +110,7 @@ where
110110
tx: &Transaction,
111111
anchors: impl IntoIterator<Item = A>,
112112
seen_at: Option<u64>,
113-
) -> IndexedAdditions<A, I::Additions> {
113+
) -> ChangeSet<A, I::ChangeSet> {
114114
let txid = tx.txid();
115115

116116
let mut update = TxGraph::<A>::default();
@@ -138,20 +138,20 @@ where
138138
&mut self,
139139
txs: impl IntoIterator<Item = (&'t Transaction, impl IntoIterator<Item = A>)>,
140140
seen_at: Option<u64>,
141-
) -> IndexedAdditions<A, I::Additions> {
141+
) -> ChangeSet<A, I::ChangeSet> {
142142
// The algorithm below allows for non-topologically ordered transactions by using two loops.
143143
// This is achieved by:
144144
// 1. insert all txs into the index. If they are irrelevant then that's fine it will just
145145
// not store anything about them.
146146
// 2. decide whether to insert them into the graph depending on whether `is_tx_relevant`
147147
// returns true or not. (in a second loop).
148-
let mut additions = IndexedAdditions::<A, I::Additions>::default();
148+
let mut changeset = ChangeSet::<A, I::ChangeSet>::default();
149149
let mut transactions = Vec::new();
150150
for (tx, anchors) in txs.into_iter() {
151-
additions.index_additions.append(self.index.index_tx(tx));
151+
changeset.index_changeset.append(self.index.index_tx(tx));
152152
transactions.push((tx, anchors));
153153
}
154-
additions.append(
154+
changeset.append(
155155
transactions
156156
.into_iter()
157157
.filter_map(|(tx, anchors)| match self.index.is_tx_relevant(tx) {
@@ -163,7 +163,7 @@ where
163163
acc
164164
}),
165165
);
166-
additions
166+
changeset
167167
}
168168
}
169169

@@ -181,64 +181,64 @@ where
181181
)
182182
)]
183183
#[must_use]
184-
pub struct IndexedAdditions<A, IA> {
185-
/// [`TxGraph`] additions.
186-
pub graph_additions: Additions<A>,
187-
/// [`Indexer`] additions.
188-
pub index_additions: IA,
184+
pub struct ChangeSet<A, IA> {
185+
/// [`TxGraph`] changeset.
186+
pub graph_changeset: tx_graph::ChangeSet<A>,
187+
/// [`Indexer`] changeset.
188+
pub index_changeset: IA,
189189
}
190190

191-
impl<A, IA: Default> Default for IndexedAdditions<A, IA> {
191+
impl<A, IA: Default> Default for ChangeSet<A, IA> {
192192
fn default() -> Self {
193193
Self {
194-
graph_additions: Default::default(),
195-
index_additions: Default::default(),
194+
graph_changeset: Default::default(),
195+
index_changeset: Default::default(),
196196
}
197197
}
198198
}
199199

200-
impl<A: Anchor, IA: Append> Append for IndexedAdditions<A, IA> {
200+
impl<A: Anchor, IA: Append> Append for ChangeSet<A, IA> {
201201
fn append(&mut self, other: Self) {
202-
self.graph_additions.append(other.graph_additions);
203-
self.index_additions.append(other.index_additions);
202+
self.graph_changeset.append(other.graph_changeset);
203+
self.index_changeset.append(other.index_changeset);
204204
}
205205

206206
fn is_empty(&self) -> bool {
207-
self.graph_additions.is_empty() && self.index_additions.is_empty()
207+
self.graph_changeset.is_empty() && self.index_changeset.is_empty()
208208
}
209209
}
210210

211-
impl<A, IA: Default> From<Additions<A>> for IndexedAdditions<A, IA> {
212-
fn from(graph_additions: Additions<A>) -> Self {
211+
impl<A, IA: Default> From<tx_graph::ChangeSet<A>> for ChangeSet<A, IA> {
212+
fn from(graph_changeset: tx_graph::ChangeSet<A>) -> Self {
213213
Self {
214-
graph_additions,
214+
graph_changeset,
215215
..Default::default()
216216
}
217217
}
218218
}
219219

220-
impl<A, K> From<DerivationAdditions<K>> for IndexedAdditions<A, DerivationAdditions<K>> {
221-
fn from(index_additions: DerivationAdditions<K>) -> Self {
220+
impl<A, K> From<keychain::ChangeSet<K>> for ChangeSet<A, keychain::ChangeSet<K>> {
221+
fn from(index_changeset: keychain::ChangeSet<K>) -> Self {
222222
Self {
223-
graph_additions: Default::default(),
224-
index_additions,
223+
graph_changeset: Default::default(),
224+
index_changeset,
225225
}
226226
}
227227
}
228228

229229
/// Represents a structure that can index transaction data.
230230
pub trait Indexer {
231-
/// The resultant "additions" when new transaction data is indexed.
232-
type Additions;
231+
/// The resultant "changeset" when new transaction data is indexed.
232+
type ChangeSet;
233233

234234
/// Scan and index the given `outpoint` and `txout`.
235-
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::Additions;
235+
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet;
236236

237237
/// Scan and index the given transaction.
238-
fn index_tx(&mut self, tx: &Transaction) -> Self::Additions;
238+
fn index_tx(&mut self, tx: &Transaction) -> Self::ChangeSet;
239239

240-
/// Apply additions to itself.
241-
fn apply_additions(&mut self, additions: Self::Additions);
240+
/// Apply changeset to itself.
241+
fn apply_changeset(&mut self, changeset: Self::ChangeSet);
242242

243243
/// Determines whether the transaction should be included in the index.
244244
fn is_tx_relevant(&self, tx: &Transaction) -> bool;

0 commit comments

Comments
 (0)