X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Ffeatures.rs;h=8f251d8478c7048f33cfb003725f4c94b388a209;hb=7dd8bd7068fdaab3c83e152b304086e1c363e999;hp=b90748aa87b7e5cd55bf0461244fd2c654feb7eb;hpb=f3b63e4dab439166827c1d9f144db1abc824d6ec;p=rust-lightning diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index b90748aa..8f251d84 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -22,8 +22,10 @@ //! [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; @@ -361,6 +363,11 @@ impl Clone for Features { } } } +impl Hash for Features { + fn hash(&self, hasher: &mut H) { + self.flags.hash(hasher); + } +} impl PartialEq for Features { fn eq(&self, o: &Self) -> bool { self.flags.eq(&o.flags) @@ -383,9 +390,8 @@ pub type InvoiceFeatures = Features; impl InitFeatures { /// Writes all features present up to, and including, 13. - pub(crate) fn write_up_to_13(&self, w: &mut W) -> Result<(), ::std::io::Error> { + pub(crate) fn write_up_to_13(&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 { @@ -547,7 +553,9 @@ impl Features { &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(); @@ -575,12 +583,6 @@ impl Features { (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 Features { @@ -692,8 +694,7 @@ impl Features { } impl Writeable for Features { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - w.size_hint(self.flags.len() + 2); + fn write(&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)?; @@ -703,7 +704,7 @@ impl Writeable for Features { } impl Readable for Features { - fn read(r: &mut R) -> Result { + fn read(r: &mut R) -> Result { let mut flags: Vec = Readable::read(r)?; flags.reverse(); // Swap to little-endian Ok(Self { @@ -826,7 +827,7 @@ mod tests { #[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();