Skip to content

Commit 321b19c

Browse files
committed
Move NetworkGraph inner structs to TLV storage
1 parent 9ec0f04 commit 321b19c

File tree

2 files changed

+46
-108
lines changed

2 files changed

+46
-108
lines changed

lightning/src/ln/msgs.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,17 @@ impl Readable for Result<NetAddress, u8> {
482482
}
483483
}
484484

485+
impl Readable for NetAddress {
486+
fn read<R: Read>(reader: &mut R) -> Result<NetAddress, DecodeError> {
487+
match Readable::read(reader) {
488+
Ok(Ok(res)) => Ok(res),
489+
Ok(Err(_)) => Err(DecodeError::UnknownVersion),
490+
Err(e) => Err(e),
491+
}
492+
}
493+
}
494+
495+
485496
/// The unsigned part of a node_announcement
486497
#[derive(Clone, Debug, PartialEq)]
487498
pub struct UnsignedNodeAnnouncement {

lightning/src/routing/network_graph.rs

Lines changed: 35 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,15 @@ impl fmt::Display for DirectionalChannelInfo {
459459
}
460460
}
461461

462-
impl_writeable!(DirectionalChannelInfo, 0, {
463-
last_update,
464-
enabled,
465-
cltv_expiry_delta,
466-
htlc_minimum_msat,
467-
htlc_maximum_msat,
468-
fees,
469-
last_update_message
470-
});
462+
impl_writeable_tlv_based!(DirectionalChannelInfo, {
463+
(0, last_update),
464+
(2, enabled),
465+
(4, cltv_expiry_delta),
466+
(6, htlc_minimum_msat),
467+
(8, htlc_maximum_msat),
468+
(10, fees),
469+
(12, last_update_message),
470+
}, {}, {});
471471

472472
#[derive(Clone, Debug, PartialEq)]
473473
/// Details about a channel (both directions).
@@ -500,15 +500,15 @@ impl fmt::Display for ChannelInfo {
500500
}
501501
}
502502

503-
impl_writeable!(ChannelInfo, 0, {
504-
features,
505-
node_one,
506-
one_to_two,
507-
node_two,
508-
two_to_one,
509-
capacity_sats,
510-
announcement_message
511-
});
503+
impl_writeable_tlv_based!(ChannelInfo, {
504+
(0, features),
505+
(2, node_one),
506+
(4, one_to_two),
507+
(6, node_two),
508+
(8, two_to_one),
509+
(10, capacity_sats),
510+
(12, announcement_message),
511+
}, {}, {});
512512

513513

514514
/// Fees for routing via a given channel or a node
@@ -521,24 +521,7 @@ pub struct RoutingFees {
521521
pub proportional_millionths: u32,
522522
}
523523

524-
impl Readable for RoutingFees{
525-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<RoutingFees, DecodeError> {
526-
let base_msat: u32 = Readable::read(reader)?;
527-
let proportional_millionths: u32 = Readable::read(reader)?;
528-
Ok(RoutingFees {
529-
base_msat,
530-
proportional_millionths,
531-
})
532-
}
533-
}
534-
535-
impl Writeable for RoutingFees {
536-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
537-
self.base_msat.write(writer)?;
538-
self.proportional_millionths.write(writer)?;
539-
Ok(())
540-
}
541-
}
524+
impl_writeable_tlv_based!(RoutingFees, {(0, base_msat), (2, proportional_millionths)}, {}, {});
542525

543526
#[derive(Clone, Debug, PartialEq)]
544527
/// Information received in the latest node_announcement from this node.
@@ -563,48 +546,16 @@ pub struct NodeAnnouncementInfo {
563546
pub announcement_message: Option<NodeAnnouncement>
564547
}
565548

566-
impl Writeable for NodeAnnouncementInfo {
567-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
568-
self.features.write(writer)?;
569-
self.last_update.write(writer)?;
570-
self.rgb.write(writer)?;
571-
self.alias.write(writer)?;
572-
(self.addresses.len() as u64).write(writer)?;
573-
for ref addr in &self.addresses {
574-
addr.write(writer)?;
575-
}
576-
self.announcement_message.write(writer)?;
577-
Ok(())
578-
}
579-
}
580-
581-
impl Readable for NodeAnnouncementInfo {
582-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
583-
let features = Readable::read(reader)?;
584-
let last_update = Readable::read(reader)?;
585-
let rgb = Readable::read(reader)?;
586-
let alias = Readable::read(reader)?;
587-
let addresses_count: u64 = Readable::read(reader)?;
588-
let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
589-
for _ in 0..addresses_count {
590-
match Readable::read(reader) {
591-
Ok(Ok(addr)) => { addresses.push(addr); },
592-
Ok(Err(_)) => return Err(DecodeError::InvalidValue),
593-
Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
594-
_ => unreachable!(),
595-
}
596-
}
597-
let announcement_message = Readable::read(reader)?;
598-
Ok(NodeAnnouncementInfo {
599-
features,
600-
last_update,
601-
rgb,
602-
alias,
603-
addresses,
604-
announcement_message
605-
})
606-
}
607-
}
549+
impl_writeable_tlv_based!(NodeAnnouncementInfo, {
550+
(0, features),
551+
(2, last_update),
552+
(4, rgb),
553+
(6, alias),
554+
}, {
555+
(8, announcement_message),
556+
}, {
557+
(10, addresses),
558+
});
608559

609560
#[derive(Clone, Debug, PartialEq)]
610561
/// Details about a node in the network, known from the network announcement.
@@ -629,36 +580,12 @@ impl fmt::Display for NodeInfo {
629580
}
630581
}
631582

632-
impl Writeable for NodeInfo {
633-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
634-
(self.channels.len() as u64).write(writer)?;
635-
for ref chan in self.channels.iter() {
636-
chan.write(writer)?;
637-
}
638-
self.lowest_inbound_channel_fees.write(writer)?;
639-
self.announcement_info.write(writer)?;
640-
Ok(())
641-
}
642-
}
643-
644-
const MAX_ALLOC_SIZE: u64 = 64*1024;
645-
646-
impl Readable for NodeInfo {
647-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
648-
let channels_count: u64 = Readable::read(reader)?;
649-
let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
650-
for _ in 0..channels_count {
651-
channels.push(Readable::read(reader)?);
652-
}
653-
let lowest_inbound_channel_fees = Readable::read(reader)?;
654-
let announcement_info = Readable::read(reader)?;
655-
Ok(NodeInfo {
656-
channels,
657-
lowest_inbound_channel_fees,
658-
announcement_info,
659-
})
660-
}
661-
}
583+
impl_writeable_tlv_based!(NodeInfo, {}, {
584+
(0, lowest_inbound_channel_fees),
585+
(2, announcement_info),
586+
}, {
587+
(4, channels),
588+
});
662589

663590
const SERIALIZATION_VERSION: u8 = 1;
664591
const MIN_SERIALIZATION_VERSION: u8 = 1;

0 commit comments

Comments
 (0)