Skip to content

Commit 7df6b15

Browse files
committed
Seal the features contexts
1 parent f26656e commit 7df6b15

File tree

1 file changed

+88
-91
lines changed

1 file changed

+88
-91
lines changed

lightning/src/ln/features.rs

+88-91
Original file line numberDiff line numberDiff line change
@@ -8,125 +8,144 @@ use std::marker::PhantomData;
88
use ln::msgs::DecodeError;
99
use util::ser::{Readable, Writeable, Writer};
1010

11-
/// The context in which a Feature object appears determines which bits of features the node
12-
/// supports will be set. We use this when creating our own Feature objects to select which bits to
13-
/// set and when passing around Feature objects to ensure the bits we're checking for are
14-
/// available.
15-
///
16-
/// This Context represents when the Feature appears in the init message, sent between peers and not
17-
/// rumored around the P2P network.
18-
pub struct FeatureContextInit {}
19-
/// The context in which a Feature object appears determines which bits of features the node
20-
/// supports will be set. We use this when creating our own Feature objects to select which bits to
21-
/// set and when passing around Feature objects to ensure the bits we're checking for are
22-
/// available.
23-
///
24-
/// This Context represents when the Feature appears in the node_announcement message, as it is
25-
/// rumored around the P2P network.
26-
pub struct FeatureContextNode {}
27-
/// The context in which a Feature object appears determines which bits of features the node
28-
/// supports will be set. We use this when creating our own Feature objects to select which bits to
29-
/// set and when passing around Feature objects to ensure the bits we're checking for are
30-
/// available.
31-
///
32-
/// This Context represents when the Feature appears in the ChannelAnnouncement message, as it is
33-
/// rumored around the P2P network.
34-
pub struct FeatureContextChannel {}
35-
/// The context in which a Feature object appears determines which bits of features the node
36-
/// supports will be set. We use this when creating our own Feature objects to select which bits to
37-
/// set and when passing around Feature objects to ensure the bits we're checking for are
38-
/// available.
39-
///
40-
/// This Context represents when the Feature appears in an invoice, used to determine the different
41-
/// options available for routing a payment.
42-
///
43-
/// Note that this is currently unused as invoices come to us via a different crate and are not
44-
/// native to rust-lightning directly.
45-
pub struct FeatureContextInvoice {}
11+
mod sealed { // You should just use the type aliases instead.
12+
pub struct InitContext {}
13+
pub struct NodeContext {}
14+
pub struct ChannelContext {}
4615

47-
/// An internal trait capturing the various future context types
48-
pub trait FeatureContext {}
49-
impl FeatureContext for FeatureContextInit {}
50-
impl FeatureContext for FeatureContextNode {}
51-
impl FeatureContext for FeatureContextChannel {}
52-
impl FeatureContext for FeatureContextInvoice {}
16+
/// An internal trait capturing the various feature context types
17+
pub trait Context {}
18+
impl Context for InitContext {}
19+
impl Context for NodeContext {}
20+
impl Context for ChannelContext {}
5321

54-
/// An internal trait capturing FeatureContextInit and FeatureContextNode
55-
pub trait FeatureContextInitNode : FeatureContext {}
56-
impl FeatureContextInitNode for FeatureContextInit {}
57-
impl FeatureContextInitNode for FeatureContextNode {}
22+
pub trait DataLossProtect: Context {}
23+
impl DataLossProtect for InitContext {}
24+
impl DataLossProtect for NodeContext {}
25+
26+
pub trait InitialRoutingSync: Context {}
27+
impl InitialRoutingSync for InitContext {}
28+
29+
pub trait UpfrontShutdownScript: Context {}
30+
impl UpfrontShutdownScript for InitContext {}
31+
impl UpfrontShutdownScript for NodeContext {}
32+
}
5833

5934
/// Tracks the set of features which a node implements, templated by the context in which it
6035
/// appears.
61-
pub struct Features<T: FeatureContext> {
36+
pub struct Features<T: sealed::Context> {
6237
/// Note that, for convinience, flags is LITTLE endian (despite being big-endian on the wire)
6338
flags: Vec<u8>,
6439
mark: PhantomData<T>,
6540
}
6641

67-
impl<T: FeatureContext> Clone for Features<T> {
42+
impl<T: sealed::Context> Clone for Features<T> {
6843
fn clone(&self) -> Self {
6944
Self {
7045
flags: self.flags.clone(),
7146
mark: PhantomData,
7247
}
7348
}
7449
}
75-
impl<T: FeatureContext> PartialEq for Features<T> {
50+
impl<T: sealed::Context> PartialEq for Features<T> {
7651
fn eq(&self, o: &Self) -> bool {
7752
self.flags.eq(&o.flags)
7853
}
7954
}
80-
impl<T: FeatureContext> fmt::Debug for Features<T> {
55+
impl<T: sealed::Context> fmt::Debug for Features<T> {
8156
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
8257
self.flags.fmt(fmt)
8358
}
8459
}
8560

8661
/// A feature message as it appears in an init message
87-
pub type InitFeatures = Features<FeatureContextInit>;
62+
pub type InitFeatures = Features<sealed::InitContext>;
8863
/// A feature message as it appears in a node_announcement message
89-
pub type NodeFeatures = Features<FeatureContextNode>;
64+
pub type NodeFeatures = Features<sealed::NodeContext>;
9065
/// A feature message as it appears in a channel_announcement message
91-
pub type ChannelFeatures = Features<FeatureContextChannel>;
66+
pub type ChannelFeatures = Features<sealed::ChannelContext>;
9267

93-
impl<T: FeatureContextInitNode> Features<T> {
68+
impl InitFeatures {
9469
/// Create a Features with the features we support
9570
#[cfg(not(feature = "fuzztarget"))]
96-
pub(crate) fn our_features() -> Features<T> {
97-
Features {
71+
pub(crate) fn our_features() -> InitFeatures {
72+
InitFeatures {
9873
flags: vec![2 | 1 << 5],
9974
mark: PhantomData,
10075
}
10176
}
10277
#[cfg(feature = "fuzztarget")]
103-
pub fn our_features() -> Features<T> {
104-
Features {
78+
pub fn our_features() -> InitFeatures {
79+
InitFeatures {
10580
flags: vec![2 | 1 << 5],
10681
mark: PhantomData,
10782
}
10883
}
84+
85+
/// Writes all features present up to, and including, 13.
86+
pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
87+
let len = cmp::min(2, self.flags.len());
88+
w.size_hint(len + 2);
89+
(len as u16).write(w)?;
90+
for i in (0..len).rev() {
91+
if i == 0 {
92+
self.flags[i].write(w)?;
93+
} else {
94+
// On byte 1, we want up-to-and-including-bit-13, 0-indexed, which is
95+
// up-to-and-including-bit-5, 0-indexed, on this byte:
96+
(self.flags[i] & 0b00_11_11_11).write(w)?;
97+
}
98+
}
99+
Ok(())
100+
}
101+
102+
/// or's another InitFeatures into this one.
103+
pub(crate) fn or(&mut self, o: &InitFeatures) {
104+
let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
105+
self.flags.resize(total_feature_len, 0u8);
106+
for (byte, o_byte) in self.flags.iter_mut().zip(o.flags.iter()) {
107+
*byte |= *o_byte;
108+
}
109+
}
109110
}
110111

111-
impl Features<FeatureContextChannel> {
112+
impl ChannelFeatures {
112113
/// Create a Features with the features we support
113114
#[cfg(not(feature = "fuzztarget"))]
114-
pub(crate) fn our_features() -> Features<FeatureContextChannel> {
115-
Features {
115+
pub(crate) fn our_features() -> ChannelFeatures {
116+
ChannelFeatures {
116117
flags: Vec::new(),
117118
mark: PhantomData,
118119
}
119120
}
120121
#[cfg(feature = "fuzztarget")]
121-
pub fn our_features() -> Features<FeatureContextChannel> {
122-
Features {
122+
pub fn our_features() -> ChannelFeatures {
123+
ChannelFeatures {
123124
flags: Vec::new(),
124125
mark: PhantomData,
125126
}
126127
}
127128
}
128129

129-
impl<T: FeatureContext> Features<T> {
130+
impl NodeFeatures {
131+
/// Create a Features with the features we support
132+
#[cfg(not(feature = "fuzztarget"))]
133+
pub(crate) fn our_features() -> NodeFeatures {
134+
NodeFeatures {
135+
flags: vec![2 | 1 << 5],
136+
mark: PhantomData,
137+
}
138+
}
139+
#[cfg(feature = "fuzztarget")]
140+
pub fn our_features() -> NodeFeatures {
141+
NodeFeatures {
142+
flags: vec![2 | 1 << 5],
143+
mark: PhantomData,
144+
}
145+
}
146+
}
147+
148+
impl<T: sealed::Context> Features<T> {
130149
/// Create a blank Features with no features set
131150
pub fn empty() -> Features<T> {
132151
Features {
@@ -186,11 +205,13 @@ impl<T: FeatureContext> Features<T> {
186205
}
187206
}
188207

189-
impl<T: FeatureContextInitNode> Features<T> {
208+
impl<T: sealed::DataLossProtect> Features<T> {
190209
pub(crate) fn supports_data_loss_protect(&self) -> bool {
191210
self.flags.len() > 0 && (self.flags[0] & 3) != 0
192211
}
212+
}
193213

214+
impl<T: sealed::UpfrontShutdownScript> Features<T> {
194215
pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
195216
self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
196217
}
@@ -200,7 +221,7 @@ impl<T: FeatureContextInitNode> Features<T> {
200221
}
201222
}
202223

203-
impl Features<FeatureContextInit> {
224+
impl<T: sealed::InitialRoutingSync> Features<T> {
204225
pub(crate) fn initial_routing_sync(&self) -> bool {
205226
self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
206227
}
@@ -211,33 +232,9 @@ impl Features<FeatureContextInit> {
211232
self.flags[0] |= 1 << 3;
212233
}
213234
}
214-
215-
/// Writes all features present up to, and including, 13.
216-
pub(crate) fn write_up_to_13<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
217-
let len = cmp::min(2, self.flags.len());
218-
w.size_hint(len + 2);
219-
(len as u16).write(w)?;
220-
for i in (0..len).rev() {
221-
if i == 0 {
222-
self.flags[i].write(w)?;
223-
} else {
224-
(self.flags[i] & ((1 << (14 - 8)) - 1)).write(w)?;
225-
}
226-
}
227-
Ok(())
228-
}
229-
230-
/// or's another InitFeatures into this one.
231-
pub(crate) fn or(&mut self, o: &InitFeatures) {
232-
let total_feature_len = cmp::max(self.flags.len(), o.flags.len());
233-
self.flags.resize(total_feature_len, 0u8);
234-
for (feature, o_feature) in self.flags.iter_mut().zip(o.flags.iter()) {
235-
*feature |= *o_feature;
236-
}
237-
}
238235
}
239236

240-
impl<T: FeatureContext> Writeable for Features<T> {
237+
impl<T: sealed::Context> Writeable for Features<T> {
241238
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
242239
w.size_hint(self.flags.len() + 2);
243240
(self.flags.len() as u16).write(w)?;
@@ -248,7 +245,7 @@ impl<T: FeatureContext> Writeable for Features<T> {
248245
}
249246
}
250247

251-
impl<R: ::std::io::Read, T: FeatureContext> Readable<R> for Features<T> {
248+
impl<R: ::std::io::Read, T: sealed::Context> Readable<R> for Features<T> {
252249
fn read(r: &mut R) -> Result<Self, DecodeError> {
253250
let mut flags: Vec<u8> = Readable::read(r)?;
254251
flags.reverse(); // Swap to little-endian

0 commit comments

Comments
 (0)