Skip to content

Commit aa49b19

Browse files
committed
Add ChannelScheduler and PayjoinHandler to Node
...
1 parent 5aef166 commit aa49b19

13 files changed

+1642
-44
lines changed

Diff for: Cargo.toml

+27-11
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,28 @@ panic = 'abort' # Abort on panic
2828
default = []
2929

3030
[dependencies]
31-
lightning = { version = "0.0.123", features = ["std"] }
32-
lightning-invoice = { version = "0.31.0" }
33-
lightning-net-tokio = { version = "0.0.123" }
34-
lightning-persister = { version = "0.0.123" }
35-
lightning-background-processor = { version = "0.0.123", features = ["futures"] }
36-
lightning-rapid-gossip-sync = { version = "0.0.123" }
37-
lightning-transaction-sync = { version = "0.0.123", features = ["esplora-async-https", "time"] }
38-
lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }
39-
31+
# lightning = { version = "0.0.123", features = ["std"] }
32+
# lightning-invoice = { version = "0.31.0" }
33+
# lightning-net-tokio = { version = "0.0.123" }
34+
# lightning-persister = { version = "0.0.123" }
35+
# lightning-background-processor = { version = "0.0.123", features = ["futures"] }
36+
# lightning-rapid-gossip-sync = { version = "0.0.123" }
37+
# lightning-transaction-sync = { version = "0.0.123", features = ["esplora-async-https", "time"] }
38+
# lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }
39+
lightning = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated", features = ["std"] }
40+
lightning-invoice = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated" }
41+
lightning-net-tokio = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated" }
42+
lightning-persister = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated" }
43+
lightning-background-processor = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated", features = ["futures"] }
44+
lightning-rapid-gossip-sync = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated" }
45+
lightning-transaction-sync = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated", features = ["esplora-async-https", "time"] }
46+
#lightning-liquidity = { version = "0.1.0-alpha.1", features = ["std"] }
47+
48+
# lightning-liquidity = {path = "../../lightning-liquidity" git = "https://github.com/jbesraa/lightning-liquidity", rev = "b6ac60d", features = ["std"] }
49+
lightning-liquidity = { git = "https://github.com/jbesraa/lightning-liquidity", branch = "pj-fixes", features = ["std"] }
50+
# lightning-liquidity = { git = "https://github.com/tnull/lightning-liquidity", rev = "abf7088c0e03221c0f122e797f34802c9e99a3d4", features = ["std"] }
51+
52+
# payjoin = { git = "https://github.com/jbesraa/rust-payjoin.git", rev = "9e4f454", features = ["v2", "receive", "send"] }
4053
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std"] }
4154
#lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
4255
#lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main" }
@@ -57,7 +70,7 @@ lightning-liquidity = { version = "0.1.0-alpha.4", features = ["std"] }
5770

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

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

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

7993
[dev-dependencies]
80-
lightning = { version = "0.0.123", features = ["std", "_test_utils"] }
94+
# lightning = { version = "0.0.123", features = ["std", "_test_utils"] }
95+
lightning = { git = "https://github.com/jbesraa/rust-lightning", branch = "danger-funding-generated", features = ["std", "_test_utils"] }
8196
#lightning = { git = "https://github.com/lightningdevkit/rust-lightning", branch="main", features = ["std", "_test_utils"] }
8297
electrum-client = { version = "0.15.1", default-features = true }
8398
bitcoincore-rpc = { version = "0.17.0", default-features = false }
8499
proptest = "1.0.0"
85100
regex = "1.5.6"
101+
reqwest = { version = "0.11", default-features = false, features = ["blocking"] }
86102

87103
[target.'cfg(not(no_download))'.dev-dependencies]
88104
electrsd = { version = "0.26.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_25_0"] }

Diff for: bindings/ldk_node.udl

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ enum NodeError {
151151
"InsufficientFunds",
152152
"LiquiditySourceUnavailable",
153153
"LiquidityFeeTooHigh",
154+
"PayjoinReqwest",
155+
"PayjoinValidation",
156+
"PayjoinEnrollment",
157+
"PayjoinUri",
158+
"PayjoinReceiver",
159+
"PayjoinSender",
160+
"BitcoinConsensusFailed",
154161
};
155162

156163
dictionary NodeStatus {

Diff for: src/builder.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::channel_scheduler::ChannelScheduler;
12
use crate::config::{
23
Config, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, DEFAULT_ESPLORA_SERVER_URL,
34
WALLET_KEYS_SEED_LEN,
@@ -11,6 +12,7 @@ use crate::io::sqlite_store::SqliteStore;
1112
use crate::liquidity::LiquiditySource;
1213
use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
1314
use crate::message_handler::NodeCustomMessageHandler;
15+
use crate::payjoin_handler::{PayjoinReceiver, PayjoinSender};
1416
use crate::payment::store::PaymentStore;
1517
use crate::peer_store::PeerStore;
1618
use crate::tx_broadcaster::TransactionBroadcaster;
@@ -94,6 +96,13 @@ struct LiquiditySourceConfig {
9496
lsps2_service: Option<(SocketAddress, PublicKey, Option<String>)>,
9597
}
9698

99+
#[derive(Debug, Clone)]
100+
struct PayjoinConfig {
101+
payjoin_directory: payjoin::Url,
102+
payjoin_relay: payjoin::Url,
103+
ohttp_keys: Option<payjoin::OhttpKeys>,
104+
}
105+
97106
impl Default for LiquiditySourceConfig {
98107
fn default() -> Self {
99108
Self { lsps2_service: None }
@@ -173,6 +182,7 @@ pub struct NodeBuilder {
173182
chain_data_source_config: Option<ChainDataSourceConfig>,
174183
gossip_source_config: Option<GossipSourceConfig>,
175184
liquidity_source_config: Option<LiquiditySourceConfig>,
185+
payjoin_config: Option<PayjoinConfig>,
176186
}
177187

178188
impl NodeBuilder {
@@ -188,12 +198,14 @@ impl NodeBuilder {
188198
let chain_data_source_config = None;
189199
let gossip_source_config = None;
190200
let liquidity_source_config = None;
201+
let payjoin_config = None;
191202
Self {
192203
config,
193204
entropy_source_config,
194205
chain_data_source_config,
195206
gossip_source_config,
196207
liquidity_source_config,
208+
payjoin_config,
197209
}
198210
}
199211

@@ -248,6 +260,16 @@ impl NodeBuilder {
248260
self
249261
}
250262

263+
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
264+
/// server.
265+
pub fn set_payjoin_config(
266+
&mut self, payjoin_directory: payjoin::Url, payjoin_relay: payjoin::Url,
267+
ohttp_keys: Option<payjoin::OhttpKeys>,
268+
) -> &mut Self {
269+
self.payjoin_config = Some(PayjoinConfig { payjoin_directory, payjoin_relay, ohttp_keys });
270+
self
271+
}
272+
251273
/// Configures the [`Node`] instance to source its inbound liquidity from the given
252274
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
253275
/// service.
@@ -369,6 +391,7 @@ impl NodeBuilder {
369391
seed_bytes,
370392
logger,
371393
vss_store,
394+
self.payjoin_config.as_ref(),
372395
)
373396
}
374397

@@ -390,6 +413,7 @@ impl NodeBuilder {
390413
seed_bytes,
391414
logger,
392415
kv_store,
416+
self.payjoin_config.as_ref(),
393417
)
394418
}
395419
}
@@ -460,6 +484,18 @@ impl ArcedNodeBuilder {
460484
self.inner.write().unwrap().set_gossip_source_rgs(rgs_server_url);
461485
}
462486

487+
/// Configures the [`Node`] instance to use payjoin.
488+
pub fn set_payjoin_config(
489+
&self, payjoin_directory: payjoin::Url, payjoin_relay: payjoin::Url,
490+
ohttp_keys: Option<payjoin::OhttpKeys>,
491+
) {
492+
self.inner.write().unwrap().set_payjoin_config(
493+
payjoin_directory,
494+
payjoin_relay,
495+
ohttp_keys,
496+
);
497+
}
498+
463499
/// Configures the [`Node`] instance to source its inbound liquidity from the given
464500
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
465501
/// service.
@@ -523,7 +559,7 @@ fn build_with_store_internal(
523559
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
524560
gossip_source_config: Option<&GossipSourceConfig>,
525561
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
526-
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>,
562+
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>, payjoin_config: Option<&PayjoinConfig>,
527563
) -> Result<Node, BuildError> {
528564
// Initialize the on-chain wallet and chain access
529565
let xprv = bitcoin::bip32::ExtendedPrivKey::new_master(config.network.into(), &seed_bytes)
@@ -556,6 +592,7 @@ fn build_with_store_internal(
556592
log_error!(logger, "Failed to set up wallet: {}", e);
557593
BuildError::WalletSetupFailed
558594
})?;
595+
let channel_scheduler = Arc::new(tokio::sync::Mutex::new(ChannelScheduler::new()));
559596

560597
let (blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config {
561598
Some(ChainDataSourceConfig::Esplora(server_url)) => {
@@ -566,6 +603,7 @@ fn build_with_store_internal(
566603
let tx_broadcaster = Arc::new(TransactionBroadcaster::new(
567604
tx_sync.client().clone(),
568605
Arc::clone(&logger),
606+
Arc::clone(&channel_scheduler),
569607
));
570608
let fee_estimator = Arc::new(OnchainFeeEstimator::new(
571609
tx_sync.client().clone(),
@@ -584,6 +622,7 @@ fn build_with_store_internal(
584622
let tx_broadcaster = Arc::new(TransactionBroadcaster::new(
585623
tx_sync.client().clone(),
586624
Arc::clone(&logger),
625+
Arc::clone(&channel_scheduler),
587626
));
588627
let fee_estimator = Arc::new(OnchainFeeEstimator::new(
589628
tx_sync.client().clone(),
@@ -973,6 +1012,29 @@ fn build_with_store_internal(
9731012
};
9741013

9751014
let (stop_sender, _) = tokio::sync::watch::channel(());
1015+
let (payjoin_receiver, payjoin_sender) = if let Some(payjoin_config) = payjoin_config {
1016+
let payjoin_receiver = match PayjoinReceiver::enroll(
1017+
&payjoin_config.ohttp_keys,
1018+
&payjoin_config.payjoin_directory,
1019+
&payjoin_config.payjoin_relay,
1020+
Arc::clone(&channel_scheduler),
1021+
Arc::clone(&wallet),
1022+
Arc::clone(&channel_manager),
1023+
Arc::clone(&logger),
1024+
) {
1025+
Ok(r) => Some(Arc::new(r)),
1026+
Err(_e) => None,
1027+
};
1028+
let payjoin_sender = PayjoinSender::new(
1029+
Arc::clone(&logger),
1030+
Arc::clone(&wallet),
1031+
&payjoin_config.payjoin_relay,
1032+
&payjoin_config.payjoin_directory,
1033+
);
1034+
(payjoin_receiver, Some(Arc::new(payjoin_sender)))
1035+
} else {
1036+
(None, None)
1037+
};
9761038

9771039
let is_listening = Arc::new(AtomicBool::new(false));
9781040
let latest_wallet_sync_timestamp = Arc::new(RwLock::new(None));
@@ -993,6 +1055,8 @@ fn build_with_store_internal(
9931055
channel_manager,
9941056
chain_monitor,
9951057
output_sweeper,
1058+
payjoin_receiver,
1059+
payjoin_sender,
9961060
peer_manager,
9971061
connection_manager,
9981062
keys_manager,

0 commit comments

Comments
 (0)