Skip to content

Commit 3b0793c

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 35dffe2 commit 3b0793c

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

lightning/src/ln/channel.rs

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

1070610706
// Unfunded channel utilities
1070710707

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

lightning/src/ln/channel_type_tests.rs

Lines changed: 107 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,112 @@ 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+
// announce_for_forwarding = false is required, but set by UserConfig::default().
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+
#[test]
52+
fn test_option_zero_fee_commitments_initial() {
53+
let mut expected_type = ChannelTypeFeatures::empty();
54+
expected_type.set_anchor_zero_fee_commitments_required();
55+
56+
do_test_get_initial_channel_type(
57+
UserConfig::default(),
58+
InitFeatures::empty(),
59+
ChannelTypeFeatures::only_static_remote_key(),
60+
|cfg: &mut UserConfig| {
61+
cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
62+
},
63+
|their_features: &mut InitFeatures| {
64+
their_features.set_anchor_zero_fee_commitments_optional();
65+
},
66+
expected_type,
67+
)
68+
}
69+
70+
#[test]
71+
fn test_option_zero_fee_commitments_from_zero_htlc_anchors_initial() {
72+
let mut start_cfg = UserConfig::default();
73+
start_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
74+
75+
let mut start_features = InitFeatures::empty();
76+
start_features.set_anchors_zero_fee_htlc_tx_optional();
77+
78+
let mut start_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
79+
80+
let mut expected_type = ChannelTypeFeatures::empty();
81+
expected_type.set_anchor_zero_fee_commitments_required();
82+
83+
do_test_get_initial_channel_type(
84+
start_cfg,
85+
start_features,
86+
start_type,
87+
|cfg: &mut UserConfig| {
88+
cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
89+
},
90+
|their_features: &mut InitFeatures| {
91+
their_features.set_anchor_zero_fee_commitments_optional();
92+
},
93+
expected_type,
94+
)
95+
}
96+
97+
fn do_test_get_initial_channel_type<F1, F2>(
98+
start_cfg: UserConfig, start_features: InitFeatures, start_type: ChannelTypeFeatures,
99+
mut local_cfg_mod: F1, mut remote_features_mod: F2, channel_type: ChannelTypeFeatures,
100+
) where
101+
F1: FnOnce(&mut UserConfig),
102+
F2: FnOnce(&mut InitFeatures),
103+
{
104+
// Local node supports feature, remote does not.
105+
let mut config = start_cfg.clone();
106+
local_cfg_mod(&mut config);
107+
assert_eq!(get_initial_channel_type(&config, &start_features), start_type);
108+
109+
// Remote node supports feature, local does not.
110+
let mut their_features = start_features.clone();
111+
remote_features_mod(&mut their_features);
112+
assert_eq!(get_initial_channel_type(&start_cfg, &their_features), start_type);
113+
114+
// Both support feature.
115+
assert_eq!(get_initial_channel_type(&config, &their_features), channel_type)
116+
}
117+
12118
#[test]
13119
fn test_zero_conf_channel_type_support() {
14120
let test_est = TestFeeEstimator::new(15000);

0 commit comments

Comments
 (0)