Skip to content

Commit

Permalink
lib: update kyoto to 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Jan 20, 2025
1 parent 965582b commit eed849e
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 613 deletions.
17 changes: 2 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ rust-version = "1.63.0"

[dependencies]
bdk_wallet = { version = "1.0.0" }
kyoto-cbf = { version = "0.6.0", default-features = false, features = ["dns", "database"] }
tracing = { version = "0.1", optional = true }
tracing-subscriber = { version = "0.3", optional = true }

[features]
default = ["events", "callbacks", "trace"]
trace = ["tracing", "tracing-subscriber"]
callbacks = []
events = []
kyoto-cbf = { version = "0.8.0", default-features = false, features = ["dns", "database"] }

[dev-dependencies]
tokio = { version = "1.37", features = ["full"], default-features = false }
Expand All @@ -33,9 +25,4 @@ tracing-subscriber = { version = "0.3" }


[[example]]
name = "callbacks"
required-features = ["trace", "callbacks"]

[[example]]
name = "events"
required-features = ["events"]
name = "example"
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ client/server relationship. Esplora and Electrum offer _proactive_ APIs, in that

In the case of running a node as a background process, the developer experience is far more _reactive_, in that the node may emit any number of events, and the application may respond to them. BDK-Kyoto curates these events into structures that are easily handled by BDK APIs, making integration of compact block filters easily understood.

Developers are free to use [`bdk_wallet`](https://docs.rs/bdk_wallet/latest/bdk_wallet/), or only primitives found in [`bdk_core`](https://docs.rs/bdk_core/latest/bdk_core/) and [`bdk_chain`](https://docs.rs/bdk_chain/latest/bdk_chain/).

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)

at your option.
at your option.
77 changes: 0 additions & 77 deletions examples/callbacks.rs

This file was deleted.

52 changes: 0 additions & 52 deletions examples/events.rs

This file was deleted.

85 changes: 85 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::net::{IpAddr, Ipv4Addr};

use bdk_kyoto::builder::{LightClientBuilder, ServiceFlags, TrustedPeer};
use bdk_kyoto::{LightClient, RequesterExt};
use bdk_wallet::bitcoin::Network;
use bdk_wallet::{KeychainKind, Wallet};
use tokio::select;

/// Peer address whitelist
const PEERS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(23, 137, 57, 100))];

/* Sync a bdk wallet */

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/0/*)";
let change_desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/1/*)";

let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)?;

let peers = PEERS
.iter()
.map(|ip| {
let mut peer = TrustedPeer::from_ip(*ip);
peer.set_services(ServiceFlags::P2P_V2);
peer
})
.collect();

let mut wallet = Wallet::create(desc, change_desc)
.network(Network::Signet)
.lookahead(30)
.create_wallet_no_persist()?;

// The light client builder handles the logic of inserting the SPKs
let LightClient {
requester,
mut log_subscriber,
mut warning_subscriber,
mut update_subscriber,
node,
} = LightClientBuilder::new()
.scan_after(170_000)
.peers(peers)
.build(&wallet)
.unwrap();

tokio::task::spawn(async move { node.run().await });

// Sync and apply updates. We can do this a continual loop while the "application" is running.
// Often this would occur on a separate thread than the underlying application user interface.
loop {
select! {
update = update_subscriber.update() => {
if let Some(update) = update {
wallet.apply_update(update)?;
tracing::info!("Tx count: {}", wallet.transactions().count());
tracing::info!("Balance: {}", wallet.balance().total().to_sat());
let last_revealed = wallet.derivation_index(KeychainKind::External);
tracing::info!("Last revealed External: {:?}", last_revealed);
tracing::info!(
"Last revealed Internal: {:?}",
wallet.derivation_index(KeychainKind::Internal)
);
tracing::info!("Local chain tip: {}", wallet.local_chain().tip().height());
let next = wallet.reveal_next_address(KeychainKind::External).address;
tracing::info!("Next receiving address: {next}");
let fee_filter = requester.broadcast_min_feerate().await.unwrap();
tracing::info!(
"Broadcast minimum fee rate: {:#}",
fee_filter
);
requester.add_revealed_scripts(&wallet).await?;
}
},
log = log_subscriber.next_log() => {
tracing::info!("{log}")
}
warn = warning_subscriber.next_warning() => {
tracing::warn!("{warn}")
}
}
}
}
22 changes: 14 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//! use bdk_wallet::Wallet;
//! use bdk_wallet::bitcoin::Network;
//! use bdk_kyoto::builder::{LightClientBuilder, TrustedPeer};
//! use bdk_kyoto::logger::PrintLogger;
//! use bdk_kyoto::LightClient;
//!
//! #[tokio::main]
Expand All @@ -30,7 +29,7 @@
//! .network(Network::Signet)
//! .create_wallet_no_persist()?;
//!
//! let LightClient { sender, receiver, node } = LightClientBuilder::new()
//! let LightClient { requester, log_subscriber, warning_subscriber, update_subscriber, 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 @@ -55,7 +54,7 @@ pub use kyoto::{
TrustedPeer,
};

use crate::{EventReceiver, LightClient, WalletExt};
use crate::{LightClient, LogSubscriber, UpdateSubscriber, WalletExt, WarningSubscriber};

const RECOMMENDED_PEERS: u8 = 2;

Expand Down Expand Up @@ -156,15 +155,22 @@ impl LightClientBuilder {
let (node, kyoto_client) = node_builder
.add_scripts(wallet.peek_revealed_plus_lookahead().collect())
.build_node()?;
let (sender, receiver) = kyoto_client.split();
let event_receiver = EventReceiver::from_index(
let kyoto::Client {
requester,
log_rx,
warn_rx,
event_rx,
} = kyoto_client;
let update_subscriber = UpdateSubscriber::from_index(
wallet.local_chain().tip(),
wallet.spk_index().clone(),
receiver,
event_rx,
)?;
Ok(LightClient {
sender,
receiver: event_receiver,
requester,
log_subscriber: LogSubscriber::new(log_rx),
warning_subscriber: WarningSubscriber::new(warn_rx),
update_subscriber,
node,
})
}
Expand Down
Loading

0 comments on commit eed849e

Please sign in to comment.