Skip to content

Commit 50aeee5

Browse files
committed
Offer features for BOLT 12
The offer message in BOLT 12 contains a features TLV record. Add a corresponding OfferFeatures type where the length is not included in the serialization as it would be redundant with the record length. Otherwise, define the features to be the same as InvoiceFeatures.
1 parent 8f525c4 commit 50aeee5

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

lightning/src/ln/features.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ mod sealed {
157157
// Byte 2
158158
BasicMPP,
159159
]);
160+
define_context!(OfferContext, []);
160161
// This isn't a "real" feature context, and is only used in the channel_type field in an
161162
// `OpenChannel` message.
162163
define_context!(ChannelTypeContext, [
@@ -366,7 +367,7 @@ mod sealed {
366367
supports_keysend, requires_keysend);
367368

368369
#[cfg(test)]
369-
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
370+
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext, OfferContext],
370371
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
371372
set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
372373
}
@@ -425,6 +426,8 @@ pub type NodeFeatures = Features<sealed::NodeContext>;
425426
pub type ChannelFeatures = Features<sealed::ChannelContext>;
426427
/// Features used within an invoice.
427428
pub type InvoiceFeatures = Features<sealed::InvoiceContext>;
429+
/// Features used within an offer.
430+
pub type OfferFeatures = Features<sealed::OfferContext>;
428431

429432
/// Features used within the channel_type field in an OpenChannel message.
430433
///
@@ -704,21 +707,26 @@ impl_feature_len_prefixed_write!(ChannelFeatures);
704707
impl_feature_len_prefixed_write!(NodeFeatures);
705708
impl_feature_len_prefixed_write!(InvoiceFeatures);
706709

707-
// Because ChannelTypeFeatures only appears inside of TLVs, it doesn't have a length prefix when
708-
// serialized. Thus, we can't use `impl_feature_len_prefixed_write`, above, and have to write our
709-
// own serialization.
710-
impl Writeable for ChannelTypeFeatures {
711-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
712-
self.write_be(w)
713-
}
714-
}
715-
impl Readable for ChannelTypeFeatures {
716-
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
717-
let v = io_extras::read_to_end(r)?;
718-
Ok(Self::from_be_bytes(v))
710+
// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.
711+
macro_rules! impl_feature_tlv_write {
712+
($features: ident) => {
713+
impl Writeable for $features {
714+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
715+
self.write_be(w)
716+
}
717+
}
718+
impl Readable for $features {
719+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
720+
let v = io_extras::read_to_end(r)?;
721+
Ok(Self::from_be_bytes(v))
722+
}
723+
}
719724
}
720725
}
721726

727+
impl_feature_tlv_write!(ChannelTypeFeatures);
728+
impl_feature_tlv_write!(OfferFeatures);
729+
722730
#[cfg(test)]
723731
mod tests {
724732
use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, sealed};

0 commit comments

Comments
 (0)