Skip to content

Commit a95b122

Browse files
committed
ln/refactor: add helper test function for channel type downgrade
Useful for the commits that follow where we add more downgrade tests.
1 parent 22908ab commit a95b122

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15026,6 +15026,7 @@ where
1502615026
mod tests {
1502715027
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1502815028
use bitcoin::secp256k1::ecdh::SharedSecret;
15029+
use lightning_types::features::ChannelTypeFeatures;
1502915030
use core::sync::atomic::Ordering;
1503015031
use crate::events::{Event, HTLCHandlingFailureType, ClosureReason};
1503115032
use crate::ln::onion_utils::AttributionData;
@@ -15041,7 +15042,7 @@ mod tests {
1504115042
use crate::util::errors::APIError;
1504215043
use crate::util::ser::Writeable;
1504315044
use crate::util::test_utils;
15044-
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelHandshakeConfigUpdate};
15045+
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelHandshakeConfigUpdate, UserConfig};
1504515046
use crate::sign::EntropySource;
1504615047

1504715048
#[test]
@@ -16153,41 +16154,55 @@ mod tests {
1615316154
}
1615416155

1615516156
#[test]
16156-
fn test_anchors_zero_fee_htlc_tx_fallback() {
16157+
fn test_anchors_zero_fee_htlc_tx_downgrade() {
1615716158
// Tests that if both nodes support anchors, but the remote node does not want to accept
1615816159
// anchor channels at the moment, an error it sent to the local node such that it can retry
1615916160
// the channel without the anchors feature.
16161+
let mut initiator_cfg = test_default_channel_config();
16162+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
16163+
16164+
let mut receiver_cfg = test_default_channel_config();
16165+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
16166+
receiver_cfg.manually_accept_inbound_channels = true;
16167+
16168+
let start_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
16169+
let end_type = ChannelTypeFeatures::only_static_remote_key();
16170+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
16171+
}
16172+
16173+
fn do_test_channel_type_downgrade(initiator_cfg: UserConfig, acceptor_cfg: UserConfig,
16174+
start_type: ChannelTypeFeatures, downgrade_types: Vec<ChannelTypeFeatures>) {
1616016175
let chanmon_cfgs = create_chanmon_cfgs(2);
1616116176
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
16162-
let mut anchors_config = test_default_channel_config();
16163-
anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
16164-
anchors_config.manually_accept_inbound_channels = true;
16165-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config.clone()), Some(anchors_config.clone())]);
16177+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(acceptor_cfg)]);
1616616178
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1616716179
let error_message = "Channel force-closed";
1616816180

1616916181
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
16170-
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
16171-
assert!(open_channel_msg.common_fields.channel_type.as_ref().unwrap().supports_anchors_zero_fee_htlc_tx());
16182+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
16183+
assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);
1617216184

16173-
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
16174-
let events = nodes[1].node.get_and_clear_pending_events();
16175-
match events[0] {
16176-
Event::OpenChannelRequest { temporary_channel_id, .. } => {
16177-
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
16185+
for downgrade_type in downgrade_types {
16186+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
16187+
let events = nodes[1].node.get_and_clear_pending_events();
16188+
match events[0] {
16189+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
16190+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
16191+
}
16192+
_ => panic!("Unexpected event"),
1617816193
}
16179-
_ => panic!("Unexpected event"),
16180-
}
1618116194

16182-
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
16183-
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
16195+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
16196+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
1618416197

16185-
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
16186-
assert!(!open_channel_msg.common_fields.channel_type.unwrap().supports_anchors_zero_fee_htlc_tx());
16198+
open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
16199+
let channel_type = open_channel_msg.common_fields.channel_type.as_ref().unwrap();
16200+
assert_eq!(channel_type, &downgrade_type);
1618716201

16188-
// Since nodes[1] should not have accepted the channel, it should
16189-
// not have generated any events.
16190-
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
16202+
// Since nodes[1] should not have accepted the channel, it should
16203+
// not have generated any events.
16204+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
16205+
}
1619116206
}
1619216207

1619316208
#[test]

0 commit comments

Comments
 (0)