Skip to content

Commit 9a7ef43

Browse files
committed
Don't remove nodes if there's no channel_update for a temp failure
Previously, we were requiring any `UPDATE` onion errors to include a `channel_update`, as the spec mandates[1]. If we see an onion error which is missing one we treat it as a misbehaving node that isn't behaving according to the spec and simply remove the node. Sadly, it appears at least some versions of CLN are such nodes, and opt to not include `channel_update` at all if they're returning a `temporary_channel_failure`. This causes us to completely remove CLN nodes from our graph after they fail to forward our HTLC. While CLN is violating the spec here, there's not a lot of reason to not allow it, so we go ahead and do so here, treating it simply as any other failure by letting the scorer handle it. [1] The spec says `Please note that the channel_update field is mandatory in messages whose failure_code includes the UPDATE flag` however doesn't repeat it in the requirements section so its not crazy that someone missed it when implementing.
1 parent bc54441 commit 9a7ef43

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,21 +493,28 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
493493
} else {
494494
log_trace!(logger, "Failure provided features a channel update without type prefix. Deprecated, but allowing for now.");
495495
}
496-
if let Ok(chan_update) = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice)) {
496+
let update_opt = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice));
497+
if update_opt.is_ok() || update_slice.is_empty() {
497498
// if channel_update should NOT have caused the failure:
498499
// MAY treat the channel_update as invalid.
499500
let is_chan_update_invalid = match error_code & 0xff {
500501
7 => false,
501-
11 => amt_to_forward > chan_update.contents.htlc_minimum_msat,
502-
12 => amt_to_forward
503-
.checked_mul(chan_update.contents.fee_proportional_millionths as u64)
502+
11 => update_opt.is_ok() &&
503+
amt_to_forward >
504+
update_opt.as_ref().unwrap().contents.htlc_minimum_msat,
505+
12 => update_opt.is_ok() && amt_to_forward
506+
.checked_mul(update_opt.as_ref().unwrap()
507+
.contents.fee_proportional_millionths as u64)
504508
.map(|prop_fee| prop_fee / 1_000_000)
505-
.and_then(|prop_fee| prop_fee.checked_add(chan_update.contents.fee_base_msat as u64))
509+
.and_then(|prop_fee| prop_fee.checked_add(
510+
update_opt.as_ref().unwrap().contents.fee_base_msat as u64))
506511
.map(|fee_msats| route_hop.fee_msat >= fee_msats)
507512
.unwrap_or(false),
508-
13 => route_hop.cltv_expiry_delta as u16 >= chan_update.contents.cltv_expiry_delta,
513+
13 => update_opt.is_ok() &&
514+
route_hop.cltv_expiry_delta as u16 >=
515+
update_opt.as_ref().unwrap().contents.cltv_expiry_delta,
509516
14 => false, // expiry_too_soon; always valid?
510-
20 => chan_update.contents.flags & 2 == 0,
517+
20 => update_opt.as_ref().unwrap().contents.flags & 2 == 0,
511518
_ => false, // unknown error code; take channel_update as valid
512519
};
513520
if is_chan_update_invalid {
@@ -518,17 +525,31 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
518525
is_permanent: true,
519526
});
520527
} else {
521-
// Make sure the ChannelUpdate contains the expected
522-
// short channel id.
523-
if failing_route_hop.short_channel_id == chan_update.contents.short_channel_id {
524-
short_channel_id = Some(failing_route_hop.short_channel_id);
528+
if let Ok(chan_update) = update_opt {
529+
// Make sure the ChannelUpdate contains the expected
530+
// short channel id.
531+
if failing_route_hop.short_channel_id == chan_update.contents.short_channel_id {
532+
short_channel_id = Some(failing_route_hop.short_channel_id);
533+
} else {
534+
log_info!(logger, "Node provided a channel_update for which it was not authoritative, ignoring.");
535+
}
536+
network_update = Some(NetworkUpdate::ChannelUpdateMessage {
537+
msg: chan_update,
538+
})
525539
} else {
526-
log_info!(logger, "Node provided a channel_update for which it was not authoritative, ignoring.");
540+
network_update = Some(NetworkUpdate::ChannelFailure {
541+
short_channel_id: route_hop.short_channel_id,
542+
is_permanent: false,
543+
});
527544
}
528-
network_update = Some(NetworkUpdate::ChannelUpdateMessage {
529-
msg: chan_update,
530-
})
531545
};
546+
} else {
547+
// If the channel_update had a non-zero length (i.e. was
548+
// present) but we coulnd't read it, treat it as a total
549+
// node failure.
550+
log_info!(logger,
551+
"Failed to read a channel_update of len {} in an onion",
552+
update_slice.len());
532553
}
533554
}
534555
}

lightning/src/routing/gossip.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,9 +1633,12 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16331633
}
16341634

16351635
/// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
1636+
///
16361637
/// If permanent, removes a channel from the local storage.
16371638
/// May cause the removal of nodes too, if this was their last channel.
1638-
/// If not permanent, makes channels unavailable for routing.
1639+
///
1640+
/// If not permanent, no action is taken as such a failure likely indicates the node simply
1641+
/// lacked liquidity and your scorer should handle this instead.
16391642
pub fn channel_failed(&self, short_channel_id: u64, is_permanent: bool) {
16401643
#[cfg(feature = "std")]
16411644
let current_time_unix = Some(SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs());
@@ -1646,9 +1649,12 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16461649
}
16471650

16481651
/// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
1652+
///
16491653
/// If permanent, removes a channel from the local storage.
16501654
/// May cause the removal of nodes too, if this was their last channel.
1651-
/// If not permanent, makes channels unavailable for routing.
1655+
///
1656+
/// If not permanent, no action is taken as such a failure likely indicates the node simply
1657+
/// lacked liquidity and your scorer should handle this instead.
16521658
fn channel_failed_with_time(&self, short_channel_id: u64, is_permanent: bool, current_time_unix: Option<u64>) {
16531659
let mut channels = self.channels.write().unwrap();
16541660
if is_permanent {
@@ -1657,15 +1663,6 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16571663
self.removed_channels.lock().unwrap().insert(short_channel_id, current_time_unix);
16581664
Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
16591665
}
1660-
} else {
1661-
if let Some(chan) = channels.get_mut(&short_channel_id) {
1662-
if let Some(one_to_two) = chan.one_to_two.as_mut() {
1663-
one_to_two.enabled = false;
1664-
}
1665-
if let Some(two_to_one) = chan.two_to_one.as_mut() {
1666-
two_to_one.enabled = false;
1667-
}
1668-
}
16691666
}
16701667
}
16711668

0 commit comments

Comments
 (0)