Skip to content

Commit 322fa64

Browse files
committed
f: Do not write deprecated UnknownNextPeer type
This allows us to more easily get rid of this variant (as we stop writing it), and removes the need for custom serialization. The tradeoff here is that downgrading nodes lose some information, as the UnknownNextPeer variant will be represented as InvalidForward (because they can't read the accompanying reason that specifies that it's a UnknownNextPeer.
1 parent 51eb743 commit 322fa64

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

lightning/src/events/mod.rs

+25-36
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ pub enum HTLCHandlingFailureType {
481481
},
482482
/// Scenario where we are unsure of the next node to forward the HTLC to.
483483
///
484-
/// Deprecated: will only be used in versions before LDK v0.2.0.
484+
/// Deprecated: will only be used in versions before LDK v0.2.0. Downgrades will result in
485+
/// this type being represented as [`Self::InvalidForward`].
485486
UnknownNextHop {
486487
/// Short channel id we are requesting to forward an HTLC to.
487488
requested_forward_scid: u64,
@@ -1801,26 +1802,10 @@ impl Writeable for Event {
18011802
},
18021803
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failure_type, ref failure_reason } => {
18031804
25u8.write(writer)?;
1804-
1805-
// The [`HTLCHandlingType::UnknownNextPeer`] variant is deprecated, but we want to
1806-
// continue writing it to allow downgrading. Detect the case where we're
1807-
// representing it as [`HTLCHandlingType::InvalidForward`] and
1808-
// [`LocalHTLCFailureReason::UnknownNextHop`] and write the old variant instead.
1809-
let downgradable_type = match (failure_type, failure_reason) {
1810-
(HTLCHandlingFailureType::InvalidForward { requested_forward_scid },
1811-
Some(HTLCHandlingFailureReason::Local {
1812-
reason: LocalHTLCFailureReason::UnknownNextPeer
1813-
}))
1814-
=> HTLCHandlingFailureType::UnknownNextHop {
1815-
requested_forward_scid: *requested_forward_scid,
1816-
},
1817-
_ => failure_type.clone()
1818-
};
1819-
18201805
write_tlv_fields!(writer, {
18211806
(0, prev_channel_id, required),
18221807
(1, failure_reason, option),
1823-
(2, downgradable_type, required),
1808+
(2, failure_type, required),
18241809
})
18251810
},
18261811
&Event::BumpTransaction(ref event)=> {
@@ -2276,30 +2261,34 @@ impl MaybeReadable for Event {
22762261
(2, failure_type_opt, upgradable_required),
22772262
});
22782263

2279-
let mut event = Event::HTLCHandlingFailed {
2264+
let event = Event::HTLCHandlingFailed {
22802265
prev_channel_id,
22812266
failure_type: _init_tlv_based_struct_field!(failure_type_opt, upgradable_required),
22822267
failure_reason,
22832268
};
22842269

2285-
// The [`HTLCHandlingFailureType::UnknownNextPeer`] variant is deprecated, but
2286-
// we continue writing it to allow downgrading. If it was written, upgrade
2287-
// it to its new representation of [`HTLCHandlingFailureType::InvalidForward`]
2288-
// and [`LocalHTLCFailureReason::UnknownNextHop`]. This will cover both the case
2289-
// where we have a legacy event and new events that are written with the legacy
2290-
// type be downgradable.
2291-
match event {
2292-
Event::HTLCHandlingFailed { failure_type: HTLCHandlingFailureType::UnknownNextHop { requested_forward_scid }, .. } => {
2293-
event = Event::HTLCHandlingFailed {
2294-
prev_channel_id,
2295-
failure_type: HTLCHandlingFailureType::InvalidForward { requested_forward_scid },
2296-
failure_reason: Some(LocalHTLCFailureReason::UnknownNextPeer.into()),
2297-
}
2298-
}
2299-
_ => panic!("HTLCHandlingFailed wrong type")
2300-
}
2270+
// If a legacy HTLCHandlingFailureType::UnknownNextHop was written, upgrade
2271+
// it to its new representation, otherwise leave unchanged.
2272+
Ok(Some(match event {
2273+
Event::HTLCHandlingFailed { prev_channel_id, ref failure_type, ref failure_reason } => {
2274+
match failure_type {
2275+
HTLCHandlingFailureType::UnknownNextHop { requested_forward_scid } => {
2276+
debug_assert!(failure_reason.is_none(),
2277+
"new failure reason should not be written with legacy UnknownNextHop type");
23012278

2302-
Ok(Some(event))
2279+
Event::HTLCHandlingFailed {
2280+
prev_channel_id,
2281+
failure_type: HTLCHandlingFailureType::InvalidForward {
2282+
requested_forward_scid: *requested_forward_scid,
2283+
},
2284+
failure_reason: Some(LocalHTLCFailureReason::UnknownNextPeer.into()),
2285+
}
2286+
}
2287+
_ => event,
2288+
}
2289+
},
2290+
_ => panic!("Event::HTLCHandlingFailed type incorrect"),
2291+
}))
23032292
};
23042293
f()
23052294
},

0 commit comments

Comments
 (0)