Skip to content

Commit 70b026c

Browse files
authored
Merge pull request #224 from TheBlueMatt/2018-10-221-whitespace
#221 with a few trailing spaces removed
2 parents f5ff5d1 + bdbb2f0 commit 70b026c

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crypto::digest::Digest;
1515
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
1616
use lightning::chain::transaction::OutPoint;
1717
use lightning::ln::channelmonitor;
18-
use lightning::ln::channelmanager::ChannelManager;
18+
use lightning::ln::channelmanager::{ChannelManager, PaymentFailReason};
1919
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
2020
use lightning::ln::router::Router;
2121
use lightning::util::events::{EventsProvider,Event};
@@ -337,7 +337,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
337337
// fulfill this HTLC, but if they are, we can just take the first byte and
338338
// place that anywhere in our preimage.
339339
if &payment[1..] != &[0; 31] {
340-
channelmanager.fail_htlc_backwards(&payment);
340+
channelmanager.fail_htlc_backwards(&payment, PaymentFailReason::PreimageUnknown);
341341
} else {
342342
let mut payment_preimage = [0; 32];
343343
payment_preimage[0] = payment[0];
@@ -347,7 +347,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
347347
},
348348
9 => {
349349
for payment in payments_received.drain(..) {
350-
channelmanager.fail_htlc_backwards(&payment);
350+
channelmanager.fail_htlc_backwards(&payment, PaymentFailReason::PreimageUnknown);
351351
}
352352
},
353353
10 => {

src/ln/channelmanager.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ impl MsgHandleErrInternal {
221221
}
222222
}
223223

224+
/// Pass to fail_htlc_backwwards to indicate the reason to fail the payment
225+
/// after a PaymentReceived event.
226+
#[derive(PartialEq)]
227+
pub enum PaymentFailReason {
228+
/// Indicate the preimage for payment_hash is not known after a PaymentReceived event
229+
PreimageUnknown,
230+
/// Indicate the payment amount is incorrect ( received is < expected or > 2*expected ) after a PaymentReceived event
231+
AmountMismatch,
232+
}
233+
224234
/// We hold back HTLCs we intend to relay for a random interval in the range (this, 5*this). This
225235
/// provides some limited amount of privacy. Ideally this would range from somewhere like 1 second
226236
/// to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly. We could
@@ -1393,16 +1403,14 @@ impl ChannelManager {
13931403
events.append(&mut new_events);
13941404
}
13951405

1396-
/// Indicates that the preimage for payment_hash is unknown after a PaymentReceived event.
1397-
pub fn fail_htlc_backwards(&self, payment_hash: &[u8; 32]) -> bool {
1398-
// TODO: Add ability to return 0x4000|16 (incorrect_payment_amount) if the amount we
1399-
// received is < expected or > 2*expected
1406+
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect after a PaymentReceived event.
1407+
pub fn fail_htlc_backwards(&self, payment_hash: &[u8; 32], reason: PaymentFailReason) -> bool {
14001408
let mut channel_state = Some(self.channel_state.lock().unwrap());
14011409
let removed_source = channel_state.as_mut().unwrap().claimable_htlcs.remove(payment_hash);
14021410
if let Some(mut sources) = removed_source {
14031411
for htlc_with_hash in sources.drain(..) {
14041412
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
1405-
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() });
1413+
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() });
14061414
}
14071415
true
14081416
} else { false }
@@ -2677,7 +2685,7 @@ mod tests {
26772685
use chain::chaininterface;
26782686
use chain::transaction::OutPoint;
26792687
use chain::chaininterface::ChainListener;
2680-
use ln::channelmanager::{ChannelManager,OnionKeys};
2688+
use ln::channelmanager::{ChannelManager,OnionKeys,PaymentFailReason};
26812689
use ln::channelmonitor::{ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS};
26822690
use ln::router::{Route, RouteHop, Router};
26832691
use ln::msgs;
@@ -3368,7 +3376,7 @@ mod tests {
33683376
}
33693377

33703378
fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: [u8; 32]) {
3371-
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
3379+
assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, PaymentFailReason::PreimageUnknown));
33723380
check_added_monitors!(expected_route.last().unwrap(), 1);
33733381

33743382
let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;

src/util/events.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ pub enum Event {
5050
},
5151
/// Indicates we've received money! Just gotta dig out that payment preimage and feed it to
5252
/// ChannelManager::claim_funds to get it....
53-
/// Note that if the preimage is not known, you must call ChannelManager::fail_htlc_backwards
54-
/// to free up resources for this HTLC.
53+
/// Note that if the preimage is not known or the amount paid is incorrect, you must call
54+
/// ChannelManager::fail_htlc_backwards with PaymentFailReason::PreimageUnknown or
55+
/// PaymentFailReason::AmountMismatch, respectively, to free up resources for this HTLC.
56+
/// The amount paid should be considered 'incorrect' when it is less than or more than twice
57+
/// the amount expected.
5558
PaymentReceived {
5659
/// The hash for which the preimage should be handed to the ChannelManager.
5760
payment_hash: [u8; 32],

0 commit comments

Comments
 (0)