Skip to content

Commit

Permalink
refactor: add Wallet init fn and call from new and new_no_persist fn
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Sep 25, 2023
1 parent f95506b commit 71adefc
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,17 @@ impl Wallet {
change_descriptor: Option<E>,
network: Network,
) -> Result<Self, crate::descriptor::DescriptorError> {
Self::new(descriptor, change_descriptor, (), network).map_err(|e| match e {
NewError::Descriptor(e) => e,
NewError::Persist(_) => unreachable!("no persistence so it can't fail"),
})
// create infallible no-op persist
let persist = Persist::new(());

// initialize empty wallet
let wallet =
Self::init(descriptor, change_descriptor, network, persist).map_err(|e| match e {
NewError::Descriptor(e) => e,
NewError::Persist(_) => unreachable!("no persistence so it can't fail"),
})?;

Ok(wallet)
}
}

Expand Down Expand Up @@ -274,11 +281,38 @@ impl<D> Wallet<D> {
mut db: D,
network: Network,
) -> Result<Self, NewError<D::LoadError>>
where
D: PersistBackend<ChangeSet>,
{
// load current changeset from db
let changeset = db.load_from_persistence().map_err(NewError::Persist)?;

// create persist
let persist = Persist::new(db);

// initialize empty wallet
let mut wallet = Self::init(descriptor, change_descriptor, network, persist)?;

// apply loaded changeset to wallet
wallet.chain.apply_changeset(&changeset.chain);
wallet
.indexed_graph
.apply_changeset(changeset.indexed_tx_graph);

Ok(wallet)
}

pub fn init<E: IntoWalletDescriptor>(
descriptor: E,
change_descriptor: Option<E>,
network: Network,
persist: Persist<D, ChangeSet>,
) -> Result<Self, NewError<D::LoadError>>
where
D: PersistBackend<ChangeSet>,

Check failure on line 312 in crates/bdk/src/wallet/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for an associated function

error: missing documentation for an associated function --> crates/bdk/src/wallet/mod.rs:305:5 | 305 | / pub fn init<E: IntoWalletDescriptor>( 306 | | descriptor: E, 307 | | change_descriptor: Option<E>, 308 | | network: Network, ... | 311 | | where 312 | | D: PersistBackend<ChangeSet>, | |_____________________________________^ | = note: `-D missing-docs` implied by `-D warnings`
{
let secp = Secp256k1::new();
let mut chain = LocalChain::default();
let chain = LocalChain::default();
let mut indexed_graph =
IndexedTxGraph::<ConfirmationTimeAnchor, KeychainTxOutIndex<KeychainKind>>::default();

Expand Down Expand Up @@ -309,12 +343,6 @@ impl<D> Wallet<D> {
None => Arc::new(SignersContainer::new()),
};

let changeset = db.load_from_persistence().map_err(NewError::Persist)?;
chain.apply_changeset(&changeset.chain);
indexed_graph.apply_changeset(changeset.indexed_tx_graph);

let persist = Persist::new(db);

Ok(Wallet {
signers,
change_signers,
Expand Down

0 comments on commit 71adefc

Please sign in to comment.