Skip to content

Commit d2ed6f6

Browse files
committed
feat(kyoto): add new conf and method to run nodes
1 parent b82acb5 commit d2ed6f6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

bdk-ffi/src/kyoto.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use bdk_kyoto::Warning as Warn;
1818
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
1919
use std::path::PathBuf;
2020
use std::sync::Arc;
21+
use std::time::Duration;
2122

2223
use tokio::sync::Mutex;
2324

@@ -33,6 +34,8 @@ type ScanType = bdk_kyoto::ScanType;
3334

3435
const DEFAULT_CONNECTIONS: u8 = 2;
3536
const CWD_PATH: &str = ".";
37+
const TCP_HANDSHAKE_TIMEOUT: u64 = 2;
38+
const MESSAGE_RESPONSE_TIMEOUT: u64 = 5;
3639

3740
/// Receive a [`CbfClient`] and [`CbfNode`].
3841
#[derive(Debug, uniffi::Record)]
@@ -76,6 +79,18 @@ impl CbfNode {
7679
})
7780
});
7881
}
82+
83+
/// Start the node on the current thread. This will block execution on the current thread for the
84+
/// duration of the application, and must be ran on a separate task or thread.
85+
pub fn run_forever(self: Arc<Self>) {
86+
tokio::runtime::Builder::new_multi_thread()
87+
.enable_all()
88+
.build()
89+
.unwrap()
90+
.block_on(async move {
91+
let _ = self.node.run().await;
92+
})
93+
}
7994
}
8095

8196
/// Build a BIP 157/158 light client to fetch transactions for a `Wallet`.
@@ -94,6 +109,8 @@ impl CbfNode {
94109
#[derive(Clone, uniffi::Object)]
95110
pub struct CbfBuilder {
96111
connections: u8,
112+
handshake_timeout: Duration,
113+
response_timeout: Duration,
97114
data_dir: Option<String>,
98115
scan_type: ScanType,
99116
log_level: LogLevel,
@@ -109,6 +126,8 @@ impl CbfBuilder {
109126
pub fn new() -> Self {
110127
CbfBuilder {
111128
connections: DEFAULT_CONNECTIONS,
129+
handshake_timeout: Duration::from_secs(TCP_HANDSHAKE_TIMEOUT),
130+
response_timeout: Duration::from_secs(MESSAGE_RESPONSE_TIMEOUT),
112131
data_dir: None,
113132
scan_type: ScanType::default(),
114133
log_level: LogLevel::default(),
@@ -160,6 +179,17 @@ impl CbfBuilder {
160179
})
161180
}
162181

182+
/// Configure the time in milliseconds that a node has to:
183+
/// 1. Respond to the initial connection
184+
/// 2. Respond to a request
185+
pub fn configure_timeout_millis(&self, handshake: u64, response: u64) -> Arc<Self> {
186+
Arc::new(CbfBuilder {
187+
handshake_timeout: Duration::from_millis(handshake),
188+
response_timeout: Duration::from_millis(response),
189+
..self.clone()
190+
})
191+
}
192+
163193
/// Configure a custom DNS resolver when querying DNS seeds. Default is `1.1.1.1` managed by
164194
/// CloudFlare.
165195
pub fn dns_resolver(&self, dns_resolver: Arc<IpAddress>) -> Arc<Self> {
@@ -193,6 +223,8 @@ impl CbfBuilder {
193223
let mut builder = BDKCbfBuilder::new(wallet.network())
194224
.required_peers(self.connections)
195225
.data_dir(path_buf)
226+
.handshake_timeout(self.handshake_timeout)
227+
.response_timeout(self.response_timeout)
196228
.log_level(self.log_level)
197229
.add_peers(trusted_peers);
198230

0 commit comments

Comments
 (0)