Skip to content

Commit 3d37831

Browse files
committed
Don't fail read b/c invalid chan upd. and node ann
1 parent ab094ff commit 3d37831

File tree

2 files changed

+228
-28
lines changed

2 files changed

+228
-28
lines changed

lightning/src/ln/msgs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,8 +2149,6 @@ mod tests {
21492149
do_encoding_channel_update(true, false, true);
21502150
do_encoding_channel_update(false, true, false);
21512151
do_encoding_channel_update(false, true, true);
2152-
do_encoding_channel_update(false, false, false);
2153-
do_encoding_channel_update(false, false, true);
21542152
do_encoding_channel_update(true, true, false);
21552153
do_encoding_channel_update(true, true, true);
21562154
}

lightning/src/routing/gossip.rs

Lines changed: 228 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHan
2828
use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter};
2929
use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
3030
use ln::msgs;
31-
use util::ser::{Readable, ReadableArgs, Writeable, Writer};
31+
use util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
3232
use util::logger::{Logger, Level};
3333
use util::events::{Event, EventHandler, MessageSendEvent, MessageSendEventsProvider};
3434
use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
@@ -628,15 +628,56 @@ impl fmt::Display for ChannelUpdateInfo {
628628
}
629629
}
630630

631-
impl_writeable_tlv_based!(ChannelUpdateInfo, {
632-
(0, last_update, required),
633-
(2, enabled, required),
634-
(4, cltv_expiry_delta, required),
635-
(6, htlc_minimum_msat, required),
636-
(8, htlc_maximum_msat, required),
637-
(10, fees, required),
638-
(12, last_update_message, required),
639-
});
631+
impl Writeable for ChannelUpdateInfo {
632+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
633+
write_tlv_fields!(writer, {
634+
(0, self.last_update, required),
635+
(2, self.enabled, required),
636+
(4, self.cltv_expiry_delta, required),
637+
(6, self.htlc_minimum_msat, required),
638+
(8, Some(self.htlc_maximum_msat), required),
639+
(10, self.fees, required),
640+
(12, self.last_update_message, required),
641+
});
642+
Ok(())
643+
}
644+
}
645+
646+
impl Readable for ChannelUpdateInfo {
647+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
648+
init_tlv_field_var!(last_update, required);
649+
init_tlv_field_var!(enabled, required);
650+
init_tlv_field_var!(cltv_expiry_delta, required);
651+
init_tlv_field_var!(htlc_minimum_msat, required);
652+
init_tlv_field_var!(htlc_maximum_msat, option);
653+
init_tlv_field_var!(fees, required);
654+
init_tlv_field_var!(last_update_message, required);
655+
656+
read_tlv_fields!(reader, {
657+
(0, last_update, required),
658+
(2, enabled, required),
659+
(4, cltv_expiry_delta, required),
660+
(6, htlc_minimum_msat, required),
661+
(8, htlc_maximum_msat, required),
662+
(10, fees, required),
663+
(12, last_update_message, required)
664+
});
665+
666+
if let Some(htlc_maximum_msat) = htlc_maximum_msat {
667+
Ok(ChannelUpdateInfo {
668+
last_update: init_tlv_based_struct_field!(last_update, required),
669+
enabled: init_tlv_based_struct_field!(enabled, required),
670+
cltv_expiry_delta: init_tlv_based_struct_field!(cltv_expiry_delta, required),
671+
htlc_minimum_msat: init_tlv_based_struct_field!(htlc_minimum_msat, required),
672+
htlc_maximum_msat,
673+
fees: init_tlv_based_struct_field!(fees, required),
674+
last_update_message: init_tlv_based_struct_field!(last_update_message, required),
675+
})
676+
} else {
677+
Err(DecodeError::InvalidValue)
678+
}
679+
}
680+
}
640681

641682
#[derive(Clone, Debug, PartialEq)]
642683
/// Details about a channel (both directions).
@@ -715,16 +756,72 @@ impl fmt::Display for ChannelInfo {
715756
}
716757
}
717758

718-
impl_writeable_tlv_based!(ChannelInfo, {
719-
(0, features, required),
720-
(1, announcement_received_time, (default_value, 0)),
721-
(2, node_one, required),
722-
(4, one_to_two, required),
723-
(6, node_two, required),
724-
(8, two_to_one, required),
725-
(10, capacity_sats, required),
726-
(12, announcement_message, required),
727-
});
759+
impl Writeable for ChannelInfo {
760+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
761+
write_tlv_fields!(writer, {
762+
(0, self.features, required),
763+
(1, self.announcement_received_time, (default_value, 0)),
764+
(2, self.node_one, required),
765+
(4, self.one_to_two, required),
766+
(6, self.node_two, required),
767+
(8, self.two_to_one, required),
768+
(10, self.capacity_sats, required),
769+
(12, self.announcement_message, required),
770+
});
771+
Ok(())
772+
}
773+
}
774+
775+
// A wrapper allowing for the optional deseralization of ChannelUpdateInfo. Utilizing this is
776+
// necessary to maintain backwards compatibility with previous serializations of `ChannelUpdateInfo`
777+
// that may have no `htlc_maximum_msat` field set. In case the field is absent, we simply ignore
778+
// the error and continue reading the `ChannelInfo`. Hopefully, we'll then eventually receive newer
779+
// channel updates via the gossip network.
780+
struct ChannelUpdateInfoDeserWrapper(Option<ChannelUpdateInfo>);
781+
782+
impl MaybeReadable for ChannelUpdateInfoDeserWrapper {
783+
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
784+
match ::util::ser::Readable::read(reader) {
785+
Ok(channel_update_option) => Ok(Some(Self(channel_update_option))),
786+
Err(DecodeError::ShortRead) => Ok(None),
787+
Err(err) => Err(err),
788+
}
789+
}
790+
}
791+
792+
impl Readable for ChannelInfo {
793+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
794+
init_tlv_field_var!(features, required);
795+
init_tlv_field_var!(announcement_received_time, (default_value, 0));
796+
init_tlv_field_var!(node_one, required);
797+
let mut one_to_two_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
798+
init_tlv_field_var!(node_two, required);
799+
let mut two_to_one_wrap: Option<ChannelUpdateInfoDeserWrapper> = None;
800+
init_tlv_field_var!(capacity_sats, required);
801+
init_tlv_field_var!(announcement_message, required);
802+
read_tlv_fields!(reader, {
803+
(0, features, required),
804+
(1, announcement_received_time, (default_value, 0)),
805+
(2, node_one, required),
806+
(4, one_to_two_wrap, ignorable),
807+
(6, node_two, required),
808+
(8, two_to_one_wrap, ignorable),
809+
(10, capacity_sats, required),
810+
(12, announcement_message, required),
811+
});
812+
813+
Ok(ChannelInfo {
814+
features: init_tlv_based_struct_field!(features, required),
815+
node_one: init_tlv_based_struct_field!(node_one, required),
816+
one_to_two: one_to_two_wrap.map(|w| w.0).unwrap_or(None),
817+
node_two: init_tlv_based_struct_field!(node_two, required),
818+
two_to_one: two_to_one_wrap.map(|w| w.0).unwrap_or(None),
819+
capacity_sats: init_tlv_based_struct_field!(capacity_sats, required),
820+
announcement_message: init_tlv_based_struct_field!(announcement_message, required),
821+
announcement_received_time: init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
822+
})
823+
}
824+
}
728825

729826
/// A wrapper around [`ChannelInfo`] representing information about the channel as directed from a
730827
/// source node to a target node.
@@ -993,11 +1090,51 @@ impl fmt::Display for NodeInfo {
9931090
}
9941091
}
9951092

996-
impl_writeable_tlv_based!(NodeInfo, {
997-
(0, lowest_inbound_channel_fees, option),
998-
(2, announcement_info, option),
999-
(4, channels, vec_type),
1000-
});
1093+
impl Writeable for NodeInfo {
1094+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1095+
write_tlv_fields!(writer, {
1096+
(0, self.lowest_inbound_channel_fees, option),
1097+
(2, self.announcement_info, option),
1098+
(4, self.channels, vec_type),
1099+
});
1100+
Ok(())
1101+
}
1102+
}
1103+
1104+
// A wrapper allowing for the optional deseralization of `NodeAnnouncementInfo`. Utilizing this is
1105+
// necessary to maintain compatibility with previous serializations of `NodeAnnouncementInfo` that have an
1106+
// invalid hostname set. In this case, we simply ignore the error and continue reading the `NodeInfo`.
1107+
struct NodeAnnouncementInfoDeserWrapper(NodeAnnouncementInfo);
1108+
1109+
impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
1110+
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
1111+
match ::util::ser::Readable::read(reader) {
1112+
Ok(node_announcement) => Ok(Some(Self(node_announcement))),
1113+
Err(DecodeError::ShortRead) => Ok(None),
1114+
Err(err) => Err(err),
1115+
}
1116+
}
1117+
}
1118+
1119+
impl Readable for NodeInfo {
1120+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1121+
init_tlv_field_var!(lowest_inbound_channel_fees, option);
1122+
let mut announcement_info_wrap: Option<NodeAnnouncementInfoDeserWrapper> = None;
1123+
init_tlv_field_var!(channels, vec_type);
1124+
1125+
read_tlv_fields!(reader, {
1126+
(0, lowest_inbound_channel_fees, option),
1127+
(2, announcement_info_wrap, ignorable),
1128+
(4, channels, vec_type),
1129+
});
1130+
1131+
Ok(NodeInfo {
1132+
lowest_inbound_channel_fees: init_tlv_based_struct_field!(lowest_inbound_channel_fees, option),
1133+
announcement_info: announcement_info_wrap.map(|w| w.0),
1134+
channels: init_tlv_based_struct_field!(channels, vec_type),
1135+
})
1136+
}
1137+
}
10011138

10021139
const SERIALIZATION_VERSION: u8 = 1;
10031140
const MIN_SERIALIZATION_VERSION: u8 = 1;
@@ -1676,7 +1813,7 @@ mod tests {
16761813
use chain;
16771814
use ln::PaymentHash;
16781815
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
1679-
use routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY};
1816+
use routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo};
16801817
use ln::msgs::{Init, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
16811818
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
16821819
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2802,6 +2939,71 @@ mod tests {
28022939
assert_eq!(format_bytes_alias(b"\xFFI <heart>\0LDK!"), "\u{FFFD}I <heart>");
28032940
assert_eq!(format_bytes_alias(b"\xFFI <heart>\tLDK!"), "\u{FFFD}I <heart>\u{FFFD}LDK!");
28042941
}
2942+
2943+
#[test]
2944+
fn written_channel_info_is_readable() {
2945+
let chanmon_cfgs = ::ln::functional_test_utils::create_chanmon_cfgs(2);
2946+
let node_cfgs = ::ln::functional_test_utils::create_node_cfgs(2, &chanmon_cfgs);
2947+
let node_chanmgrs = ::ln::functional_test_utils::create_node_chanmgrs(2, &node_cfgs, &[None, None, None, None]);
2948+
let nodes = ::ln::functional_test_utils::create_network(2, &node_cfgs, &node_chanmgrs);
2949+
2950+
// First make sure we can encode/decode ChannelUpdateInfo.
2951+
let chan_update_info = ChannelUpdateInfo {
2952+
last_update: 23,
2953+
enabled: true,
2954+
cltv_expiry_delta: 42,
2955+
htlc_minimum_msat: 1234,
2956+
htlc_maximum_msat: 5678,
2957+
fees: RoutingFees { base_msat: 9, proportional_millionths: 10 },
2958+
last_update_message: None,
2959+
};
2960+
2961+
let mut buf: Vec<u8> = Vec::new();
2962+
assert!(chan_update_info.write(&mut buf).is_ok());
2963+
2964+
let read_chan_update_info_result: Option<ChannelUpdateInfo> = ::util::ser::MaybeReadable::read(&mut buf.as_slice()).unwrap();
2965+
if let Some(read_chan_update_info) = read_chan_update_info_result {
2966+
assert_eq!(chan_update_info, read_chan_update_info);
2967+
} else {
2968+
panic!();
2969+
}
2970+
2971+
// Then check we can encode/decode ChannelInfo without ChannelUpdateInfo fields present.
2972+
let chan_info_none_updates = ChannelInfo {
2973+
features: ChannelFeatures::known(),
2974+
node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
2975+
one_to_two: None,
2976+
node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
2977+
two_to_one: None,
2978+
capacity_sats: None,
2979+
announcement_message: None,
2980+
announcement_received_time: 87654,
2981+
};
2982+
2983+
let mut buf: Vec<u8> = Vec::new();
2984+
assert!(chan_info_none_updates.write(&mut buf).is_ok());
2985+
2986+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut buf.as_slice()).unwrap();
2987+
assert_eq!(chan_info_none_updates, read_chan_info);
2988+
2989+
// Finally check we can encode/decode ChannelInfo with ChannelUpdateInfo fields present.
2990+
let chan_info_some_updates = ChannelInfo {
2991+
features: ChannelFeatures::known(),
2992+
node_one: NodeId::from_pubkey(&nodes[0].node.get_our_node_id()),
2993+
one_to_two: Some(chan_update_info.clone()),
2994+
node_two: NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
2995+
two_to_one: Some(chan_update_info.clone()),
2996+
capacity_sats: None,
2997+
announcement_message: None,
2998+
announcement_received_time: 87654,
2999+
};
3000+
3001+
let mut buf: Vec<u8> = Vec::new();
3002+
assert!(chan_info_some_updates.write(&mut buf).is_ok());
3003+
3004+
let read_chan_info: ChannelInfo = ::util::ser::Readable::read(&mut buf.as_slice()).unwrap();
3005+
assert_eq!(chan_info_some_updates, read_chan_info);
3006+
}
28053007
}
28063008

28073009
#[cfg(all(test, feature = "_bench_unstable"))]

0 commit comments

Comments
 (0)