@@ -7521,24 +7521,69 @@ fn test_upfront_shutdown_script() {
7521
7521
}
7522
7522
7523
7523
#[test]
7524
- fn test_upfront_shutdown_script_unsupport_segwit() {
7525
- // We test that channel is closed early
7526
- // if a segwit program is passed as upfront shutdown script,
7527
- // but the peer does not support segwit.
7524
+ fn test_unsupported_anysegwit_upfront_shutdown_script() {
7528
7525
let chanmon_cfgs = create_chanmon_cfgs(2);
7529
7526
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7530
7527
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7531
7528
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7532
7529
7530
+ // Use a non-v0 segwit script supported by option_shutdown_anysegwit
7531
+ let node_features = InitFeatures::known().clear_shutdown_anysegwit();
7532
+ let anysegwit_shutdown_script = Builder::new()
7533
+ .push_int(16)
7534
+ .push_slice(&[0, 40])
7535
+ .into_script();
7536
+
7537
+ // Check script when handling an open_channel message
7533
7538
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7539
+ let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7540
+ open_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
7541
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), node_features.clone(), &open_channel);
7534
7542
7543
+ let events = nodes[1].node.get_and_clear_pending_msg_events();
7544
+ assert_eq!(events.len(), 1);
7545
+ match events[0] {
7546
+ MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7547
+ assert_eq!(node_id, nodes[0].node.get_our_node_id());
7548
+ assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
7549
+ },
7550
+ _ => panic!("Unexpected event"),
7551
+ }
7552
+
7553
+ // Check script when handling an accept_channel message
7554
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7555
+ let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7556
+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
7557
+ let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7558
+ accept_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
7559
+ nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), node_features, &accept_channel);
7560
+
7561
+ let events = nodes[0].node.get_and_clear_pending_msg_events();
7562
+ assert_eq!(events.len(), 1);
7563
+ match events[0] {
7564
+ MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7565
+ assert_eq!(node_id, nodes[1].node.get_our_node_id());
7566
+ assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
7567
+ },
7568
+ _ => panic!("Unexpected event"),
7569
+ }
7570
+ }
7571
+
7572
+ #[test]
7573
+ fn test_invalid_upfront_shutdown_script() {
7574
+ let chanmon_cfgs = create_chanmon_cfgs(2);
7575
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7576
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7577
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7578
+
7579
+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7580
+
7581
+ // Use a segwit v0 script with an unsupported witness program
7535
7582
let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7536
- open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(16 )
7583
+ open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(0 )
7537
7584
.push_slice(&[0, 0])
7538
7585
.into_script());
7539
-
7540
- let features = InitFeatures::known().clear_shutdown_anysegwit();
7541
- nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), features, &open_channel);
7586
+ nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
7542
7587
7543
7588
let events = nodes[0].node.get_and_clear_pending_msg_events();
7544
7589
assert_eq!(events.len(), 1);
@@ -7552,7 +7597,7 @@ fn test_upfront_shutdown_script_unsupport_segwit() {
7552
7597
}
7553
7598
7554
7599
#[test]
7555
- fn test_shutdown_script_any_segwit_allowed () {
7600
+ fn test_segwit_v0_shutdown_script () {
7556
7601
let mut config = UserConfig::default();
7557
7602
config.channel_options.announced_channel = true;
7558
7603
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7563,14 +7608,50 @@ fn test_shutdown_script_any_segwit_allowed() {
7563
7608
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7564
7609
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7565
7610
7566
- //// We test if the remote peer accepts opt_shutdown_anysegwit, a witness program can be used on shutdown
7567
- let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7611
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7568
7612
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7613
+
7614
+ // Use a segwit v0 script supported even without option_shutdown_anysegwit
7615
+ let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7616
+ node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7617
+ .push_slice(&[0; 20])
7618
+ .into_script();
7619
+ nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7620
+
7621
+ let events = nodes[0].node.get_and_clear_pending_msg_events();
7622
+ assert_eq!(events.len(), 2);
7623
+ match events[0] {
7624
+ MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7625
+ _ => panic!("Unexpected event"),
7626
+ }
7627
+ match events[1] {
7628
+ MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7629
+ _ => panic!("Unexpected event"),
7630
+ }
7631
+ }
7632
+
7633
+ #[test]
7634
+ fn test_anysegwit_shutdown_script() {
7635
+ let mut config = UserConfig::default();
7636
+ config.channel_options.announced_channel = true;
7637
+ config.peer_channel_config_limits.force_announced_channel_preference = false;
7638
+ config.channel_options.commit_upfront_shutdown_pubkey = false;
7639
+ let user_cfgs = [None, Some(config), None];
7640
+ let chanmon_cfgs = create_chanmon_cfgs(3);
7641
+ let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7642
+ let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7643
+ let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7644
+
7645
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7646
+ nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7647
+
7648
+ // Use a non-v0 segwit script supported by option_shutdown_anysegwit
7569
7649
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7570
7650
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
7571
7651
.push_slice(&[0, 0])
7572
7652
.into_script();
7573
7653
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7654
+
7574
7655
let events = nodes[0].node.get_and_clear_pending_msg_events();
7575
7656
assert_eq!(events.len(), 2);
7576
7657
match events[0] {
@@ -7584,7 +7665,7 @@ fn test_shutdown_script_any_segwit_allowed() {
7584
7665
}
7585
7666
7586
7667
#[test]
7587
- fn test_shutdown_script_any_segwit_not_allowed () {
7668
+ fn test_unsupported_anysegwit_shutdown_script () {
7588
7669
let mut config = UserConfig::default();
7589
7670
config.channel_options.announced_channel = true;
7590
7671
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7595,30 +7676,31 @@ fn test_shutdown_script_any_segwit_not_allowed() {
7595
7676
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7596
7677
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7597
7678
7598
- //// We test that if the remote peer does not accept opt_shutdown_anysegwit, the witness program cannot be used on shutdown
7599
- let chan = create_announced_chan_between_nodes_with_value (&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known ());
7679
+ let node_features = InitFeatures::known().clear_shutdown_anysegwit();
7680
+ let chan = create_announced_chan_between_nodes (&nodes, 0, 1, InitFeatures::known(), node_features.clone ());
7600
7681
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7682
+
7683
+ // Use a non-v0 segwit script supported by option_shutdown_anysegwit
7601
7684
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7602
- // Make an any segwit version script
7603
7685
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
7604
- .push_slice(&[0, 0 ])
7686
+ .push_slice(&[0, 40 ])
7605
7687
.into_script();
7606
- let flags_no = InitFeatures::known().clear_shutdown_anysegwit( );
7607
- nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &flags_no, &node_0_shutdown);
7688
+ nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_features, &node_0_shutdown );
7689
+
7608
7690
let events = nodes[0].node.get_and_clear_pending_msg_events();
7609
7691
assert_eq!(events.len(), 2);
7610
7692
match events[1] {
7611
7693
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7612
7694
assert_eq!(node_id, nodes[1].node.get_our_node_id());
7613
- assert_eq!(msg.data, "Got a nonstandard scriptpubkey (60020000 ) from remote peer".to_owned())
7695
+ assert_eq!(msg.data, "Got a nonstandard scriptpubkey (60020028 ) from remote peer".to_owned());
7614
7696
},
7615
7697
_ => panic!("Unexpected event"),
7616
7698
}
7617
7699
check_added_monitors!(nodes[0], 1);
7618
7700
}
7619
7701
7620
7702
#[test]
7621
- fn test_shutdown_script_segwit_but_not_anysegwit () {
7703
+ fn test_invalid_shutdown_script () {
7622
7704
let mut config = UserConfig::default();
7623
7705
config.channel_options.announced_channel = true;
7624
7706
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7629,15 +7711,16 @@ fn test_shutdown_script_segwit_but_not_anysegwit() {
7629
7711
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7630
7712
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7631
7713
7632
- //// We test that if shutdown any segwit is supported and we send a witness script with 0 version, this is not accepted
7633
- let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7714
+ let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7634
7715
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7716
+
7717
+ // Use a segwit v0 script with an unsupported witness program
7635
7718
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7636
- // Make a segwit script that is not a valid as any segwit
7637
7719
node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7638
7720
.push_slice(&[0, 0])
7639
7721
.into_script();
7640
7722
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7723
+
7641
7724
let events = nodes[0].node.get_and_clear_pending_msg_events();
7642
7725
assert_eq!(events.len(), 2);
7643
7726
match events[1] {
0 commit comments