Skip to content

Commit 86f3028

Browse files
committed
feat!: change KeychainTxOutIndex methods to always return changesets
Change the out signature of methods `reveal_to_target`, `reveal_next_spk` and `next_unused_spk` to return a tuple of `(Option<spk(s)>, changeset)` instead of `Option<(spk(s), changeset)>`. This makes the API more consistent. Also refactored various helper methods to take in a descriptor instead of a descriptor id. `.expect` calls now exist outside of these helper methods, making it more obvious where they are being called. Docs are updated to reflect the new API.
1 parent 47ea015 commit 86f3028

File tree

5 files changed

+217
-210
lines changed

5 files changed

+217
-210
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ impl Append for ChangeSet {
171171
}
172172
}
173173

174+
impl From<keychain::ChangeSet<KeychainKind>> for ChangeSet {
175+
fn from(keychain_changeset: keychain::ChangeSet<KeychainKind>) -> Self {
176+
Self {
177+
indexed_tx_graph: keychain_changeset.into(),
178+
..Default::default()
179+
}
180+
}
181+
}
182+
174183
impl From<local_chain::ChangeSet> for ChangeSet {
175184
fn from(chain: local_chain::ChangeSet) -> Self {
176185
Self {
@@ -813,18 +822,14 @@ impl Wallet {
813822
/// If writing to persistent storage fails.
814823
pub fn reveal_next_address(&mut self, keychain: KeychainKind) -> anyhow::Result<AddressInfo> {
815824
let keychain = self.map_keychain(keychain);
816-
let ((index, spk), index_changeset) = self
817-
.indexed_graph
818-
.index
819-
.reveal_next_spk(&keychain)
820-
.expect("Must exist (we called map_keychain)");
821-
825+
let (next_spk, index_changeset) = self.indexed_graph.index.reveal_next_spk(&keychain);
826+
let (index, spk) = next_spk.expect("Must exist (we called map_keychain)");
822827
self.persist
823828
.stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?;
824829

825830
Ok(AddressInfo {
826831
index,
827-
address: Address::from_script(spk, self.network).expect("must have address form"),
832+
address: Address::from_script(&spk, self.network).expect("must have address form"),
828833
keychain,
829834
})
830835
}
@@ -845,11 +850,9 @@ impl Wallet {
845850
index: u32,
846851
) -> anyhow::Result<impl Iterator<Item = AddressInfo> + '_> {
847852
let keychain = self.map_keychain(keychain);
848-
let (spk_iter, index_changeset) = self
849-
.indexed_graph
850-
.index
851-
.reveal_to_target(&keychain, index)
852-
.expect("must exist (we called map_keychain)");
853+
let (spk_iter, index_changeset) =
854+
self.indexed_graph.index.reveal_to_target(&keychain, index);
855+
let spk_iter = spk_iter.expect("Must exist (we called map_keychain)");
853856

854857
self.persist
855858
.stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?;
@@ -872,18 +875,15 @@ impl Wallet {
872875
/// If writing to persistent storage fails.
873876
pub fn next_unused_address(&mut self, keychain: KeychainKind) -> anyhow::Result<AddressInfo> {
874877
let keychain = self.map_keychain(keychain);
875-
let ((index, spk), index_changeset) = self
876-
.indexed_graph
877-
.index
878-
.next_unused_spk(&keychain)
879-
.expect("must exist (we called map_keychain)");
878+
let (next_spk, index_changeset) = self.indexed_graph.index.next_unused_spk(&keychain);
879+
let (index, spk) = next_spk.expect("Must exist (we called map_keychain)");
880880

881881
self.persist
882882
.stage_and_commit(indexed_tx_graph::ChangeSet::from(index_changeset).into())?;
883883

884884
Ok(AddressInfo {
885885
index,
886-
address: Address::from_script(spk, self.network).expect("must have address form"),
886+
address: Address::from_script(&spk, self.network).expect("must have address form"),
887887
keychain,
888888
})
889889
}
@@ -1038,7 +1038,7 @@ impl Wallet {
10381038
/// [`commit`]: Self::commit
10391039
pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) {
10401040
let additions = self.indexed_graph.insert_txout(outpoint, txout);
1041-
self.persist.stage(ChangeSet::from(additions));
1041+
self.persist.stage(additions.into());
10421042
}
10431043

10441044
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
@@ -1607,17 +1607,11 @@ impl Wallet {
16071607
Some(ref drain_recipient) => drain_recipient.clone(),
16081608
None => {
16091609
let change_keychain = self.map_keychain(KeychainKind::Internal);
1610-
let ((index, spk), index_changeset) = self
1611-
.indexed_graph
1612-
.index
1613-
.next_unused_spk(&change_keychain)
1614-
.expect("Keychain exists (we called map_keychain)");
1615-
let spk = spk.into();
1610+
let (next_spk, index_changeset) =
1611+
self.indexed_graph.index.next_unused_spk(&change_keychain);
1612+
let (index, spk) = next_spk.expect("Keychain exists (we called map_keychain)");
16161613
self.indexed_graph.index.mark_used(change_keychain, index);
1617-
self.persist
1618-
.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
1619-
index_changeset,
1620-
)));
1614+
self.persist.stage(index_changeset.into());
16211615
self.persist.commit().map_err(CreateTxError::Persist)?;
16221616
spk
16231617
}
@@ -2432,21 +2426,19 @@ impl Wallet {
24322426
/// [`commit`]: Self::commit
24332427
pub fn apply_update(&mut self, update: impl Into<Update>) -> Result<(), CannotConnectError> {
24342428
let update = update.into();
2435-
let mut changeset = match update.chain {
2436-
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
2437-
None => ChangeSet::default(),
2438-
};
2429+
let mut changeset = ChangeSet::default();
24392430

2440-
let (_, index_changeset) = self
2441-
.indexed_graph
2442-
.index
2443-
.reveal_to_target_multi(&update.last_active_indices);
2444-
changeset.append(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
2445-
index_changeset,
2446-
)));
2447-
changeset.append(ChangeSet::from(
2448-
self.indexed_graph.apply_update(update.graph),
2449-
));
2431+
if let Some(chain_update) = update.chain {
2432+
changeset.append(self.chain.apply_update(chain_update)?.into());
2433+
}
2434+
changeset.append({
2435+
let (_, index_changeset) = self
2436+
.indexed_graph
2437+
.index
2438+
.reveal_to_target_multi(&update.last_active_indices);
2439+
index_changeset.into()
2440+
});
2441+
changeset.append(self.indexed_graph.apply_update(update.graph).into());
24502442
self.persist.stage(changeset);
24512443
Ok(())
24522444
}
@@ -2552,7 +2544,7 @@ impl Wallet {
25522544
let indexed_graph_changeset = self
25532545
.indexed_graph
25542546
.batch_insert_relevant_unconfirmed(unconfirmed_txs);
2555-
self.persist.stage(ChangeSet::from(indexed_graph_changeset));
2547+
self.persist.stage(indexed_graph_changeset.into());
25562548
}
25572549
}
25582550

0 commit comments

Comments
 (0)