Skip to content

Commit 9b523ad

Browse files
committed
Don't fail read b/c invalid chan upd. and node ann
1 parent ab08d38 commit 9b523ad

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.
@@ -989,11 +1086,51 @@ impl fmt::Display for NodeInfo {
9891086
}
9901087
}
9911088

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

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

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

0 commit comments

Comments
 (0)