From 0bc73c3a5861fe2cdfc03b636b23dfec95d6cc5c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 10 Jan 2025 19:47:35 +0000 Subject: [PATCH] Update to LDK 0.1-beta1 --- Cargo.toml | 14 +++++------ src/bitcoind_client.rs | 4 ++-- src/cli.rs | 9 +++---- src/disk.rs | 10 ++++---- src/main.rs | 54 ++++++++++++++++-------------------------- 5 files changed, 38 insertions(+), 53 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3311131..1d8e4a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,13 +8,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lightning = { version = "0.0.125", features = ["max_level_trace"] } -lightning-block-sync = { version = "0.0.125", features = [ "rpc-client", "tokio" ] } -lightning-invoice = { version = "0.32.0" } -lightning-net-tokio = { version = "0.0.125" } -lightning-persister = { version = "0.0.125" } -lightning-background-processor = { version = "0.0.125", features = [ "futures" ] } -lightning-rapid-gossip-sync = { version = "0.0.125" } +lightning = { version = "0.1.0-beta1", features = ["dnssec"] } +lightning-block-sync = { version = "0.1.0-beta1", features = [ "rpc-client", "tokio" ] } +lightning-invoice = { version = "0.33.0-beta1" } +lightning-net-tokio = { version = "0.1.0-beta1" } +lightning-persister = { version = "0.1.0-beta1" } +lightning-background-processor = { version = "0.1.0-beta1", features = [ "futures" ] } +lightning-rapid-gossip-sync = { version = "0.1.0-beta1" } base64 = "0.13.0" bitcoin = "0.32" diff --git a/src/bitcoind_client.rs b/src/bitcoind_client.rs index 31b328c..bfa4e58 100644 --- a/src/bitcoind_client.rs +++ b/src/bitcoind_client.rs @@ -71,7 +71,7 @@ impl BitcoindClient { let http_endpoint = HttpEndpoint::for_host(host.clone()).with_port(port); let rpc_credentials = base64::encode(format!("{}:{}", rpc_user.clone(), rpc_password.clone())); - let bitcoind_rpc_client = RpcClient::new(&rpc_credentials, http_endpoint)?; + let bitcoind_rpc_client = RpcClient::new(&rpc_credentials, http_endpoint); let _dummy = bitcoind_rpc_client .call_method::("getblockchaininfo", &vec![]) .await @@ -226,7 +226,7 @@ impl BitcoindClient { }); } - pub fn get_new_rpc_client(&self) -> std::io::Result { + pub fn get_new_rpc_client(&self) -> RpcClient { let http_endpoint = HttpEndpoint::for_host(self.host.clone()).with_port(self.port); let rpc_credentials = base64::encode(format!("{}:{}", self.rpc_user.clone(), self.rpc_password.clone())); diff --git a/src/cli.rs b/src/cli.rs index c55623a..ff705b1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,7 +10,7 @@ use bitcoin::network::Network; use bitcoin::secp256k1::PublicKey; use lightning::chain::channelmonitor::Balance; use lightning::ln::bolt11_payment::payment_parameters_from_invoice; -use lightning::ln::bolt11_payment::payment_parameters_from_zero_amount_invoice; +use lightning::ln::bolt11_payment::payment_parameters_from_variable_amount_invoice; use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry}; use lightning::ln::invoice_utils as utils; use lightning::ln::msgs::SocketAddress; @@ -746,7 +746,7 @@ fn send_payment( invoice.amount_milli_satoshis().is_none() || invoice.amount_milli_satoshis() == Some(0); let pay_params_opt = if zero_amt_invoice { if let Some(amt_msat) = required_amount_msat { - payment_parameters_from_zero_amount_invoice(invoice, amt_msat) + payment_parameters_from_variable_amount_invoice(invoice, amt_msat) } else { println!("Need an amount for the given 0-value invoice"); print!("> "); @@ -826,7 +826,7 @@ fn keysend( }, ); fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap(); - match channel_manager.send_spontaneous_payment_with_retry( + match channel_manager.send_spontaneous_payment( Some(payment_preimage), RecipientOnionFields::spontaneous_empty(), payment_id, @@ -859,9 +859,6 @@ fn get_invoice( }; let invoice = match utils::create_invoice_from_channelmanager( channel_manager, - keys_manager, - logger, - currency, Some(amt_msat), "ldk-tutorial-node".to_string(), expiry_secs, diff --git a/src/disk.rs b/src/disk.rs index 6db81d4..49f3abc 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -3,9 +3,9 @@ use bitcoin::secp256k1::PublicKey; use bitcoin::Network; use chrono::Utc; use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringDecayParameters}; +use lightning::util::hash_tables::{HashMap, new_hash_map}; use lightning::util::logger::{Logger, Record}; use lightning::util::ser::{Readable, ReadableArgs}; -use std::collections::HashMap; use std::fs; use std::fs::File; use std::io::{BufRead, BufReader, Write}; @@ -58,9 +58,9 @@ pub(crate) fn persist_channel_peer(path: &Path, peer_info: &str) -> std::io::Res pub(crate) fn read_channel_peer_data( path: &Path, ) -> Result, std::io::Error> { - let mut peer_data = HashMap::new(); + let mut peer_data = new_hash_map(); if !Path::new(&path).exists() { - return Ok(HashMap::new()); + return Ok(new_hash_map()); } let file = File::open(path)?; let reader = BufReader::new(file); @@ -92,7 +92,7 @@ pub(crate) fn read_inbound_payment_info(path: &Path) -> InboundPaymentInfoStorag return info; } } - InboundPaymentInfoStorage { payments: HashMap::new() } + InboundPaymentInfoStorage { payments: new_hash_map() } } pub(crate) fn read_outbound_payment_info(path: &Path) -> OutboundPaymentInfoStorage { @@ -101,7 +101,7 @@ pub(crate) fn read_outbound_payment_info(path: &Path) -> OutboundPaymentInfoStor return info; } } - OutboundPaymentInfoStorage { payments: HashMap::new() } + OutboundPaymentInfoStorage { payments: new_hash_map() } } pub(crate) fn read_scorer( diff --git a/src/main.rs b/src/main.rs index cfe5705..ae2840b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,8 @@ use lightning::routing::router::DefaultRouter; use lightning::routing::scoring::ProbabilisticScoringFeeParameters; use lightning::sign::{EntropySource, InMemorySigner, KeysManager}; use lightning::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; +use lightning::util::hash_tables::hash_map::Entry; +use lightning::util::hash_tables::HashMap; use lightning::util::config::UserConfig; use lightning::util::persist::{ self, KVStore, MonitorUpdatingPersister, OUTPUT_SWEEPER_PERSISTENCE_KEY, @@ -49,8 +51,7 @@ use lightning_block_sync::UnboundedCache; use lightning_net_tokio::SocketDescriptor; use lightning_persister::fs_store::FilesystemStore; use rand::{thread_rng, Rng}; -use std::collections::hash_map::Entry; -use std::collections::HashMap; +use std::collections::HashMap as StdHashMap; use std::convert::TryInto; use std::fmt; use std::fs; @@ -158,7 +159,7 @@ pub(crate) type PeerManager = SimpleArcPeerManager< ChainMonitor, BitcoindClient, BitcoindClient, - GossipVerifier, + Arc, FilesystemLogger, >; @@ -219,7 +220,7 @@ async fn handle_ldk_events( ) .expect("Lightning funding tx should always be to a SegWit output") .to_address(); - let mut outputs = vec![HashMap::with_capacity(1)]; + let mut outputs = vec![StdHashMap::new()]; outputs[0].insert(addr, channel_value_satoshis as f64 / 100_000_000.0); let raw_tx = bitcoind_client.create_raw_transaction(outputs).await; @@ -246,17 +247,7 @@ async fn handle_ldk_events( Event::FundingTxBroadcastSafe { .. } => { // We don't use the manual broadcasting feature, so this event should never be seen. }, - Event::PaymentClaimable { - payment_hash, - purpose, - amount_msat, - receiver_node_id: _, - via_channel_id: _, - via_user_channel_id: _, - claim_deadline: _, - onion_fields: _, - counterparty_skimmed_fee_msat: _, - } => { + Event::PaymentClaimable { payment_hash, purpose, amount_msat, .. } => { println!( "\nEVENT: received payment from payment hash {} of {} millisatoshis", payment_hash, amount_msat, @@ -402,9 +393,7 @@ async fn handle_ldk_events( total_fee_earned_msat, claim_from_onchain_tx, outbound_amount_forwarded_msat, - skimmed_fee_msat: _, - prev_user_channel_id: _, - next_user_channel_id: _, + .. } => { let read_only_network_graph = network_graph.read_only(); let nodes = read_only_network_graph.nodes(); @@ -497,14 +486,7 @@ async fn handle_ldk_events( print!("> "); std::io::stdout().flush().unwrap(); }, - Event::ChannelClosed { - channel_id, - reason, - user_channel_id: _, - counterparty_node_id, - channel_capacity_sats: _, - channel_funding_txo: _, - } => { + Event::ChannelClosed { channel_id, reason, counterparty_node_id, .. } => { println!( "\nEVENT: Channel {} with counterparty {} closed due to: {:?}", channel_id, @@ -688,7 +670,7 @@ async fn start_ldk() { Arc::clone(&logger), ))); - // Step 10: Create Router + // Step 10: Create Routers let scoring_fee_params = ProbabilisticScoringFeeParameters::default(); let router = Arc::new(DefaultRouter::new( network_graph.clone(), @@ -698,6 +680,9 @@ async fn start_ldk() { scoring_fee_params, )); + let message_router = + Arc::new(DefaultMessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager))); + // Step 11: Initialize the ChannelManager let mut user_config = UserConfig::default(); user_config.channel_handshake_limits.force_announced_channel_preference = false; @@ -706,9 +691,9 @@ async fn start_ldk() { let mut restarting_node = true; let (channel_manager_blockhash, channel_manager) = { if let Ok(f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) { - let mut channel_monitor_mut_references = Vec::new(); - for (_, channel_monitor) in channelmonitors.iter_mut() { - channel_monitor_mut_references.push(channel_monitor); + let mut channel_monitor_references = Vec::new(); + for (_, channel_monitor) in channelmonitors.iter() { + channel_monitor_references.push(channel_monitor); } let read_args = ChannelManagerReadArgs::new( keys_manager.clone(), @@ -718,9 +703,10 @@ async fn start_ldk() { chain_monitor.clone(), broadcaster.clone(), router, + Arc::clone(&message_router), logger.clone(), user_config, - channel_monitor_mut_references, + channel_monitor_references, ); <(BlockHash, ChannelManager)>::read(&mut BufReader::new(f), read_args).unwrap() } else { @@ -736,6 +722,7 @@ async fn start_ldk() { chain_monitor.clone(), broadcaster.clone(), router, + Arc::clone(&message_router), logger.clone(), keys_manager.clone(), keys_manager.clone(), @@ -842,7 +829,8 @@ async fn start_ldk() { Arc::clone(&keys_manager), Arc::clone(&logger), Arc::clone(&channel_manager), - Arc::new(DefaultMessageRouter::new(Arc::clone(&network_graph), Arc::clone(&keys_manager))), + Arc::clone(&message_router), + Arc::clone(&channel_manager), Arc::clone(&channel_manager), Arc::clone(&channel_manager), IgnoringMessageHandler {}, @@ -871,7 +859,7 @@ async fn start_ldk() { Arc::clone(&gossip_sync), Arc::clone(&peer_manager), ); - gossip_sync.add_utxo_lookup(Some(utxo_lookup)); + gossip_sync.add_utxo_lookup(Some(Arc::new(utxo_lookup))); // ## Running LDK // Step 17: Initialize networking