Skip to content

Commit 3ceff37

Browse files
committed
refactor: configure rest sync
- Adds set_chain_source_bitcoind_rest to configure REST synchronization. - Update docs to reflect the configuration order if REST sync is preferred. - Simplify the configuration object to capture only REST-related fields.
1 parent e4adcfc commit 3ceff37

File tree

6 files changed

+102
-57
lines changed

6 files changed

+102
-57
lines changed

bindings/ldk_node.udl

+2-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ dictionary ElectrumSyncConfig {
3434
BackgroundSyncConfig? background_sync_config;
3535
};
3636

37-
[Enum]
38-
interface BitcoindSyncClientConfig {
39-
Rpc();
40-
Rest(string rest_host, u16 rest_port);
41-
};
42-
4337
dictionary LSPS2ServiceConfig {
4438
string? require_token;
4539
boolean advertise_service;
@@ -83,7 +77,8 @@ interface Builder {
8377
void set_entropy_bip39_mnemonic(Mnemonic mnemonic, string? passphrase);
8478
void set_chain_source_esplora(string server_url, EsploraSyncConfig? config);
8579
void set_chain_source_electrum(string server_url, ElectrumSyncConfig? config);
86-
void set_chain_source_bitcoind(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password, BitcoindSyncClientConfig? sync_client_config);
80+
void set_chain_source_bitcoind_rpc(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
81+
void set_chain_source_bitcoind_rest(string rest_host, u16 rest_port);
8782
void set_gossip_source_p2p();
8883
void set_gossip_source_rgs(string rgs_server_url);
8984
void set_liquidity_source_lsps1(PublicKey node_id, SocketAddress address, string? token);

src/builder.rs

+77-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, may_announce_channel, AnnounceError, BitcoindSyncClientConfig, Config,
10+
default_user_config, may_announce_channel, AnnounceError, BitcoindRestSyncClientConfig, Config,
1111
ElectrumSyncConfig, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
1212
WALLET_KEYS_SEED_LEN,
1313
};
@@ -96,7 +96,7 @@ enum ChainDataSourceConfig {
9696
rpc_port: u16,
9797
rpc_user: String,
9898
rpc_password: String,
99-
sync_client_config: BitcoindSyncClientConfig,
99+
sync_client_config: Option<BitcoindRestSyncClientConfig>,
100100
},
101101
}
102102

@@ -310,29 +310,70 @@ impl NodeBuilder {
310310
self
311311
}
312312

313-
/// Configures the [`Node`] instance to synchronize its chain data from the given Bitcoin Core RPC
314-
/// endpoint.
313+
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
315314
///
316-
/// This method configures an RPC connection for essential operations, with options for
317-
/// synchronization via either RPC (default) or REST.
315+
/// This method establishes an RPC connection that enables all essential chain operations including
316+
/// transaction broadcasting and chain data synchronization. RPC is the minimum required configuration
317+
/// for Bitcoin Core chain interaction and must be set up before any other Bitcoin Core connection options.
318318
///
319-
/// # Parameters:
320-
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC connection
321-
/// * `sync_client_config` - Optional synchronization client configuration; defaults to using RPC for sync
322-
pub fn set_chain_source_bitcoind(
319+
/// ## Parameters:
320+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
321+
/// connection.
322+
pub fn set_chain_source_bitcoind_rpc(
323323
&mut self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
324-
sync_client_config: Option<BitcoindSyncClientConfig>,
325324
) -> &mut Self {
326325
self.chain_data_source_config = Some(ChainDataSourceConfig::Bitcoind {
327326
rpc_host,
328327
rpc_port,
329328
rpc_user,
330329
rpc_password,
331-
sync_client_config: sync_client_config.unwrap_or(BitcoindSyncClientConfig::Rpc),
330+
sync_client_config: None,
332331
});
333332
self
334333
}
335334

335+
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
336+
///
337+
/// This method enables chain data synchronization via Bitcoin Core's REST interface.
338+
/// It must be called after [`Self::set_chain_source_bitcoind_rpc`] because REST is used only for chain
339+
/// synchronization, while RPC is still required for other essential operations like transaction
340+
/// broadcasting.
341+
///
342+
/// ## Parameters:
343+
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
344+
pub fn set_chain_source_bitcoind_rest(
345+
&mut self, rest_host: String, rest_port: u16,
346+
) -> &mut Self {
347+
let rest_client_config = BitcoindRestSyncClientConfig { rest_host, rest_port };
348+
349+
match &mut self.chain_data_source_config {
350+
Some(ChainDataSourceConfig::Bitcoind {
351+
rpc_host,
352+
rpc_port,
353+
rpc_user,
354+
rpc_password,
355+
sync_client_config,
356+
}) => {
357+
if rpc_host.is_empty()
358+
|| *rpc_port == 0
359+
|| rpc_user.is_empty()
360+
|| rpc_password.is_empty()
361+
{
362+
panic!("RPC configuration is incomplete. Must fully configure via RPC first.");
363+
}
364+
365+
*sync_client_config = Some(rest_client_config);
366+
},
367+
Some(cs) => {
368+
panic!("This option is only valid for ChainSource::Bitcoind. Current chain source config: {cs:?}")
369+
},
370+
None => {
371+
panic!("Must configure via RPC first.")
372+
},
373+
}
374+
375+
self
376+
}
336377
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
337378
/// network.
338379
pub fn set_gossip_source_p2p(&mut self) -> &mut Self {
@@ -740,21 +781,39 @@ impl ArcedNodeBuilder {
740781
self.inner.write().unwrap().set_chain_source_electrum(server_url, sync_config);
741782
}
742783

743-
/// Configures the [`Node`] instance to source its chain data from the given Bitcoin Core RPC
744-
/// endpoint.
745-
pub fn set_chain_source_bitcoind(
784+
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
785+
///
786+
/// This method establishes an RPC connection that enables all essential chain operations including
787+
/// transaction broadcasting and chain data synchronization. RPC is the minimum required configuration
788+
/// for Bitcoin Core chain interactions and must be set up before any other Bitcoin Core connection options.
789+
///
790+
/// ## Parameters:
791+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
792+
/// connection.
793+
pub fn set_chain_source_bitcoind_rpc(
746794
&self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
747-
sync_client_config: Option<BitcoindSyncClientConfig>,
748795
) {
749-
self.inner.write().unwrap().set_chain_source_bitcoind(
796+
self.inner.write().unwrap().set_chain_source_bitcoind_rpc(
750797
rpc_host,
751798
rpc_port,
752799
rpc_user,
753800
rpc_password,
754-
sync_client_config,
755801
);
756802
}
757803

804+
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
805+
///
806+
/// This method enables chain data synchronization via Bitcoin Core's REST interface.
807+
/// It must be called after [`set_chain_source_bitcoind_rpc`] because REST is used only for chain
808+
/// synchronization, while RPC is still required for other essential operations like transaction
809+
/// broadcasting.
810+
///
811+
/// ## Parameters:
812+
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
813+
pub fn set_chain_source_bitcoind_rest(&self, rest_host: String, rest_port: u16) {
814+
self.inner.write().unwrap().set_chain_source_bitcoind_rest(rest_host, rest_port);
815+
}
816+
758817
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
759818
/// network.
760819
pub fn set_gossip_source_p2p(&self) {

src/chain/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::chain::bitcoind_rpc::{
1313
};
1414
use crate::chain::electrum::ElectrumRuntimeClient;
1515
use crate::config::{
16-
BackgroundSyncConfig, BitcoindSyncClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig,
17-
BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, BDK_WALLET_SYNC_TIMEOUT_SECS,
16+
BackgroundSyncConfig, BitcoindRestSyncClientConfig, Config, ElectrumSyncConfig,
17+
EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, BDK_WALLET_SYNC_TIMEOUT_SECS,
1818
FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS,
1919
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL, TX_BROADCAST_TIMEOUT_SECS,
2020
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
@@ -340,7 +340,7 @@ impl ChainSource {
340340
host: String, port: u16, rpc_user: String, rpc_password: String,
341341
onchain_wallet: Arc<Wallet>, fee_estimator: Arc<OnchainFeeEstimator>,
342342
tx_broadcaster: Arc<Broadcaster>, kv_store: Arc<DynStore>, config: Arc<Config>,
343-
sync_client_config: BitcoindSyncClientConfig, logger: Arc<Logger>,
343+
sync_client_config: Option<BitcoindRestSyncClientConfig>, logger: Arc<Logger>,
344344
node_metrics: Arc<RwLock<NodeMetrics>>,
345345
) -> Self {
346346
let bitcoind_rpc_client = Arc::new(BitcoindRpcClient::new(
@@ -350,15 +350,15 @@ impl ChainSource {
350350
rpc_password.clone(),
351351
));
352352
let bitcoind_sync_client = match sync_client_config {
353-
BitcoindSyncClientConfig::Rpc => Arc::new(BitcoindSyncClient::Rpc(RpcClient::new(
354-
&rpc_credentials(rpc_user, rpc_password),
355-
endpoint(host, port),
356-
))),
357-
BitcoindSyncClientConfig::Rest { rest_host, rest_port } => {
353+
Some(BitcoindRestSyncClientConfig { rest_host, rest_port }) => {
358354
Arc::new(BitcoindSyncClient::Rest(RestClient::new(
359355
endpoint(rest_host, rest_port).with_path("/rest".to_string()),
360356
)))
361357
},
358+
None => Arc::new(BitcoindSyncClient::Rpc(RpcClient::new(
359+
&rpc_credentials(rpc_user, rpc_password),
360+
endpoint(host, port),
361+
))),
362362
};
363363

364364
let header_cache = tokio::sync::Mutex::new(BoundedHeaderCache::new());

src/config.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -397,18 +397,13 @@ impl Default for ElectrumSyncConfig {
397397
}
398398
}
399399

400-
/// Configuration for syncing with Bitcoin Core backend.
400+
/// Configuration for syncing with Bitcoin Core backend via REST.
401401
#[derive(Debug, Clone)]
402-
pub enum BitcoindSyncClientConfig {
403-
/// Syncs via RPC.
404-
Rpc,
405-
/// Syncs via REST.
406-
Rest {
407-
/// Host URL.
408-
rest_host: String,
409-
/// Host port.
410-
rest_port: u16,
411-
},
402+
pub struct BitcoindRestSyncClientConfig {
403+
/// Host URL.
404+
pub rest_host: String,
405+
/// Host port.
406+
pub rest_port: u16,
412407
}
413408

414409
/// Options which apply on a per-channel basis and may change at runtime or based on negotiation

src/uniffi_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// Make sure to add any re-exported items that need to be used in uniffi below.
1212

1313
pub use crate::config::{
14-
default_config, AnchorChannelsConfig, BackgroundSyncConfig, BitcoindSyncClientConfig,
15-
ElectrumSyncConfig, EsploraSyncConfig, MaxDustHTLCExposure,
14+
default_config, AnchorChannelsConfig, BackgroundSyncConfig, ElectrumSyncConfig,
15+
EsploraSyncConfig, MaxDustHTLCExposure,
1616
};
1717
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
1818
pub use crate::liquidity::{LSPS1OrderStatus, LSPS2ServiceConfig, OnchainPaymentInfo, PaymentInfo};

tests/common/mod.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) mod logging;
1212

1313
use logging::TestLogWriter;
1414

15-
use ldk_node::config::{BitcoindSyncClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig};
15+
use ldk_node::config::{Config, ElectrumSyncConfig, EsploraSyncConfig};
1616
use ldk_node::io::sqlite_store::SqliteStore;
1717
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
1818
use ldk_node::{
@@ -325,23 +325,19 @@ pub(crate) fn setup_node(
325325
let values = bitcoind.params.get_cookie_values().unwrap().unwrap();
326326
let rpc_user = values.user;
327327
let rpc_password = values.password;
328-
builder.set_chain_source_bitcoind(rpc_host, rpc_port, rpc_user, rpc_password, None);
328+
builder.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password);
329329
},
330330
TestChainSource::BitcoindRestSync(bitcoind) => {
331331
let rpc_host = bitcoind.params.rpc_socket.ip().to_string();
332332
let rpc_port = bitcoind.params.rpc_socket.port();
333333
let values = bitcoind.params.get_cookie_values().unwrap().unwrap();
334334
let rpc_user = values.user;
335335
let rpc_password = values.password;
336-
let sync_client_config =
337-
BitcoindSyncClientConfig::Rest { rest_host: rpc_host.clone(), rest_port: rpc_port };
338-
builder.set_chain_source_bitcoind(
339-
rpc_host,
340-
rpc_port,
341-
rpc_user,
342-
rpc_password,
343-
Some(sync_client_config),
344-
);
336+
let rest_host = bitcoind.params.rpc_socket.ip().to_string();
337+
let rest_port = bitcoind.params.rpc_socket.port();
338+
builder
339+
.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password)
340+
.set_chain_source_bitcoind_rest(rest_host, rest_port);
345341
},
346342
}
347343

0 commit comments

Comments
 (0)