1 //! Feature flag definitions for the Lightning protocol according to [BOLT #9].
3 //! Lightning nodes advertise a supported set of operation through feature flags. Features are
4 //! applicable for a specific context as indicated in some [messages]. [`Features`] encapsulates
5 //! behavior for specifying and checking feature flags for a particular context. Each feature is
6 //! defined internally by a trait specifying the corresponding flags (i.e., even and odd bits). A
7 //! [`Context`] is used to parameterize [`Features`] and defines which features it can support.
9 //! Whether a feature is considered \"known\" or \"unknown\" is relative to the implementation, whereas
10 //! the term \"supports\" is used in reference to a particular set of [`Features`]. That is, a node
11 //! supports a feature if it advertises the feature (as either required or optional) to its peers.
12 //! And the implementation can interpret a feature if the feature is known to it.
14 //! [BOLT #9]: https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md
15 //! [messages]: ../msgs/index.html
16 //! [`Features`]: struct.Features.html
17 //! [`Context`]: sealed/trait.Context.html
20 use bitcoin::hashes::Hash;
21 use crate::c_types::*;
23 impl Clone for InitFeatures {
24 fn clone(&self) -> Self {
26 inner: if <*mut nativeInitFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
27 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
33 /// Used only if an object of this type is returned as a trait impl by a method
34 pub(crate) extern "C" fn InitFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
35 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInitFeatures)).clone() })) as *mut c_void
38 /// Creates a copy of the InitFeatures
39 pub extern "C" fn InitFeatures_clone(orig: &InitFeatures) -> InitFeatures {
42 impl Clone for NodeFeatures {
43 fn clone(&self) -> Self {
45 inner: if <*mut nativeNodeFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
46 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
52 /// Used only if an object of this type is returned as a trait impl by a method
53 pub(crate) extern "C" fn NodeFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
54 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeNodeFeatures)).clone() })) as *mut c_void
57 /// Creates a copy of the NodeFeatures
58 pub extern "C" fn NodeFeatures_clone(orig: &NodeFeatures) -> NodeFeatures {
61 impl Clone for ChannelFeatures {
62 fn clone(&self) -> Self {
64 inner: if <*mut nativeChannelFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
65 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
71 /// Used only if an object of this type is returned as a trait impl by a method
72 pub(crate) extern "C" fn ChannelFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
73 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelFeatures)).clone() })) as *mut c_void
76 /// Creates a copy of the ChannelFeatures
77 pub extern "C" fn ChannelFeatures_clone(orig: &ChannelFeatures) -> ChannelFeatures {
80 impl Clone for InvoiceFeatures {
81 fn clone(&self) -> Self {
83 inner: if <*mut nativeInvoiceFeatures>::is_null(self.inner) { std::ptr::null_mut() } else {
84 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
90 /// Used only if an object of this type is returned as a trait impl by a method
91 pub(crate) extern "C" fn InvoiceFeatures_clone_void(this_ptr: *const c_void) -> *mut c_void {
92 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInvoiceFeatures)).clone() })) as *mut c_void
95 /// Creates a copy of the InvoiceFeatures
96 pub extern "C" fn InvoiceFeatures_clone(orig: &InvoiceFeatures) -> InvoiceFeatures {
100 use lightning::ln::features::InitFeatures as nativeInitFeaturesImport;
101 type nativeInitFeatures = nativeInitFeaturesImport;
103 /// Features used within an `init` message.
106 pub struct InitFeatures {
107 /// A pointer to the opaque Rust object.
109 /// Nearly everywhere, inner must be non-null, however in places where
110 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
111 pub inner: *mut nativeInitFeatures,
112 /// Indicates that this is the only struct which contains the same pointer.
114 /// Rust functions which take ownership of an object provided via an argument require
115 /// this to be true and invalidate the object pointed to by inner.
119 impl Drop for InitFeatures {
121 if self.is_owned && !<*mut nativeInitFeatures>::is_null(self.inner) {
122 let _ = unsafe { Box::from_raw(self.inner) };
126 /// Frees any resources used by the InitFeatures, if is_owned is set and inner is non-NULL.
128 pub extern "C" fn InitFeatures_free(this_obj: InitFeatures) { }
130 /// Used only if an object of this type is returned as a trait impl by a method
131 extern "C" fn InitFeatures_free_void(this_ptr: *mut c_void) {
132 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInitFeatures); }
135 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
137 pub(crate) fn take_inner(mut self) -> *mut nativeInitFeatures {
138 assert!(self.is_owned);
139 let ret = self.inner;
140 self.inner = std::ptr::null_mut();
145 use lightning::ln::features::NodeFeatures as nativeNodeFeaturesImport;
146 type nativeNodeFeatures = nativeNodeFeaturesImport;
148 /// Features used within a `node_announcement` message.
151 pub struct NodeFeatures {
152 /// A pointer to the opaque Rust object.
154 /// Nearly everywhere, inner must be non-null, however in places where
155 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
156 pub inner: *mut nativeNodeFeatures,
157 /// Indicates that this is the only struct which contains the same pointer.
159 /// Rust functions which take ownership of an object provided via an argument require
160 /// this to be true and invalidate the object pointed to by inner.
164 impl Drop for NodeFeatures {
166 if self.is_owned && !<*mut nativeNodeFeatures>::is_null(self.inner) {
167 let _ = unsafe { Box::from_raw(self.inner) };
171 /// Frees any resources used by the NodeFeatures, if is_owned is set and inner is non-NULL.
173 pub extern "C" fn NodeFeatures_free(this_obj: NodeFeatures) { }
175 /// Used only if an object of this type is returned as a trait impl by a method
176 extern "C" fn NodeFeatures_free_void(this_ptr: *mut c_void) {
177 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNodeFeatures); }
180 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
182 pub(crate) fn take_inner(mut self) -> *mut nativeNodeFeatures {
183 assert!(self.is_owned);
184 let ret = self.inner;
185 self.inner = std::ptr::null_mut();
190 use lightning::ln::features::ChannelFeatures as nativeChannelFeaturesImport;
191 type nativeChannelFeatures = nativeChannelFeaturesImport;
193 /// Features used within a `channel_announcement` message.
196 pub struct ChannelFeatures {
197 /// A pointer to the opaque Rust object.
199 /// Nearly everywhere, inner must be non-null, however in places where
200 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
201 pub inner: *mut nativeChannelFeatures,
202 /// Indicates that this is the only struct which contains the same pointer.
204 /// Rust functions which take ownership of an object provided via an argument require
205 /// this to be true and invalidate the object pointed to by inner.
209 impl Drop for ChannelFeatures {
211 if self.is_owned && !<*mut nativeChannelFeatures>::is_null(self.inner) {
212 let _ = unsafe { Box::from_raw(self.inner) };
216 /// Frees any resources used by the ChannelFeatures, if is_owned is set and inner is non-NULL.
218 pub extern "C" fn ChannelFeatures_free(this_obj: ChannelFeatures) { }
220 /// Used only if an object of this type is returned as a trait impl by a method
221 extern "C" fn ChannelFeatures_free_void(this_ptr: *mut c_void) {
222 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelFeatures); }
225 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
226 impl ChannelFeatures {
227 pub(crate) fn take_inner(mut self) -> *mut nativeChannelFeatures {
228 assert!(self.is_owned);
229 let ret = self.inner;
230 self.inner = std::ptr::null_mut();
235 use lightning::ln::features::InvoiceFeatures as nativeInvoiceFeaturesImport;
236 type nativeInvoiceFeatures = nativeInvoiceFeaturesImport;
238 /// Features used within an invoice.
241 pub struct InvoiceFeatures {
242 /// A pointer to the opaque Rust object.
244 /// Nearly everywhere, inner must be non-null, however in places where
245 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
246 pub inner: *mut nativeInvoiceFeatures,
247 /// Indicates that this is the only struct which contains the same pointer.
249 /// Rust functions which take ownership of an object provided via an argument require
250 /// this to be true and invalidate the object pointed to by inner.
254 impl Drop for InvoiceFeatures {
256 if self.is_owned && !<*mut nativeInvoiceFeatures>::is_null(self.inner) {
257 let _ = unsafe { Box::from_raw(self.inner) };
261 /// Frees any resources used by the InvoiceFeatures, if is_owned is set and inner is non-NULL.
263 pub extern "C" fn InvoiceFeatures_free(this_obj: InvoiceFeatures) { }
265 /// Used only if an object of this type is returned as a trait impl by a method
266 extern "C" fn InvoiceFeatures_free_void(this_ptr: *mut c_void) {
267 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvoiceFeatures); }
270 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
271 impl InvoiceFeatures {
272 pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceFeatures {
273 assert!(self.is_owned);
274 let ret = self.inner;
275 self.inner = std::ptr::null_mut();
279 /// Create a blank Features with no features set
282 pub extern "C" fn InitFeatures_empty() -> InitFeatures {
283 let mut ret = lightning::ln::features::InitFeatures::empty();
284 InitFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
287 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
289 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
292 pub extern "C" fn InitFeatures_known() -> InitFeatures {
293 let mut ret = lightning::ln::features::InitFeatures::known();
294 InitFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
297 /// Create a blank Features with no features set
300 pub extern "C" fn NodeFeatures_empty() -> NodeFeatures {
301 let mut ret = lightning::ln::features::NodeFeatures::empty();
302 NodeFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
305 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
307 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
310 pub extern "C" fn NodeFeatures_known() -> NodeFeatures {
311 let mut ret = lightning::ln::features::NodeFeatures::known();
312 NodeFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
315 /// Create a blank Features with no features set
318 pub extern "C" fn ChannelFeatures_empty() -> ChannelFeatures {
319 let mut ret = lightning::ln::features::ChannelFeatures::empty();
320 ChannelFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
323 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
325 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
328 pub extern "C" fn ChannelFeatures_known() -> ChannelFeatures {
329 let mut ret = lightning::ln::features::ChannelFeatures::known();
330 ChannelFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
333 /// Create a blank Features with no features set
336 pub extern "C" fn InvoiceFeatures_empty() -> InvoiceFeatures {
337 let mut ret = lightning::ln::features::InvoiceFeatures::empty();
338 InvoiceFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
341 /// Creates features known by the implementation as defined by [`T::KNOWN_FEATURE_FLAGS`].
343 /// [`T::KNOWN_FEATURE_FLAGS`]: sealed/trait.Context.html#associatedconstant.KNOWN_FEATURE_FLAGS
346 pub extern "C" fn InvoiceFeatures_known() -> InvoiceFeatures {
347 let mut ret = lightning::ln::features::InvoiceFeatures::known();
348 InvoiceFeatures { inner: Box::into_raw(Box::new(ret)), is_owned: true }
352 /// Serialize the InitFeatures object into a byte array which can be read by InitFeatures_read
353 pub extern "C" fn InitFeatures_write(obj: &InitFeatures) -> crate::c_types::derived::CVec_u8Z {
354 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
357 pub(crate) extern "C" fn InitFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
358 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInitFeatures) })
361 /// Serialize the NodeFeatures object into a byte array which can be read by NodeFeatures_read
362 pub extern "C" fn NodeFeatures_write(obj: &NodeFeatures) -> crate::c_types::derived::CVec_u8Z {
363 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
366 pub(crate) extern "C" fn NodeFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
367 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeNodeFeatures) })
370 /// Serialize the ChannelFeatures object into a byte array which can be read by ChannelFeatures_read
371 pub extern "C" fn ChannelFeatures_write(obj: &ChannelFeatures) -> crate::c_types::derived::CVec_u8Z {
372 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
375 pub(crate) extern "C" fn ChannelFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
376 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelFeatures) })
379 /// Serialize the InvoiceFeatures object into a byte array which can be read by InvoiceFeatures_read
380 pub extern "C" fn InvoiceFeatures_write(obj: &InvoiceFeatures) -> crate::c_types::derived::CVec_u8Z {
381 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
384 pub(crate) extern "C" fn InvoiceFeatures_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
385 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInvoiceFeatures) })
388 /// Read a InitFeatures from a byte array, created by InitFeatures_write
389 pub extern "C" fn InitFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InitFeaturesDecodeErrorZ {
390 let res = crate::c_types::deserialize_obj(ser);
391 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::InitFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
395 /// Read a NodeFeatures from a byte array, created by NodeFeatures_write
396 pub extern "C" fn NodeFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_NodeFeaturesDecodeErrorZ {
397 let res = crate::c_types::deserialize_obj(ser);
398 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::NodeFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
402 /// Read a ChannelFeatures from a byte array, created by ChannelFeatures_write
403 pub extern "C" fn ChannelFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelFeaturesDecodeErrorZ {
404 let res = crate::c_types::deserialize_obj(ser);
405 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::ChannelFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
409 /// Read a InvoiceFeatures from a byte array, created by InvoiceFeatures_write
410 pub extern "C" fn InvoiceFeatures_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InvoiceFeaturesDecodeErrorZ {
411 let res = crate::c_types::deserialize_obj(ser);
412 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::features::InvoiceFeatures { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };