Merge pull request #2400 from TheBlueMatt/2023-07-kill-vec_type
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Tue, 11 Jul 2023 19:58:34 +0000 (19:58 +0000)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2023 19:58:34 +0000 (19:58 +0000)
Fix backwards compat for blocked_monitor_updates and finally kill `vec_type`

12 files changed:
lightning/src/chain/channelmonitor.rs
lightning/src/events/mod.rs
lightning/src/ln/chan_utils.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/msgs.rs
lightning/src/ln/onion_utils.rs
lightning/src/onion_message/packet.rs
lightning/src/routing/gossip.rs
lightning/src/routing/router.rs
lightning/src/util/chacha20poly1305rfc.rs
lightning/src/util/ser_macros.rs

index 36e3bc46bfcc40370519a3d906d06e9e90458bea..78b5c65e1407170df338b71ca678e79b7738fda0 100644 (file)
@@ -262,7 +262,7 @@ impl_writeable_tlv_based!(HolderSignedTx, {
        (8, delayed_payment_key, required),
        (10, per_commitment_point, required),
        (12, feerate_per_kw, required),
-       (14, htlc_outputs, vec_type)
+       (14, htlc_outputs, required_vec)
 });
 
 impl HolderSignedTx {
@@ -538,15 +538,15 @@ impl ChannelMonitorUpdateStep {
 impl_writeable_tlv_based_enum_upgradable!(ChannelMonitorUpdateStep,
        (0, LatestHolderCommitmentTXInfo) => {
                (0, commitment_tx, required),
-               (1, claimed_htlcs, vec_type),
-               (2, htlc_outputs, vec_type),
+               (1, claimed_htlcs, optional_vec),
+               (2, htlc_outputs, required_vec),
                (4, nondust_htlc_sources, optional_vec),
        },
        (1, LatestCounterpartyCommitmentTXInfo) => {
                (0, commitment_txid, required),
                (2, commitment_number, required),
                (4, their_per_commitment_point, required),
-               (6, htlc_outputs, vec_type),
+               (6, htlc_outputs, required_vec),
        },
        (2, PaymentPreimage) => {
                (0, payment_preimage, required),
@@ -1071,12 +1071,12 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
 
                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),
+                       (3, self.htlcs_resolved_on_chain, required_vec),
+                       (5, self.pending_monitor_events, required_vec),
                        (7, self.funding_spend_seen, required),
                        (9, self.counterparty_node_id, option),
                        (11, self.confirmed_commitment_tx_counterparty_output, option),
-                       (13, self.spendable_txids_confirmed, vec_type),
+                       (13, self.spendable_txids_confirmed, required_vec),
                        (15, self.counterparty_fulfilled_htlcs, required),
                });
 
@@ -4112,12 +4112,12 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
                let mut counterparty_fulfilled_htlcs = Some(HashMap::new());
                read_tlv_fields!(reader, {
                        (1, funding_spend_confirmed, option),
-                       (3, htlcs_resolved_on_chain, vec_type),
-                       (5, pending_monitor_events, vec_type),
+                       (3, htlcs_resolved_on_chain, optional_vec),
+                       (5, pending_monitor_events, optional_vec),
                        (7, funding_spend_seen, option),
                        (9, counterparty_node_id, option),
                        (11, confirmed_commitment_tx_counterparty_output, option),
-                       (13, spendable_txids_confirmed, vec_type),
+                       (13, spendable_txids_confirmed, optional_vec),
                        (15, counterparty_fulfilled_htlcs, option),
                });
 
index a6601e4d99c21653ee991614d6662fefba92176b..a5a20e7d42b3e016d5ea219f1427cd3dba0e67c2 100644 (file)
@@ -909,7 +909,7 @@ impl Writeable for Event {
                                        (2, payment_failed_permanently, required),
                                        (3, false, required), // all_paths_failed in LDK versions prior to 0.0.114
                                        (4, path.blinded_tail, option),
-                                       (5, path.hops, vec_type),
+                                       (5, path.hops, required_vec),
                                        (7, short_channel_id, option),
                                        (9, None::<RouteParameters>, option), // retry in LDK versions prior to 0.0.115
                                        (11, payment_id, option),
@@ -977,7 +977,7 @@ impl Writeable for Event {
                                write_tlv_fields!(writer, {
                                        (0, payment_id, required),
                                        (2, payment_hash, option),
-                                       (4, path.hops, vec_type),
+                                       (4, path.hops, required_vec),
                                        (6, path.blinded_tail, option),
                                })
                        },
@@ -1008,7 +1008,7 @@ impl Writeable for Event {
                                write_tlv_fields!(writer, {
                                        (0, payment_id, required),
                                        (2, payment_hash, required),
-                                       (4, path.hops, vec_type),
+                                       (4, path.hops, required_vec),
                                        (6, path.blinded_tail, option),
                                })
                        },
@@ -1017,7 +1017,7 @@ impl Writeable for Event {
                                write_tlv_fields!(writer, {
                                        (0, payment_id, required),
                                        (2, payment_hash, required),
-                                       (4, path.hops, vec_type),
+                                       (4, path.hops, required_vec),
                                        (6, short_channel_id, option),
                                        (8, path.blinded_tail, option),
                                })
@@ -1162,7 +1162,9 @@ impl MaybeReadable for Event {
                                                (1, network_update, upgradable_option),
                                                (2, payment_failed_permanently, required),
                                                (4, blinded_tail, option),
-                                               (5, path, vec_type),
+                                               // Added as a part of LDK 0.0.101 and always filled in since.
+                                               // Defaults to an empty Vec, though likely should have been `Option`al.
+                                               (5, path, optional_vec),
                                                (7, short_channel_id, option),
                                                (11, payment_id, option),
                                                (13, failure_opt, upgradable_option),
@@ -1279,13 +1281,13 @@ impl MaybeReadable for Event {
                                        _init_and_read_tlv_fields!(reader, {
                                                (0, payment_id, required),
                                                (2, payment_hash, option),
-                                               (4, path, vec_type),
+                                               (4, path, required_vec),
                                                (6, blinded_tail, option),
                                        });
                                        Ok(Some(Event::PaymentPathSuccessful {
                                                payment_id: payment_id.0.unwrap(),
                                                payment_hash,
-                                               path: Path { hops: path.unwrap(), blinded_tail },
+                                               path: Path { hops: path, blinded_tail },
                                        }))
                                };
                                f()
@@ -1338,13 +1340,13 @@ impl MaybeReadable for Event {
                                        _init_and_read_tlv_fields!(reader, {
                                                (0, payment_id, required),
                                                (2, payment_hash, required),
-                                               (4, path, vec_type),
+                                               (4, path, required_vec),
                                                (6, blinded_tail, option),
                                        });
                                        Ok(Some(Event::ProbeSuccessful {
                                                payment_id: payment_id.0.unwrap(),
                                                payment_hash: payment_hash.0.unwrap(),
-                                               path: Path { hops: path.unwrap(), blinded_tail },
+                                               path: Path { hops: path, blinded_tail },
                                        }))
                                };
                                f()
@@ -1354,14 +1356,14 @@ impl MaybeReadable for Event {
                                        _init_and_read_tlv_fields!(reader, {
                                                (0, payment_id, required),
                                                (2, payment_hash, required),
-                                               (4, path, vec_type),
+                                               (4, path, required_vec),
                                                (6, short_channel_id, option),
                                                (8, blinded_tail, option),
                                        });
                                        Ok(Some(Event::ProbeFailed {
                                                payment_id: payment_id.0.unwrap(),
                                                payment_hash: payment_hash.0.unwrap(),
-                                               path: Path { hops: path.unwrap(), blinded_tail },
+                                               path: Path { hops: path, blinded_tail },
                                                short_channel_id,
                                        }))
                                };
index fd27d2308664469034564691395c263adb1808bb..85490afaec1292bf792d245c18b13c3b54d78b0a 100644 (file)
@@ -1056,7 +1056,7 @@ impl_writeable_tlv_based!(HolderCommitmentTransaction, {
        (0, inner, required),
        (2, counterparty_sig, required),
        (4, holder_sig_first, required),
-       (6, counterparty_htlc_sigs, vec_type),
+       (6, counterparty_htlc_sigs, required_vec),
 });
 
 impl HolderCommitmentTransaction {
@@ -1346,7 +1346,7 @@ impl Writeable for CommitmentTransaction {
                        (6, self.feerate_per_kw, required),
                        (8, self.keys, required),
                        (10, self.built, required),
-                       (12, self.htlcs, vec_type),
+                       (12, self.htlcs, required_vec),
                        (14, legacy_deserialization_prevention_marker, option),
                        (15, self.channel_type_features, required),
                });
@@ -1356,24 +1356,14 @@ impl Writeable for CommitmentTransaction {
 
 impl Readable for CommitmentTransaction {
        fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
-               let mut commitment_number = RequiredWrapper(None);
-               let mut to_broadcaster_value_sat = RequiredWrapper(None);
-               let mut to_countersignatory_value_sat = RequiredWrapper(None);
-               let mut feerate_per_kw = RequiredWrapper(None);
-               let mut keys = RequiredWrapper(None);
-               let mut built = RequiredWrapper(None);
-               _init_tlv_field_var!(htlcs, vec_type);
-               let mut _legacy_deserialization_prevention_marker: Option<()> = None;
-               let mut channel_type_features = None;
-
-               read_tlv_fields!(reader, {
+               _init_and_read_tlv_fields!(reader, {
                        (0, commitment_number, required),
                        (2, to_broadcaster_value_sat, required),
                        (4, to_countersignatory_value_sat, required),
                        (6, feerate_per_kw, required),
                        (8, keys, required),
                        (10, built, required),
-                       (12, htlcs, vec_type),
+                       (12, htlcs, required_vec),
                        (14, _legacy_deserialization_prevention_marker, option),
                        (15, channel_type_features, option),
                });
@@ -1389,7 +1379,7 @@ impl Readable for CommitmentTransaction {
                        feerate_per_kw: feerate_per_kw.0.unwrap(),
                        keys: keys.0.unwrap(),
                        built: built.0.unwrap(),
-                       htlcs: _init_tlv_based_struct_field!(htlcs, vec_type),
+                       htlcs,
                        channel_type_features: channel_type_features.unwrap_or(ChannelTypeFeatures::only_static_remote_key())
                })
        }
index ff34b05ff0e1e08180781e3fbbab3dcdd58fea1c..8232c5e1b1611e22b2a91f467f5a11e35a526215 100644 (file)
@@ -6856,11 +6856,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
                        (5, self.context.config, required),
                        (6, serialized_holder_htlc_max_in_flight, option),
                        (7, self.context.shutdown_scriptpubkey, option),
-                       (8, self.context.blocked_monitor_updates, vec_type),
+                       (8, self.context.blocked_monitor_updates, optional_vec),
                        (9, self.context.target_closing_feerate_sats_per_kw, option),
-                       (11, self.context.monitor_pending_finalized_fulfills, vec_type),
+                       (11, self.context.monitor_pending_finalized_fulfills, required_vec),
                        (13, self.context.channel_creation_height, required),
-                       (15, preimages, vec_type),
+                       (15, preimages, required_vec),
                        (17, self.context.announcement_sigs_state, required),
                        (19, self.context.latest_inbound_scid_alias, option),
                        (21, self.context.outbound_scid_alias, required),
@@ -7164,11 +7164,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
                        (5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
                        (6, holder_max_htlc_value_in_flight_msat, option),
                        (7, shutdown_scriptpubkey, option),
-                       (8, blocked_monitor_updates, vec_type),
+                       (8, blocked_monitor_updates, optional_vec),
                        (9, target_closing_feerate_sats_per_kw, option),
-                       (11, monitor_pending_finalized_fulfills, vec_type),
+                       (11, monitor_pending_finalized_fulfills, optional_vec),
                        (13, channel_creation_height, option),
-                       (15, preimages_opt, vec_type),
+                       (15, preimages_opt, optional_vec),
                        (17, announcement_sigs_state, option),
                        (19, latest_inbound_scid_alias, option),
                        (21, outbound_scid_alias, option),
index b87be9cbb75cca9bc838592145ca924eee3d6a7a..7df125b9323b9c3987cbfa91cf7dbaf1114d3a7d 100644 (file)
@@ -45,7 +45,7 @@ use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, No
 #[cfg(any(feature = "_test_utils", test))]
 use crate::ln::features::InvoiceFeatures;
 use crate::routing::gossip::NetworkGraph;
-use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteHop, RouteParameters, Router};
+use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, Payee, PaymentParameters, Route, RouteParameters, Router};
 use crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
 use crate::ln::msgs;
 use crate::ln::onion_utils;
@@ -3198,6 +3198,7 @@ where
        /// irrevocably committed to on our end. In such a case, do NOT retry the payment with a
        /// different route unless you intend to pay twice!
        ///
+       /// [`RouteHop`]: crate::routing::router::RouteHop
        /// [`Event::PaymentSent`]: events::Event::PaymentSent
        /// [`Event::PaymentFailed`]: events::Event::PaymentFailed
        /// [`UpdateHTLCs`]: events::MessageSendEvent::UpdateHTLCs
@@ -7515,7 +7516,7 @@ impl Readable for ChannelDetails {
 }
 
 impl_writeable_tlv_based!(PhantomRouteHints, {
-       (2, channels, vec_type),
+       (2, channels, required_vec),
        (4, phantom_scid, required),
        (6, real_node_pubkey, required),
 });
@@ -7707,7 +7708,7 @@ impl Readable for HTLCSource {
                        0 => {
                                let mut session_priv: crate::util::ser::RequiredWrapper<SecretKey> = crate::util::ser::RequiredWrapper(None);
                                let mut first_hop_htlc_msat: u64 = 0;
-                               let mut path_hops: Option<Vec<RouteHop>> = Some(Vec::new());
+                               let mut path_hops = Vec::new();
                                let mut payment_id = None;
                                let mut payment_params: Option<PaymentParameters> = None;
                                let mut blinded_tail: Option<BlindedTail> = None;
@@ -7715,7 +7716,7 @@ impl Readable for HTLCSource {
                                        (0, session_priv, required),
                                        (1, payment_id, option),
                                        (2, first_hop_htlc_msat, required),
-                                       (4, path_hops, vec_type),
+                                       (4, path_hops, required_vec),
                                        (5, payment_params, (option: ReadableArgs, 0)),
                                        (6, blinded_tail, option),
                                });
@@ -7724,7 +7725,7 @@ impl Readable for HTLCSource {
                                        // instead.
                                        payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
                                }
-                               let path = Path { hops: path_hops.ok_or(DecodeError::InvalidValue)?, blinded_tail };
+                               let path = Path { hops: path_hops, blinded_tail };
                                if path.hops.len() == 0 {
                                        return Err(DecodeError::InvalidValue);
                                }
@@ -7759,7 +7760,7 @@ impl Writeable for HTLCSource {
                                        (1, payment_id_opt, option),
                                        (2, first_hop_htlc_msat, required),
                                        // 3 was previously used to write a PaymentSecret for the payment.
-                                       (4, path.hops, vec_type),
+                                       (4, path.hops, required_vec),
                                        (5, None::<PaymentParameters>, option), // payment_params in LDK versions prior to 0.0.115
                                        (6, path.blinded_tail, option),
                                 });
@@ -8009,7 +8010,7 @@ where
                        (6, monitor_update_blocked_actions_per_peer, option),
                        (7, self.fake_scid_rand_bytes, required),
                        (8, if events_not_backwards_compatible { Some(&*events) } else { None }, option),
-                       (9, htlc_purposes, vec_type),
+                       (9, htlc_purposes, required_vec),
                        (10, in_flight_monitor_updates, option),
                        (11, self.probing_cookie_secret, required),
                        (13, htlc_onion_fields, optional_vec),
@@ -8456,7 +8457,7 @@ where
                        (6, monitor_update_blocked_actions_per_peer, option),
                        (7, fake_scid_rand_bytes, option),
                        (8, events_override, option),
-                       (9, claimable_htlc_purposes, vec_type),
+                       (9, claimable_htlc_purposes, optional_vec),
                        (10, in_flight_monitor_updates, option),
                        (11, probing_cookie_secret, option),
                        (13, claimable_htlc_onion_fields, optional_vec),
index 672c6ae6e96e8387cd840ae97f19080a5d4d8016..a514f4923268ce8639bafbb5065c43e6e1a7ea3d 100644 (file)
@@ -3760,7 +3760,7 @@ mod tests {
                let test_bytes = vec![42u8; 1000];
                if let OnionHopDataFormat::NonFinalNode { short_channel_id } = payload.format {
                        _encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
-                               (1, test_bytes, vec_type),
+                               (1, test_bytes, required_vec),
                                (2, HighZeroBytesDroppedBigSize(payload.amt_to_forward), required),
                                (4, HighZeroBytesDroppedBigSize(payload.outgoing_cltv_value), required),
                                (6, short_channel_id, required)
index 3b62c856334596b85bf58b8ee96b1a0eed8f36f6..52eb7bcb54298a7cc8e8fc418f77d4de66babd69 100644 (file)
@@ -645,7 +645,7 @@ impl_writeable_tlv_based_enum!(HTLCFailReasonRepr,
        },
        (1, Reason) => {
                (0, failure_code, required),
-               (2, data, vec_type),
+               (2, data, required_vec),
        },
 ;);
 
index 1c3595c3712526191e0e5f1707dfa51d82ca7505..8a5628f164ca358dd8df1d7aec5e58730247c67e 100644 (file)
@@ -169,7 +169,7 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
                match &self.0 {
                        Payload::Forward(ForwardControlTlvs::Blinded(encrypted_bytes)) => {
                                _encode_varint_length_prefixed_tlv!(w, {
-                                       (4, *encrypted_bytes, vec_type)
+                                       (4, *encrypted_bytes, required_vec)
                                })
                        },
                        Payload::Receive {
@@ -177,7 +177,7 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
                        } => {
                                _encode_varint_length_prefixed_tlv!(w, {
                                        (2, reply_path, option),
-                                       (4, *encrypted_bytes, vec_type),
+                                       (4, *encrypted_bytes, required_vec),
                                        (message.tlv_type(), message, required)
                                })
                        },
index 5ef107e299cfc1d3d1de02dfa043fc0a58b5df3d..b9b70e0a03165e49f71f7f8c9bf185fd0388d7a2 100644 (file)
@@ -1143,26 +1143,26 @@ impl Writeable for NodeAnnouncementInfo {
                        (4, self.rgb, required),
                        (6, self.alias, required),
                        (8, self.announcement_message, option),
-                       (10, empty_addresses, vec_type), // Versions prior to 0.0.115 require this field
+                       (10, empty_addresses, required_vec), // Versions prior to 0.0.115 require this field
                });
                Ok(())
        }
 }
 
 impl Readable for NodeAnnouncementInfo {
-    fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+       fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
                _init_and_read_tlv_fields!(reader, {
                        (0, features, required),
                        (2, last_update, required),
                        (4, rgb, required),
                        (6, alias, required),
                        (8, announcement_message, option),
-                       (10, _addresses, vec_type), // deprecated, not used anymore
+                       (10, _addresses, optional_vec), // deprecated, not used anymore
                });
                let _: Option<Vec<NetAddress>> = _addresses;
                Ok(Self { features: features.0.unwrap(), last_update: last_update.0.unwrap(), rgb: rgb.0.unwrap(),
                        alias: alias.0.unwrap(), announcement_message })
-    }
+       }
 }
 
 /// A user-defined name for a node, which may be used when displaying the node in a graph.
@@ -1228,7 +1228,7 @@ impl Writeable for NodeInfo {
                write_tlv_fields!(writer, {
                        // Note that older versions of LDK wrote the lowest inbound fees here at type 0
                        (2, self.announcement_info, option),
-                       (4, self.channels, vec_type),
+                       (4, self.channels, required_vec),
                });
                Ok(())
        }
@@ -1259,19 +1259,17 @@ impl Readable for NodeInfo {
                // with zero inbound fees, causing that heuristic to provide little gain. Worse, because it
                // requires additional complexity and lookups during routing, it ends up being a
                // performance loss. Thus, we simply ignore the old field here and no longer track it.
-               let mut _lowest_inbound_channel_fees: Option<RoutingFees> = None;
-               let mut announcement_info_wrap: Option<NodeAnnouncementInfoDeserWrapper> = None;
-               _init_tlv_field_var!(channels, vec_type);
-
-               read_tlv_fields!(reader, {
+               _init_and_read_tlv_fields!(reader, {
                        (0, _lowest_inbound_channel_fees, option),
                        (2, announcement_info_wrap, upgradable_option),
-                       (4, channels, vec_type),
+                       (4, channels, required_vec),
                });
+               let _: Option<RoutingFees> = _lowest_inbound_channel_fees;
+               let announcement_info_wrap: Option<NodeAnnouncementInfoDeserWrapper> = announcement_info_wrap;
 
                Ok(NodeInfo {
                        announcement_info: announcement_info_wrap.map(|w| w.0),
-                       channels: _init_tlv_based_struct_field!(channels, vec_type),
+                       channels,
                })
        }
 }
index a83af28fcd62f9cae1ae268011ea5218d274c136..d3539579c50ae16d8a8d92eb806af6364243327d 100644 (file)
@@ -292,7 +292,7 @@ pub struct BlindedTail {
 }
 
 impl_writeable_tlv_based!(BlindedTail, {
-       (0, hops, vec_type),
+       (0, hops, required_vec),
        (2, blinding_point, required),
        (4, excess_final_cltv_expiry_delta, required),
        (6, final_value_msat, required),
@@ -559,10 +559,10 @@ impl Writeable for PaymentParameters {
                        (1, self.max_total_cltv_expiry_delta, required),
                        (2, self.payee.features(), option),
                        (3, self.max_path_count, required),
-                       (4, *clear_hints, vec_type),
+                       (4, *clear_hints, required_vec),
                        (5, self.max_channel_saturation_power_of_half, required),
                        (6, self.expiry_time, option),
-                       (7, self.previously_failed_channels, vec_type),
+                       (7, self.previously_failed_channels, required_vec),
                        (8, *blinded_hints, optional_vec),
                        (9, self.payee.final_cltv_expiry_delta(), option),
                });
@@ -577,14 +577,13 @@ impl ReadableArgs<u32> for PaymentParameters {
                        (1, max_total_cltv_expiry_delta, (default_value, DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA)),
                        (2, features, (option: ReadableArgs, payee_pubkey.is_some())),
                        (3, max_path_count, (default_value, DEFAULT_MAX_PATH_COUNT)),
-                       (4, route_hints, vec_type),
+                       (4, clear_route_hints, required_vec),
                        (5, max_channel_saturation_power_of_half, (default_value, DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF)),
                        (6, expiry_time, option),
-                       (7, previously_failed_channels, vec_type),
+                       (7, previously_failed_channels, optional_vec),
                        (8, blinded_route_hints, optional_vec),
                        (9, final_cltv_expiry_delta, (default_value, default_final_cltv_expiry_delta)),
                });
-               let clear_route_hints = route_hints.unwrap_or(vec![]);
                let blinded_route_hints = blinded_route_hints.unwrap_or(vec![]);
                let payee = if blinded_route_hints.len() != 0 {
                        if clear_route_hints.len() != 0 || payee_pubkey.is_some() { return Err(DecodeError::InvalidValue) }
index 052968377a5e58fe6a4236c0812718a236eaab54..a5bec2c82b8cf00da2a597d3c68026aa7e87152f 100644 (file)
@@ -341,9 +341,9 @@ mod tests {
                field3: Vec<u8>,
        }
        impl_writeable_tlv_based!(TestWriteable, {
-               (1, field1, vec_type),
-               (2, field2, vec_type),
-               (3, field3, vec_type),
+               (1, field1, required_vec),
+               (2, field2, required_vec),
+               (3, field3, required_vec),
        });
 
        #[test]
index 742ea25714dafb4d291f249e30376366cfeebbaa..1744b923d5e92589eee665c5a1ef1e263b6cfd8b 100644 (file)
 //! [`Readable`]: crate::util::ser::Readable
 //! [`Writeable`]: crate::util::ser::Writeable
 
+// There are quite a few TLV serialization "types" which behave differently. We currently only
+// publicly document the `optional` and `required` types, not supporting anything else publicly and
+// changing them at will.
+//
+// Some of the other types include:
+//  * (default_value, $default) - reads optionally, reading $default if no TLV is present
+//  * (static_value, $value) - ignores any TLVs, always using $value
+//  * required_vec - reads into a Vec without a length prefix, failing if no TLV is present.
+//  * optional_vec - reads into an Option<Vec> without a length prefix, continuing if no TLV is
+//                   present. Writes from a Vec directly, only if any elements are present. Note
+//                   that the struct deserialization macros return a Vec, not an Option.
+//  * upgradable_option - reads via MaybeReadable.
+//  * upgradable_required - reads via MaybeReadable, requiring a TLV be present but may return None
+//                          if MaybeReadable::read() returns None.
+
 /// Implements serialization for a single TLV record.
 /// This is exported for use by other exported macros, do not use directly.
 #[doc(hidden)]
@@ -29,7 +44,7 @@ macro_rules! _encode_tlv {
                BigSize($field.serialized_length() as u64).write($stream)?;
                $field.write($stream)?;
        };
-       ($stream: expr, $type: expr, $field: expr, vec_type) => {
+       ($stream: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -41,7 +56,7 @@ macro_rules! _encode_tlv {
        };
        ($stream: expr, $type: expr, $field: expr, optional_vec) => {
                if !$field.is_empty() {
-                       $crate::_encode_tlv!($stream, $type, $field, vec_type);
+                       $crate::_encode_tlv!($stream, $type, $field, required_vec);
                }
        };
        ($stream: expr, $type: expr, $field: expr, upgradable_required) => {
@@ -159,7 +174,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
                BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
                $len.0 += field_len;
        };
-       ($len: expr, $type: expr, $field: expr, vec_type) => {
+       ($len: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($len: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -172,7 +187,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
        };
        ($len: expr, $type: expr, $field: expr, optional_vec) => {
                if !$field.is_empty() {
-                       $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, vec_type);
+                       $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
                }
        };
        ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
@@ -236,8 +251,8 @@ macro_rules! _check_decoded_tlv_order {
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
                // no-op
        }};
-       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{
-               // no-op
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{
+               $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
        }};
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => {{
                // no-op
@@ -281,8 +296,8 @@ macro_rules! _check_missing_tlv {
        ($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
                $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
        }};
-       ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{
-               // no-op
+       ($last_seen_type: expr, $type: expr, $field: ident, required_vec) => {{
+               $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
        }};
        ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
                // no-op
@@ -320,15 +335,16 @@ macro_rules! _decode_tlv {
        ($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
                $field = $trait::read(&mut $reader $(, $read_arg)*)?;
        }};
-       ($reader: expr, $field: ident, vec_type) => {{
+       ($reader: expr, $field: ident, required_vec) => {{
                let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
-               $field = Some(f.0);
+               $field = f.0;
        }};
        ($reader: expr, $field: ident, option) => {{
                $field = Some($crate::util::ser::Readable::read(&mut $reader)?);
        }};
        ($reader: expr, $field: ident, optional_vec) => {{
-               $crate::_decode_tlv!($reader, $field, vec_type);
+               let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
+               $field = Some(f.0);
        }};
        // `upgradable_required` indicates we're reading a required TLV that may have been upgraded
        // without backwards compat. We'll error if the field is missing, and return `Ok(None)` if the
@@ -694,8 +710,8 @@ macro_rules! _init_tlv_based_struct_field {
        ($field: ident, required) => {
                $field.0.unwrap()
        };
-       ($field: ident, vec_type) => {
-               $field.unwrap()
+       ($field: ident, required_vec) => {
+               $field
        };
        ($field: ident, optional_vec) => {
                $field.unwrap()
@@ -720,8 +736,8 @@ macro_rules! _init_tlv_field_var {
        ($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {
                $crate::_init_tlv_field_var!($field, required);
        };
-       ($field: ident, vec_type) => {
-               let mut $field = Some(Vec::new());
+       ($field: ident, required_vec) => {
+               let mut $field = Vec::new();
        };
        ($field: ident, option) => {
                let mut $field = None;