Skip to content

Commit 94ca454

Browse files
committed
Require option_shutdown_anysegwit feature
1 parent 1f1d7c6 commit 94ca454

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
],
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!(ChannelContext {
@@ -635,11 +635,6 @@ impl<T: sealed::ShutdownAnySegwit> Features<T> {
635635
pub(crate) fn supports_shutdown_anysegwit(&self) -> bool {
636636
<T as sealed::ShutdownAnySegwit>::supports_feature(&self.flags)
637637
}
638-
#[cfg(test)]
639-
pub(crate) fn clear_shutdown_anysegwit(mut self) -> Self {
640-
<T as sealed::ShutdownAnySegwit>::clear_bits(&mut self.flags);
641-
self
642-
}
643638
}
644639

645640
impl<T: sealed::Context> Writeable for Features<T> {
@@ -756,12 +751,12 @@ mod tests {
756751
// - option_data_loss_protect
757752
// - var_onion_optin (req) | static_remote_key (req) | payment_secret(req)
758753
// - basic_mpp
759-
// - opt_shutdown_anysegwit
754+
// - opt_shutdown_anysegwit (req)
760755
assert_eq!(node_features.flags.len(), 4);
761756
assert_eq!(node_features.flags[0], 0b00000010);
762757
assert_eq!(node_features.flags[1], 0b01010001);
763758
assert_eq!(node_features.flags[2], 0b00000010);
764-
assert_eq!(node_features.flags[3], 0b00001000);
759+
assert_eq!(node_features.flags[3], 0b00000100);
765760
}
766761

767762
// 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
@@ -7474,24 +7474,20 @@ fn test_upfront_shutdown_script() {
74747474
}
74757475

74767476
#[test]
7477-
fn test_upfront_shutdown_script_unsupport_segwit() {
7478-
// We test that channel is closed early
7479-
// if a segwit program is passed as upfront shutdown script,
7480-
// but the peer does not support segwit.
7477+
fn test_invalid_upfront_shutdown_script() {
74817478
let chanmon_cfgs = create_chanmon_cfgs(2);
74827479
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
74837480
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
74847481
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
74857482

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

7485+
// Use a segwit v0 script with an unsupported witness program
74887486
let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7489-
open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(16)
7487+
open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(0)
74907488
.push_slice(&[0, 0])
74917489
.into_script());
7492-
7493-
let features = InitFeatures::known().clear_shutdown_anysegwit();
7494-
nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), features, &open_channel);
7490+
nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
74957491

74967492
let events = nodes[0].node.get_and_clear_pending_msg_events();
74977493
assert_eq!(events.len(), 1);
@@ -7505,7 +7501,7 @@ fn test_upfront_shutdown_script_unsupport_segwit() {
75057501
}
75067502

75077503
#[test]
7508-
fn test_shutdown_script_any_segwit_allowed() {
7504+
fn test_segwit_v0_shutdown_script() {
75097505
let mut config = UserConfig::default();
75107506
config.channel_options.announced_channel = true;
75117507
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7516,14 +7512,16 @@ fn test_shutdown_script_any_segwit_allowed() {
75167512
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
75177513
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75187514

7519-
//// We test if the remote peer accepts opt_shutdown_anysegwit, a witness program can be used on shutdown
75207515
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
75217516
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7517+
7518+
// Use a segwit v0 script supported even without opt_shutdown_anysegwit
75227519
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7523-
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
7524-
.push_slice(&[0, 0])
7520+
node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7521+
.push_slice(&[0; 20])
75257522
.into_script();
75267523
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7524+
75277525
let events = nodes[0].node.get_and_clear_pending_msg_events();
75287526
assert_eq!(events.len(), 2);
75297527
match events[0] {
@@ -7537,7 +7535,7 @@ fn test_shutdown_script_any_segwit_allowed() {
75377535
}
75387536

75397537
#[test]
7540-
fn test_shutdown_script_any_segwit_not_allowed() {
7538+
fn test_anysegwit_shutdown_script() {
75417539
let mut config = UserConfig::default();
75427540
config.channel_options.announced_channel = true;
75437541
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7548,30 +7546,30 @@ fn test_shutdown_script_any_segwit_not_allowed() {
75487546
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
75497547
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75507548

7551-
//// We test that if the remote peer does not accept opt_shutdown_anysegwit, the witness program cannot be used on shutdown
75527549
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
75537550
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7551+
7552+
// Use a non-v0 segwit script supported by opt_shutdown_anysegwit
75547553
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7555-
// Make an any segwit version script
75567554
node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
75577555
.push_slice(&[0, 0])
75587556
.into_script();
7559-
let flags_no = InitFeatures::known().clear_shutdown_anysegwit();
7560-
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &flags_no, &node_0_shutdown);
7557+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7558+
75617559
let events = nodes[0].node.get_and_clear_pending_msg_events();
75627560
assert_eq!(events.len(), 2);
7561+
match events[0] {
7562+
MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7563+
_ => panic!("Unexpected event"),
7564+
}
75637565
match events[1] {
7564-
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7565-
assert_eq!(node_id, nodes[1].node.get_our_node_id());
7566-
assert_eq!(msg.data, "Got a nonstandard scriptpubkey (60020000) from remote peer".to_owned())
7567-
},
7566+
MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
75687567
_ => panic!("Unexpected event"),
75697568
}
7570-
check_added_monitors!(nodes[0], 1);
75717569
}
75727570

75737571
#[test]
7574-
fn test_shutdown_script_segwit_but_not_anysegwit() {
7572+
fn test_invalid_shutdown_script() {
75757573
let mut config = UserConfig::default();
75767574
config.channel_options.announced_channel = true;
75777575
config.peer_channel_config_limits.force_announced_channel_preference = false;
@@ -7582,15 +7580,16 @@ fn test_shutdown_script_segwit_but_not_anysegwit() {
75827580
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
75837581
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
75847582

7585-
//// We test that if shutdown any segwit is supported and we send a witness script with 0 version, this is not accepted
75867583
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
75877584
nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7585+
7586+
// Use a segwit v0 script with an unsupported witness program
75887587
let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7589-
// Make a segwit script that is not a valid as any segwit
75907588
node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
75917589
.push_slice(&[0, 0])
75927590
.into_script();
75937591
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7592+
75947593
let events = nodes[0].node.get_and_clear_pending_msg_events();
75957594
assert_eq!(events.len(), 2);
75967595
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)