+impl<T: sealed::Context> Readable for WithoutLength<Features<T>> {
+ fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+ let v = io_extras::read_to_end(r)?;
+ Ok(WithoutLength(Features::<T>::from_be_bytes(v)))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, OfferFeatures, sealed};
+ use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, u5};
+ use crate::util::ser::{Readable, WithoutLength, Writeable};
+
+ #[test]
+ fn sanity_test_unknown_bits() {
+ let features = ChannelFeatures::empty();
+ assert!(!features.requires_unknown_bits());
+ assert!(!features.supports_unknown_bits());
+
+ let mut features = ChannelFeatures::empty();
+ features.set_unknown_feature_required();
+ assert!(features.requires_unknown_bits());
+ assert!(features.supports_unknown_bits());
+
+ let mut features = ChannelFeatures::empty();
+ features.set_unknown_feature_optional();
+ assert!(!features.requires_unknown_bits());
+ assert!(features.supports_unknown_bits());
+ }
+
+ #[test]
+ fn convert_to_context_with_relevant_flags() {
+ let mut init_features = InitFeatures::empty();
+ // Set a bunch of features we use, plus initial_routing_sync_required (which shouldn't get
+ // converted as it's only relevant in an init context).
+ init_features.set_initial_routing_sync_required();
+ init_features.set_data_loss_protect_optional();
+ init_features.set_variable_length_onion_required();
+ init_features.set_static_remote_key_required();
+ init_features.set_payment_secret_required();
+ init_features.set_basic_mpp_optional();
+ init_features.set_wumbo_optional();
+ init_features.set_shutdown_any_segwit_optional();
+ init_features.set_onion_messages_optional();
+ init_features.set_channel_type_optional();
+ init_features.set_scid_privacy_optional();
+ init_features.set_zero_conf_optional();
+ init_features.set_anchors_zero_fee_htlc_tx_optional();
+
+ assert!(init_features.initial_routing_sync());
+ assert!(!init_features.supports_upfront_shutdown_script());
+ assert!(!init_features.supports_gossip_queries());
+
+ let node_features: NodeFeatures = init_features.to_context();
+ {
+ // Check that the flags are as expected:
+ // - option_data_loss_protect
+ // - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
+ // - basic_mpp | wumbo
+ // - opt_shutdown_anysegwit
+ // - onion_messages
+ // - option_channel_type | option_scid_alias
+ // - option_zeroconf
+ assert_eq!(node_features.flags.len(), 7);
+ assert_eq!(node_features.flags[0], 0b00000010);
+ assert_eq!(node_features.flags[1], 0b01010001);
+ assert_eq!(node_features.flags[2], 0b10001010);
+ assert_eq!(node_features.flags[3], 0b00001000);
+ assert_eq!(node_features.flags[4], 0b10000000);
+ assert_eq!(node_features.flags[5], 0b10100000);
+ assert_eq!(node_features.flags[6], 0b00001000);
+ }
+
+ // Check that cleared flags are kept blank when converting back:
+ // - initial_routing_sync was not applicable to NodeContext
+ // - upfront_shutdown_script was cleared before converting
+ // - gossip_queries was cleared before converting
+ let features: InitFeatures = node_features.to_context_internal();
+ assert!(!features.initial_routing_sync());
+ assert!(!features.supports_upfront_shutdown_script());
+ assert!(!init_features.supports_gossip_queries());
+ }
+
+ #[test]
+ fn convert_to_context_with_unknown_flags() {
+ // Ensure the `from` context has fewer known feature bytes than the `to` context.
+ assert!(<sealed::InvoiceContext as sealed::Context>::KNOWN_FEATURE_MASK.len() <
+ <sealed::NodeContext as sealed::Context>::KNOWN_FEATURE_MASK.len());
+ let mut invoice_features = InvoiceFeatures::empty();
+ invoice_features.set_unknown_feature_optional();
+ assert!(invoice_features.supports_unknown_bits());
+ let node_features: NodeFeatures = invoice_features.to_context();
+ assert!(!node_features.supports_unknown_bits());
+ }
+
+ #[test]
+ fn set_feature_bits() {
+ let mut features = InvoiceFeatures::empty();
+ features.set_basic_mpp_optional();
+ features.set_payment_secret_required();
+ assert!(features.supports_basic_mpp());
+ assert!(!features.requires_basic_mpp());
+ assert!(features.requires_payment_secret());
+ assert!(features.supports_payment_secret());
+ }
+
+ #[test]
+ fn encodes_features_without_length() {
+ let features = OfferFeatures::from_le_bytes(vec![1, 2, 3, 4, 5, 42, 100, 101]);
+ assert_eq!(features.flags.len(), 8);
+
+ let mut serialized_features = Vec::new();
+ WithoutLength(&features).write(&mut serialized_features).unwrap();
+ assert_eq!(serialized_features.len(), 8);
+
+ let deserialized_features =
+ WithoutLength::<OfferFeatures>::read(&mut &serialized_features[..]).unwrap().0;
+ assert_eq!(features, deserialized_features);
+ }
+
+ #[test]
+ fn invoice_features_encoding() {
+ let features_as_u5s = vec![
+ u5::try_from_u8(6).unwrap(),
+ u5::try_from_u8(10).unwrap(),
+ u5::try_from_u8(25).unwrap(),
+ u5::try_from_u8(1).unwrap(),
+ u5::try_from_u8(10).unwrap(),
+ u5::try_from_u8(0).unwrap(),
+ u5::try_from_u8(20).unwrap(),
+ u5::try_from_u8(2).unwrap(),
+ u5::try_from_u8(0).unwrap(),
+ u5::try_from_u8(6).unwrap(),
+ u5::try_from_u8(0).unwrap(),
+ u5::try_from_u8(16).unwrap(),
+ u5::try_from_u8(1).unwrap(),
+ ];
+ let features = InvoiceFeatures::from_le_bytes(vec![1, 2, 3, 4, 5, 42, 100, 101]);
+
+ // Test length calculation.
+ assert_eq!(features.base32_len(), 13);
+
+ // Test serialization.
+ let features_serialized = features.to_base32();
+ assert_eq!(features_as_u5s, features_serialized);
+
+ // Test deserialization.
+ let features_deserialized = InvoiceFeatures::from_base32(&features_as_u5s).unwrap();
+ assert_eq!(features, features_deserialized);
+ }
+
+ #[test]
+ fn test_channel_type_mapping() {
+ // If we map an InvoiceFeatures with StaticRemoteKey optional, it should map into a
+ // required-StaticRemoteKey ChannelTypeFeatures.
+ let mut init_features = InitFeatures::empty();
+ init_features.set_static_remote_key_optional();
+ let converted_features = ChannelTypeFeatures::from_init(&init_features);
+ assert_eq!(converted_features, ChannelTypeFeatures::only_static_remote_key());
+ assert!(!converted_features.supports_any_optional_bits());
+ assert!(converted_features.requires_static_remote_key());