Skip to content

Commit 693beec

Browse files
committed
Add esplora config
1 parent ae2535f commit 693beec

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-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 = "127.0.0.1:3000"
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(format!("http://{}", 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: 53 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: SocketAddr },
29+
}
30+
2731
impl TryFrom<TomlConfig> for Config {
2832
type Error = io::Error;
2933

@@ -42,13 +46,32 @@ 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 }), _) => {
51+
let server_url = SocketAddr::from_str(&server_url).map_err(|e| {
52+
io::Error::new(
53+
io::ErrorKind::InvalidInput,
54+
format!("Invalid esplora server url configured: {}", e),
55+
)
56+
})?;
57+
ChainSource::Esplora { server_url }
58+
},
59+
(None, Some(BitcoindConfig { rpc_address, rpc_user, rpc_password })) => {
60+
let rpc_address = SocketAddr::from_str(&rpc_address).map_err(|e| {
61+
io::Error::new(
62+
io::ErrorKind::InvalidInput,
63+
format!("Invalid bitcoind RPC address configured: {}", e),
64+
)
65+
})?;
66+
ChainSource::Rpc { rpc_address, rpc_user, rpc_password }
67+
},
68+
(None, None) => {
69+
return Err(io::Error::new(
4870
io::ErrorKind::InvalidInput,
49-
format!("Invalid bitcoind RPC address configured: {}", e),
50-
)
51-
})?;
71+
format!("At least one of esplora or rpc config must be set"),
72+
))
73+
},
74+
};
5275

5376
let alias = if let Some(alias_str) = toml_config.node.alias {
5477
let mut bytes = [0u8; 32];
@@ -97,9 +120,7 @@ impl TryFrom<TomlConfig> for Config {
97120
alias,
98121
rest_service_addr,
99122
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,
123+
chain_source,
103124
rabbitmq_connection_string,
104125
rabbitmq_exchange_name,
105126
lsps2_service_config,
@@ -112,7 +133,8 @@ impl TryFrom<TomlConfig> for Config {
112133
pub struct TomlConfig {
113134
node: NodeConfig,
114135
storage: StorageConfig,
115-
bitcoind: BitcoindConfig,
136+
bitcoind: Option<BitcoindConfig>,
137+
esplora: Option<EsploraConfig>,
116138
rabbitmq: Option<RabbitmqConfig>,
117139
liquidity: Option<LiquidityConfig>,
118140
}
@@ -142,6 +164,11 @@ struct BitcoindConfig {
142164
rpc_password: String,
143165
}
144166

167+
#[derive(Deserialize, Serialize)]
168+
struct EsploraConfig {
169+
server_url: String,
170+
}
171+
145172
#[derive(Deserialize, Serialize)]
146173
struct RabbitmqConfig {
147174
connection_string: String,
@@ -237,6 +264,9 @@ mod tests {
237264
rpc_address = "127.0.0.1:8332" # RPC endpoint
238265
rpc_user = "bitcoind-testuser"
239266
rpc_password = "bitcoind-testpassword"
267+
268+
[esplora]
269+
server_url = "127.0.0.1:3000"
240270
241271
[rabbitmq]
242272
connection_string = "rabbitmq_connection_string"
@@ -266,9 +296,9 @@ mod tests {
266296
network: Network::Regtest,
267297
rest_service_addr: SocketAddr::from_str("127.0.0.1:3002").unwrap(),
268298
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(),
299+
chain_source: ChainSource::Esplora {
300+
server_url: SocketAddr::from_str("127.0.0.1:3000").unwrap(),
301+
},
272302
rabbitmq_connection_string: "rabbitmq_connection_string".to_string(),
273303
rabbitmq_exchange_name: "rabbitmq_exchange_name".to_string(),
274304
lsps2_service_config: Some(LSPS2ServiceConfig {
@@ -288,9 +318,13 @@ mod tests {
288318
assert_eq!(config.network, expected.network);
289319
assert_eq!(config.rest_service_addr, expected.rest_service_addr);
290320
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);
321+
let ChainSource::Esplora { server_url } = config.chain_source else {
322+
panic!("unexpected config chain source");
323+
};
324+
let ChainSource::Esplora { server_url: expected_server_url } = expected.chain_source else {
325+
panic!("unexpected chain source");
326+
};
327+
assert_eq!(server_url, expected_server_url);
294328
assert_eq!(config.rabbitmq_connection_string, expected.rabbitmq_connection_string);
295329
assert_eq!(config.rabbitmq_exchange_name, expected.rabbitmq_exchange_name);
296330
#[cfg(feature = "experimental-lsps2-support")]

0 commit comments

Comments
 (0)