Skip to content

Commit 0c27b92

Browse files
committed
WIP
1 parent 965582b commit 0c27b92

File tree

9 files changed

+192
-593
lines changed

9 files changed

+192
-593
lines changed

Cargo.toml

+3-13
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ rust-version = "1.63.0"
1313

1414
[dependencies]
1515
bdk_wallet = { version = "1.0.0" }
16-
kyoto-cbf = { version = "0.6.0", default-features = false, features = ["dns", "database"] }
16+
## kyoto-cbf = { version = "0.7.0", default-features = false, features = ["dns", "database"] }
17+
kyoto-cbf = { git = "https://github.com/rustaceanrob/kyoto.git", rev = "60dca38c79af0187a4c7353be2ed6a956abcfb3a" ,default-features = false, features = ["dns", "database"] }
1718
tracing = { version = "0.1", optional = true }
1819
tracing-subscriber = { version = "0.3", optional = true }
1920

20-
[features]
21-
default = ["events", "callbacks", "trace"]
22-
trace = ["tracing", "tracing-subscriber"]
23-
callbacks = []
24-
events = []
25-
2621
[dev-dependencies]
2722
tokio = { version = "1.37", features = ["full"], default-features = false }
2823
anyhow = "1.0"
@@ -33,9 +28,4 @@ tracing-subscriber = { version = "0.3" }
3328

3429

3530
[[example]]
36-
name = "callbacks"
37-
required-features = ["trace", "callbacks"]
38-
39-
[[example]]
40-
name = "events"
41-
required-features = ["events"]
31+
name = "example"

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ client/server relationship. Esplora and Electrum offer _proactive_ APIs, in that
99

1010
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.
1111

12-
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/).
13-
1412
## License
1513

1614
Licensed under either of
1715

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

21-
at your option.
19+
at your option.

examples/callbacks.rs

-77
This file was deleted.

examples/events.rs

-52
This file was deleted.

examples/example.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::net::{IpAddr, Ipv4Addr};
2+
3+
use bdk_kyoto::builder::{LightClientBuilder, ServiceFlags, TrustedPeer};
4+
use bdk_kyoto::{LightClient, Log, RequesterExt};
5+
use bdk_wallet::bitcoin::Network;
6+
use bdk_wallet::{KeychainKind, Wallet};
7+
use tokio::select;
8+
9+
/// Peer address whitelist
10+
const PEERS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(23, 137, 57, 100))];
11+
12+
/* Sync a bdk wallet */
13+
14+
#[tokio::main]
15+
async fn main() -> anyhow::Result<()> {
16+
let desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/0/*)";
17+
let change_desc = "tr([7d94197e/86'/1'/0']tpubDCyQVJj8KzjiQsFjmb3KwECVXPvMwvAxxZGCP9XmWSopmjW3bCV3wD7TgxrUhiGSueDS1MU5X1Vb1YjYcp8jitXc5fXfdC1z68hDDEyKRNr/1/*)";
18+
19+
let subscriber = tracing_subscriber::FmtSubscriber::new();
20+
tracing::subscriber::set_global_default(subscriber)?;
21+
22+
let peers = PEERS
23+
.iter()
24+
.map(|ip| {
25+
let mut peer = TrustedPeer::from_ip(*ip);
26+
peer.set_services(ServiceFlags::P2P_V2);
27+
peer
28+
})
29+
.collect();
30+
31+
let mut wallet = Wallet::create(desc, change_desc)
32+
.network(Network::Signet)
33+
.lookahead(30)
34+
.create_wallet_no_persist()?;
35+
36+
// The light client builder handles the logic of inserting the SPKs
37+
let LightClient {
38+
requester,
39+
mut log_subscriber,
40+
mut update_subscriber,
41+
node,
42+
} = LightClientBuilder::new()
43+
.scan_after(170_000)
44+
.peers(peers)
45+
.build(&wallet)
46+
.unwrap();
47+
48+
tokio::task::spawn(async move { node.run().await });
49+
50+
// Sync and apply updates. We can do this a continual loop while the "application" is running.
51+
// Often this would occur on a separate thread than the underlying application user interface.
52+
loop {
53+
select! {
54+
update = update_subscriber.update() => {
55+
if let Some(update) = update {
56+
wallet.apply_update(update)?;
57+
tracing::info!("Tx count: {}", wallet.transactions().count());
58+
tracing::info!("Balance: {}", wallet.balance().total().to_sat());
59+
let last_revealed = wallet.derivation_index(KeychainKind::External);
60+
tracing::info!("Last revealed External: {:?}", last_revealed);
61+
tracing::info!(
62+
"Last revealed Internal: {:?}",
63+
wallet.derivation_index(KeychainKind::Internal)
64+
);
65+
tracing::info!("Local chain tip: {}", wallet.local_chain().tip().height());
66+
let next = wallet.reveal_next_address(KeychainKind::External).address;
67+
tracing::info!("Next receiving address: {next}");
68+
let fee_filter = requester.broadcast_min_feerate().await.unwrap();
69+
tracing::info!(
70+
"Broadcast minimum fee rate: {}",
71+
fee_filter
72+
);
73+
requester.add_revealed_scripts(&wallet).await?;
74+
}
75+
},
76+
log = log_subscriber.next_log() => {
77+
match log {
78+
Log::Dialog(d) => {
79+
tracing::info!("{}", d);
80+
},
81+
Log::Warning(w) => {
82+
tracing::warn!("{}", w);
83+
}
84+
_ => (),
85+
}
86+
}
87+
}
88+
}
89+
}

src/builder.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//! use bdk_wallet::Wallet;
1515
//! use bdk_wallet::bitcoin::Network;
1616
//! use bdk_kyoto::builder::{LightClientBuilder, TrustedPeer};
17-
//! use bdk_kyoto::logger::PrintLogger;
1817
//! use bdk_kyoto::LightClient;
1918
//!
2019
//! #[tokio::main]
@@ -30,7 +29,7 @@
3029
//! .network(Network::Signet)
3130
//! .create_wallet_no_persist()?;
3231
//!
33-
//! let LightClient { sender, receiver, node } = LightClientBuilder::new()
32+
//! let LightClient { requester, log_subscriber, update_subscriber, node } = LightClientBuilder::new()
3433
//! // When recovering a user's wallet, specify a height to start at
3534
//! .scan_after(200_000)
3635
//! // A node may handle mutliple connections
@@ -55,7 +54,7 @@ pub use kyoto::{
5554
TrustedPeer,
5655
};
5756

58-
use crate::{EventReceiver, LightClient, WalletExt};
57+
use crate::{LightClient, LogSubscriber, UpdateSubscriber, WalletExt};
5958

6059
const RECOMMENDED_PEERS: u8 = 2;
6160

@@ -156,15 +155,20 @@ impl LightClientBuilder {
156155
let (node, kyoto_client) = node_builder
157156
.add_scripts(wallet.peek_revealed_plus_lookahead().collect())
158157
.build_node()?;
159-
let (sender, receiver) = kyoto_client.split();
160-
let event_receiver = EventReceiver::from_index(
158+
let kyoto::Client {
159+
requester,
160+
log_rx,
161+
event_rx,
162+
} = kyoto_client;
163+
let update_subscriber = UpdateSubscriber::from_index(
161164
wallet.local_chain().tip(),
162165
wallet.spk_index().clone(),
163-
receiver,
166+
event_rx,
164167
)?;
165168
Ok(LightClient {
166-
sender,
167-
receiver: event_receiver,
169+
requester,
170+
log_subscriber: LogSubscriber::new(log_rx),
171+
update_subscriber,
168172
node,
169173
})
170174
}

0 commit comments

Comments
 (0)