//! [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
//! [messages]: crate::ln::msgs
+use io;
use prelude::*;
use core::{cmp, fmt};
+use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use bitcoin::bech32;
)*
];
}
+
+ impl alloc::fmt::Display for Features<$context> {
+ fn fmt(&self, fmt: &mut alloc::fmt::Formatter) -> Result<(), alloc::fmt::Error> {
+ $(
+ $(
+ fmt.write_fmt(format_args!("{}: {}, ", stringify!($required_feature),
+ if <$context as $required_feature>::requires_feature(&self.flags) { "required" }
+ else if <$context as $required_feature>::supports_feature(&self.flags) { "supported" }
+ else { "not supported" }))?;
+ )*
+ $(
+ fmt.write_fmt(format_args!("{}: {}, ", stringify!($optional_feature),
+ if <$context as $optional_feature>::requires_feature(&self.flags) { "required" }
+ else if <$context as $optional_feature>::supports_feature(&self.flags) { "supported" }
+ else { "not supported" }))?;
+ )*
+ )*
+ fmt.write_fmt(format_args!("unknown flags: {}",
+ if self.requires_unknown_bits() { "required" }
+ else if self.supports_unknown_bits() { "supported" } else { "none" }))
+ }
+ }
};
}
}
}
}
+impl<T: sealed::Context> Hash for Features<T> {
+ fn hash<H: Hasher>(&self, hasher: &mut H) {
+ self.flags.hash(hasher);
+ }
+}
impl<T: sealed::Context> PartialEq for Features<T> {
fn eq(&self, o: &Self) -> bool {
self.flags.eq(&o.flags)
impl InitFeatures {
/// Writes all features present up to, and including, 13.
- pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+ pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
let len = cmp::min(2, self.flags.len());
- w.size_hint(len + 2);
(len as u16).write(w)?;
for i in (0..len).rev() {
if i == 0 {
/// Getting a route for a keysend payment to a private node requires providing the payee's
/// features (since they were not announced in a node announcement). However, keysend payments
/// don't have an invoice to pull the payee's features from, so this method is provided for use in
- /// [`get_keysend_route`], thus omitting the need for payers to manually construct an
- /// `InvoiceFeatures` for [`get_route`].
+ /// [`Payee::for_keysend`], thus omitting the need for payers to manually construct an
+ /// `InvoiceFeatures` for [`find_route`].
///
- /// [`get_keysend_route`]: crate::routing::router::get_keysend_route
- /// [`get_route`]: crate::routing::router::get_route
+ /// [`Payee::for_keysend`]: crate::routing::router::Payee::for_keysend
+ /// [`find_route`]: crate::routing::router::find_route
pub(crate) fn for_keysend() -> InvoiceFeatures {
InvoiceFeatures::empty().set_variable_length_onion_optional()
}
&self.flags
}
- pub(crate) fn requires_unknown_bits(&self) -> bool {
+ /// Returns true if this `Features` object contains unknown feature flags which are set as
+ /// "required".
+ pub fn requires_unknown_bits(&self) -> bool {
// Bitwise AND-ing with all even bits set except for known features will select required
// unknown features.
let byte_count = T::KNOWN_FEATURE_MASK.len();
(byte & unknown_features) != 0
})
}
-
- /// The number of bytes required to represent the feature flags present. This does not include
- /// the length bytes which are included in the serialized form.
- pub(crate) fn byte_count(&self) -> usize {
- self.flags.len()
- }
}
impl<T: sealed::DataLossProtect> Features<T> {
pub(crate) fn requires_data_loss_protect(&self) -> bool {
<T as sealed::DataLossProtect>::requires_feature(&self.flags)
}
+ #[cfg(test)]
pub(crate) fn supports_data_loss_protect(&self) -> bool {
<T as sealed::DataLossProtect>::supports_feature(&self.flags)
}
}
impl<T: sealed::Context> Writeable for Features<T> {
- fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
- w.size_hint(self.flags.len() + 2);
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
(self.flags.len() as u16).write(w)?;
for f in self.flags.iter().rev() { // Swap back to big-endian
f.write(w)?;
}
impl<T: sealed::Context> Readable for Features<T> {
- fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+ fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
let mut flags: Vec<u8> = Readable::read(r)?;
flags.reverse(); // Swap to little-endian
Ok(Self {
#[test]
fn convert_to_context_with_unknown_flags() {
// Ensure the `from` context has fewer known feature bytes than the `to` context.
- assert!(InvoiceFeatures::known().byte_count() < NodeFeatures::known().byte_count());
+ assert!(InvoiceFeatures::known().flags.len() < NodeFeatures::known().flags.len());
let invoice_features = InvoiceFeatures::known().set_unknown_feature_optional();
assert!(invoice_features.supports_unknown_bits());
let node_features: NodeFeatures = invoice_features.to_context();