Skip to content

Commit 7ff3c7e

Browse files
committed
Add builders' stub for bip157 sync
Introduce new backend for syncing via compact filters (bip157). Create new chain source which start kyoto under the hood. Fees retrieval has two options: either esplora/electrum or a rough estimate via the latest block from kyoto. Test bitcoind node is run with filter flags.
1 parent fae2746 commit 7ff3c7e

8 files changed

Lines changed: 701 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ serde_json = { version = "1.0.128", default-features = false, features = ["std"]
7676
log = { version = "0.4.22", default-features = false, features = ["std"]}
7777

7878
async-trait = { version = "0.1", default-features = false }
79+
bip157 = "0.3.4"
7980
vss-client = { package = "vss-client-ng", version = "0.5" }
8081
prost = { version = "0.11.6", default-features = false}
8182
#bitcoin-payment-instructions = { version = "0.6" }
@@ -91,7 +92,6 @@ proptest = "1.0.0"
9192
regex = "1.5.6"
9293
criterion = { version = "0.7.0", features = ["async_tokio"] }
9394
ldk-node-062 = { package = "ldk-node", version = "=0.6.2" }
94-
9595
[target.'cfg(not(no_download))'.dev-dependencies]
9696
electrsd = { version = "0.36.1", default-features = false, features = ["legacy", "esplora_a33e97e1", "corepc-node_27_2"] }
9797

src/builder.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use lightning::util::sweep::OutputSweeper;
4242
use lightning_persister::fs_store::v1::FilesystemStore;
4343
use vss_client::headers::VssHeaderProvider;
4444

45-
use crate::chain::ChainSource;
45+
use crate::chain::{ChainSource, FeeSourceConfig};
4646
use crate::config::{
4747
default_user_config, may_announce_channel, AnnounceError, AsyncPaymentsRole,
4848
BitcoindRestClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig,
@@ -105,6 +105,9 @@ enum ChainDataSourceConfig {
105105
rpc_password: String,
106106
rest_client_config: Option<BitcoindRestClientConfig>,
107107
},
108+
CompactBlockFilter {
109+
peers: Vec<std::net::SocketAddr>,
110+
},
108111
}
109112

110113
#[derive(Debug, Clone)]
@@ -240,6 +243,7 @@ impl std::error::Error for BuildError {}
240243
pub struct NodeBuilder {
241244
config: Config,
242245
chain_data_source_config: Option<ChainDataSourceConfig>,
246+
fee_source_config: Option<FeeSourceConfig>,
243247
gossip_source_config: Option<GossipSourceConfig>,
244248
liquidity_source_config: Option<LiquiditySourceConfig>,
245249
log_writer_config: Option<LogWriterConfig>,
@@ -259,6 +263,7 @@ impl NodeBuilder {
259263
/// Creates a new builder instance from an [`Config`].
260264
pub fn from_config(config: Config) -> Self {
261265
let chain_data_source_config = None;
266+
let fee_source_config = None;
262267
let gossip_source_config = None;
263268
let liquidity_source_config = None;
264269
let log_writer_config = None;
@@ -268,6 +273,7 @@ impl NodeBuilder {
268273
Self {
269274
config,
270275
chain_data_source_config,
276+
fee_source_config,
271277
gossip_source_config,
272278
liquidity_source_config,
273279
log_writer_config,
@@ -352,6 +358,40 @@ impl NodeBuilder {
352358
self
353359
}
354360

361+
/// Configures the [`Node`] instance to source its chain data via BIP157 compact block filters.
362+
///
363+
/// The given `peers` will be used as seed peers for the compact block filter node. An empty
364+
/// list is valid for standard networks (mainnet, testnet, signet) where DNS seeds are used
365+
/// for peer discovery. For custom networks (e.g. custom signets), at least one peer must be
366+
/// provided.
367+
pub fn set_chain_source_bip157(&mut self, peers: Vec<std::net::SocketAddr>) -> &mut Self {
368+
self.chain_data_source_config =
369+
Some(ChainDataSourceConfig::CompactBlockFilter { peers });
370+
self
371+
}
372+
373+
/// Configures the BIP-157 chain source to use an Esplora server for fee rate estimation.
374+
///
375+
/// By default the BIP-157 backend derives fee rates from the latest block's coinbase output.
376+
/// Setting this provides more accurate, per-target fee estimates from a mempool-aware server.
377+
///
378+
/// Only takes effect when the chain source is set to BIP-157 via [`Self::set_chain_source_bip157`].
379+
pub fn set_fee_source_esplora(&mut self, server_url: String) -> &mut Self {
380+
self.fee_source_config = Some(FeeSourceConfig::Esplora(server_url));
381+
self
382+
}
383+
384+
/// Configures the BIP-157 chain source to use an Electrum server for fee rate estimation.
385+
///
386+
/// By default the BIP-157 backend derives fee rates from the latest block's coinbase output.
387+
/// Setting this provides more accurate, per-target fee estimates from a mempool-aware server.
388+
///
389+
/// Only takes effect when the chain source is set to BIP-157 via [`Self::set_chain_source_bip157`].
390+
pub fn set_fee_source_electrum(&mut self, server_url: String) -> &mut Self {
391+
self.fee_source_config = Some(FeeSourceConfig::Electrum(server_url));
392+
self
393+
}
394+
355395
/// Configures the [`Node`] instance to synchronize chain data from a Bitcoin Core REST endpoint.
356396
///
357397
/// This method enables chain data synchronization via Bitcoin Core's REST interface. We pass
@@ -727,6 +767,7 @@ impl NodeBuilder {
727767
build_with_store_internal(
728768
config,
729769
self.chain_data_source_config.as_ref(),
770+
self.fee_source_config.clone(),
730771
self.gossip_source_config.as_ref(),
731772
self.liquidity_source_config.as_ref(),
732773
self.pathfinding_scores_sync_config.as_ref(),
@@ -847,6 +888,21 @@ impl ArcedNodeBuilder {
847888
);
848889
}
849890

891+
/// Configures the [`Node`] instance to synchronize chain data via BIP-157 compact block filters.
892+
pub fn set_chain_source_bip157(&self, peers: Vec<std::net::SocketAddr>) {
893+
self.inner.write().unwrap().set_chain_source_bip157(peers);
894+
}
895+
896+
/// Configures the BIP-157 chain source to use an Esplora server for fee rate estimation.
897+
pub fn set_fee_source_esplora(&self, server_url: String) {
898+
self.inner.write().unwrap().set_fee_source_esplora(server_url);
899+
}
900+
901+
/// Configures the BIP-157 chain source to use an Electrum server for fee rate estimation.
902+
pub fn set_fee_source_electrum(&self, server_url: String) {
903+
self.inner.write().unwrap().set_fee_source_electrum(server_url);
904+
}
905+
850906
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
851907
/// network.
852908
pub fn set_gossip_source_p2p(&self) {
@@ -1121,7 +1177,7 @@ impl ArcedNodeBuilder {
11211177
/// Builds a [`Node`] instance according to the options previously configured.
11221178
fn build_with_store_internal(
11231179
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
1124-
gossip_source_config: Option<&GossipSourceConfig>,
1180+
fee_source_config: Option<FeeSourceConfig>, gossip_source_config: Option<&GossipSourceConfig>,
11251181
liquidity_source_config: Option<&LiquiditySourceConfig>,
11261182
pathfinding_scores_sync_config: Option<&PathfindingScoresSyncConfig>,
11271183
async_payments_role: Option<AsyncPaymentsRole>, recovery_mode: bool, seed_bytes: [u8; 64],
@@ -1254,6 +1310,16 @@ fn build_with_store_internal(
12541310
.await
12551311
}),
12561312
},
1313+
Some(ChainDataSourceConfig::CompactBlockFilter { peers }) => ChainSource::new_kyoto(
1314+
peers.clone(),
1315+
fee_source_config,
1316+
Arc::clone(&fee_estimator),
1317+
Arc::clone(&tx_broadcaster),
1318+
Arc::clone(&kv_store),
1319+
Arc::clone(&config),
1320+
Arc::clone(&logger),
1321+
Arc::clone(&node_metrics),
1322+
),
12571323

12581324
None => {
12591325
// Default to Esplora client.

src/chain/electrum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::types::{ChainMonitor, ChannelManager, DynStore, Sweeper, Wallet};
3737
use crate::NodeMetrics;
3838

3939
const BDK_ELECTRUM_CLIENT_BATCH_SIZE: usize = 5;
40-
const ELECTRUM_CLIENT_NUM_RETRIES: u8 = 3;
40+
pub(crate) const ELECTRUM_CLIENT_NUM_RETRIES: u8 = 3;
4141

4242
pub(super) struct ElectrumChainSource {
4343
server_url: String,

0 commit comments

Comments
 (0)