]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Move MonitorEvent serialization to TLV-enum-upgradable from custom
authorMatt Corallo <git@bluematt.me>
Thu, 7 Oct 2021 04:14:35 +0000 (04:14 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 18 Oct 2021 22:04:56 +0000 (22:04 +0000)
lightning/src/chain/channelmonitor.rs
lightning/src/util/ser_macros.rs

index c483d01f37d129697a25d7a369f9f4b8cddce21f..4c7da902be94455589b354f39358775d7d8cd31f 100644 (file)
@@ -132,6 +132,10 @@ pub enum MonitorEvent {
        /// A monitor event that the Channel's commitment transaction was confirmed.
        CommitmentTxConfirmed(OutPoint),
 }
+impl_writeable_tlv_based_enum_upgradable!(MonitorEvent, ;
+       (2, HTLCEvent),
+       (4, CommitmentTxConfirmed),
+);
 
 /// Simple structure sent back by `chain::Watch` when an HTLC from a forward channel is detected on
 /// chain. Used to update the corresponding HTLC in the backward channel. Failing to pass the
@@ -891,6 +895,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
                write_tlv_fields!(writer, {
                        (1, self.funding_spend_confirmed, option),
                        (3, self.htlcs_resolved_on_chain, vec_type),
+                       (5, self.pending_monitor_events, vec_type),
                });
 
                Ok(())
@@ -3000,14 +3005,15 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                }
 
                let pending_monitor_events_len: u64 = Readable::read(reader)?;
-               let mut pending_monitor_events = Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3)));
+               let mut pending_monitor_events = Some(
+                       Vec::with_capacity(cmp::min(pending_monitor_events_len as usize, MAX_ALLOC_SIZE / (32 + 8*3))));
                for _ in 0..pending_monitor_events_len {
                        let ev = match <u8 as Readable>::read(reader)? {
                                0 => MonitorEvent::HTLCEvent(Readable::read(reader)?),
                                1 => MonitorEvent::CommitmentTxConfirmed(funding_info.0),
                                _ => return Err(DecodeError::InvalidValue)
                        };
-                       pending_monitor_events.push(ev);
+                       pending_monitor_events.as_mut().unwrap().push(ev);
                }
 
                let pending_events_len: u64 = Readable::read(reader)?;
@@ -3068,6 +3074,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                read_tlv_fields!(reader, {
                        (1, funding_spend_confirmed, option),
                        (3, htlcs_resolved_on_chain, vec_type),
+                       (5, pending_monitor_events, vec_type),
                });
 
                let mut secp_ctx = Secp256k1::new();
@@ -3107,7 +3114,7 @@ impl<'a, Signer: Sign, K: KeysInterface<Signer = Signer>> ReadableArgs<&'a K>
                                current_holder_commitment_number,
 
                                payment_preimages,
-                               pending_monitor_events,
+                               pending_monitor_events: pending_monitor_events.unwrap(),
                                pending_events,
 
                                onchain_events_awaiting_threshold_conf,
index b80ca537bbf03bd86d07912687d32ce81cd11f81..0827a023aedfbe141ca6df425f8c3f11e447b17f 100644 (file)
@@ -455,9 +455,12 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
 macro_rules! impl_writeable_tlv_based_enum_upgradable {
        ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
                {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
-       ),* $(,)*) => {
+       ),* $(,)*
+       $(;
+       $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*)*) => {
                _impl_writeable_tlv_based_enum_common!($st,
-                       $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; );
+                       $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
+                       $($(($tuple_variant_id, $tuple_variant_name)),*)*);
 
                impl ::util::ser::MaybeReadable for $st {
                        fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Option<Self>, ::ln::msgs::DecodeError> {
@@ -481,6 +484,9 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
                                                };
                                                f()
                                        }),*
+                                       $($($tuple_variant_id => {
+                                               Ok(Some($st::$tuple_variant_name(Readable::read(reader)?)))
+                                       }),*)*
                                        _ if id % 2 == 1 => Ok(None),
                                        _ => Err(DecodeError::UnknownRequiredFeature),
                                }