Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ async fn main() -> anyhow::Result<()> {
warning_subscriber,
mut update_subscriber,
node,
} = Builder::new(NETWORK)
.build_with_wallet(&wallet, scan_type)
.unwrap();
} = Builder::new(NETWORK).build_with_wallet(
wallet.spk_index().clone(),
wallet.latest_checkpoint(),
wallet.network(),
scan_type,
)?;

tokio::task::spawn(async move { node.run().await });
tokio::task::spawn(async move { traces(info_subscriber, warning_subscriber).await });
Expand Down
19 changes: 12 additions & 7 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@

use std::fmt::Display;

use bdk_wallet::bitcoin::Network;
use bdk_wallet::chain::keychain_txout::KeychainTxOutIndex;
use bdk_wallet::{
chain::{CheckPoint, IndexedTxGraph},
Wallet,
KeychainKind,
};
pub use bip157::Builder;
use bip157::{chain::ChainState, HeaderCheckpoint};
Expand All @@ -68,24 +70,27 @@ pub trait BuilderExt {
/// Attempt to build the node with scripts from a [`Wallet`] and following a [`ScanType`].
fn build_with_wallet(
self,
wallet: &Wallet,
txout_index: KeychainTxOutIndex<KeychainKind>,
chain_tip: CheckPoint,
network: Network,
scan_type: ScanType,
) -> Result<LightClient, BuilderError>;
}

impl BuilderExt for Builder {
fn build_with_wallet(
mut self,
wallet: &Wallet,
txout_index: KeychainTxOutIndex<KeychainKind>,
chain_tip: CheckPoint,
network: Network,
scan_type: ScanType,
) -> Result<LightClient, BuilderError> {
let network = wallet.network();
if self.network().ne(&network) {
return Err(BuilderError::NetworkMismatch);
}
match scan_type {
ScanType::Sync => {
let current_cp = wallet.latest_checkpoint();
let current_cp = chain_tip.clone();
let sync_start = walk_back_max_reorg(current_cp);
self = self.chain_state(ChainState::Checkpoint(sync_start));
}
Expand All @@ -101,12 +106,12 @@ impl BuilderExt for Builder {
warn_rx,
event_rx,
} = client;
let indexed_graph = IndexedTxGraph::new(wallet.spk_index().clone());
let indexed_graph = IndexedTxGraph::new(txout_index);
let update_subscriber = UpdateSubscriber::new(
requester.clone(),
scan_type,
event_rx,
wallet.latest_checkpoint(),
chain_tip,
indexed_graph,
);
Ok(LightClient {
Expand Down
7 changes: 6 additions & 1 deletion tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ fn init_node(
.add_peer(peer)
.data_dir(tempdir)
.required_peers(1)
.build_with_wallet(wallet, ScanType::Sync)?)
.build_with_wallet(
wallet.spk_index().clone(),
wallet.latest_checkpoint(),
wallet.network(),
ScanType::Sync,
)?)
}

#[tokio::test]
Expand Down