Skip to content

Commit f0753cd

Browse files
committed
feat!: make Update take in a generic
and implemented `apply_update`
1 parent f35e716 commit f0753cd

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/wallet/mod.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ pub struct Wallet<K: Ord> {
134134
/// An update to [`Wallet`].
135135
///
136136
/// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`LocalChain`] atomically.
137-
#[derive(Debug, Clone, Default)]
138-
pub struct Update {
137+
#[derive(Debug, Clone)]
138+
pub struct Update<K> {
139139
/// Contains the last active derivation indices per keychain (`K`), which is used to update the
140140
/// [`KeychainTxOutIndex`].
141-
pub last_active_indices: BTreeMap<KeychainKind, u32>,
141+
pub last_active_indices: BTreeMap<K, u32>,
142142

143143
/// Update for the wallet's internal [`TxGraph`].
144144
pub tx_update: TxUpdate<ConfirmationBlockTime>,
@@ -147,8 +147,8 @@ pub struct Update {
147147
pub chain: Option<CheckPoint>,
148148
}
149149

150-
impl From<FullScanResponse<KeychainKind>> for Update {
151-
fn from(value: FullScanResponse<KeychainKind>) -> Self {
150+
impl<K> From<FullScanResponse<K>> for Update<K> {
151+
fn from(value: FullScanResponse<K>) -> Self {
152152
Self {
153153
last_active_indices: value.last_active_indices,
154154
tx_update: value.tx_update,
@@ -157,7 +157,7 @@ impl From<FullScanResponse<KeychainKind>> for Update {
157157
}
158158
}
159159

160-
impl From<SyncResponse> for Update {
160+
impl<K> From<SyncResponse> for Update<K> {
161161
fn from(value: SyncResponse) -> Self {
162162
Self {
163163
last_active_indices: BTreeMap::new(),
@@ -167,6 +167,16 @@ impl From<SyncResponse> for Update {
167167
}
168168
}
169169

170+
impl<K> Default for Update<K> {
171+
fn default() -> Self {
172+
Update {
173+
last_active_indices: Default::default(),
174+
tx_update: Default::default(),
175+
chain: Default::default(),
176+
}
177+
}
178+
}
179+
170180
/// A derived address and the index it was found at.
171181
/// For convenience this automatically derefs to `Address`
172182
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -581,6 +591,43 @@ where
581591
}
582592
}
583593

594+
impl<K> Wallet<K>
595+
where
596+
K: Ord + Clone + Debug,
597+
{
598+
/// Apply update.
599+
pub fn apply_update(&mut self, update: impl Into<Update<K>>) -> Result<(), CannotConnectError> {
600+
let Update {
601+
last_active_indices,
602+
tx_update,
603+
chain,
604+
} = update.into();
605+
606+
let mut changeset = ChangeSet::default();
607+
608+
if let Some(tip) = chain {
609+
changeset.merge(self.chain.apply_update(tip)?.into());
610+
}
611+
612+
changeset.merge(
613+
self.tx_graph
614+
.index
615+
.reveal_to_target_multi(&last_active_indices)
616+
.into(),
617+
);
618+
619+
changeset.merge(self.tx_graph.apply_update(tx_update).into());
620+
621+
self.stage(changeset);
622+
623+
Ok(())
624+
}
625+
626+
fn stage(&mut self, changeset: impl Into<ChangeSet<K>>) {
627+
self.stage.merge(changeset.into());
628+
}
629+
}
630+
584631
// impl Wallet {
585632
// /// Build a new single descriptor [`Wallet`].
586633
// ///

0 commit comments

Comments
 (0)