1
- #![ cfg_attr( rustfmt, rustfmt_skip) ]
2
-
3
- use bitcoin:: constants:: ChainHash ;
4
- use bitcoin:: network:: Network ;
5
- use lightning_types:: features:: { ChannelTypeFeatures , InitFeatures } ;
6
- use crate :: ln:: channel:: { OutboundV1Channel , InboundV1Channel } ;
7
1
use crate :: chain:: chaininterface:: LowerBoundedFeeEstimator ;
8
- use bitcoin :: secp256k1 :: { SecretKey , PublicKey } ;
2
+ use crate :: ln :: channel :: { InboundV1Channel , OutboundV1Channel } ;
9
3
use crate :: ln:: channelmanager;
4
+ use crate :: prelude:: * ;
10
5
use crate :: util:: config:: UserConfig ;
11
6
use crate :: util:: test_utils:: { TestFeeEstimator , TestKeysInterface , TestLogger } ;
12
- use bitcoin:: secp256k1:: Secp256k1 ;
13
- use crate :: prelude:: * ;
7
+ use bitcoin:: constants:: ChainHash ;
8
+ use bitcoin:: network:: Network ;
9
+ use bitcoin:: secp256k1:: { PublicKey , Secp256k1 , SecretKey } ;
10
+ use lightning_types:: features:: { ChannelTypeFeatures , InitFeatures } ;
14
11
15
12
#[ test]
16
13
fn test_zero_conf_channel_type_support ( ) {
@@ -22,20 +19,48 @@ fn test_zero_conf_channel_type_support() {
22
19
let keys_provider = TestKeysInterface :: new ( & seed, network) ;
23
20
let logger = TestLogger :: new ( ) ;
24
21
25
- let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
22
+ let node_b_node_id =
23
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
26
24
let config = UserConfig :: default ( ) ;
27
- let mut node_a_chan = OutboundV1Channel :: < & TestKeysInterface > :: new ( & feeest, & & keys_provider, & & keys_provider,
28
- node_b_node_id, & channelmanager:: provided_init_features ( & config) , 10000000 , 100000 , 42 , & config, 0 , 42 , None , & logger) . unwrap ( ) ;
25
+ let mut node_a_chan = OutboundV1Channel :: < & TestKeysInterface > :: new (
26
+ & feeest,
27
+ & & keys_provider,
28
+ & & keys_provider,
29
+ node_b_node_id,
30
+ & channelmanager:: provided_init_features ( & config) ,
31
+ 10000000 ,
32
+ 100000 ,
33
+ 42 ,
34
+ & config,
35
+ 0 ,
36
+ 42 ,
37
+ None ,
38
+ & logger,
39
+ )
40
+ . unwrap ( ) ;
29
41
30
42
let mut channel_type_features = ChannelTypeFeatures :: only_static_remote_key ( ) ;
31
43
channel_type_features. set_zero_conf_required ( ) ;
32
44
33
- let mut open_channel_msg = node_a_chan. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
45
+ let mut open_channel_msg =
46
+ node_a_chan. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
34
47
open_channel_msg. common_fields . channel_type = Some ( channel_type_features) ;
35
- let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
36
- let res = InboundV1Channel :: < & TestKeysInterface > :: new ( & feeest, & & keys_provider, & & keys_provider,
37
- node_b_node_id, & channelmanager:: provided_channel_type_features ( & config) ,
38
- & channelmanager:: provided_init_features ( & config) , & open_channel_msg, 7 , & config, 0 , & & logger, /*is_0conf=*/ false ) ;
48
+ let node_b_node_id =
49
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
50
+ let res = InboundV1Channel :: < & TestKeysInterface > :: new (
51
+ & feeest,
52
+ & & keys_provider,
53
+ & & keys_provider,
54
+ node_b_node_id,
55
+ & channelmanager:: provided_channel_type_features ( & config) ,
56
+ & channelmanager:: provided_init_features ( & config) ,
57
+ & open_channel_msg,
58
+ 7 ,
59
+ & config,
60
+ 0 ,
61
+ & & logger,
62
+ /*is_0conf=*/ false ,
63
+ ) ;
39
64
assert ! ( res. is_ok( ) ) ;
40
65
}
41
66
@@ -50,37 +75,72 @@ fn test_supports_anchors_zero_htlc_tx_fee() {
50
75
let keys_provider = TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
51
76
let logger = TestLogger :: new ( ) ;
52
77
53
- let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
54
- let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
78
+ let node_id_a =
79
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
80
+ let node_id_b =
81
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
55
82
56
83
let mut config = UserConfig :: default ( ) ;
57
84
config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx = true ;
58
85
59
86
// It is not enough for just the initiator to signal `option_anchors_zero_fee_htlc_tx`, both
60
87
// need to signal it.
61
88
let channel_a = OutboundV1Channel :: < & TestKeysInterface > :: new (
62
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_b,
63
- & channelmanager:: provided_init_features ( & UserConfig :: default ( ) ) , 10000000 , 100000 , 42 ,
64
- & config, 0 , 42 , None , & logger
65
- ) . unwrap ( ) ;
89
+ & fee_estimator,
90
+ & & keys_provider,
91
+ & & keys_provider,
92
+ node_id_b,
93
+ & channelmanager:: provided_init_features ( & UserConfig :: default ( ) ) ,
94
+ 10000000 ,
95
+ 100000 ,
96
+ 42 ,
97
+ & config,
98
+ 0 ,
99
+ 42 ,
100
+ None ,
101
+ & logger,
102
+ )
103
+ . unwrap ( ) ;
66
104
assert ! ( !channel_a. funding. get_channel_type( ) . supports_anchors_zero_fee_htlc_tx( ) ) ;
67
105
68
106
let mut expected_channel_type = ChannelTypeFeatures :: empty ( ) ;
69
107
expected_channel_type. set_static_remote_key_required ( ) ;
70
108
expected_channel_type. set_anchors_zero_fee_htlc_tx_required ( ) ;
71
109
72
110
let mut channel_a = OutboundV1Channel :: < & TestKeysInterface > :: new (
73
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_b,
74
- & channelmanager:: provided_init_features ( & config) , 10000000 , 100000 , 42 , & config, 0 , 42 ,
75
- None , & logger
76
- ) . unwrap ( ) ;
77
-
78
- let open_channel_msg = channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
111
+ & fee_estimator,
112
+ & & keys_provider,
113
+ & & keys_provider,
114
+ node_id_b,
115
+ & channelmanager:: provided_init_features ( & config) ,
116
+ 10000000 ,
117
+ 100000 ,
118
+ 42 ,
119
+ & config,
120
+ 0 ,
121
+ 42 ,
122
+ None ,
123
+ & logger,
124
+ )
125
+ . unwrap ( ) ;
126
+
127
+ let open_channel_msg =
128
+ channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
79
129
let channel_b = InboundV1Channel :: < & TestKeysInterface > :: new (
80
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_a,
81
- & channelmanager:: provided_channel_type_features ( & config) , & channelmanager:: provided_init_features ( & config) ,
82
- & open_channel_msg, 7 , & config, 0 , & & logger, /*is_0conf=*/ false
83
- ) . unwrap ( ) ;
130
+ & fee_estimator,
131
+ & & keys_provider,
132
+ & & keys_provider,
133
+ node_id_a,
134
+ & channelmanager:: provided_channel_type_features ( & config) ,
135
+ & channelmanager:: provided_init_features ( & config) ,
136
+ & open_channel_msg,
137
+ 7 ,
138
+ & config,
139
+ 0 ,
140
+ & & logger,
141
+ /*is_0conf=*/ false ,
142
+ )
143
+ . unwrap ( ) ;
84
144
85
145
assert_eq ! ( channel_a. funding. get_channel_type( ) , & expected_channel_type) ;
86
146
assert_eq ! ( channel_b. funding. get_channel_type( ) , & expected_channel_type) ;
@@ -97,33 +157,57 @@ fn test_rejects_implicit_simple_anchors() {
97
157
let keys_provider = TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
98
158
let logger = TestLogger :: new ( ) ;
99
159
100
- let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
101
- let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
160
+ let node_id_a =
161
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
162
+ let node_id_b =
163
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
102
164
103
165
let config = UserConfig :: default ( ) ;
104
166
105
167
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
106
168
let static_remote_key_required: u64 = 1 << 12 ;
107
169
let simple_anchors_required: u64 = 1 << 20 ;
108
170
let raw_init_features = static_remote_key_required | simple_anchors_required;
109
- let init_features_with_simple_anchors = InitFeatures :: from_le_bytes ( raw_init_features. to_le_bytes ( ) . to_vec ( ) ) ;
171
+ let init_features_with_simple_anchors =
172
+ InitFeatures :: from_le_bytes ( raw_init_features. to_le_bytes ( ) . to_vec ( ) ) ;
110
173
111
174
let mut channel_a = OutboundV1Channel :: < & TestKeysInterface > :: new (
112
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_b,
113
- & channelmanager:: provided_init_features ( & config) , 10000000 , 100000 , 42 , & config, 0 , 42 ,
114
- None , & logger
115
- ) . unwrap ( ) ;
175
+ & fee_estimator,
176
+ & & keys_provider,
177
+ & & keys_provider,
178
+ node_id_b,
179
+ & channelmanager:: provided_init_features ( & config) ,
180
+ 10000000 ,
181
+ 100000 ,
182
+ 42 ,
183
+ & config,
184
+ 0 ,
185
+ 42 ,
186
+ None ,
187
+ & logger,
188
+ )
189
+ . unwrap ( ) ;
116
190
117
191
// Set `channel_type` to `None` to force the implicit feature negotiation.
118
- let mut open_channel_msg = channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
192
+ let mut open_channel_msg =
193
+ channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
119
194
open_channel_msg. common_fields . channel_type = None ;
120
195
121
196
// Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
122
197
// `static_remote_key`, it will fail the channel.
123
198
let channel_b = InboundV1Channel :: < & TestKeysInterface > :: new (
124
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_a,
125
- & channelmanager:: provided_channel_type_features ( & config) , & init_features_with_simple_anchors,
126
- & open_channel_msg, 7 , & config, 0 , & & logger, /*is_0conf=*/ false
199
+ & fee_estimator,
200
+ & & keys_provider,
201
+ & & keys_provider,
202
+ node_id_a,
203
+ & channelmanager:: provided_channel_type_features ( & config) ,
204
+ & init_features_with_simple_anchors,
205
+ & open_channel_msg,
206
+ 7 ,
207
+ & config,
208
+ 0 ,
209
+ & & logger,
210
+ /*is_0conf=*/ false ,
127
211
) ;
128
212
assert ! ( channel_b. is_err( ) ) ;
129
213
}
@@ -139,36 +223,61 @@ fn test_rejects_simple_anchors_channel_type() {
139
223
let keys_provider = TestKeysInterface :: new ( & [ 42 ; 32 ] , network) ;
140
224
let logger = TestLogger :: new ( ) ;
141
225
142
- let node_id_a = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
143
- let node_id_b = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
226
+ let node_id_a =
227
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ) ;
228
+ let node_id_b =
229
+ PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 2 ; 32 ] ) . unwrap ( ) ) ;
144
230
145
231
let config = UserConfig :: default ( ) ;
146
232
147
233
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
148
234
let static_remote_key_required: u64 = 1 << 12 ;
149
235
let simple_anchors_required: u64 = 1 << 20 ;
150
236
let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
151
- let simple_anchors_init = InitFeatures :: from_le_bytes ( simple_anchors_raw_features. to_le_bytes ( ) . to_vec ( ) ) ;
152
- let simple_anchors_channel_type = ChannelTypeFeatures :: from_le_bytes ( simple_anchors_raw_features. to_le_bytes ( ) . to_vec ( ) ) ;
237
+ let simple_anchors_init =
238
+ InitFeatures :: from_le_bytes ( simple_anchors_raw_features. to_le_bytes ( ) . to_vec ( ) ) ;
239
+ let simple_anchors_channel_type =
240
+ ChannelTypeFeatures :: from_le_bytes ( simple_anchors_raw_features. to_le_bytes ( ) . to_vec ( ) ) ;
153
241
assert ! ( !simple_anchors_init. requires_unknown_bits( ) ) ;
154
242
assert ! ( !simple_anchors_channel_type. requires_unknown_bits( ) ) ;
155
243
156
244
// First, we'll try to open a channel between A and B where A requests a channel type for
157
245
// the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
158
246
// B as it's not supported by LDK.
159
247
let mut channel_a = OutboundV1Channel :: < & TestKeysInterface > :: new (
160
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_b,
161
- & channelmanager:: provided_init_features ( & config) , 10000000 , 100000 , 42 , & config, 0 , 42 ,
162
- None , & logger
163
- ) . unwrap ( ) ;
164
-
165
- let mut open_channel_msg = channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
248
+ & fee_estimator,
249
+ & & keys_provider,
250
+ & & keys_provider,
251
+ node_id_b,
252
+ & channelmanager:: provided_init_features ( & config) ,
253
+ 10000000 ,
254
+ 100000 ,
255
+ 42 ,
256
+ & config,
257
+ 0 ,
258
+ 42 ,
259
+ None ,
260
+ & logger,
261
+ )
262
+ . unwrap ( ) ;
263
+
264
+ let mut open_channel_msg =
265
+ channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
166
266
open_channel_msg. common_fields . channel_type = Some ( simple_anchors_channel_type. clone ( ) ) ;
167
267
168
268
let res = InboundV1Channel :: < & TestKeysInterface > :: new (
169
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_a,
170
- & channelmanager:: provided_channel_type_features ( & config) , & simple_anchors_init,
171
- & open_channel_msg, 7 , & config, 0 , & & logger, /*is_0conf=*/ false
269
+ & fee_estimator,
270
+ & & keys_provider,
271
+ & & keys_provider,
272
+ node_id_a,
273
+ & channelmanager:: provided_channel_type_features ( & config) ,
274
+ & simple_anchors_init,
275
+ & open_channel_msg,
276
+ 7 ,
277
+ & config,
278
+ 0 ,
279
+ & & logger,
280
+ /*is_0conf=*/ false ,
172
281
) ;
173
282
assert ! ( res. is_err( ) ) ;
174
283
@@ -177,23 +286,48 @@ fn test_rejects_simple_anchors_channel_type() {
177
286
// original `option_anchors` feature, which should be rejected by A as it's not supported by
178
287
// LDK.
179
288
let mut channel_a = OutboundV1Channel :: < & TestKeysInterface > :: new (
180
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_b, & simple_anchors_init,
181
- 10000000 , 100000 , 42 , & config, 0 , 42 , None , & logger
182
- ) . unwrap ( ) ;
183
-
184
- let open_channel_msg = channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
289
+ & fee_estimator,
290
+ & & keys_provider,
291
+ & & keys_provider,
292
+ node_id_b,
293
+ & simple_anchors_init,
294
+ 10000000 ,
295
+ 100000 ,
296
+ 42 ,
297
+ & config,
298
+ 0 ,
299
+ 42 ,
300
+ None ,
301
+ & logger,
302
+ )
303
+ . unwrap ( ) ;
304
+
305
+ let open_channel_msg =
306
+ channel_a. get_open_channel ( ChainHash :: using_genesis_block ( network) , & & logger) . unwrap ( ) ;
185
307
186
308
let mut channel_b = InboundV1Channel :: < & TestKeysInterface > :: new (
187
- & fee_estimator, & & keys_provider, & & keys_provider, node_id_a,
188
- & channelmanager:: provided_channel_type_features ( & config) , & channelmanager:: provided_init_features ( & config) ,
189
- & open_channel_msg, 7 , & config, 0 , & & logger, /*is_0conf=*/ false
190
- ) . unwrap ( ) ;
309
+ & fee_estimator,
310
+ & & keys_provider,
311
+ & & keys_provider,
312
+ node_id_a,
313
+ & channelmanager:: provided_channel_type_features ( & config) ,
314
+ & channelmanager:: provided_init_features ( & config) ,
315
+ & open_channel_msg,
316
+ 7 ,
317
+ & config,
318
+ 0 ,
319
+ & & logger,
320
+ /*is_0conf=*/ false ,
321
+ )
322
+ . unwrap ( ) ;
191
323
192
324
let mut accept_channel_msg = channel_b. get_accept_channel_message ( & & logger) . unwrap ( ) ;
193
325
accept_channel_msg. common_fields . channel_type = Some ( simple_anchors_channel_type. clone ( ) ) ;
194
326
195
327
let res = channel_a. accept_channel (
196
- & accept_channel_msg, & config. channel_handshake_limits , & simple_anchors_init
328
+ & accept_channel_msg,
329
+ & config. channel_handshake_limits ,
330
+ & simple_anchors_init,
197
331
) ;
198
332
assert ! ( res. is_err( ) ) ;
199
333
}
0 commit comments