From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Thu, 24 Jun 2021 17:00:00 +0000 (+0000) Subject: Merge pull request #20 from TheBlueMatt/main X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-sample;a=commitdiff_plain;h=2ce4eb6de2eb3d550ddbee3134ac902199eb052f;hp=4f7247976bb14191352fb1c8897619d9068caaef Merge pull request #20 from TheBlueMatt/main Include bad-txns-inputs-missingorspent in allowed tx-broadcast errs --- diff --git a/Cargo.lock b/Cargo.lock index 37e41e6..abb8455 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -256,16 +256,16 @@ checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "lightning" -version = "0.0.14" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.0.98" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bitcoin", ] [[package]] name = "lightning-background-processor" -version = "0.0.14" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.0.98" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bitcoin", "lightning", @@ -274,8 +274,8 @@ dependencies = [ [[package]] name = "lightning-block-sync" -version = "0.0.14" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.0.98" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bitcoin", "chunked_transfer", @@ -287,8 +287,8 @@ dependencies = [ [[package]] name = "lightning-invoice" -version = "0.5.0" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.6.0" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bech32 0.7.2", "bitcoin_hashes", @@ -299,8 +299,8 @@ dependencies = [ [[package]] name = "lightning-net-tokio" -version = "0.0.14" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.0.98" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bitcoin", "lightning", @@ -309,8 +309,8 @@ dependencies = [ [[package]] name = "lightning-persister" -version = "0.0.14" -source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#499d84c165426b6f05bb414f5a49a7423eb9e0c5" +version = "0.0.98" +source = "git+https://github.com/rust-bitcoin/rust-lightning?branch=main#294009969aef617df4184ff41ef6daa5a445f213" dependencies = [ "bitcoin", "libc", diff --git a/Cargo.toml b/Cargo.toml index 2ae52ea..9b4fd9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,12 +8,12 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lightning = { version = "0.0.14", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } -lightning-block-sync = { version = "0.0.14", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main", features = [ "rpc-client" ] } -lightning-invoice = { version = "0.5", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } -lightning-net-tokio = { version = "0.0.14", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } -lightning-persister = { version = "0.0.14", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } -lightning-background-processor = { version = "0.0.14", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } +lightning = { version = "0.0.98", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } +lightning-block-sync = { version = "0.0.98", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main", features = [ "rpc-client" ] } +lightning-invoice = { version = "0.6", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } +lightning-net-tokio = { version = "0.0.98", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } +lightning-persister = { version = "0.0.98", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } +lightning-background-processor = { version = "0.0.98", git = "https://github.com/rust-bitcoin/rust-lightning", branch = "main" } base64 = "0.13.0" bitcoin = "0.26" diff --git a/src/cli.rs b/src/cli.rs index 5d778e8..b69f9af 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,6 +4,7 @@ use crate::{ ChannelManager, FilesystemLogger, HTLCStatus, MillisatAmount, PaymentInfo, PaymentInfoStorage, PeerManager, }; +use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; use bitcoin::secp256k1::key::PublicKey; use lightning::chain; @@ -13,8 +14,8 @@ use lightning::ln::msgs::NetAddress; use lightning::ln::{PaymentHash, PaymentSecret}; use lightning::routing::network_graph::NetGraphMsgHandler; use lightning::routing::router; -use lightning::routing::router::RouteHintHop; -use lightning::util::config::UserConfig; +use lightning::routing::router::RouteHint; +use lightning::util::config::{ChannelConfig, ChannelHandshakeLimits, UserConfig}; use lightning_invoice::{utils, Currency, Invoice}; use std::env; use std::io; @@ -227,11 +228,7 @@ pub(crate) async fn poll_for_user_input( continue; } }; - let mut route_hints = invoice.routes().clone(); - let mut last_hops = Vec::new(); - for hint in route_hints.drain(..) { - last_hops.push(hint[hint.len() - 1].clone()); - } + let last_hops = invoice.route_hints(); let amt_pico_btc = invoice.amount_pico_btc(); if amt_pico_btc.is_none() { @@ -244,23 +241,9 @@ pub(crate) async fn poll_for_user_input( let payee_pubkey = invoice.recover_payee_pub_key(); let final_cltv = invoice.min_final_cltv_expiry() as u32; - - let mut payment_hash = PaymentHash([0; 32]); - payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); - - let payment_secret = match invoice.payment_secret() { - Some(secret) => { - let mut payment_secret = PaymentSecret([0; 32]); - payment_secret.0.copy_from_slice(&secret.0); - Some(payment_secret) - } - None => None, - }; - - let invoice_features = match invoice.features() { - Some(feat) => Some(feat.clone()), - None => None, - }; + let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner()); + let payment_secret = invoice.payment_secret().cloned(); + let invoice_features = invoice.features().cloned(); send_payment( payee_pubkey, @@ -488,24 +471,21 @@ pub(crate) async fn connect_peer_if_necessary( } match lightning_net_tokio::connect_outbound(Arc::clone(&peer_manager), pubkey, peer_addr).await { - Some(conn_closed_fut) => { - let mut closed_fut_box = Box::pin(conn_closed_fut); - let mut peer_connected = false; - while !peer_connected { - match futures::poll!(&mut closed_fut_box) { + Some(connection_closed_future) => { + let mut connection_closed_future = Box::pin(connection_closed_future); + loop { + match futures::poll!(&mut connection_closed_future) { std::task::Poll::Ready(_) => { println!("ERROR: Peer disconnected before we finished the handshake"); return Err(()); } std::task::Poll::Pending => {} } - for node_pubkey in peer_manager.get_peer_node_ids() { - if node_pubkey == pubkey { - peer_connected = true; - } - } // Avoid blocking the tokio context by sleeping a bit - tokio::time::sleep(Duration::from_millis(10)).await; + match peer_manager.get_peer_node_ids().iter().find(|id| **id == pubkey) { + Some(_) => break, + None => tokio::time::sleep(Duration::from_millis(10)).await, + } } } None => { @@ -517,15 +497,19 @@ pub(crate) async fn connect_peer_if_necessary( } fn open_channel( - peer_pubkey: PublicKey, channel_amt_sat: u64, announce_channel: bool, + peer_pubkey: PublicKey, channel_amt_sat: u64, announced_channel: bool, channel_manager: Arc, ) -> Result<(), ()> { - let mut config = UserConfig::default(); - if announce_channel { - config.channel_options.announced_channel = true; - } - // lnd's max to_self_delay is 2016, so we want to be compatible. - config.peer_channel_config_limits.their_to_self_delay = 2016; + let config = UserConfig { + peer_channel_config_limits: ChannelHandshakeLimits { + // lnd's max to_self_delay is 2016, so we want to be compatible. + their_to_self_delay: 2016, + ..Default::default() + }, + channel_options: ChannelConfig { announced_channel, ..Default::default() }, + ..Default::default() + }; + match channel_manager.create_channel(peer_pubkey, channel_amt_sat, 0, 0, Some(config)) { Ok(_) => { println!("EVENT: initiated channel with peer {}. ", peer_pubkey); @@ -541,7 +525,7 @@ fn open_channel( fn send_payment( payee: PublicKey, amt_msat: u64, final_cltv: u32, payment_hash: PaymentHash, payment_secret: Option, payee_features: Option, - route_hints: Vec, + route_hints: Vec<&RouteHint>, router: Arc, Arc>>, channel_manager: Arc, payment_storage: PaymentInfoStorage, logger: Arc, @@ -556,7 +540,7 @@ fn send_payment( &payee, payee_features, Some(&first_hops.iter().collect::>()), - &route_hints.iter().collect::>(), + &route_hints, amt_msat, final_cltv, logger, @@ -616,16 +600,12 @@ fn get_invoice( } }; - let mut payment_hash = PaymentHash([0; 32]); - payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); + let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner()); payments.insert( payment_hash, PaymentInfo { preimage: None, - // We can't add payment secrets to invoices until we support features in invoices. - // Otherwise lnd errors with "destination hop doesn't understand payment addresses" - // (for context, lnd calls payment secrets "payment addresses"). - secret: Some(invoice.payment_secret().unwrap().clone()), + secret: invoice.payment_secret().cloned(), status: HTLCStatus::Pending, amt_msat: MillisatAmount(Some(amt_msat)), },