Skip to content

Commit

Permalink
Cleaner way to eat all NodeAnnouncementInfo errs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 22, 2022
1 parent c6ab815 commit c1bfa3f
Showing 1 changed file with 16 additions and 72 deletions.
88 changes: 16 additions & 72 deletions lightning/src/routing/gossip.rs
Expand Up @@ -1012,73 +1012,14 @@ pub struct NodeAnnouncementInfo {
pub announcement_message: Option<NodeAnnouncement>
}

impl Writeable for NodeAnnouncementInfo {
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.features, required),
(2, self.last_update, required),
(4, self.rgb, required),
(6, self.alias, required),
(8, self.announcement_message, option),
(10, self.addresses, vec_type),
});

Ok(())
}
}

// A wrapper allowing for the optional deseralization of `Vec<NetAddress>`. Utilizing this is
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
// invalid hostname set. In this case, we simply ignore such invalid entries.
struct NetAddressVecDeserWrapper(Vec<NetAddress>);

impl Readable for NetAddressVecDeserWrapper {
fn read<R: io::Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
let mut values = Vec::new();
loop {
let mut track_read = ::util::ser::ReadTrackingReader::new(&mut reader);
match MaybeReadable::read(&mut track_read) {
Ok(Some(v)) => { values.push(v); },
Ok(None) => { },
Err(DecodeError::InvalidValue) => { },
Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
Err(e) => return Err(e),
}
}
Ok(Self(values))
}
}


impl Readable for NodeAnnouncementInfo {
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
init_tlv_field_var!(features, required);
init_tlv_field_var!(last_update, required);
init_tlv_field_var!(rgb, required);
init_tlv_field_var!(alias, required);
init_tlv_field_var!(announcement_message, option);
let mut addresses = NetAddressVecDeserWrapper(Vec::new());

read_tlv_fields!(reader, {
(0, features, required),
(2, last_update, required),
(4, rgb, required),
(6, alias, required),
(8, announcement_message, option),
(10, addresses, required),
});

Ok(NodeAnnouncementInfo{
features: init_tlv_based_struct_field!(features, required),
last_update: init_tlv_based_struct_field!(last_update, required),
rgb: init_tlv_based_struct_field!(rgb, required),
alias: init_tlv_based_struct_field!(alias, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, option),
addresses: addresses.0,
})
}
}

impl_writeable_tlv_based!(NodeAnnouncementInfo, {
(0, features, required),
(2, last_update, required),
(4, rgb, required),
(6, alias, required),
(8, announcement_message, option),
(10, addresses, vec_type),
});

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

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

impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
match ::util::ser::Readable::read(reader) {
Ok(node_announcement) => Ok(Some(Self(node_announcement))),
Err(DecodeError::ShortRead) => Ok(None),
Err(err) => Err(err),
loop {
match ::util::ser::Readable::read(reader) {
Ok(node_announcement_info) => return Ok(Some(Self(node_announcement_info))),
Err(DecodeError::ShortRead) => return Ok(None),
Err(_) => {},
};
}
}
}
Expand Down

0 comments on commit c1bfa3f

Please sign in to comment.