Skip to content

Commit

Permalink
Add ChannelScheduler and PayjoinHandler to Node
Browse files Browse the repository at this point in the history
...
  • Loading branch information
jbesraa committed May 18, 2024
1 parent 5aef166 commit 8cc8544
Show file tree
Hide file tree
Showing 13 changed files with 1,540 additions and 44 deletions.
38 changes: 27 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ panic = 'abort' # Abort on panic
default = []

[dependencies]
lightning = { version = "0.0.123", features = ["std"] }
lightning-invoice = { version = "0.31.0" }
lightning-net-tokio = { version = "0.0.123" }
lightning-persister = { version = "0.0.123" }
lightning-background-processor = { version = "0.0.123", features = ["futures"] }
lightning-rapid-gossip-sync = { version = "0.0.123" }
lightning-transaction-sync = { version = "0.0.123", features = ["esplora-async-https", "time"] }
lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }

# lightning = { version = "0.0.123", features = ["std"] }
# lightning-invoice = { version = "0.31.0" }
# lightning-net-tokio = { version = "0.0.123" }
# lightning-persister = { version = "0.0.123" }
# lightning-background-processor = { version = "0.0.123", features = ["futures"] }
# lightning-rapid-gossip-sync = { version = "0.0.123" }
# lightning-transaction-sync = { version = "0.0.123", features = ["esplora-async-https", "time"] }
# lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }
lightning = { git = "https://github.com/jbesraa/rust-lightning",rev = "0cb3d57", features = ["std"] }
lightning-invoice = { git = "https://github.com/jbesraa/rust-lightning",rev = "0cb3d57" }
lightning-net-tokio = { git = "https://github.com/jbesraa/rust-lightning",rev = "0cb3d57" }
lightning-persister = { git = "https://github.com/jbesraa/rust-lightning",rev = "0cb3d57" }
lightning-background-processor = { git = "https://github.com/jbesraa/rust-lightning", rev = "0cb3d57", features = ["futures"] }
lightning-rapid-gossip-sync = { git = "https://github.com/jbesraa/rust-lightning", rev = "0cb3d57" }
lightning-transaction-sync = { git = "https://github.com/jbesraa/rust-lightning", rev = "0cb3d57", features = ["esplora-async-https", "time"] }
#lightning-liquidity = { version = "0.1.0-alpha.1", features = ["std"] }

# lightning-liquidity = {path = "../../lightning-liquidity" git = "https://github.com/jbesraa/lightning-liquidity", rev = "b6ac60d", features = ["std"] }
lightning-liquidity = { git = "https://github.com/jbesraa/lightning-liquidity", rev = "02ca30b", features = ["std"] }
# lightning-liquidity = { git = "https://github.com/tnull/lightning-liquidity", rev = "abf7088c0e03221c0f122e797f34802c9e99a3d4", features = ["std"] }

# payjoin = { git = "https://github.com/jbesraa/rust-payjoin.git", rev = "9e4f454", features = ["v2", "receive", "send"] }
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std"] }
#lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
#lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
Expand All @@ -57,7 +70,7 @@ lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }

bdk = { version = "0.29.0", default-features = false, features = ["std", "async-interface", "use-esplora-async", "sqlite-bundled", "keys-bip39"]}

reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls", "blocking"] }
rusqlite = { version = "0.28.0", features = ["bundled"] }
bitcoin = { version = "0.30.2", features = ["bitcoinconsensus"] }
bip39 = "2.0.0"
Expand All @@ -68,6 +81,7 @@ tokio = { version = "1", default-features = false, features = [ "rt-multi-thread
esplora-client = { version = "0.6", default-features = false }
libc = "0.2"
uniffi = { version = "0.26.0", features = ["build"], optional = true }
payjoin = { version = "0.15.0", features = ["v2", "send", "receive"] }

[target.'cfg(vss)'.dependencies]
vss-client = "0.2"
Expand All @@ -77,12 +91,14 @@ prost = { version = "0.11.6", default-features = false}
winapi = { version = "0.3", features = ["winbase"] }

[dev-dependencies]
lightning = { version = "0.0.123", features = ["std", "_test_utils"] }
# lightning = { version = "0.0.123", features = ["std", "_test_utils"] }
lightning = { git = "https://github.com/jbesraa/rust-lightning",rev = "0cb3d57", features = ["std", "_test_utils"] }
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std", "_test_utils"] }
electrum-client = { version = "0.15.1", default-features = true }
bitcoincore-rpc = { version = "0.17.0", default-features = false }
proptest = "1.0.0"
regex = "1.5.6"
reqwest = { version = "0.11", default-features = false, features = ["blocking"] }

[target.'cfg(not(no_download))'.dev-dependencies]
electrsd = { version = "0.26.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_25_0"] }
Expand Down
7 changes: 7 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ enum NodeError {
"InsufficientFunds",
"LiquiditySourceUnavailable",
"LiquidityFeeTooHigh",
"PayjoinReqwest",
"PayjoinValidation",
"PayjoinEnrollment",
"PayjoinUri",
"PayjoinReceiver",
"PayjoinSender",
"BitcoinConsensusFailed",
};

dictionary NodeStatus {
Expand Down
66 changes: 65 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::channel_scheduler::ChannelScheduler;
use crate::config::{
Config, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, DEFAULT_ESPLORA_SERVER_URL,
WALLET_KEYS_SEED_LEN,
Expand All @@ -11,6 +12,7 @@ use crate::io::sqlite_store::SqliteStore;
use crate::liquidity::LiquiditySource;
use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
use crate::message_handler::NodeCustomMessageHandler;
use crate::payjoin_handler::{PayjoinReceiver, PayjoinSender};
use crate::payment::store::PaymentStore;
use crate::peer_store::PeerStore;
use crate::tx_broadcaster::TransactionBroadcaster;
Expand Down Expand Up @@ -94,6 +96,13 @@ struct LiquiditySourceConfig {
lsps2_service: Option<(SocketAddress, PublicKey, Option<String>)>,
}

#[derive(Debug, Clone)]
struct PayjoinConfig {
payjoin_directory: payjoin::Url,
payjoin_relay: payjoin::Url,
ohttp_keys: Option<payjoin::OhttpKeys>,
}

impl Default for LiquiditySourceConfig {
fn default() -> Self {
Self { lsps2_service: None }
Expand Down Expand Up @@ -173,6 +182,7 @@ pub struct NodeBuilder {
chain_data_source_config: Option<ChainDataSourceConfig>,
gossip_source_config: Option<GossipSourceConfig>,
liquidity_source_config: Option<LiquiditySourceConfig>,
payjoin_config: Option<PayjoinConfig>,
}

impl NodeBuilder {
Expand All @@ -188,12 +198,14 @@ impl NodeBuilder {
let chain_data_source_config = None;
let gossip_source_config = None;
let liquidity_source_config = None;
let payjoin_config = None;
Self {
config,
entropy_source_config,
chain_data_source_config,
gossip_source_config,
liquidity_source_config,
payjoin_config,
}
}

Expand Down Expand Up @@ -248,6 +260,16 @@ impl NodeBuilder {
self
}

/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
/// server.
pub fn set_payjoin_config(
&mut self, payjoin_directory: payjoin::Url, payjoin_relay: payjoin::Url,
ohttp_keys: Option<payjoin::OhttpKeys>,
) -> &mut Self {
self.payjoin_config = Some(PayjoinConfig { payjoin_directory, payjoin_relay, ohttp_keys });
self
}

/// Configures the [`Node`] instance to source its inbound liquidity from the given
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
/// service.
Expand Down Expand Up @@ -369,6 +391,7 @@ impl NodeBuilder {
seed_bytes,
logger,
vss_store,
self.payjoin_config.as_ref(),
)
}

Expand All @@ -390,6 +413,7 @@ impl NodeBuilder {
seed_bytes,
logger,
kv_store,
self.payjoin_config.as_ref(),
)
}
}
Expand Down Expand Up @@ -460,6 +484,18 @@ impl ArcedNodeBuilder {
self.inner.write().unwrap().set_gossip_source_rgs(rgs_server_url);
}

/// Configures the [`Node`] instance to use payjoin.
pub fn set_payjoin_config(
&self, payjoin_directory: payjoin::Url, payjoin_relay: payjoin::Url,
ohttp_keys: Option<payjoin::OhttpKeys>,
) {
self.inner.write().unwrap().set_payjoin_config(
payjoin_directory,
payjoin_relay,
ohttp_keys,
);
}

/// Configures the [`Node`] instance to source its inbound liquidity from the given
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
/// service.
Expand Down Expand Up @@ -523,7 +559,7 @@ fn build_with_store_internal(
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
gossip_source_config: Option<&GossipSourceConfig>,
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>,
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>, payjoin_config: Option<&PayjoinConfig>,
) -> Result<Node, BuildError> {
// Initialize the on-chain wallet and chain access
let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes)
Expand Down Expand Up @@ -556,6 +592,7 @@ fn build_with_store_internal(
log_error!(logger, "Failed to set up wallet: {}", e);
BuildError::WalletSetupFailed
})?;
let channel_scheduler = Arc::new(tokio::sync::Mutex::new(ChannelScheduler::new()));

let (blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config {
Some(ChainDataSourceConfig::Esplora(server_url)) => {
Expand All @@ -566,6 +603,7 @@ fn build_with_store_internal(
let tx_broadcaster = Arc::new(TransactionBroadcaster::new(
tx_sync.client().clone(),
Arc::clone(&logger),
Arc::clone(&channel_scheduler),
));
let fee_estimator = Arc::new(OnchainFeeEstimator::new(
tx_sync.client().clone(),
Expand All @@ -584,6 +622,7 @@ fn build_with_store_internal(
let tx_broadcaster = Arc::new(TransactionBroadcaster::new(
tx_sync.client().clone(),
Arc::clone(&logger),
Arc::clone(&channel_scheduler),
));
let fee_estimator = Arc::new(OnchainFeeEstimator::new(
tx_sync.client().clone(),
Expand Down Expand Up @@ -973,6 +1012,29 @@ fn build_with_store_internal(
};

let (stop_sender, _) = tokio::sync::watch::channel(());
let (payjoin_receiver, payjoin_sender) = if let Some(payjoin_config) = payjoin_config {
let payjoin_receiver = match PayjoinReceiver::enroll(
&payjoin_config.ohttp_keys,
&payjoin_config.payjoin_directory,
&payjoin_config.payjoin_relay,
Arc::clone(&channel_scheduler),
Arc::clone(&wallet),
Arc::clone(&channel_manager),
Arc::clone(&logger),
) {
Ok(r) => Some(Arc::new(r)),
Err(_e) => None,
};
let payjoin_sender = PayjoinSender::new(
Arc::clone(&logger),
Arc::clone(&wallet),
&payjoin_config.payjoin_relay,
&payjoin_config.payjoin_directory,
);
(payjoin_receiver, Some(Arc::new(payjoin_sender)))
} else {
(None, None)
};

let is_listening = Arc::new(AtomicBool::new(false));
let latest_wallet_sync_timestamp = Arc::new(RwLock::new(None));
Expand All @@ -993,6 +1055,8 @@ fn build_with_store_internal(
channel_manager,
chain_monitor,
output_sweeper,
payjoin_receiver,
payjoin_sender,
peer_manager,
connection_manager,
keys_manager,
Expand Down

0 comments on commit 8cc8544

Please sign in to comment.