Skip to content

Commit 35dffe2

Browse files
committed
ln/format: run rust fmt on channel_type_tests
1 parent f681015 commit 35dffe2

File tree

1 file changed

+205
-68
lines changed

1 file changed

+205
-68
lines changed

lightning/src/ln/channel_type_tests.rs

Lines changed: 205 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
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};
71
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
8-
use bitcoin::secp256k1::{SecretKey, PublicKey};
2+
use crate::ln::channel::{InboundV1Channel, OutboundV1Channel};
93
use crate::ln::channelmanager;
4+
use crate::prelude::*;
105
use crate::util::config::UserConfig;
116
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};
1411

1512
#[test]
1613
fn test_zero_conf_channel_type_support() {
@@ -22,20 +19,48 @@ fn test_zero_conf_channel_type_support() {
2219
let keys_provider = TestKeysInterface::new(&seed, network);
2320
let logger = TestLogger::new();
2421

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());
2624
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();
2941

3042
let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
3143
channel_type_features.set_zero_conf_required();
3244

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();
3447
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+
);
3964
assert!(res.is_ok());
4065
}
4166

@@ -89,29 +114,67 @@ fn do_test_supports_channel_type(config: UserConfig, expected_channel_type: Chan
89114
let keys_provider = TestKeysInterface::new(&[42; 32], network);
90115
let logger = TestLogger::new();
91116

92-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
93-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
117+
let node_id_a =
118+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
119+
let node_id_b =
120+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
94121

95122
// Assert that we get `static_remotekey` when no custom config is negotiated.
96123
let channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
97-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
98-
&channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
99-
&config, 0, 42, None, &logger
100-
).unwrap();
101-
assert_eq!(channel_a.funding.get_channel_type(), &ChannelTypeFeatures::only_static_remote_key());
124+
&fee_estimator,
125+
&&keys_provider,
126+
&&keys_provider,
127+
node_id_b,
128+
&channelmanager::provided_init_features(&UserConfig::default()),
129+
10000000,
130+
100000,
131+
42,
132+
&config,
133+
0,
134+
42,
135+
None,
136+
&logger,
137+
)
138+
.unwrap();
139+
assert_eq!(
140+
channel_a.funding.get_channel_type(),
141+
&ChannelTypeFeatures::only_static_remote_key()
142+
);
102143

103144
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
104-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
105-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
106-
None, &logger
107-
).unwrap();
108-
109-
let open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
145+
&fee_estimator,
146+
&&keys_provider,
147+
&&keys_provider,
148+
node_id_b,
149+
&channelmanager::provided_init_features(&config),
150+
10000000,
151+
100000,
152+
42,
153+
&config,
154+
0,
155+
42,
156+
None,
157+
&logger,
158+
)
159+
.unwrap();
160+
161+
let open_channel_msg =
162+
channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
110163
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
111-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
112-
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
113-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
114-
).unwrap();
164+
&fee_estimator,
165+
&&keys_provider,
166+
&&keys_provider,
167+
node_id_a,
168+
&channelmanager::provided_channel_type_features(&config),
169+
&channelmanager::provided_init_features(&config),
170+
&open_channel_msg,
171+
7,
172+
&config,
173+
0,
174+
&&logger,
175+
/*is_0conf=*/ false,
176+
)
177+
.unwrap();
115178

116179
assert_eq!(channel_a.funding.get_channel_type(), &expected_channel_type);
117180
assert_eq!(channel_b.funding.get_channel_type(), &expected_channel_type);
@@ -136,33 +199,57 @@ fn test_rejects_implicit_simple_anchors() {
136199
let keys_provider = TestKeysInterface::new(&[42; 32], network);
137200
let logger = TestLogger::new();
138201

139-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
140-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
202+
let node_id_a =
203+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
204+
let node_id_b =
205+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
141206

142207
let config = UserConfig::default();
143208

144209
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
145210
let static_remote_key_required: u64 = 1 << 12;
146211
let simple_anchors_required: u64 = 1 << 20;
147212
let raw_init_features = static_remote_key_required | simple_anchors_required;
148-
let init_features_with_simple_anchors = InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
213+
let init_features_with_simple_anchors =
214+
InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
149215

150216
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
151-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
152-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
153-
None, &logger
154-
).unwrap();
217+
&fee_estimator,
218+
&&keys_provider,
219+
&&keys_provider,
220+
node_id_b,
221+
&channelmanager::provided_init_features(&config),
222+
10000000,
223+
100000,
224+
42,
225+
&config,
226+
0,
227+
42,
228+
None,
229+
&logger,
230+
)
231+
.unwrap();
155232

156233
// Set `channel_type` to `None` to force the implicit feature negotiation.
157-
let mut open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
234+
let mut open_channel_msg =
235+
channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
158236
open_channel_msg.common_fields.channel_type = None;
159237

160238
// Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
161239
// `static_remote_key`, it will fail the channel.
162240
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
163-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
164-
&channelmanager::provided_channel_type_features(&config), &init_features_with_simple_anchors,
165-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
241+
&fee_estimator,
242+
&&keys_provider,
243+
&&keys_provider,
244+
node_id_a,
245+
&channelmanager::provided_channel_type_features(&config),
246+
&init_features_with_simple_anchors,
247+
&open_channel_msg,
248+
7,
249+
&config,
250+
0,
251+
&&logger,
252+
/*is_0conf=*/ false,
166253
);
167254
assert!(channel_b.is_err());
168255
}
@@ -178,36 +265,61 @@ fn test_rejects_simple_anchors_channel_type() {
178265
let keys_provider = TestKeysInterface::new(&[42; 32], network);
179266
let logger = TestLogger::new();
180267

181-
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
182-
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
268+
let node_id_a =
269+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
270+
let node_id_b =
271+
PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
183272

184273
let config = UserConfig::default();
185274

186275
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
187276
let static_remote_key_required: u64 = 1 << 12;
188277
let simple_anchors_required: u64 = 1 << 20;
189278
let simple_anchors_raw_features = static_remote_key_required | simple_anchors_required;
190-
let simple_anchors_init = InitFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
191-
let simple_anchors_channel_type = ChannelTypeFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
279+
let simple_anchors_init =
280+
InitFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
281+
let simple_anchors_channel_type =
282+
ChannelTypeFeatures::from_le_bytes(simple_anchors_raw_features.to_le_bytes().to_vec());
192283
assert!(!simple_anchors_init.requires_unknown_bits());
193284
assert!(!simple_anchors_channel_type.requires_unknown_bits());
194285

195286
// First, we'll try to open a channel between A and B where A requests a channel type for
196287
// the original `option_anchors` feature (non zero fee htlc tx). This should be rejected by
197288
// B as it's not supported by LDK.
198289
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
199-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
200-
&channelmanager::provided_init_features(&config), 10000000, 100000, 42, &config, 0, 42,
201-
None, &logger
202-
).unwrap();
203-
204-
let mut open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
290+
&fee_estimator,
291+
&&keys_provider,
292+
&&keys_provider,
293+
node_id_b,
294+
&channelmanager::provided_init_features(&config),
295+
10000000,
296+
100000,
297+
42,
298+
&config,
299+
0,
300+
42,
301+
None,
302+
&logger,
303+
)
304+
.unwrap();
305+
306+
let mut open_channel_msg =
307+
channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
205308
open_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
206309

207310
let res = InboundV1Channel::<&TestKeysInterface>::new(
208-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
209-
&channelmanager::provided_channel_type_features(&config), &simple_anchors_init,
210-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
311+
&fee_estimator,
312+
&&keys_provider,
313+
&&keys_provider,
314+
node_id_a,
315+
&channelmanager::provided_channel_type_features(&config),
316+
&simple_anchors_init,
317+
&open_channel_msg,
318+
7,
319+
&config,
320+
0,
321+
&&logger,
322+
/*is_0conf=*/ false,
211323
);
212324
assert!(res.is_err());
213325

@@ -216,23 +328,48 @@ fn test_rejects_simple_anchors_channel_type() {
216328
// original `option_anchors` feature, which should be rejected by A as it's not supported by
217329
// LDK.
218330
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
219-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b, &simple_anchors_init,
220-
10000000, 100000, 42, &config, 0, 42, None, &logger
221-
).unwrap();
222-
223-
let open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
331+
&fee_estimator,
332+
&&keys_provider,
333+
&&keys_provider,
334+
node_id_b,
335+
&simple_anchors_init,
336+
10000000,
337+
100000,
338+
42,
339+
&config,
340+
0,
341+
42,
342+
None,
343+
&logger,
344+
)
345+
.unwrap();
346+
347+
let open_channel_msg =
348+
channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
224349

225350
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
226-
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
227-
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
228-
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
229-
).unwrap();
351+
&fee_estimator,
352+
&&keys_provider,
353+
&&keys_provider,
354+
node_id_a,
355+
&channelmanager::provided_channel_type_features(&config),
356+
&channelmanager::provided_init_features(&config),
357+
&open_channel_msg,
358+
7,
359+
&config,
360+
0,
361+
&&logger,
362+
/*is_0conf=*/ false,
363+
)
364+
.unwrap();
230365

231366
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
232367
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
233368

234369
let res = channel_a.accept_channel(
235-
&accept_channel_msg, &config.channel_handshake_limits, &simple_anchors_init
370+
&accept_channel_msg,
371+
&config.channel_handshake_limits,
372+
&simple_anchors_init,
236373
);
237374
assert!(res.is_err());
238375
}

0 commit comments

Comments
 (0)