Skip to content

Commit 9ca767d

Browse files
committed
Require option_shutdown_anysegwit feature
1 parent f3b63e4 commit 9ca767d

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
@@ -123,7 +123,7 @@ mod sealed {
123123
// Byte 2
124124
,
125125
// Byte 3
126-
,
126+
ShutdownAnySegwit,
127127
],
128128
optional_features: [
129129
// Byte 0
@@ -133,7 +133,7 @@ mod sealed {
133133
// Byte 2
134134
BasicMPP,
135135
// Byte 3
136-
ShutdownAnySegwit,
136+
,
137137
],
138138
});
139139
define_context!(NodeContext {
@@ -145,7 +145,7 @@ mod sealed {
145145
// Byte 2
146146
,
147147
// Byte 3
148-
,
148+
ShutdownAnySegwit,
149149
// Byte 4
150150
,
151151
// Byte 5
@@ -161,7 +161,7 @@ mod sealed {
161161
// Byte 2
162162
BasicMPP,
163163
// Byte 3
164-
ShutdownAnySegwit,
164+
,
165165
// Byte 4
166166
,
167167
// Byte 5
@@ -684,11 +684,6 @@ impl<T: sealed::ShutdownAnySegwit> Features<T> {
684684
pub(crate) fn supports_shutdown_anysegwit(&self) -> bool {
685685
<T as sealed::ShutdownAnySegwit>::supports_feature(&self.flags)
686686
}
687-
#[cfg(test)]
688-
pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
689-
<T as sealed::ShutdownAnySegwit>::clear_bits(&mut self.flags);
690-
self
691-
}
692687
}
693688

694689
impl<T: sealed::Context> Writeable for Features<T> {
@@ -805,12 +800,12 @@ mod tests {
805800
// - option_data_loss_protect
806801
// - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
807802
// - basic_mpp
808-
// - opt_shutdown_anysegwit
803+
// - opt_shutdown_anysegwit (req)
809804
assert_eq!(node_features.flags.len(), 4);
810805
assert_eq!(node_features.flags[0], 0b00000010);
811806
assert_eq!(node_features.flags[1], 0b01010001);
812807
assert_eq!(node_features.flags[2], 0b00000010);
813-
assert_eq!(node_features.flags[3], 0b00001000);
808+
assert_eq!(node_features.flags[3], 0b00000100);
814809
}
815810

816811
// 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
@@ -897,6 +897,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
897897
log_debug!(self.logger, "Peer {} does not support static remote key, disconnecting with no_connection_possible", log_pubkey!(peer.their_node_id.unwrap()));
898898
return Err(PeerHandleError{ no_connection_possible: true }.into());
899899
}
900+
if !msg.features.supports_shutdown_anysegwit() {
901+
log_debug!(self.logger, "Peer {} does not support option_shutdown_anysegwit, disconnecting with no_connection_possible", log_pubkey!(peer.their_node_id.unwrap()));
902+
return Err(PeerHandleError{ no_connection_possible: true }.into());
903+
}
900904

901905
self.message_handler.route_handler.sync_routing_table(&peer.their_node_id.unwrap(), &msg);
902906

0 commit comments

Comments
 (0)