@@ -69,10 +69,6 @@ mod sealed {
69
69
/// The context in which [`Features`] are applicable. Defines which features are required and
70
70
/// which are optional for the context.
71
71
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
-
76
72
/// Bitmask for selecting features that are known to the implementation, regardless of
77
73
/// whether each feature is required or optional.
78
74
const KNOWN_FEATURE_MASK : & ' static [ u8 ] ;
@@ -82,41 +78,18 @@ mod sealed {
82
78
/// are specified as a comma-separated list of bytes where each byte is a pipe-delimited list of
83
79
/// feature identifiers.
84
80
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 ) |* , ) * ] ) => {
89
82
#[ derive( Eq , PartialEq ) ]
90
83
pub struct $context { }
91
84
92
85
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
-
110
86
const KNOWN_FEATURE_MASK : & ' static [ u8 ] = & [
111
87
// Similar as above, but set both flags for each feature regardless of whether
112
88
// the feature is required or optional.
113
89
$(
114
90
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 ) * ,
120
93
) *
121
94
] ;
122
95
}
@@ -125,17 +98,12 @@ mod sealed {
125
98
fn fmt( & self , fmt: & mut alloc:: fmt:: Formatter ) -> Result <( ) , alloc:: fmt:: Error > {
126
99
$(
127
100
$(
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" }
137
104
else { "not supported" } ) ) ?;
138
105
) *
106
+ { } // Rust gets mad if we only have a $()* block here, so add a dummy {}
139
107
) *
140
108
fmt. write_fmt( format_args!( "unknown flags: {}" ,
141
109
if self . requires_unknown_bits( ) { "required" }
@@ -145,132 +113,65 @@ mod sealed {
145
113
} ;
146
114
}
147
115
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
+ ] ) ;
238
157
// This isn't a "real" feature context, and is only used in the channel_type field in an
239
158
// `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
+ ] ) ;
274
175
275
176
/// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
276
177
/// useful for manipulating feature flags.
0 commit comments