From: Matt Corallo Date: Tue, 25 May 2021 21:20:02 +0000 (+0000) Subject: Move ChannelManager-inner structs to TLV storage X-Git-Tag: v0.0.98~14^2~5 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=9ec0f04b74b01faa7854904820af04d875598b03;p=rust-lightning Move ChannelManager-inner structs to TLV storage Note that enums are left alone as we can use the type byte already present for future compatibility. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 9d4d55c40..1a032eefe 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4316,9 +4316,9 @@ impl PersistenceNotifier { const SERIALIZATION_VERSION: u8 = 1; const MIN_SERIALIZATION_VERSION: u8 = 1; -impl Writeable for PendingHTLCInfo { +impl Writeable for PendingHTLCRouting { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - match &self.routing { + match &self { &PendingHTLCRouting::Forward { ref onion_packet, ref short_channel_id } => { 0u8.write(writer)?; onion_packet.write(writer)?; @@ -4331,39 +4331,37 @@ impl Writeable for PendingHTLCInfo { incoming_cltv_expiry.write(writer)?; }, } - self.incoming_shared_secret.write(writer)?; - self.payment_hash.write(writer)?; - self.amt_to_forward.write(writer)?; - self.outgoing_cltv_value.write(writer)?; Ok(()) } } -impl Readable for PendingHTLCInfo { - fn read(reader: &mut R) -> Result { - Ok(PendingHTLCInfo { - routing: match Readable::read(reader)? { - 0u8 => PendingHTLCRouting::Forward { - onion_packet: Readable::read(reader)?, - short_channel_id: Readable::read(reader)?, - }, - 1u8 => PendingHTLCRouting::Receive { - payment_data: msgs::FinalOnionHopData { - payment_secret: Readable::read(reader)?, - total_msat: Readable::read(reader)?, - }, - incoming_cltv_expiry: Readable::read(reader)?, +impl Readable for PendingHTLCRouting { + fn read(reader: &mut R) -> Result { + match Readable::read(reader)? { + 0u8 => Ok(PendingHTLCRouting::Forward { + onion_packet: Readable::read(reader)?, + short_channel_id: Readable::read(reader)?, + }), + 1u8 => Ok(PendingHTLCRouting::Receive { + payment_data: msgs::FinalOnionHopData { + payment_secret: Readable::read(reader)?, + total_msat: Readable::read(reader)?, }, - _ => return Err(DecodeError::InvalidValue), - }, - incoming_shared_secret: Readable::read(reader)?, - payment_hash: Readable::read(reader)?, - amt_to_forward: Readable::read(reader)?, - outgoing_cltv_value: Readable::read(reader)?, - }) + incoming_cltv_expiry: Readable::read(reader)?, + }), + _ => Err(DecodeError::InvalidValue), + } } } +impl_writeable_tlv_based!(PendingHTLCInfo, { + (0, routing), + (2, incoming_shared_secret), + (4, payment_hash), + (6, amt_to_forward), + (8, outgoing_cltv_value) +}, {}, {}); + impl Writeable for HTLCFailureMsg { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { match self { @@ -4416,33 +4414,52 @@ impl Readable for PendingHTLCStatus { } } -impl_writeable!(HTLCPreviousHopData, 0, { - short_channel_id, - outpoint, - htlc_id, - incoming_packet_shared_secret -}); +impl_writeable_tlv_based!(HTLCPreviousHopData, { + (0, short_channel_id), + (2, outpoint), + (4, htlc_id), + (6, incoming_packet_shared_secret) +}, {}, {}); impl Writeable for ClaimableHTLC { fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - self.prev_hop.write(writer)?; - self.value.write(writer)?; - self.payment_data.payment_secret.write(writer)?; - self.payment_data.total_msat.write(writer)?; - self.cltv_expiry.write(writer) + write_tlv_fields!(writer, { + (0, self.prev_hop), + (2, self.value), + (4, self.payment_data.payment_secret), + (6, self.payment_data.total_msat), + (8, self.cltv_expiry) + }, {}); + Ok(()) } } impl Readable for ClaimableHTLC { fn read(reader: &mut R) -> Result { + let mut prev_hop = HTLCPreviousHopData { + short_channel_id: 0, htlc_id: 0, + incoming_packet_shared_secret: [0; 32], + outpoint: OutPoint::null(), + }; + let mut value = 0; + let mut payment_secret = PaymentSecret([0; 32]); + let mut total_msat = 0; + let mut cltv_expiry = 0; + read_tlv_fields!(reader, { + (0, prev_hop), + (2, value), + (4, payment_secret), + (6, total_msat), + (8, cltv_expiry) + }, {}); Ok(ClaimableHTLC { - prev_hop: Readable::read(reader)?, - value: Readable::read(reader)?, + prev_hop, + value, payment_data: msgs::FinalOnionHopData { - payment_secret: Readable::read(reader)?, - total_msat: Readable::read(reader)?, + payment_secret, + total_msat, }, - cltv_expiry: Readable::read(reader)?, + cltv_expiry, }) } } @@ -4547,13 +4564,13 @@ impl Readable for HTLCForwardInfo { } } -impl_writeable!(PendingInboundPayment, 0, { - payment_secret, - expiry_time, - user_payment_id, - payment_preimage, - min_value_msat -}); +impl_writeable_tlv_based!(PendingInboundPayment, { + (0, payment_secret), + (2, expiry_time), + (4, user_payment_id), + (6, payment_preimage), + (8, min_value_msat), +}, {}, {}); impl Writeable for ChannelManager where M::Target: chain::Watch,