Skip to content

Commit 0509e99

Browse files
authored
Merge pull request #526 from enigbe/2025-04-bitcoind-rest-sync-v2
feat: chain source rest sync
2 parents 3385495 + 36e4c7f commit 0509e99

File tree

10 files changed

+621
-112
lines changed

10 files changed

+621
-112
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ lightning-net-tokio = { version = "0.1.0" }
3535
lightning-persister = { version = "0.1.0" }
3636
lightning-background-processor = { version = "0.1.0", features = ["futures"] }
3737
lightning-rapid-gossip-sync = { version = "0.1.0" }
38-
lightning-block-sync = { version = "0.1.0", features = ["rpc-client", "tokio"] }
38+
lightning-block-sync = { version = "0.1.0", features = ["rpc-client", "rest-client", "tokio"] }
3939
lightning-transaction-sync = { version = "0.1.0", features = ["esplora-async-https", "time", "electrum"] }
4040
lightning-liquidity = { version = "0.1.0", features = ["std"] }
4141

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ interface Builder {
7878
void set_chain_source_esplora(string server_url, EsploraSyncConfig? config);
7979
void set_chain_source_electrum(string server_url, ElectrumSyncConfig? config);
8080
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, string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
8182
void set_gossip_source_p2p();
8283
void set_gossip_source_rgs(string rgs_server_url);
8384
void set_liquidity_source_lsps1(PublicKey node_id, SocketAddress address, string? token);

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ services:
1313
"-rpcbind=0.0.0.0",
1414
"-rpcuser=user",
1515
"-rpcpassword=pass",
16-
"-fallbackfee=0.00001"
16+
"-fallbackfee=0.00001",
17+
"-rest"
1718
]
1819
ports:
19-
- "18443:18443" # Regtest RPC port
20+
- "18443:18443" # Regtest REST and RPC port
2021
- "18444:18444" # Regtest P2P port
2122
networks:
2223
- bitcoin-electrs

src/builder.rs

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, may_announce_channel, AnnounceError, Config, ElectrumSyncConfig,
11-
EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
10+
default_user_config, may_announce_channel, AnnounceError, BitcoindRestClientConfig, Config,
11+
ElectrumSyncConfig, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
12+
WALLET_KEYS_SEED_LEN,
1213
};
1314

1415
use crate::connection::ConnectionManager;
@@ -84,9 +85,21 @@ const LSPS_HARDENED_CHILD_INDEX: u32 = 577;
8485

8586
#[derive(Debug, Clone)]
8687
enum ChainDataSourceConfig {
87-
Esplora { server_url: String, sync_config: Option<EsploraSyncConfig> },
88-
Electrum { server_url: String, sync_config: Option<ElectrumSyncConfig> },
89-
BitcoindRpc { rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String },
88+
Esplora {
89+
server_url: String,
90+
sync_config: Option<EsploraSyncConfig>,
91+
},
92+
Electrum {
93+
server_url: String,
94+
sync_config: Option<ElectrumSyncConfig>,
95+
},
96+
Bitcoind {
97+
rpc_host: String,
98+
rpc_port: u16,
99+
rpc_user: String,
100+
rpc_password: String,
101+
rest_client_config: Option<BitcoindRestClientConfig>,
102+
},
90103
}
91104

92105
#[derive(Debug, Clone)]
@@ -299,13 +312,48 @@ impl NodeBuilder {
299312
self
300313
}
301314

302-
/// Configures the [`Node`] instance to source its chain data from the given Bitcoin Core RPC
303-
/// endpoint.
315+
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
316+
///
317+
/// This method establishes an RPC connection that enables all essential chain operations including
318+
/// transaction broadcasting and chain data synchronization.
319+
///
320+
/// ## Parameters:
321+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
322+
/// connection.
304323
pub fn set_chain_source_bitcoind_rpc(
305324
&mut self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
306325
) -> &mut Self {
307-
self.chain_data_source_config =
308-
Some(ChainDataSourceConfig::BitcoindRpc { rpc_host, rpc_port, rpc_user, rpc_password });
326+
self.chain_data_source_config = Some(ChainDataSourceConfig::Bitcoind {
327+
rpc_host,
328+
rpc_port,
329+
rpc_user,
330+
rpc_password,
331+
rest_client_config: None,
332+
});
333+
self
334+
}
335+
336+
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
337+
///
338+
/// This method enables chain data synchronization via Bitcoin Core's REST interface. We pass
339+
/// additional RPC configuration to non-REST-supported API calls like transaction broadcasting.
340+
///
341+
/// ## Parameters:
342+
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
343+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
344+
/// connection
345+
pub fn set_chain_source_bitcoind_rest(
346+
&mut self, rest_host: String, rest_port: u16, rpc_host: String, rpc_port: u16,
347+
rpc_user: String, rpc_password: String,
348+
) -> &mut Self {
349+
self.chain_data_source_config = Some(ChainDataSourceConfig::Bitcoind {
350+
rpc_host,
351+
rpc_port,
352+
rpc_user,
353+
rpc_password,
354+
rest_client_config: Some(BitcoindRestClientConfig { rest_host, rest_port }),
355+
});
356+
309357
self
310358
}
311359

@@ -716,8 +764,14 @@ impl ArcedNodeBuilder {
716764
self.inner.write().unwrap().set_chain_source_electrum(server_url, sync_config);
717765
}
718766

719-
/// Configures the [`Node`] instance to source its chain data from the given Bitcoin Core RPC
720-
/// endpoint.
767+
/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
768+
///
769+
/// This method establishes an RPC connection that enables all essential chain operations including
770+
/// transaction broadcasting and chain data synchronization.
771+
///
772+
/// ## Parameters:
773+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
774+
/// connection.
721775
pub fn set_chain_source_bitcoind_rpc(
722776
&self, rpc_host: String, rpc_port: u16, rpc_user: String, rpc_password: String,
723777
) {
@@ -729,6 +783,29 @@ impl ArcedNodeBuilder {
729783
);
730784
}
731785

786+
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
787+
///
788+
/// This method enables chain data synchronization via Bitcoin Core's REST interface. We pass
789+
/// additional RPC configuration to non-REST-supported API calls like transaction broadcasting.
790+
///
791+
/// ## Parameters:
792+
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
793+
/// * `rpc_host`, `rpc_port`, `rpc_user`, `rpc_password` - Required parameters for the Bitcoin Core RPC
794+
/// connection
795+
pub fn set_chain_source_bitcoind_rest(
796+
&self, rest_host: String, rest_port: u16, rpc_host: String, rpc_port: u16,
797+
rpc_user: String, rpc_password: String,
798+
) {
799+
self.inner.write().unwrap().set_chain_source_bitcoind_rest(
800+
rest_host,
801+
rest_port,
802+
rpc_host,
803+
rpc_port,
804+
rpc_user,
805+
rpc_password,
806+
);
807+
}
808+
732809
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
733810
/// network.
734811
pub fn set_gossip_source_p2p(&self) {
@@ -1068,8 +1145,14 @@ fn build_with_store_internal(
10681145
Arc::clone(&node_metrics),
10691146
))
10701147
},
1071-
Some(ChainDataSourceConfig::BitcoindRpc { rpc_host, rpc_port, rpc_user, rpc_password }) => {
1072-
Arc::new(ChainSource::new_bitcoind_rpc(
1148+
Some(ChainDataSourceConfig::Bitcoind {
1149+
rpc_host,
1150+
rpc_port,
1151+
rpc_user,
1152+
rpc_password,
1153+
rest_client_config,
1154+
}) => match rest_client_config {
1155+
Some(rest_client_config) => Arc::new(ChainSource::new_bitcoind_rest(
10731156
rpc_host.clone(),
10741157
*rpc_port,
10751158
rpc_user.clone(),
@@ -1079,10 +1162,25 @@ fn build_with_store_internal(
10791162
Arc::clone(&tx_broadcaster),
10801163
Arc::clone(&kv_store),
10811164
Arc::clone(&config),
1165+
rest_client_config.clone(),
10821166
Arc::clone(&logger),
10831167
Arc::clone(&node_metrics),
1084-
))
1168+
)),
1169+
None => Arc::new(ChainSource::new_bitcoind_rpc(
1170+
rpc_host.clone(),
1171+
*rpc_port,
1172+
rpc_user.clone(),
1173+
rpc_password.clone(),
1174+
Arc::clone(&wallet),
1175+
Arc::clone(&fee_estimator),
1176+
Arc::clone(&tx_broadcaster),
1177+
Arc::clone(&kv_store),
1178+
Arc::clone(&config),
1179+
Arc::clone(&logger),
1180+
Arc::clone(&node_metrics),
1181+
)),
10851182
},
1183+
10861184
None => {
10871185
// Default to Esplora client.
10881186
let server_url = DEFAULT_ESPLORA_SERVER_URL.to_string();

0 commit comments

Comments
 (0)