Skip to content

Commit 14a4587

Browse files
committed
Move NetworkGraph inner structs to TLV storage
1 parent 0afb048 commit 14a4587

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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, 0), (2, proportional_millionths, 0)}, {}, {});
542525

543526
#[derive(Clone, Debug, PartialEq)]
544527
/// Information received in the latest node_announcement from this node.
@@ -565,25 +548,22 @@ pub struct NodeAnnouncementInfo {
565548

566549
impl Writeable for NodeAnnouncementInfo {
567550
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)?;
572551
(self.addresses.len() as u64).write(writer)?;
573552
for ref addr in &self.addresses {
574553
addr.write(writer)?;
575554
}
576-
self.announcement_message.write(writer)?;
555+
write_tlv_fields!(writer, {
556+
(0, self.features),
557+
(2, self.last_update),
558+
(4, self.rgb),
559+
(6, self.alias)
560+
}, { (8, self.announcement_message) });
577561
Ok(())
578562
}
579563
}
580564

581565
impl Readable for NodeAnnouncementInfo {
582566
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)?;
587567
let addresses_count: u64 = Readable::read(reader)?;
588568
let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize);
589569
for _ in 0..addresses_count {
@@ -594,7 +574,17 @@ impl Readable for NodeAnnouncementInfo {
594574
_ => unreachable!(),
595575
}
596576
}
597-
let announcement_message = Readable::read(reader)?;
577+
let mut features = NodeFeatures::empty();
578+
let mut last_update = 0;
579+
let mut rgb = [0; 3];
580+
let mut alias = [0; 32];
581+
let mut announcement_message = None;
582+
read_tlv_fields!(reader, {
583+
(0, features),
584+
(2, last_update),
585+
(4, rgb),
586+
(6, alias)
587+
}, { (8, announcement_message) });
598588
Ok(NodeAnnouncementInfo {
599589
features,
600590
last_update,
@@ -629,37 +619,15 @@ impl fmt::Display for NodeInfo {
629619
}
630620
}
631621

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-
}
622+
impl_writeable_tlv_based!(NodeInfo, {}, {
623+
(0, lowest_inbound_channel_fees),
624+
(2, announcement_info),
625+
}, {
626+
(4, channels),
627+
});
643628

644629
const MAX_ALLOC_SIZE: u64 = 64*1024;
645630

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-
}
662-
663631
const SERIALIZATION_VERSION: u8 = 1;
664632
const MIN_SERIALIZATION_VERSION: u8 = 1;
665633

0 commit comments

Comments
 (0)