Skip to content
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
2 changes: 1 addition & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use bdk_wallet::{
pub use bip157::Builder;
use bip157::{chain::ChainState, HeaderCheckpoint};

use crate::{Idle, LightClient, LoggingSubscribers, ScanType, UpdateSubscriber};
use crate::{state::Idle, LightClient, LoggingSubscribers, ScanType, UpdateSubscriber};

const IMPOSSIBLE_REORG_DEPTH: usize = 7;

Expand Down
47 changes: 25 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,30 @@ pub use bip157::UnboundedReceiver;
pub use builder::BuilderExt;
pub mod builder;

/// Client state when idle.
pub struct Idle;
/// Client state when subscribed to events.
pub struct Subscribed;
/// Client state when active.
pub struct Active;
/// State of the light client.
pub mod state {
/// Client state when idle.
pub struct Idle;
/// Client state when subscribed to events.
pub struct Subscribed;
/// Client state when active.
pub struct Active;
}

mod sealed {
pub trait Sealed {}
}

impl sealed::Sealed for Idle {}
impl sealed::Sealed for Subscribed {}
impl sealed::Sealed for Active {}
impl sealed::Sealed for state::Idle {}
impl sealed::Sealed for state::Subscribed {}
impl sealed::Sealed for state::Active {}

/// State of the client.
pub trait State: sealed::Sealed {}

impl State for Idle {}
impl State for Subscribed {}
impl State for Active {}
impl State for state::Idle {}
impl State for state::Subscribed {}
impl State for state::Active {}

/// Subscribe to events, notably
#[derive(Debug)]
Expand Down Expand Up @@ -115,13 +118,13 @@ pub struct LightClient<S: State> {
_marker: core::marker::PhantomData<S>,
}

impl LightClient<Idle> {
impl LightClient<state::Idle> {
fn new(
requester: Requester,
logging: LoggingSubscribers,
update: UpdateSubscriber,
node: bip157::Node,
) -> LightClient<Idle> {
) -> LightClient<state::Idle> {
LightClient {
requester,
logging_subscribers: Some(logging),
Expand All @@ -143,7 +146,7 @@ impl LightClient<Idle> {
pub fn subscribe(
mut self,
) -> (
LightClient<Subscribed>,
LightClient<state::Subscribed>,
LoggingSubscribers,
UpdateSubscriber,
) {
Expand All @@ -162,15 +165,15 @@ impl LightClient<Idle> {
}
}

impl LightClient<Subscribed> {
impl LightClient<state::Subscribed> {
/// Start fetching data for the wallet on a dedicated [`tokio::task`]. This will continually
/// run until terminated or no peers could be found.
///
/// # Panics
///
/// If there is no [`tokio::runtime::Runtime`] to drive execution. Common in synchronous
/// setups.
pub fn start(mut self) -> LightClient<Active> {
pub fn start(mut self) -> LightClient<state::Active> {
let node = core::mem::take(&mut self.node).expect("cannot start twice.");
tokio::task::spawn(async move { node.run().await });
LightClient {
Expand All @@ -184,7 +187,7 @@ impl LightClient<Subscribed> {

/// Take the underlying node process to run in a custom way. Examples include using a dedicated
/// [`tokio::runtime::Runtime`] or [`tokio::runtime::Handle`] to drive execution.
pub fn managed_start(mut self) -> (LightClient<Active>, Node) {
pub fn managed_start(mut self) -> (LightClient<state::Active>, Node) {
let node = core::mem::take(&mut self.node).expect("cannot start twice.");
let client = LightClient {
requester: self.requester,
Expand All @@ -197,20 +200,20 @@ impl LightClient<Subscribed> {
}
}

impl LightClient<Active> {
impl LightClient<state::Active> {
/// The client is active and may now handle requests with a [`Requester`].
pub fn requester(self) -> Requester {
self.requester
}
}

impl From<LightClient<Active>> for Requester {
fn from(value: LightClient<Active>) -> Self {
impl From<LightClient<state::Active>> for Requester {
fn from(value: LightClient<state::Active>) -> Self {
value.requester
}
}

impl AsRef<Requester> for LightClient<Active> {
impl AsRef<Requester> for LightClient<state::Active> {
fn as_ref(&self) -> &Requester {
&self.requester
}
Expand Down
2 changes: 1 addition & 1 deletion tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// #![allow(unused)]
use bdk_kyoto::Idle;
use bdk_kyoto::state::Idle;
use std::net::IpAddr;
use std::path::PathBuf;
use std::time::Duration;
Expand Down
Loading