From: Matt Corallo <649246+TheBlueMatt@users.noreply.github.com> Date: Fri, 26 Oct 2018 01:21:54 +0000 (-0400) Subject: Merge pull request #224 from TheBlueMatt/2018-10-221-whitespace X-Git-Tag: v0.0.12~287 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=70b026c3c58e74f99ca8e7e6b6b08bf046bb45d7;hp=f5ff5d1ad011a43a4ed1b86c29bf7ea67dca6f17;p=rust-lightning Merge pull request #224 from TheBlueMatt/2018-10-221-whitespace #221 with a few trailing spaces removed --- diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index c74201e5..d990ca7a 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -15,7 +15,7 @@ use crypto::digest::Digest; use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil}; use lightning::chain::transaction::OutPoint; use lightning::ln::channelmonitor; -use lightning::ln::channelmanager::ChannelManager; +use lightning::ln::channelmanager::{ChannelManager, PaymentFailReason}; use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; use lightning::ln::router::Router; use lightning::util::events::{EventsProvider,Event}; @@ -337,7 +337,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { // fulfill this HTLC, but if they are, we can just take the first byte and // place that anywhere in our preimage. if &payment[1..] != &[0; 31] { - channelmanager.fail_htlc_backwards(&payment); + channelmanager.fail_htlc_backwards(&payment, PaymentFailReason::PreimageUnknown); } else { let mut payment_preimage = [0; 32]; payment_preimage[0] = payment[0]; @@ -347,7 +347,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { }, 9 => { for payment in payments_received.drain(..) { - channelmanager.fail_htlc_backwards(&payment); + channelmanager.fail_htlc_backwards(&payment, PaymentFailReason::PreimageUnknown); } }, 10 => { diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index d6ba67c2..7e736ba1 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -221,6 +221,16 @@ impl MsgHandleErrInternal { } } +/// Pass to fail_htlc_backwwards to indicate the reason to fail the payment +/// after a PaymentReceived event. +#[derive(PartialEq)] +pub enum PaymentFailReason { + /// Indicate the preimage for payment_hash is not known after a PaymentReceived event + PreimageUnknown, + /// Indicate the payment amount is incorrect ( received is < expected or > 2*expected ) after a PaymentReceived event + AmountMismatch, +} + /// We hold back HTLCs we intend to relay for a random interval in the range (this, 5*this). This /// provides some limited amount of privacy. Ideally this would range from somewhere like 1 second /// to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly. We could @@ -1393,16 +1403,14 @@ impl ChannelManager { events.append(&mut new_events); } - /// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event. - pub fn fail_htlc_backwards(&self, payment_hash: &[u8; 32]) -> bool { - // TODO: Add ability to return 0x4000|16 (incorrect_payment_amount) if the amount we - // received is < expected or > 2*expected + /// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect after a PaymentReceived event. + pub fn fail_htlc_backwards(&self, payment_hash: &[u8; 32], reason: PaymentFailReason) -> bool { let mut channel_state = Some(self.channel_state.lock().unwrap()); let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(payment_hash); if let Some(mut sources) = removed_source { for htlc_with_hash in sources.drain(..) { if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); } - self.fail_htlc_backwards_internal(channel_state.take().unwrap(), HTLCSource::PreviousHopData(htlc_with_hash), payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: Vec::new() }); + self.fail_htlc_backwards_internal(channel_state.take().unwrap(), HTLCSource::PreviousHopData(htlc_with_hash), payment_hash, HTLCFailReason::Reason { failure_code: if reason == PaymentFailReason::PreimageUnknown {0x4000 | 15} else {0x4000 | 16}, data: Vec::new() }); } true } else { false } @@ -2677,7 +2685,7 @@ mod tests { use chain::chaininterface; use chain::transaction::OutPoint; use chain::chaininterface::ChainListener; - use ln::channelmanager::{ChannelManager,OnionKeys}; + use ln::channelmanager::{ChannelManager,OnionKeys,PaymentFailReason}; use ln::channelmonitor::{ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS}; use ln::router::{Route, RouteHop, Router}; use ln::msgs; @@ -3368,7 +3376,7 @@ mod tests { } fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) { - assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash)); + assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, PaymentFailReason::PreimageUnknown)); check_added_monitors!(expected_route.last().unwrap(), 1); let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None; diff --git a/src/util/events.rs b/src/util/events.rs index e11e4e82..ccfe0f8a 100644 --- a/src/util/events.rs +++ b/src/util/events.rs @@ -50,8 +50,11 @@ pub enum Event { }, /// Indicates we've received money! Just gotta dig out that payment preimage and feed it to /// ChannelManager::claim_funds to get it.... - /// Note that if the preimage is not known, you must call ChannelManager::fail_htlc_backwards - /// to free up resources for this HTLC. + /// Note that if the preimage is not known or the amount paid is incorrect, you must call + /// ChannelManager::fail_htlc_backwards with PaymentFailReason::PreimageUnknown or + /// PaymentFailReason::AmountMismatch, respectively, to free up resources for this HTLC. + /// The amount paid should be considered 'incorrect' when it is less than or more than twice + /// the amount expected. PaymentReceived { /// The hash for which the preimage should be handed to the ChannelManager. payment_hash: [u8; 32],