From: Matt Corallo Date: Wed, 18 Aug 2021 23:48:08 +0000 (+0000) Subject: sample no clone X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=refs%2Fheads%2F2021-08-handle-network-updates;p=ldk-sample sample no clone --- diff --git a/Cargo.lock b/Cargo.lock index 03d573f..5717867 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "autocfg" version = "1.0.1" diff --git a/src/main.rs b/src/main.rs index 7846ad6..601b479 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,7 @@ pub(crate) type ChannelManager = async fn handle_ldk_events( channel_manager: Arc, bitcoind_client: Arc, keys_manager: Arc, inbound_payments: PaymentInfoStorage, - outbound_payments: PaymentInfoStorage, network: Network, event: Event, + outbound_payments: PaymentInfoStorage, network: Network, event: &Event, ) { match event { Event::FundingGenerationReady { @@ -123,7 +123,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)]; - outputs[0].insert(addr, channel_value_satoshis as f64 / 100_000_000.0); + outputs[0].insert(addr, *channel_value_satoshis as f64 / 100_000_000.0); let raw_tx = bitcoind_client.create_raw_transaction(outputs).await; // Have your wallet put the inputs into the transaction such that the output is @@ -152,9 +152,9 @@ async fn handle_ldk_events( let mut payments = inbound_payments.lock().unwrap(); let (payment_preimage, payment_secret) = match purpose { PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => { - (payment_preimage, Some(payment_secret)) + (payment_preimage.map(|a| a), Some(payment_secret)) } - PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None), + PaymentPurpose::SpontaneousPayment(preimage) => (Some(*preimage), None), }; let status = match channel_manager.claim_funds(payment_preimage.unwrap()) { true => { @@ -169,19 +169,19 @@ async fn handle_ldk_events( } _ => HTLCStatus::Failed, }; - match payments.entry(payment_hash) { + match payments.entry(*payment_hash) { Entry::Occupied(mut e) => { let payment = e.get_mut(); payment.status = status; payment.preimage = payment_preimage; - payment.secret = payment_secret; + payment.secret = payment_secret.map(|a| *a); } Entry::Vacant(e) => { e.insert(PaymentInfo { preimage: payment_preimage, - secret: payment_secret, + secret: payment_secret.map(|a| *a), status, - amt_msat: MillisatAmount(Some(amt)), + amt_msat: MillisatAmount(Some(*amt)), }); } } @@ -191,7 +191,7 @@ async fn handle_ldk_events( let mut payments = outbound_payments.lock().unwrap(); for (payment_hash, payment) in payments.iter_mut() { if *payment_hash == hashed { - payment.preimage = Some(payment_preimage); + payment.preimage = Some(*payment_preimage); payment.status = HTLCStatus::Succeeded; println!( "\nEVENT: successfully sent payment of {} millisatoshis from \ @@ -210,7 +210,7 @@ async fn handle_ldk_events( "\nEVENT: Failed to send payment to payment hash {:?}: ", hex_utils::hex_str(&payment_hash.0) ); - if rejected_by_dest { + if *rejected_by_dest { println!("re-attempting the payment will not succeed"); } else { println!("payment may be retried"); @@ -225,7 +225,7 @@ async fn handle_ldk_events( } } Event::PaymentForwarded { fee_earned_msat, claim_from_onchain_tx } => { - let from_onchain_str = if claim_from_onchain_tx { + let from_onchain_str = if *claim_from_onchain_tx { "from onchain downstream claim" } else { "from HTLC fulfill message" @@ -243,8 +243,8 @@ async fn handle_ldk_events( } Event::PendingHTLCsForwardable { time_forwardable } => { let forwarding_channel_manager = channel_manager.clone(); + let min = time_forwardable.as_millis() as u64; tokio::spawn(async move { - let min = time_forwardable.as_millis() as u64; let millis_to_sleep = thread_rng().gen_range(min, min * 5) as u64; tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await; forwarding_channel_manager.process_pending_htlc_forwards(); @@ -557,7 +557,7 @@ async fn start_ldk() { inbound_pmts_for_events.clone(), outbound_pmts_for_events.clone(), network, - event.clone(), + event, )) }; // Step 16: Persist ChannelManager