Skip to content

Commit c1bfa3f

Browse files
committed
Cleaner way to eat all NodeAnnouncementInfo errs
1 parent c6ab815 commit c1bfa3f

File tree

1 file changed

+16
-72
lines changed

1 file changed

+16
-72
lines changed

lightning/src/routing/gossip.rs

+16-72
Original file line numberDiff line numberDiff line change
@@ -1012,73 +1012,14 @@ pub struct NodeAnnouncementInfo {
10121012
pub announcement_message: Option<NodeAnnouncement>
10131013
}
10141014

1015-
impl Writeable for NodeAnnouncementInfo {
1016-
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1017-
write_tlv_fields!(writer, {
1018-
(0, self.features, required),
1019-
(2, self.last_update, required),
1020-
(4, self.rgb, required),
1021-
(6, self.alias, required),
1022-
(8, self.announcement_message, option),
1023-
(10, self.addresses, vec_type),
1024-
});
1025-
1026-
Ok(())
1027-
}
1028-
}
1029-
1030-
// A wrapper allowing for the optional deseralization of `Vec<NetAddress>`. Utilizing this is
1031-
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
1032-
// invalid hostname set. In this case, we simply ignore such invalid entries.
1033-
struct NetAddressVecDeserWrapper(Vec<NetAddress>);
1034-
1035-
impl Readable for NetAddressVecDeserWrapper {
1036-
fn read<R: io::Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
1037-
let mut values = Vec::new();
1038-
loop {
1039-
let mut track_read = ::util::ser::ReadTrackingReader::new(&mut reader);
1040-
match MaybeReadable::read(&mut track_read) {
1041-
Ok(Some(v)) => { values.push(v); },
1042-
Ok(None) => { },
1043-
Err(DecodeError::InvalidValue) => { },
1044-
Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
1045-
Err(e) => return Err(e),
1046-
}
1047-
}
1048-
Ok(Self(values))
1049-
}
1050-
}
1051-
1052-
1053-
impl Readable for NodeAnnouncementInfo {
1054-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1055-
init_tlv_field_var!(features, required);
1056-
init_tlv_field_var!(last_update, required);
1057-
init_tlv_field_var!(rgb, required);
1058-
init_tlv_field_var!(alias, required);
1059-
init_tlv_field_var!(announcement_message, option);
1060-
let mut addresses = NetAddressVecDeserWrapper(Vec::new());
1061-
1062-
read_tlv_fields!(reader, {
1063-
(0, features, required),
1064-
(2, last_update, required),
1065-
(4, rgb, required),
1066-
(6, alias, required),
1067-
(8, announcement_message, option),
1068-
(10, addresses, required),
1069-
});
1070-
1071-
Ok(NodeAnnouncementInfo{
1072-
features: init_tlv_based_struct_field!(features, required),
1073-
last_update: init_tlv_based_struct_field!(last_update, required),
1074-
rgb: init_tlv_based_struct_field!(rgb, required),
1075-
alias: init_tlv_based_struct_field!(alias, required),
1076-
announcement_message: init_tlv_based_struct_field!(announcement_message, option),
1077-
addresses: addresses.0,
1078-
})
1079-
}
1080-
}
1081-
1015+
impl_writeable_tlv_based!(NodeAnnouncementInfo, {
1016+
(0, features, required),
1017+
(2, last_update, required),
1018+
(4, rgb, required),
1019+
(6, alias, required),
1020+
(8, announcement_message, option),
1021+
(10, addresses, vec_type),
1022+
});
10821023

10831024
/// A user-defined name for a node, which may be used when displaying the node in a graph.
10841025
///
@@ -1161,15 +1102,18 @@ impl Writeable for NodeInfo {
11611102

11621103
// A wrapper allowing for the optional deseralization of `NodeAnnouncementInfo`. Utilizing this is
11631104
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
1164-
// invalid hostname set.
1105+
// invalid hostname set. We ignore and eat all errors until we are either able to read a
1106+
// `NodeAnnouncementInfo` or hit a `ShortRead`, i.e., read the TLV field to the end.
11651107
struct NodeAnnouncementInfoDeserWrapper(NodeAnnouncementInfo);
11661108

11671109
impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
11681110
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
1169-
match ::util::ser::Readable::read(reader) {
1170-
Ok(node_announcement) => Ok(Some(Self(node_announcement))),
1171-
Err(DecodeError::ShortRead) => Ok(None),
1172-
Err(err) => Err(err),
1111+
loop {
1112+
match ::util::ser::Readable::read(reader) {
1113+
Ok(node_announcement_info) => return Ok(Some(Self(node_announcement_info))),
1114+
Err(DecodeError::ShortRead) => return Ok(None),
1115+
Err(_) => {},
1116+
};
11731117
}
11741118
}
11751119
}

0 commit comments

Comments
 (0)