Skip to content

Commit 385c18a

Browse files
committed
Add esplora config
1 parent ae2535f commit 385c18a

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

ldk-server/ldk-server-config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ rest_service_address = "127.0.0.1:3002" # LDK Server REST address
88
[storage.disk]
99
dir_path = "/tmp/ldk-server/" # Path for LDK and BDK data persistence
1010

11+
12+
# Must set either the bitcoind or the esplora table; the esplora table is prioritized over the bitcoind table
13+
1114
# Bitcoin Core settings
1215
[bitcoind]
1316
rpc_address = "127.0.0.1:18444" # RPC endpoint
1417
rpc_user = "polaruser" # RPC username
1518
rpc_password = "polarpass" # RPC password
1619

20+
[esplora]
21+
server_url = "https://mempool.space/api"
22+
1723
# RabbitMQ settings (only required if using events-rabbitmq feature)
1824
[rabbitmq]
1925
connection_string = "" # RabbitMQ connection string

ldk-server/src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::io::persist::{
2424
FORWARDED_PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE, PAYMENTS_PERSISTENCE_PRIMARY_NAMESPACE,
2525
PAYMENTS_PERSISTENCE_SECONDARY_NAMESPACE,
2626
};
27-
use crate::util::config::load_config;
27+
use crate::util::config::{load_config, ChainSource};
2828
use crate::util::proto_adapter::{forwarded_payment_to_proto, payment_to_proto};
2929
use hex::DisplayHex;
3030
use ldk_node::config::Config;
@@ -79,14 +79,19 @@ fn main() {
7979
let mut builder = Builder::from_config(ldk_node_config);
8080
builder.set_log_facade_logger();
8181

82-
let bitcoind_rpc_addr = config_file.bitcoind_rpc_addr;
83-
84-
builder.set_chain_source_bitcoind_rpc(
85-
bitcoind_rpc_addr.ip().to_string(),
86-
bitcoind_rpc_addr.port(),
87-
config_file.bitcoind_rpc_user,
88-
config_file.bitcoind_rpc_password,
89-
);
82+
match config_file.chain_source {
83+
ChainSource::Rpc { rpc_address, rpc_user, rpc_password } => {
84+
builder.set_chain_source_bitcoind_rpc(
85+
rpc_address.ip().to_string(),
86+
rpc_address.port(),
87+
rpc_user,
88+
rpc_password,
89+
);
90+
},
91+
ChainSource::Esplora { server_url } => {
92+
builder.set_chain_source_esplora(server_url, None);
93+
},
94+
}
9095

9196
// LSPS2 support is highly experimental and for testing purposes only.
9297
#[cfg(feature = "experimental-lsps2-support")]

ldk-server/src/util/config.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ pub struct Config {
1616
pub network: Network,
1717
pub rest_service_addr: SocketAddr,
1818
pub storage_dir_path: String,
19-
pub bitcoind_rpc_addr: SocketAddr,
20-
pub bitcoind_rpc_user: String,
21-
pub bitcoind_rpc_password: String,
19+
pub chain_source: ChainSource,
2220
pub rabbitmq_connection_string: String,
2321
pub rabbitmq_exchange_name: String,
2422
pub lsps2_service_config: Option<LSPS2ServiceConfig>,
2523
}
2624

25+
#[derive(Debug)]
26+
pub enum ChainSource {
27+
Rpc { rpc_address: SocketAddr, rpc_user: String, rpc_password: String },
28+
Esplora { server_url: String },
29+
}
30+
2731
impl TryFrom<TomlConfig> for Config {
2832
type Error = io::Error;
2933

@@ -42,13 +46,24 @@ impl TryFrom<TomlConfig> for Config {
4246
format!("Invalid rest service address configured: {}", e),
4347
)
4448
})?;
45-
let bitcoind_rpc_addr =
46-
SocketAddr::from_str(&toml_config.bitcoind.rpc_address).map_err(|e| {
47-
io::Error::new(
49+
let chain_source = match (toml_config.esplora, toml_config.bitcoind) {
50+
(Some(EsploraConfig { server_url }), _) => ChainSource::Esplora { server_url },
51+
(None, Some(BitcoindConfig { rpc_address, rpc_user, rpc_password })) => {
52+
let rpc_address = SocketAddr::from_str(&rpc_address).map_err(|e| {
53+
io::Error::new(
54+
io::ErrorKind::InvalidInput,
55+
format!("Invalid bitcoind RPC address configured: {}", e),
56+
)
57+
})?;
58+
ChainSource::Rpc { rpc_address, rpc_user, rpc_password }
59+
},
60+
(None, None) => {
61+
return Err(io::Error::new(
4862
io::ErrorKind::InvalidInput,
49-
format!("Invalid bitcoind RPC address configured: {}", e),
50-
)
51-
})?;
63+
format!("At least one of esplora or rpc config must be set"),
64+
))
65+
},
66+
};
5267

5368
let alias = if let Some(alias_str) = toml_config.node.alias {
5469
let mut bytes = [0u8; 32];
@@ -97,9 +112,7 @@ impl TryFrom<TomlConfig> for Config {
97112
alias,
98113
rest_service_addr,
99114
storage_dir_path: toml_config.storage.disk.dir_path,
100-
bitcoind_rpc_addr,
101-
bitcoind_rpc_user: toml_config.bitcoind.rpc_user,
102-
bitcoind_rpc_password: toml_config.bitcoind.rpc_password,
115+
chain_source,
103116
rabbitmq_connection_string,
104117
rabbitmq_exchange_name,
105118
lsps2_service_config,
@@ -112,7 +125,8 @@ impl TryFrom<TomlConfig> for Config {
112125
pub struct TomlConfig {
113126
node: NodeConfig,
114127
storage: StorageConfig,
115-
bitcoind: BitcoindConfig,
128+
bitcoind: Option<BitcoindConfig>,
129+
esplora: Option<EsploraConfig>,
116130
rabbitmq: Option<RabbitmqConfig>,
117131
liquidity: Option<LiquidityConfig>,
118132
}
@@ -142,6 +156,11 @@ struct BitcoindConfig {
142156
rpc_password: String,
143157
}
144158

159+
#[derive(Deserialize, Serialize)]
160+
struct EsploraConfig {
161+
server_url: String,
162+
}
163+
145164
#[derive(Deserialize, Serialize)]
146165
struct RabbitmqConfig {
147166
connection_string: String,
@@ -237,6 +256,9 @@ mod tests {
237256
rpc_address = "127.0.0.1:8332" # RPC endpoint
238257
rpc_user = "bitcoind-testuser"
239258
rpc_password = "bitcoind-testpassword"
259+
260+
[esplora]
261+
server_url = "https://mempool.space/api"
240262
241263
[rabbitmq]
242264
connection_string = "rabbitmq_connection_string"
@@ -266,9 +288,9 @@ mod tests {
266288
network: Network::Regtest,
267289
rest_service_addr: SocketAddr::from_str("127.0.0.1:3002").unwrap(),
268290
storage_dir_path: "/tmp".to_string(),
269-
bitcoind_rpc_addr: SocketAddr::from_str("127.0.0.1:8332").unwrap(),
270-
bitcoind_rpc_user: "bitcoind-testuser".to_string(),
271-
bitcoind_rpc_password: "bitcoind-testpassword".to_string(),
291+
chain_source: ChainSource::Esplora {
292+
server_url: String::from("https://mempool.space/api"),
293+
},
272294
rabbitmq_connection_string: "rabbitmq_connection_string".to_string(),
273295
rabbitmq_exchange_name: "rabbitmq_exchange_name".to_string(),
274296
lsps2_service_config: Some(LSPS2ServiceConfig {
@@ -288,9 +310,13 @@ mod tests {
288310
assert_eq!(config.network, expected.network);
289311
assert_eq!(config.rest_service_addr, expected.rest_service_addr);
290312
assert_eq!(config.storage_dir_path, expected.storage_dir_path);
291-
assert_eq!(config.bitcoind_rpc_addr, expected.bitcoind_rpc_addr);
292-
assert_eq!(config.bitcoind_rpc_user, expected.bitcoind_rpc_user);
293-
assert_eq!(config.bitcoind_rpc_password, expected.bitcoind_rpc_password);
313+
let ChainSource::Esplora { server_url } = config.chain_source else {
314+
panic!("unexpected config chain source");
315+
};
316+
let ChainSource::Esplora { server_url: expected_server_url } = expected.chain_source else {
317+
panic!("unexpected chain source");
318+
};
319+
assert_eq!(server_url, expected_server_url);
294320
assert_eq!(config.rabbitmq_connection_string, expected.rabbitmq_connection_string);
295321
assert_eq!(config.rabbitmq_exchange_name, expected.rabbitmq_exchange_name);
296322
#[cfg(feature = "experimental-lsps2-support")]

0 commit comments

Comments
 (0)