Skip to content
Open
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ default = [
"chain-esplora",
"chain-electrum",
"chain-bitcoind",
"chain-cbf",
"storage-sqlite",
"storage-filesystem",
"storage-vss",
Expand All @@ -52,6 +53,7 @@ chain-electrum = [
"lightning-transaction-sync/electrum-rustls-ring",
]
chain-bitcoind = ["dep:lightning-block-sync"]
chain-cbf = ["dep:bip157", "chain-esplora", "chain-electrum"]
storage-sqlite = ["dep:rusqlite"]
storage-filesystem = ["dep:lightning-persister"]
storage-vss = ["dep:vss-client", "dep:prost"]
Expand Down Expand Up @@ -103,6 +105,7 @@ bdk_chain = { version = "0.23.3", default-features = false, features = ["std"] }
bdk_esplora = { version = "0.22.2", default-features = false, features = ["async-https-rustls", "tokio"], optional = true }
bdk_electrum = { version = "0.24.0", default-features = false, features = ["use-rustls-ring"], optional = true }
bdk_wallet = { version = "3.1.0", default-features = false, features = ["std", "keys-bip39"]}
bip157 = { version = "0.6.3", default-features = false, optional = true }

bitreq = { version = "0.3", default-features = false, features = ["async-https", "json-using-serde"] }
rustls = { version = "0.23", default-features = false }
Expand Down
34 changes: 34 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ use lightning_dns_resolver::OMDomainResolver;
use vss_client::headers::VssHeaderProvider;

use crate::chain::ChainSource;
#[cfg(feature = "chain-cbf")]
use crate::chain::CbfFeeSourceConfig;
#[cfg(feature = "chain-bitcoind")]
use crate::config::BitcoindRestClientConfig;
use crate::config::{
Expand Down Expand Up @@ -131,6 +133,11 @@ enum ChainDataSourceConfig {
rest_client_config: Option<BitcoindRestClientConfig>,
wallet_rescan_from_height: Option<u32>,
},
#[cfg(feature = "chain-cbf")]
Cbf {
peers: Vec<String>,
fee_source_config: Option<CbfFeeSourceConfig>,
},
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -421,6 +428,20 @@ impl NodeBuilder {
self
}

/// Configures the [`Node`] instance to source chain data via compact block filters
/// (BIP157/BIP158), connecting to the given peers (`ip:port`).
///
/// `fee_source_config` optionally delegates fee estimation to an Esplora or Electrum server;
/// if `None`, fee rates are derived from recent blocks.
#[cfg(feature = "chain-cbf")]
pub fn set_chain_source_cbf(
&mut self, peers: Vec<String>, fee_source_config: Option<CbfFeeSourceConfig>,
) -> &mut Self {
self.chain_data_source_config =
Some(ChainDataSourceConfig::Cbf { peers, fee_source_config });
self
}

/// Configures the [`Node`] instance to connect to a Bitcoin Core node via RPC.
///
/// This method establishes an RPC connection that enables all essential chain operations including
Expand Down Expand Up @@ -1609,6 +1630,19 @@ fn build_with_store_internal(
Arc::clone(&node_metrics),
)
},
#[cfg(feature = "chain-cbf")]
Some(ChainDataSourceConfig::Cbf { peers, fee_source_config }) => ChainSource::new_cbf(
peers.clone(),
fee_source_config.clone(),
Arc::clone(&runtime),
Arc::clone(&fee_estimator),
Arc::clone(&tx_broadcaster),
Arc::clone(&kv_store),
Arc::clone(&config),
Arc::clone(&logger),
Arc::clone(&node_metrics),
)
.map_err(|_| BuildError::ChainSourceSetupFailed)?,
#[cfg(feature = "chain-bitcoind")]
Some(ChainDataSourceConfig::Bitcoind {
rpc_host,
Expand Down
59 changes: 1 addition & 58 deletions src/chain/bitcoind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use lightning_block_sync::{
};
use serde::Serialize;

use super::{WalletSyncGuard, WalletSyncStatus};
use super::{ChainListener, WalletSyncGuard, WalletSyncStatus};
use crate::config::{
BitcoindRestClientConfig, Config, DEFAULT_FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS,
DEFAULT_TX_BROADCAST_TIMEOUT_SECS,
Expand Down Expand Up @@ -1534,63 +1534,6 @@ pub(crate) enum FeeRateEstimationMode {
Conservative,
}

pub(crate) struct ChainListener {
pub(crate) onchain_wallet: std::sync::Weak<Wallet>,
pub(crate) channel_manager: std::sync::Weak<ChannelManager>,
pub(crate) chain_monitor: std::sync::Weak<ChainMonitor>,
pub(crate) output_sweeper: std::sync::Weak<Sweeper>,
}

impl ChainListener {
fn upgrade(
&self,
) -> Option<(Arc<Wallet>, Arc<ChannelManager>, Arc<ChainMonitor>, Arc<Sweeper>)> {
Some((
self.onchain_wallet.upgrade()?,
self.channel_manager.upgrade()?,
self.chain_monitor.upgrade()?,
self.output_sweeper.upgrade()?,
))
}
}

impl Listen for ChainListener {
fn filtered_block_connected(
&self, header: &bitcoin::block::Header,
txdata: &lightning::chain::transaction::TransactionData, height: u32,
) {
if let Some((onchain_wallet, channel_manager, chain_monitor, output_sweeper)) =
self.upgrade()
{
onchain_wallet.filtered_block_connected(header, txdata, height);
channel_manager.filtered_block_connected(header, txdata, height);
chain_monitor.filtered_block_connected(header, txdata, height);
output_sweeper.filtered_block_connected(header, txdata, height);
}
}
fn block_connected(&self, block: &bitcoin::Block, height: u32) {
if let Some((onchain_wallet, channel_manager, chain_monitor, output_sweeper)) =
self.upgrade()
{
onchain_wallet.block_connected(block, height);
channel_manager.block_connected(block, height);
chain_monitor.block_connected(block, height);
output_sweeper.block_connected(block, height);
}
}

fn blocks_disconnected(&self, fork_point_block: lightning::chain::BlockLocator) {
if let Some((onchain_wallet, channel_manager, chain_monitor, output_sweeper)) =
self.upgrade()
{
onchain_wallet.blocks_disconnected(fork_point_block);
channel_manager.blocks_disconnected(fork_point_block);
chain_monitor.blocks_disconnected(fork_point_block);
output_sweeper.blocks_disconnected(fork_point_block);
}
}
}

pub(crate) fn rpc_credentials(rpc_user: String, rpc_password: String) -> String {
BASE64_STANDARD.encode(format!("{}:{}", rpc_user, rpc_password))
}
Expand Down
Loading
Loading