From: Valentine Wallace Date: Wed, 14 Apr 2021 02:34:45 +0000 (-0400) Subject: Update to latest upstream RL X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=0f8635873858a05cdffee21eb99dd2e100e6cebc;p=ldk-sample Update to latest upstream RL --- diff --git a/Cargo.lock b/Cargo.lock index 1fd4f83..8c606e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -256,7 +256,7 @@ checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "lightning" version = "0.0.13" -source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=6fcac8bc65ed6d372e0b8c367e9934c754f99ff3#6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" +source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=3d51b11fe99c9d6a41b0d662efdf5693d9600552#3d51b11fe99c9d6a41b0d662efdf5693d9600552" dependencies = [ "bitcoin", ] @@ -264,7 +264,7 @@ dependencies = [ [[package]] name = "lightning-background-processor" version = "0.0.13" -source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=6fcac8bc65ed6d372e0b8c367e9934c754f99ff3#6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" +source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=3d51b11fe99c9d6a41b0d662efdf5693d9600552#3d51b11fe99c9d6a41b0d662efdf5693d9600552" dependencies = [ "bitcoin", "lightning", @@ -274,7 +274,7 @@ dependencies = [ [[package]] name = "lightning-block-sync" version = "0.0.13" -source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=6fcac8bc65ed6d372e0b8c367e9934c754f99ff3#6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" +source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=3d51b11fe99c9d6a41b0d662efdf5693d9600552#3d51b11fe99c9d6a41b0d662efdf5693d9600552" dependencies = [ "bitcoin", "chunked_transfer", @@ -298,7 +298,7 @@ dependencies = [ [[package]] name = "lightning-net-tokio" version = "0.0.13" -source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=6fcac8bc65ed6d372e0b8c367e9934c754f99ff3#6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" +source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=3d51b11fe99c9d6a41b0d662efdf5693d9600552#3d51b11fe99c9d6a41b0d662efdf5693d9600552" dependencies = [ "bitcoin", "lightning", @@ -308,7 +308,7 @@ dependencies = [ [[package]] name = "lightning-persister" version = "0.0.13" -source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=6fcac8bc65ed6d372e0b8c367e9934c754f99ff3#6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" +source = "git+https://github.com/rust-bitcoin/rust-lightning?rev=3d51b11fe99c9d6a41b0d662efdf5693d9600552#3d51b11fe99c9d6a41b0d662efdf5693d9600552" dependencies = [ "bitcoin", "libc", diff --git a/Cargo.toml b/Cargo.toml index 4ce68df..35bae49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,21 +8,21 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lightning-background-processor = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } +lightning-background-processor = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "3d51b11fe99c9d6a41b0d662efdf5693d9600552" } # lightning-background-processor = { path = "../rust-lightning/background-processor" } base64 = "0.13.0" bitcoin = "0.26" bitcoin-bech32 = "0.7" bech32 = "0.7" hex = "0.3" -lightning = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } +lightning = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "3d51b11fe99c9d6a41b0d662efdf5693d9600552" } # lightning = { path = "../rust-lightning/lightning" } -lightning-block-sync = { git = "https://github.com/rust-bitcoin/rust-lightning", features = ["rpc-client"], rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } +lightning-block-sync = { git = "https://github.com/rust-bitcoin/rust-lightning", features = ["rpc-client"], rev = "3d51b11fe99c9d6a41b0d662efdf5693d9600552" } # lightning-block-sync = { path = "../rust-lightning/lightning-block-sync", features = ["rpc-client"] } lightning-invoice = { git = "https://github.com/rust-bitcoin/rust-lightning-invoice", rev = "aa3a57b9dca5205fa25fa333a2db165d7e77b3b0" } -lightning-net-tokio = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } +lightning-net-tokio = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "3d51b11fe99c9d6a41b0d662efdf5693d9600552" } # lightning-net-tokio = { path = "../rust-lightning/lightning-net-tokio" } -lightning-persister = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" } +lightning-persister = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "3d51b11fe99c9d6a41b0d662efdf5693d9600552" } # lightning-persister = { path = "../rust-lightning/lightning-persister" } time = "0.2" rand = "0.4" diff --git a/src/disk.rs b/src/disk.rs index 13ac5e7..827cb7e 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -1,20 +1,13 @@ use crate::cli; -use bitcoin::hashes::hex::FromHex; use bitcoin::secp256k1::key::PublicKey; -use bitcoin::{BlockHash, Txid}; -use lightning::chain::channelmonitor::ChannelMonitor; -use lightning::chain::keysinterface::{InMemorySigner, KeysManager}; -use lightning::chain::transaction::OutPoint; use lightning::util::logger::{Logger, Record}; -use lightning::util::ser::{ReadableArgs, Writer}; +use lightning::util::ser::Writer; use std::collections::HashMap; use std::fs; use std::fs::File; -// use std::io::{BufRead, BufReader, Cursor, Write}; -use std::io::{BufRead, BufReader, Cursor}; +use std::io::{BufRead, BufReader}; use std::net::SocketAddr; use std::path::Path; -use std::sync::Arc; use time::OffsetDateTime; pub(crate) struct FilesystemLogger { @@ -72,58 +65,3 @@ pub(crate) fn read_channel_peer_data( } Ok(peer_data) } - -pub(crate) fn read_channelmonitors( - path: String, keys_manager: Arc, -) -> Result)>, std::io::Error> { - if !Path::new(&path).exists() { - return Ok(HashMap::new()); - } - let mut outpoint_to_channelmonitor = HashMap::new(); - for file_option in fs::read_dir(path).unwrap() { - let file = file_option.unwrap(); - let owned_file_name = file.file_name(); - let filename = owned_file_name.to_str(); - if !filename.is_some() || !filename.unwrap().is_ascii() || filename.unwrap().len() < 65 { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid ChannelMonitor file name", - )); - } - - let txid = Txid::from_hex(filename.unwrap().split_at(64).0); - if txid.is_err() { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid tx ID in filename", - )); - } - - let index = filename.unwrap().split_at(65).1.split('.').next().unwrap().parse(); - if index.is_err() { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Invalid tx index in filename", - )); - } - - let contents = fs::read(&file.path())?; - - if let Ok((blockhash, channel_monitor)) = - <(BlockHash, ChannelMonitor)>::read( - &mut Cursor::new(&contents), - &*keys_manager, - ) { - outpoint_to_channelmonitor.insert( - OutPoint { txid: txid.unwrap(), index: index.unwrap() }, - (blockhash, channel_monitor), - ); - } else { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Failed to deserialize ChannelMonitor", - )); - } - } - Ok(outpoint_to_channelmonitor) -} diff --git a/src/main.rs b/src/main.rs index 5f5dc0a..5f213fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,6 @@ use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator}; use lightning::chain::chainmonitor; use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager}; -use lightning::chain::transaction::OutPoint; use lightning::chain::Filter; use lightning::chain::Watch; use lightning::ln::channelmanager; @@ -102,13 +101,11 @@ pub(crate) type ChannelManager = SimpleArcChannelManager; async fn handle_ldk_events( - peer_manager: Arc, channel_manager: Arc, - chain_monitor: Arc, bitcoind_client: Arc, - keys_manager: Arc, payment_storage: PaymentInfoStorage, network: Network, + channel_manager: Arc, chain_monitor: Arc, + bitcoind_client: Arc, keys_manager: Arc, + payment_storage: PaymentInfoStorage, network: Network, ) { - let mut pending_txs: HashMap = HashMap::new(); loop { - peer_manager.process_events(); let loop_channel_manager = channel_manager.clone(); let mut events = channel_manager.get_and_clear_pending_events(); events.append(&mut chain_monitor.get_and_clear_pending_events()); @@ -149,22 +146,12 @@ async fn handle_ldk_events( assert_eq!(signed_tx.complete, true); let final_tx: Transaction = encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap(); - let outpoint = OutPoint { - txid: final_tx.txid(), - index: if change_output_position == 0 { 1 } else { 0 }, - }; // Give the funding transaction back to LDK for opening the channel. loop_channel_manager - .funding_transaction_generated(&temporary_channel_id, outpoint); - pending_txs.insert(outpoint, final_tx); + .funding_transaction_generated(&temporary_channel_id, final_tx) + .unwrap(); } Event::FundingBroadcastSafe { funding_txo, .. } => { - let funding_tx = pending_txs.remove(&funding_txo).unwrap(); - bitcoind_client.broadcast_transaction(&funding_tx); - println!("\nEVENT: broadcasted funding transaction"); - print!("> "); - io::stdout().flush().unwrap(); - } Event::PaymentReceived { payment_hash, payment_secret, amt: amt_msat } => { let mut payments = payment_storage.lock().unwrap(); if let Some((Some(preimage), _, _, _)) = payments.get(&payment_hash) { @@ -345,9 +332,7 @@ async fn start_ldk() { let keys_manager = Arc::new(KeysManager::new(&keys_seed, cur.as_secs(), cur.subsec_nanos())); // Step 7: Read ChannelMonitor state from disk - let monitors_path = format!("{}/monitors", ldk_data_dir.clone()); - let mut outpoint_to_channelmonitor = - disk::read_channelmonitors(monitors_path.to_string(), keys_manager.clone()).unwrap(); + let mut channelmonitors = persister.read_channelmonitors(keys_manager.clone()).unwrap(); // Step 9: Initialize the ChannelManager let user_config = UserConfig::default(); @@ -355,8 +340,8 @@ async fn start_ldk() { let (channel_manager_blockhash, mut channel_manager) = { if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) { let mut channel_monitor_mut_references = Vec::new(); - for (_, channel_monitor) in outpoint_to_channelmonitor.iter_mut() { - channel_monitor_mut_references.push(&mut channel_monitor.1); + for (_, channel_monitor) in channelmonitors.iter_mut() { + channel_monitor_mut_references.push(channel_monitor); } let read_args = ChannelManagerReadArgs::new( keys_manager.clone(), @@ -399,9 +384,8 @@ async fn start_ldk() { let mut chain_listeners = vec![(channel_manager_blockhash, &mut channel_manager as &mut dyn chain::Listen)]; - for (outpoint, blockhash_and_monitor) in outpoint_to_channelmonitor.drain() { - let blockhash = blockhash_and_monitor.0; - let channel_monitor = blockhash_and_monitor.1; + for (blockhash, channel_monitor) in channelmonitors.drain(..) { + let outpoint = channel_monitor.get_funding_txo().0; chain_listener_channel_monitors.push(( blockhash, (channel_monitor, broadcaster.clone(), fee_estimator.clone(), logger.clone()), @@ -504,6 +488,7 @@ async fn start_ldk() { BackgroundProcessor::start( persist_channel_manager_callback, channel_manager.clone(), + peer_manager.clone(), logger.clone(), ); @@ -516,7 +501,6 @@ async fn start_ldk() { }); // Step 15: Initialize LDK Event Handling - let peer_manager_event_listener = peer_manager.clone(); let channel_manager_event_listener = channel_manager.clone(); let chain_monitor_event_listener = chain_monitor.clone(); let keys_manager_listener = keys_manager.clone(); @@ -526,7 +510,6 @@ async fn start_ldk() { let bitcoind_rpc = bitcoind_client.clone(); tokio::spawn(async move { handle_ldk_events( - peer_manager_event_listener, channel_manager_event_listener, chain_monitor_event_listener, bitcoind_rpc,