Skip to content

Commit 20b3458

Browse files
authored
Fix CBF chain source build errors and UniFFI bindings (#5)
1 parent 9a41713 commit 20b3458

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

bindings/ldk_node.udl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface Builder {
4040
constructor(Config config);
4141
void set_chain_source_esplora(string server_url, EsploraSyncConfig? config);
4242
void set_chain_source_electrum(string server_url, ElectrumSyncConfig? config);
43-
void set_chain_source_cbf(sequence<string> peers, CbfSyncConfig? sync_config);
43+
void set_chain_source_cbf(sequence<string> peers, CbfSyncConfig? sync_config, FeeSourceConfig? fee_source_config);
4444
void set_chain_source_bitcoind_rpc(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
4545
void set_chain_source_bitcoind_rest(string rest_host, u16 rest_port, string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
4646
void set_gossip_source_p2p();
@@ -357,6 +357,8 @@ enum Currency {
357357

358358
typedef enum AsyncPaymentsRole;
359359

360+
typedef enum FeeSourceConfig;
361+
360362
[Custom]
361363
typedef string Txid;
362364

src/builder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ pub enum BuildError {
198198
NetworkMismatch,
199199
/// The role of the node in an asynchronous payments context is not compatible with the current configuration.
200200
AsyncPaymentsConfigMismatch,
201+
/// We failed to setup the chain source.
202+
ChainSourceSetupFailed,
201203
}
202204

203205
impl fmt::Display for BuildError {
@@ -231,6 +233,7 @@ impl fmt::Display for BuildError {
231233
"The async payments role is not compatible with the current configuration."
232234
)
233235
},
236+
Self::ChainSourceSetupFailed => write!(f, "Failed to setup chain source."),
234237
}
235238
}
236239
}
@@ -1424,6 +1427,10 @@ fn build_with_store_internal(
14241427
Arc::clone(&logger),
14251428
Arc::clone(&node_metrics),
14261429
)
1430+
.map_err(|e| {
1431+
log_error!(logger, "Failed to initialize CBF chain source: {}", e);
1432+
BuildError::ChainSourceSetupFailed
1433+
})?
14271434
},
14281435

14291436
None => {

src/chain/cbf.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ impl CbfChainSource {
126126
peers: Vec<String>, sync_config: CbfSyncConfig, fee_source_config: Option<FeeSourceConfig>,
127127
fee_estimator: Arc<OnchainFeeEstimator>, kv_store: Arc<DynStore>, config: Arc<Config>,
128128
logger: Arc<Logger>, node_metrics: Arc<RwLock<NodeMetrics>>,
129-
) -> Self {
129+
) -> Result<Self, Error> {
130130
let fee_source = match fee_source_config {
131131
Some(FeeSourceConfig::Esplora(server_url)) => {
132132
let timeout = sync_config.timeouts_config.per_request_timeout_secs;
133133
let mut builder = esplora_client::Builder::new(&server_url);
134134
builder = builder.timeout(timeout as u64);
135-
let client = builder.build_async().unwrap();
135+
let client = builder.build_async().map_err(|e| {
136+
log_error!(logger, "Failed to build esplora client: {}", e);
137+
Error::ConnectionFailed
138+
})?;
136139
FeeSource::Esplora { client }
137140
},
138141
Some(FeeSourceConfig::Electrum(server_url)) => FeeSource::Electrum { server_url },
@@ -152,7 +155,7 @@ impl CbfChainSource {
152155
let last_lightning_synced_height = Arc::new(Mutex::new(None));
153156
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
154157
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
155-
Self {
158+
Ok(Self {
156159
peers,
157160
sync_config,
158161
fee_source,
@@ -174,7 +177,7 @@ impl CbfChainSource {
174177
config,
175178
logger,
176179
node_metrics,
177-
}
180+
})
178181
}
179182

180183
/// Start the bip157 node and spawn background tasks for event processing.

src/chain/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl WalletSyncStatus {
9090
/// Setting an external source provides more accurate, per-target estimates
9191
/// from a mempool-aware server.
9292
#[derive(Debug, Clone)]
93+
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
9394
pub enum FeeSourceConfig {
9495
/// Use an Esplora HTTP server for fee rate estimation.
9596
Esplora(String),
@@ -205,7 +206,7 @@ impl ChainSource {
205206
fee_estimator: Arc<OnchainFeeEstimator>, tx_broadcaster: Arc<Broadcaster>,
206207
kv_store: Arc<DynStore>, config: Arc<Config>, logger: Arc<Logger>,
207208
node_metrics: Arc<RwLock<NodeMetrics>>,
208-
) -> (Self, Option<BestBlock>) {
209+
) -> Result<(Self, Option<BestBlock>), Error> {
209210
let cbf_chain_source = CbfChainSource::new(
210211
peers,
211212
sync_config,
@@ -215,10 +216,10 @@ impl ChainSource {
215216
config,
216217
Arc::clone(&logger),
217218
node_metrics,
218-
);
219+
)?;
219220
let kind = ChainSourceKind::Cbf(cbf_chain_source);
220221
let registered_txids = Mutex::new(Vec::new());
221-
(Self { kind, registered_txids, tx_broadcaster, logger }, None)
222+
Ok((Self { kind, registered_txids, tx_broadcaster, logger }, None))
222223
}
223224

224225
pub(crate) fn start(&self, runtime: Arc<Runtime>) -> Result<(), Error> {

tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#![cfg(any(test, cln_test, lnd_test, vss_test))]
99
#![allow(dead_code)]
10+
#![allow(unused_imports)]
11+
#![allow(unused_macros)]
1012

1113
pub(crate) mod logging;
1214

0 commit comments

Comments
 (0)