Skip to content

Commit 1276535

Browse files
committed
Require option_shutdown_anysegwit feature
1 parent 1bb9e64 commit 1276535

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

lightning/src/ln/features.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod sealed {
101101
// Byte 2
102102
,
103103
// Byte 3
104-
,
104+
ShutdownAnySegwit,
105105
],
106106
optional_features: [
107107
// Byte 0
@@ -111,7 +111,7 @@ mod sealed {
111111
// Byte 2
112112
BasicMPP,
113113
// Byte 3
114-
ShutdownAnySegwit,
114+
,
115115
],
116116
});
117117
define_context!(NodeContext {
@@ -123,7 +123,7 @@ mod sealed {
123123
// Byte 2
124124
,
125125
// Byte 3
126-
,
126+
ShutdownAnySegwit,
127127
// Byte 4
128128
,
129129
// Byte 5
@@ -139,7 +139,7 @@ mod sealed {
139139
// Byte 2
140140
BasicMPP,
141141
// Byte 3
142-
ShutdownAnySegwit,
142+
,
143143
// Byte 4
144144
,
145145
// Byte 5
@@ -661,11 +661,6 @@ impl<T: sealed::ShutdownAnySegwit> Features<T> {
661661
pub(crate) fn supports_shutdown_anysegwit(&self) -> bool {
662662
<T as sealed::ShutdownAnySegwit>::supports_feature(&self.flags)
663663
}
664-
#[cfg(test)]
665-
pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
666-
<T as sealed::ShutdownAnySegwit>::clear_bits(&mut self.flags);
667-
self
668-
}
669664
}
670665

671666
impl<T: sealed::Context> Writeable for Features<T> {
@@ -782,12 +777,12 @@ mod tests {
782777
// - option_data_loss_protect
783778
// - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
784779
// - basic_mpp
785-
// - opt_shutdown_anysegwit
780+
// - opt_shutdown_anysegwit (req)
786781
assert_eq!(node_features.flags.len(), 4);
787782
assert_eq!(node_features.flags[0], 0b00000010);
788783
assert_eq!(node_features.flags[1], 0b01010001);
789784
assert_eq!(node_features.flags[2], 0b00000010);
790-
assert_eq!(node_features.flags[3], 0b00001000);
785+
assert_eq!(node_features.flags[3], 0b00000100);
791786
}
792787

793788
// Check that cleared flags are kept blank when converting back:

lightning/src/ln/functional_tests.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7518,24 +7518,20 @@ fn test_upfront_shutdown_script() {
75187518
}
75197519

75207520
#[test]
7521-
fn test_upfront_shutdown_script_unsupport_segwit() {
7522-
// We test that channel is closed early
7523-
// if a segwit program is passed as upfront shutdown script,
7524-
// but the peer does not support segwit.
7521+
fn test_invalid_upfront_shutdown_script() {
75257522
let chanmon_cfgs = create_chanmon_cfgs(2);
75267523
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
75277524
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
75287525
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
75297526

75307527
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
75317528

7529+
// Use a segwit v0 script with an unsupported witness program
75327530
let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7533-
open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(16)
7531+
open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(0)
75347532
.push_slice(&[0, 0])
75357533
.into_script());
7536-
7537-
let features = InitFeatures::known().clear_shutdown_anysegwit();
7538-
nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), features, &open_channel);
7534+
nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
75397535

75407536
let events = nodes[0].node.get_and_clear_pending_msg_events();
75417537
assert_eq!(events.len(), 1);
@@ -7549,7 +7545,7 @@ fn test_upfront_shutdown_script_unsupport_segwit() {
75497545
}
75507546

75517547
#[test]
7552-
fn test_shutdown_script_any_segwit_allowed() {
7548+
fn test_segwit_v0_shutdown_script() {
75537549
let mut config = UserConfig::default();
75547550
config.channel_options.announced_channel = true;
75557551
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7560,14 +7556,16 @@ fn test_shutdown_script_any_segwit_allowed() {
75607556
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
75617557
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75627558

7563-
//// We test if the remote peer accepts opt_shutdown_anysegwit, a witness program can be used on shutdown
75647559
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
75657560
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7561+
7562+
// Use a segwit v0 script supported even without opt_shutdown_anysegwit
75667563
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7567-
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
7568-
.push_slice(&[0, 0])
7564+
node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7565+
.push_slice(&[0; 20])
75697566
.into_script();
75707567
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7568+
75717569
let events = nodes[0].node.get_and_clear_pending_msg_events();
75727570
assert_eq!(events.len(), 2);
75737571
match events[0] {
@@ -7581,7 +7579,7 @@ fn test_shutdown_script_any_segwit_allowed() {
75817579
}
75827580

75837581
#[test]
7584-
fn test_shutdown_script_any_segwit_not_allowed() {
7582+
fn test_anysegwit_shutdown_script() {
75857583
let mut config = UserConfig::default();
75867584
config.channel_options.announced_channel = true;
75877585
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7592,30 +7590,30 @@ fn test_shutdown_script_any_segwit_not_allowed() {
75927590
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
75937591
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75947592

7595-
//// We test that if the remote peer does not accept opt_shutdown_anysegwit, the witness program cannot be used on shutdown
75967593
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
75977594
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7595+
7596+
// Use a non-v0 segwit script supported by opt_shutdown_anysegwit
75987597
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7599-
// Make an any segwit version script
76007598
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
76017599
.push_slice(&[0, 0])
76027600
.into_script();
7603-
let flags_no = InitFeatures::known().clear_shutdown_anysegwit();
7604-
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &flags_no, &node_0_shutdown);
7601+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7602+
76057603
let events = nodes[0].node.get_and_clear_pending_msg_events();
76067604
assert_eq!(events.len(), 2);
7605+
match events[0] {
7606+
MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7607+
_ => panic!("Unexpected event"),
7608+
}
76077609
match events[1] {
7608-
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7609-
assert_eq!(node_id, nodes[1].node.get_our_node_id());
7610-
assert_eq!(msg.data, "Got a nonstandard scriptpubkey (60020000) from remote peer".to_owned())
7611-
},
7610+
MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
76127611
_ => panic!("Unexpected event"),
76137612
}
7614-
check_added_monitors!(nodes[0], 1);
76157613
}
76167614

76177615
#[test]
7618-
fn test_shutdown_script_segwit_but_not_anysegwit() {
7616+
fn test_invalid_shutdown_script() {
76197617
let mut config = UserConfig::default();
76207618
config.channel_options.announced_channel = true;
76217619
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7626,15 +7624,16 @@ fn test_shutdown_script_segwit_but_not_anysegwit() {
76267624
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
76277625
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
76287626

7629-
//// We test that if shutdown any segwit is supported and we send a witness script with 0 version, this is not accepted
76307627
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
76317628
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7629+
7630+
// Use a segwit v0 script with an unsupported witness program
76327631
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7633-
// Make a segwit script that is not a valid as any segwit
76347632
node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
76357633
.push_slice(&[0, 0])
76367634
.into_script();
76377635
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7636+
76387637
let events = nodes[0].node.get_and_clear_pending_msg_events();
76397638
assert_eq!(events.len(), 2);
76407639
match events[1] {

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
905905
log_debug!(self.logger, "Peer {} does not support static remote key, disconnecting with no_connection_possible", log_pubkey!(peer.their_node_id.unwrap()));
906906
return Err(PeerHandleError{ no_connection_possible: true }.into());
907907
}
908+
if !msg.features.supports_shutdown_anysegwit() {
909+
log_debug!(self.logger, "Peer {} does not support option_shutdown_anysegwit, disconnecting with no_connection_possible", log_pubkey!(peer.their_node_id.unwrap()));
910+
return Err(PeerHandleError{ no_connection_possible: true }.into());
911+
}
908912

909913
self.message_handler.route_handler.sync_routing_table(&peer.their_node_id.unwrap(), &msg);
910914

0 commit comments

Comments
 (0)