Skip to content

Commit 2376478

Browse files
committed
feat(kyoto): configure log level and DNS resolver
1 parent 2c9d903 commit 2376478

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

bdk-ffi/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bdk-ffi/src/kyoto.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use bdk_kyoto::builder::ServiceFlags;
33
use bdk_kyoto::builder::TrustedPeer;
44
use bdk_kyoto::kyoto::tokio;
55
use bdk_kyoto::kyoto::AddrV2;
6+
use bdk_kyoto::kyoto::LogLevel as BDKLogLevel;
67
use bdk_kyoto::kyoto::ScriptBuf;
78
use bdk_kyoto::LightClient as BDKLightClient;
89
use bdk_kyoto::NodeDefault;
@@ -94,6 +95,8 @@ pub struct LightClientBuilder {
9495
connections: u8,
9596
data_dir: Option<String>,
9697
scan_type: ScanType,
98+
log_level: LogLevel,
99+
dns_resolver: Option<Arc<IpAddress>>,
97100
peers: Vec<Peer>,
98101
}
99102

@@ -106,6 +109,8 @@ impl LightClientBuilder {
106109
connections: DEFAULT_CONNECTIONS,
107110
data_dir: None,
108111
scan_type: ScanType::default(),
112+
log_level: LogLevel::default(),
113+
dns_resolver: None,
109114
peers: Vec::new(),
110115
}
111116
}
@@ -135,6 +140,15 @@ impl LightClientBuilder {
135140
})
136141
}
137142

143+
/// Set the log level for the node. Production applications may want to omit `Debug` messages
144+
/// to avoid heap allocations.
145+
pub fn log_level(&self, log_level: LogLevel) -> Arc<Self> {
146+
Arc::new(LightClientBuilder {
147+
log_level,
148+
..self.clone()
149+
})
150+
}
151+
138152
/// Bitcoin full-nodes to attempt a connection with.
139153
pub fn peers(&self, peers: Vec<Peer>) -> Arc<Self> {
140154
Arc::new(LightClientBuilder {
@@ -143,6 +157,15 @@ impl LightClientBuilder {
143157
})
144158
}
145159

160+
/// Configure a custom DNS resolver when querying DNS seeds. Default is `1.1.1.1` managed by
161+
/// CloudFlare.
162+
pub fn dns_resolver(&self, dns_resolver: Arc<IpAddress>) -> Arc<Self> {
163+
Arc::new(LightClientBuilder {
164+
dns_resolver: Some(dns_resolver),
165+
..self.clone()
166+
})
167+
}
168+
146169
/// Construct a [`LightClient`] for a [`Wallet`].
147170
pub fn build(&self, wallet: &Wallet) -> Result<LightClient, LightClientBuilderError> {
148171
let wallet = wallet.get_wallet();
@@ -157,13 +180,18 @@ impl LightClientBuilder {
157180
.map(|path| PathBuf::from(&path))
158181
.unwrap_or(PathBuf::from(CWD_PATH));
159182

160-
let builder = BDKLightClientBuilder::new()
183+
let mut builder = BDKLightClientBuilder::new()
161184
.connections(self.connections)
162185
.data_dir(path_buf)
163186
.scan_type(self.scan_type.into())
187+
.log_level(self.log_level.into())
164188
.timeout_duration(Duration::from_secs(TIMEOUT))
165189
.peers(trusted_peers);
166190

191+
if let Some(ip_addr) = self.dns_resolver.clone().map(|ip| ip.inner) {
192+
builder = builder.dns_resolver(ip_addr);
193+
}
194+
167195
let BDKLightClient {
168196
requester,
169197
log_subscriber,
@@ -361,6 +389,27 @@ impl From<Warn> for Warning {
361389
}
362390
}
363391

392+
/// Select the category of messages for the node to emit.
393+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
394+
pub enum LogLevel {
395+
/// Send `Log::Debug` messages. These messages are intended for debugging or troubleshooting
396+
/// node operation.
397+
#[default]
398+
Debug,
399+
/// Omit `Log::Debug` messages, including their memory allocations. Ideal for a production
400+
/// application that uses minimal logging.
401+
Warning,
402+
}
403+
404+
impl From<LogLevel> for BDKLogLevel {
405+
fn from(value: LogLevel) -> Self {
406+
match value {
407+
LogLevel::Debug => Self::Debug,
408+
LogLevel::Warning => Self::Warning,
409+
}
410+
}
411+
}
412+
364413
/// Sync a wallet from the last known block hash, recover a wallet from a specified height,
365414
/// or perform an expedited block header download for a new wallet.
366415
#[derive(Debug, Clone, Copy, Default, uniffi::Enum)]

0 commit comments

Comments
 (0)