Skip to content

Commit 56eff67

Browse files
committed
ln/test: add unit test for get_initial_channel_type
Add straightforward unit tests for get_initial_channel_type, which will be expanded when we add zero_fee_commitment type.
1 parent c6c2c5e commit 56eff67

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

lightning/src/ln/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10647,7 +10647,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1064710647

1064810648
// Unfunded channel utilities
1064910649

10650-
fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures) -> ChannelTypeFeatures {
10650+
pub(crate) fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures) -> ChannelTypeFeatures {
1065110651
// The default channel type (ie the first one we try) depends on whether the channel is
1065210652
// public - if it is, we just go with `only_static_remotekey` as it's the only option
1065310653
// available. If it's private, we first try `scid_privacy` as it provides better privacy

lightning/src/ln/channel_type_tests.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
2-
use crate::ln::channel::{InboundV1Channel, OutboundV1Channel};
2+
use crate::ln::channel::{get_initial_channel_type, InboundV1Channel, OutboundV1Channel};
33
use crate::ln::channelmanager;
44
use crate::prelude::*;
55
use crate::util::config::UserConfig;
@@ -9,6 +9,66 @@ use bitcoin::network::Network;
99
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1010
use lightning_types::features::{ChannelTypeFeatures, InitFeatures};
1111

12+
#[test]
13+
fn test_option_scid_privacy_initial() {
14+
let mut expected_type = ChannelTypeFeatures::only_static_remote_key();
15+
expected_type.set_scid_privacy_required();
16+
17+
do_test_get_initial_channel_type(
18+
UserConfig::default(),
19+
InitFeatures::empty(),
20+
ChannelTypeFeatures::only_static_remote_key(),
21+
|cfg: &mut UserConfig| {
22+
cfg.channel_handshake_config.announce_for_forwarding = false;
23+
cfg.channel_handshake_config.negotiate_scid_privacy = true;
24+
},
25+
|their_features: &mut InitFeatures| {
26+
their_features.set_scid_privacy_optional();
27+
},
28+
expected_type,
29+
)
30+
}
31+
32+
#[test]
33+
fn test_option_anchors_zero_fee_initial() {
34+
let mut expected_type = ChannelTypeFeatures::only_static_remote_key();
35+
expected_type.set_anchors_zero_fee_htlc_tx_required();
36+
37+
do_test_get_initial_channel_type(
38+
UserConfig::default(),
39+
InitFeatures::empty(),
40+
ChannelTypeFeatures::only_static_remote_key(),
41+
|cfg: &mut UserConfig| {
42+
cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
43+
},
44+
|their_features: &mut InitFeatures| {
45+
their_features.set_anchors_zero_fee_htlc_tx_optional();
46+
},
47+
expected_type,
48+
)
49+
}
50+
51+
fn do_test_get_initial_channel_type<F1, F2>(
52+
start_cfg: UserConfig, start_features: InitFeatures, start_type: ChannelTypeFeatures,
53+
mut local_cfg_mod: F1, mut remote_features_mod: F2, channel_type: ChannelTypeFeatures,
54+
) where
55+
F1: FnOnce(&mut UserConfig),
56+
F2: FnOnce(&mut InitFeatures),
57+
{
58+
// Local node supports feature, remote does not.
59+
let mut config = start_cfg.clone();
60+
local_cfg_mod(&mut config);
61+
assert_eq!(get_initial_channel_type(&config, &start_features), start_type);
62+
63+
// Remote node supports feature, local does not.
64+
let mut their_features = start_features.clone();
65+
remote_features_mod(&mut their_features);
66+
assert_eq!(get_initial_channel_type(&start_cfg, &their_features), start_type);
67+
68+
// Both support feature.
69+
assert_eq!(get_initial_channel_type(&config, &their_features), channel_type)
70+
}
71+
1272
#[test]
1373
fn test_zero_conf_channel_type_support() {
1474
let test_est = TestFeeEstimator::new(15000);

0 commit comments

Comments
 (0)