@@ -69,10 +69,6 @@ mod sealed {
6969 /// The context in which [`Features`] are applicable. Defines which features are required and
7070 /// which are optional for the context.
7171 pub trait Context {
72- /// Features that are known to the implementation, where a required feature is indicated by
73- /// its even bit and an optional feature is indicated by its odd bit.
74- const KNOWN_FEATURE_FLAGS : & ' static [ u8 ] ;
75-
7672 /// Bitmask for selecting features that are known to the implementation, regardless of
7773 /// whether each feature is required or optional.
7874 const KNOWN_FEATURE_MASK : & ' static [ u8 ] ;
@@ -82,41 +78,18 @@ mod sealed {
8278 /// are specified as a comma-separated list of bytes where each byte is a pipe-delimited list of
8379 /// feature identifiers.
8480 macro_rules! define_context {
85- ( $context: ident {
86- required_features: [ $( $( $required_feature: ident ) |* , ) * ] ,
87- optional_features: [ $( $( $optional_feature: ident ) |* , ) * ] ,
88- } ) => {
81+ ( $context: ident, [ $( $( $known_feature: ident ) |* , ) * ] ) => {
8982 #[ derive( Eq , PartialEq ) ]
9083 pub struct $context { }
9184
9285 impl Context for $context {
93- const KNOWN_FEATURE_FLAGS : & ' static [ u8 ] = & [
94- // For each byte, use bitwise-OR to compute the applicable flags for known
95- // required features `r_i` and optional features `o_j` for all `i` and `j` such
96- // that the following slice is formed:
97- //
98- // [
99- // `r_0` | `r_1` | ... | `o_0` | `o_1` | ...,
100- // ...,
101- // ]
102- $(
103- 0b00_00_00_00 $( |
104- <Self as $required_feature>:: REQUIRED_MASK ) *
105- $( |
106- <Self as $optional_feature>:: OPTIONAL_MASK ) * ,
107- ) *
108- ] ;
109-
11086 const KNOWN_FEATURE_MASK : & ' static [ u8 ] = & [
11187 // Similar as above, but set both flags for each feature regardless of whether
11288 // the feature is required or optional.
11389 $(
11490 0b00_00_00_00 $( |
115- <Self as $required_feature>:: REQUIRED_MASK |
116- <Self as $required_feature>:: OPTIONAL_MASK ) *
117- $( |
118- <Self as $optional_feature>:: REQUIRED_MASK |
119- <Self as $optional_feature>:: OPTIONAL_MASK ) * ,
91+ <Self as $known_feature>:: REQUIRED_MASK |
92+ <Self as $known_feature>:: OPTIONAL_MASK ) * ,
12093 ) *
12194 ] ;
12295 }
@@ -125,17 +98,12 @@ mod sealed {
12598 fn fmt( & self , fmt: & mut alloc:: fmt:: Formatter ) -> Result <( ) , alloc:: fmt:: Error > {
12699 $(
127100 $(
128- fmt. write_fmt( format_args!( "{}: {}, " , stringify!( $required_feature) ,
129- if <$context as $required_feature>:: requires_feature( & self . flags) { "required" }
130- else if <$context as $required_feature>:: supports_feature( & self . flags) { "supported" }
131- else { "not supported" } ) ) ?;
132- ) *
133- $(
134- fmt. write_fmt( format_args!( "{}: {}, " , stringify!( $optional_feature) ,
135- if <$context as $optional_feature>:: requires_feature( & self . flags) { "required" }
136- else if <$context as $optional_feature>:: supports_feature( & self . flags) { "supported" }
101+ fmt. write_fmt( format_args!( "{}: {}, " , stringify!( $known_feature) ,
102+ if <$context as $known_feature>:: requires_feature( & self . flags) { "required" }
103+ else if <$context as $known_feature>:: supports_feature( & self . flags) { "supported" }
137104 else { "not supported" } ) ) ?;
138105 ) *
106+ { } // Rust gets mad if we only have a $()* block here, so add a dummy {}
139107 ) *
140108 fmt. write_fmt( format_args!( "unknown flags: {}" ,
141109 if self . requires_unknown_bits( ) { "required" }
@@ -145,132 +113,65 @@ mod sealed {
145113 } ;
146114 }
147115
148- define_context ! ( InitContext {
149- required_features: [
150- // Byte 0
151- ,
152- // Byte 1
153- VariableLengthOnion | StaticRemoteKey | PaymentSecret ,
154- // Byte 2
155- ,
156- // Byte 3
157- ,
158- // Byte 4
159- ,
160- // Byte 5
161- ,
162- // Byte 6
163- ,
164- ] ,
165- optional_features: [
166- // Byte 0
167- DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries ,
168- // Byte 1
169- ,
170- // Byte 2
171- BasicMPP | Wumbo ,
172- // Byte 3
173- ShutdownAnySegwit ,
174- // Byte 4
175- ,
176- // Byte 5
177- ChannelType | SCIDPrivacy ,
178- // Byte 6
179- ZeroConf ,
180- ] ,
181- } ) ;
182- define_context ! ( NodeContext {
183- required_features: [
184- // Byte 0
185- ,
186- // Byte 1
187- VariableLengthOnion | StaticRemoteKey | PaymentSecret ,
188- // Byte 2
189- ,
190- // Byte 3
191- ,
192- // Byte 4
193- ,
194- // Byte 5
195- ,
196- // Byte 6
197- ,
198- ] ,
199- optional_features: [
200- // Byte 0
201- DataLossProtect | UpfrontShutdownScript | GossipQueries ,
202- // Byte 1
203- ,
204- // Byte 2
205- BasicMPP | Wumbo ,
206- // Byte 3
207- ShutdownAnySegwit ,
208- // Byte 4
209- ,
210- // Byte 5
211- ChannelType | SCIDPrivacy ,
212- // Byte 6
213- ZeroConf | Keysend ,
214- ] ,
215- } ) ;
216- define_context ! ( ChannelContext {
217- required_features: [ ] ,
218- optional_features: [ ] ,
219- } ) ;
220- define_context ! ( InvoiceContext {
221- required_features: [
222- // Byte 0
223- ,
224- // Byte 1
225- VariableLengthOnion | PaymentSecret ,
226- // Byte 2
227- ,
228- ] ,
229- optional_features: [
230- // Byte 0
231- ,
232- // Byte 1
233- ,
234- // Byte 2
235- BasicMPP ,
236- ] ,
237- } ) ;
116+ define_context ! ( InitContext , [
117+ // Byte 0
118+ DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries ,
119+ // Byte 1
120+ VariableLengthOnion | StaticRemoteKey | PaymentSecret ,
121+ // Byte 2
122+ BasicMPP | Wumbo ,
123+ // Byte 3
124+ ShutdownAnySegwit ,
125+ // Byte 4
126+ ,
127+ // Byte 5
128+ ChannelType | SCIDPrivacy ,
129+ // Byte 6
130+ ZeroConf ,
131+ ] ) ;
132+ define_context ! ( NodeContext , [
133+ // Byte 0
134+ DataLossProtect | UpfrontShutdownScript | GossipQueries ,
135+ // Byte 1
136+ VariableLengthOnion | StaticRemoteKey | PaymentSecret ,
137+ // Byte 2
138+ BasicMPP | Wumbo ,
139+ // Byte 3
140+ ShutdownAnySegwit ,
141+ // Byte 4
142+ ,
143+ // Byte 5
144+ ChannelType | SCIDPrivacy ,
145+ // Byte 6
146+ ZeroConf | Keysend ,
147+ ] ) ;
148+ define_context ! ( ChannelContext , [ ] ) ;
149+ define_context ! ( InvoiceContext , [
150+ // Byte 0
151+ ,
152+ // Byte 1
153+ VariableLengthOnion | PaymentSecret ,
154+ // Byte 2
155+ BasicMPP ,
156+ ] ) ;
238157 // This isn't a "real" feature context, and is only used in the channel_type field in an
239158 // `OpenChannel` message.
240- define_context ! ( ChannelTypeContext {
241- required_features: [
242- // Byte 0
243- ,
244- // Byte 1
245- StaticRemoteKey ,
246- // Byte 2
247- ,
248- // Byte 3
249- ,
250- // Byte 4
251- ,
252- // Byte 5
253- SCIDPrivacy ,
254- // Byte 6
255- ZeroConf ,
256- ] ,
257- optional_features: [
258- // Byte 0
259- ,
260- // Byte 1
261- ,
262- // Byte 2
263- ,
264- // Byte 3
265- ,
266- // Byte 4
267- ,
268- // Byte 5
269- ,
270- // Byte 6
271- ,
272- ] ,
273- } ) ;
159+ define_context ! ( ChannelTypeContext , [
160+ // Byte 0
161+ ,
162+ // Byte 1
163+ StaticRemoteKey ,
164+ // Byte 2
165+ ,
166+ // Byte 3
167+ ,
168+ // Byte 4
169+ ,
170+ // Byte 5
171+ SCIDPrivacy ,
172+ // Byte 6
173+ ZeroConf ,
174+ ] ) ;
274175
275176 /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
276177 /// useful for manipulating feature flags.
0 commit comments