Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require &Wallet to build light client #87

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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
4 changes: 2 additions & 2 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async fn main() -> anyhow::Result<()> {
sender: _,
mut receiver,
node,
} = LightClientBuilder::new(&wallet)
} = LightClientBuilder::new()
.scan_after(170_000)
.build()
.build(&wallet)
.unwrap();

tokio::task::spawn(async move { node.run().await });
Expand Down
4 changes: 2 additions & 2 deletions examples/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ async fn main() -> anyhow::Result<()> {
sender: _,
mut receiver,
node,
} = LightClientBuilder::new(&wallet)
} = LightClientBuilder::new()
.scan_after(170_000)
.peers(peers)
.build()
.build(&wallet)
.unwrap();

tokio::task::spawn(async move { node.run().await });
Expand Down
42 changes: 21 additions & 21 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! .network(Network::Signet)
//! .create_wallet_no_persist()?;
//!
//! let LightClient { sender, receiver, node } = LightClientBuilder::new(&wallet)
//! let LightClient { sender, receiver, node } = LightClientBuilder::new()
//! // When recovering a user's wallet, specify a height to start at
//! .scan_after(200_000)
//! // A node may handle mutliple connections
Expand All @@ -40,7 +40,7 @@
//! // How long peers have to respond messages
//! .timeout_duration(Duration::from_secs(10))
//! .peers(peers)
//! .build()?;
//! .build(&wallet)?;
//! Ok(())
//! }
//! ```
Expand All @@ -61,20 +61,18 @@ const RECOMMENDED_PEERS: u8 = 2;

#[derive(Debug)]
/// Construct a light client from a [`Wallet`] reference.
pub struct LightClientBuilder<'a> {
wallet: &'a Wallet,
pub struct LightClientBuilder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think LightClientBuilder can just derive Default now.

peers: Option<Vec<TrustedPeer>>,
connections: Option<u8>,
birthday_height: Option<u32>,
data_dir: Option<PathBuf>,
timeout: Option<Duration>,
}

impl<'a> LightClientBuilder<'a> {
impl LightClientBuilder {
/// Construct a new node builder.
pub fn new(wallet: &'a Wallet) -> Self {
pub fn new() -> Self {
Self {
wallet,
peers: None,
connections: None,
birthday_height: None,
Expand Down Expand Up @@ -115,8 +113,8 @@ impl<'a> LightClientBuilder<'a> {
}

/// Build a light client node and a client to interact with the node.
pub fn build(self) -> Result<LightClient, BuilderError> {
let network = self.wallet.network();
pub fn build(self, wallet: &Wallet) -> Result<LightClient, BuilderError> {
let network = wallet.network();
let mut node_builder = NodeBuilder::new(network);
if let Some(whitelist) = self.peers {
node_builder = node_builder.add_peers(whitelist);
Expand All @@ -126,8 +124,8 @@ impl<'a> LightClientBuilder<'a> {
// If there is a birthday at a height less than our local chain, we may assume we've
// already synced the wallet past the birthday height and no longer
// need it.
if birthday < self.wallet.local_chain().tip().height() {
let block_id = self.wallet.local_chain().tip();
if birthday < wallet.local_chain().tip().height() {
let block_id = wallet.local_chain().tip();
let header_cp = HeaderCheckpoint::new(block_id.height(), block_id.hash());
node_builder = node_builder.anchor_checkpoint(header_cp)
} else {
Expand All @@ -140,7 +138,7 @@ impl<'a> LightClientBuilder<'a> {
// we assume this is a new wallet and use the most recent
// checkpoint. Otherwise we sync from the last known tip in the
// LocalChain.
let block_id = self.wallet.local_chain().tip();
let block_id = wallet.local_chain().tip();
if block_id.height() > 0 {
let header_cp = HeaderCheckpoint::new(block_id.height(), block_id.hash());
node_builder = node_builder.anchor_checkpoint(header_cp)
Expand All @@ -159,23 +157,19 @@ impl<'a> LightClientBuilder<'a> {
for keychain in [KeychainKind::External, KeychainKind::Internal] {
// The user may choose to recover a wallet with lookahead scripts
// or use the last revealed index plus some padding to find new transactions
let last_revealed = self
.wallet
let last_revealed = wallet
.spk_index()
.last_revealed_index(keychain)
.unwrap_or(0);
let lookahead_index = last_revealed + self.wallet.spk_index().lookahead();
let lookahead_index = last_revealed + wallet.spk_index().lookahead();
for index in 0..=lookahead_index {
spks.insert(self.wallet.peek_address(keychain, index).script_pubkey());
spks.insert(wallet.peek_address(keychain, index).script_pubkey());
}
}
let (node, kyoto_client) = node_builder.add_scripts(spks).build_node()?;
let (sender, receiver) = kyoto_client.split();
let event_receiver = EventReceiver::from_index(
self.wallet.local_chain().tip(),
self.wallet.spk_index(),
receiver,
)?;
let event_receiver =
EventReceiver::from_index(wallet.local_chain().tip(), wallet.spk_index(), receiver)?;
Ok(LightClient {
sender,
receiver: event_receiver,
Expand All @@ -184,6 +178,12 @@ impl<'a> LightClientBuilder<'a> {
}
}

impl Default for LightClientBuilder {
fn default() -> Self {
Self::new()
}
}

/// Errors thrown by the [`LightClientBuilder`].
#[derive(Debug)]
pub enum BuilderError {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! .network(Network::Signet)
//! .create_wallet_no_persist()?;
//!
//! let LightClient { sender, mut receiver, node } = LightClientBuilder::new(&wallet).build()?;
//! let LightClient { sender, mut receiver, node } = LightClientBuilder::new().build(&wallet)?;
//!
//! tokio::task::spawn(async move { node.run().await });
//!
Expand Down Expand Up @@ -64,7 +64,7 @@
//! .network(Network::Signet)
//! .create_wallet_no_persist()?;
//!
//! let LightClient { sender, mut receiver, node } = LightClientBuilder::new(&wallet).build()?;
//! let LightClient { sender, mut receiver, node } = LightClientBuilder::new().build(&wallet)?;
//!
//! tokio::task::spawn(async move { node.run().await });
//!
Expand Down
4 changes: 2 additions & 2 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ fn init_node(
let port = peer.port();
let mut peer = TrustedPeer::from_ip(ip);
peer.port = Some(port);
Ok(LightClientBuilder::new(wallet)
Ok(LightClientBuilder::new()
.peers(vec![peer])
.data_dir(tempdir)
.connections(1)
.build()?)
.build(wallet)?)
}

#[tokio::test]
Expand Down
Loading