X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchannelmanager.rs;h=31529343d56d22b646ab37c44f0f3639be7d2dec;hb=a8bc8fb3494def71db645cefac89abc133b95b99;hp=524f4045406847cce23d86359b27b44809110b52;hpb=5a1404809fcf2548924b0a7c0d7be73f24e59e09;p=rust-lightning diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 524f4045..31529343 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -451,7 +451,7 @@ pub struct ChannelManager>, @@ -588,6 +588,11 @@ pub(crate) const MAX_LOCAL_BREAKDOWN_TIMEOUT: u16 = 2 * 6 * 24 * 7; pub const MIN_CLTV_EXPIRY_DELTA: u16 = 6 * 6; pub(super) const CLTV_FAR_FAR_AWAY: u32 = 6 * 24 * 7; //TODO? +/// Minimum CLTV difference between the current block height and received inbound payments. +/// Invoices generated for payment to us must set their `min_final_cltv_expiry` field to at least +/// this value. +pub const MIN_FINAL_CLTV_EXPIRY: u32 = HTLC_FAIL_BACK_BUFFER; + // Check that our CLTV_EXPIRY is at least CLTV_CLAIM_BUFFER + ANTI_REORG_DELAY + LATENCY_GRACE_PERIOD_BLOCKS, // ie that if the next-hop peer fails the HTLC within // LATENCY_GRACE_PERIOD_BLOCKS then we'll still have CLTV_CLAIM_BUFFER left to timeout it onchain, @@ -2024,7 +2029,8 @@ impl ChannelMana } else if total_value == payment_data.total_msat { new_events.push(events::Event::PaymentReceived { payment_hash, - payment_secret: Some(payment_data.payment_secret), + payment_preimage: inbound_payment.get().payment_preimage, + payment_secret: payment_data.payment_secret, amt: total_value, user_payment_id: inbound_payment.get().user_payment_id, }); @@ -2303,7 +2309,7 @@ impl ChannelMana /// /// [`create_inbound_payment`]: Self::create_inbound_payment /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash - pub fn claim_funds(&self, payment_preimage: PaymentPreimage, expected_amount: u64) -> bool { + pub fn claim_funds(&self, payment_preimage: PaymentPreimage) -> bool { let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0).into_inner()); let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier); @@ -2324,7 +2330,7 @@ impl ChannelMana // we got all the HTLCs and then a channel closed while we were waiting for the user to // provide the preimage, so worrying too much about the optimal handling isn't worth // it. - let mut valid_mpp = sources[0].payment_data.total_msat >= expected_amount; + let mut valid_mpp = true; for htlc in sources.iter() { if let None = channel_state.as_ref().unwrap().short_to_id.get(&htlc.prev_hop.short_channel_id) { valid_mpp = false; @@ -3410,8 +3416,15 @@ impl ChannelMana /// This differs from [`create_inbound_payment_for_hash`] only in that it generates the /// [`PaymentHash`] and [`PaymentPreimage`] for you, returning the first and storing the second. /// + /// The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentReceived`], which + /// will have the [`PaymentReceived::payment_preimage`] field filled in. That should then be + /// passed directly to [`claim_funds`]. + /// /// See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements. /// + /// [`claim_funds`]: Self::claim_funds + /// [`PaymentReceived`]: events::Event::PaymentReceived + /// [`PaymentReceived::payment_preimage`]: events::Event::PaymentReceived::payment_preimage /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash pub fn create_inbound_payment(&self, min_value_msat: Option, invoice_expiry_delta_secs: u32, user_payment_id: u64) -> (PaymentHash, PaymentSecret) { let payment_preimage = PaymentPreimage(self.keys_manager.get_secure_random_bytes()); @@ -3457,6 +3470,9 @@ impl ChannelMana /// /// May panic if `invoice_expiry_delta_secs` is greater than one year. /// + /// Note that invoices generated for inbound payments should have their `min_final_cltv_expiry` + /// set to at least [`MIN_FINAL_CLTV_EXPIRY`]. + /// /// [`create_inbound_payment`]: Self::create_inbound_payment /// [`PaymentReceived`]: events::Event::PaymentReceived /// [`PaymentReceived::user_payment_id`]: events::Event::PaymentReceived::user_payment_id @@ -3593,6 +3609,10 @@ where } max_time!(self.last_node_announcement_serial); max_time!(self.highest_seen_timestamp); + let mut payment_secrets = self.pending_inbound_payments.lock().unwrap(); + payment_secrets.retain(|_, inbound_payment| { + inbound_payment.expiry_time > header.time as u64 + }); } fn get_relevant_txids(&self) -> Vec { @@ -4846,7 +4866,7 @@ pub mod bench { expect_pending_htlcs_forwardable!(NodeHolder { node: &$node_b }); expect_payment_received!(NodeHolder { node: &$node_b }, payment_hash, payment_secret, 10_000); - assert!($node_b.claim_funds(payment_preimage, 10_000)); + assert!($node_b.claim_funds(payment_preimage)); match $node_b.get_and_clear_pending_msg_events().pop().unwrap() { MessageSendEvent::UpdateHTLCs { node_id, updates } => {