Skip to content

Commit ef3e9dd

Browse files
authored
Merge pull request #348 from ariard/2019-07-upfront-shutdown-script
Implement option_upfront_shutdown_script on both sides
2 parents 8470e60 + 504d9f5 commit ef3e9dd

File tree

10 files changed

+430
-271
lines changed

10 files changed

+430
-271
lines changed

fuzz/fuzz_targets/chanmon_fail_consistency.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use lightning::ln::channelmonitor;
3636
use lightning::ln::channelmonitor::{ChannelMonitorUpdateErr, HTLCUpdate};
3737
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
3838
use lightning::ln::router::{Route, RouteHop};
39-
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC};
39+
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC, LocalFeatures};
4040
use lightning::util::{reset_rng_state, fill_bytes, events};
4141
use lightning::util::logger::Logger;
4242
use lightning::util::config::UserConfig;
@@ -168,7 +168,7 @@ pub fn do_test(data: &[u8]) {
168168
} else { panic!("Wrong event type"); }
169169
};
170170

171-
$dest.handle_open_channel(&$source.get_our_node_id(), &open_channel).unwrap();
171+
$dest.handle_open_channel(&$source.get_our_node_id(), LocalFeatures::new(), &open_channel).unwrap();
172172
let accept_channel = {
173173
let events = $dest.get_and_clear_pending_msg_events();
174174
assert_eq!(events.len(), 1);
@@ -177,7 +177,7 @@ pub fn do_test(data: &[u8]) {
177177
} else { panic!("Wrong event type"); }
178178
};
179179

180-
$source.handle_accept_channel(&$dest.get_our_node_id(), &accept_channel).unwrap();
180+
$source.handle_accept_channel(&$dest.get_our_node_id(), LocalFeatures::new(), &accept_channel).unwrap();
181181
{
182182
let events = $source.get_and_clear_pending_events();
183183
assert_eq!(events.len(), 1);

src/ln/chanmon_update_fail_tests.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use ln::channelmanager::{RAACommitmentOrder, PaymentPreimage, PaymentHash};
77
use ln::channelmonitor::ChannelMonitorUpdateErr;
88
use ln::msgs;
9-
use ln::msgs::ChannelMessageHandler;
9+
use ln::msgs::{ChannelMessageHandler, LocalFeatures};
1010
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
1111
use util::errors::APIError;
1212

@@ -18,8 +18,8 @@ use ln::functional_test_utils::*;
1818
#[test]
1919
fn test_simple_monitor_permanent_update_fail() {
2020
// Test that we handle a simple permanent monitor update failure
21-
let mut nodes = create_network(2);
22-
create_announced_chan_between_nodes(&nodes, 0, 1);
21+
let mut nodes = create_network(2, &[None, None]);
22+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2323

2424
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
2525
let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -48,8 +48,8 @@ fn test_simple_monitor_permanent_update_fail() {
4848
fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) {
4949
// Test that we can recover from a simple temporary monitor update failure optionally with
5050
// a disconnect in between
51-
let mut nodes = create_network(2);
52-
create_announced_chan_between_nodes(&nodes, 0, 1);
51+
let mut nodes = create_network(2, &[None, None]);
52+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5353

5454
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
5555
let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -147,8 +147,8 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
147147
// * We then walk through more message exchanges to get the original update_add_htlc
148148
// through, swapping message ordering based on disconnect_count & 8 and optionally
149149
// disconnect/reconnecting based on disconnect_count.
150-
let mut nodes = create_network(2);
151-
create_announced_chan_between_nodes(&nodes, 0, 1);
150+
let mut nodes = create_network(2, &[None, None]);
151+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
152152

153153
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
154154

@@ -473,8 +473,8 @@ fn test_monitor_temporary_update_fail_c() {
473473
#[test]
474474
fn test_monitor_update_fail_cs() {
475475
// Tests handling of a monitor update failure when processing an incoming commitment_signed
476-
let mut nodes = create_network(2);
477-
create_announced_chan_between_nodes(&nodes, 0, 1);
476+
let mut nodes = create_network(2, &[None, None]);
477+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
478478

479479
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
480480
let (payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -552,8 +552,8 @@ fn test_monitor_update_fail_no_rebroadcast() {
552552
// Tests handling of a monitor update failure when no message rebroadcasting on
553553
// test_restore_channel_monitor() is required. Backported from
554554
// chanmon_fail_consistency fuzz tests.
555-
let mut nodes = create_network(2);
556-
create_announced_chan_between_nodes(&nodes, 0, 1);
555+
let mut nodes = create_network(2, &[None, None]);
556+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
557557

558558
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
559559
let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
@@ -594,8 +594,8 @@ fn test_monitor_update_fail_no_rebroadcast() {
594594
fn test_monitor_update_raa_while_paused() {
595595
// Tests handling of an RAA while monitor updating has already been marked failed.
596596
// Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
597-
let mut nodes = create_network(2);
598-
create_announced_chan_between_nodes(&nodes, 0, 1);
597+
let mut nodes = create_network(2, &[None, None]);
598+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
599599

600600
send_payment(&nodes[0], &[&nodes[1]], 5000000);
601601

@@ -661,9 +661,9 @@ fn test_monitor_update_raa_while_paused() {
661661

662662
fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
663663
// Tests handling of a monitor update failure when processing an incoming RAA
664-
let mut nodes = create_network(3);
665-
create_announced_chan_between_nodes(&nodes, 0, 1);
666-
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
664+
let mut nodes = create_network(3, &[None, None, None]);
665+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
666+
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
667667

668668
// Rebalance a bit so that we can send backwards from 2 to 1.
669669
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
@@ -914,9 +914,9 @@ fn test_monitor_update_fail_reestablish() {
914914
// Simple test for message retransmission after monitor update failure on
915915
// channel_reestablish generating a monitor update (which comes from freeing holding cell
916916
// HTLCs).
917-
let mut nodes = create_network(3);
918-
create_announced_chan_between_nodes(&nodes, 0, 1);
919-
create_announced_chan_between_nodes(&nodes, 1, 2);
917+
let mut nodes = create_network(3, &[None, None, None]);
918+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
919+
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
920920

921921
let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
922922

@@ -992,8 +992,8 @@ fn raa_no_response_awaiting_raa_state() {
992992
// due to a previous monitor update failure, we still set AwaitingRemoteRevoke on the channel
993993
// in question (assuming it intends to respond with a CS after monitor updating is restored).
994994
// Backported from chanmon_fail_consistency fuzz tests as this used to be broken.
995-
let mut nodes = create_network(2);
996-
create_announced_chan_between_nodes(&nodes, 0, 1);
995+
let mut nodes = create_network(2, &[None, None]);
996+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
997997

998998
let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
999999
let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
@@ -1105,8 +1105,8 @@ fn claim_while_disconnected_monitor_update_fail() {
11051105
// Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
11061106
// code introduced a regression in this test (specifically, this caught a removal of the
11071107
// channel_reestablish handling ensuring the order was sensical given the messages used).
1108-
let mut nodes = create_network(2);
1109-
create_announced_chan_between_nodes(&nodes, 0, 1);
1108+
let mut nodes = create_network(2, &[None, None]);
1109+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
11101110

11111111
// Forward a payment for B to claim
11121112
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
@@ -1220,8 +1220,8 @@ fn monitor_failed_no_reestablish_response() {
12201220
// response to a commitment_signed.
12211221
// Backported from chanmon_fail_consistency fuzz tests as it caught a long-standing
12221222
// debug_assert!() failure in channel_reestablish handling.
1223-
let mut nodes = create_network(2);
1224-
create_announced_chan_between_nodes(&nodes, 0, 1);
1223+
let mut nodes = create_network(2, &[None, None]);
1224+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
12251225

12261226
// Route the payment and deliver the initial commitment_signed (with a monitor update failure
12271227
// on receipt).
@@ -1286,8 +1286,8 @@ fn first_message_on_recv_ordering() {
12861286
// have no pending response but will want to send a RAA/CS (with the updates for the second
12871287
// payment applied).
12881288
// Backported from chanmon_fail_consistency fuzz tests as it caught a bug here.
1289-
let mut nodes = create_network(2);
1290-
create_announced_chan_between_nodes(&nodes, 0, 1);
1289+
let mut nodes = create_network(2, &[None, None]);
1290+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
12911291

12921292
// Route the first payment outbound, holding the last RAA for B until we are set up so that we
12931293
// can deliver it and fail the monitor update.
@@ -1371,9 +1371,9 @@ fn test_monitor_update_fail_claim() {
13711371
// update to claim the payment. We then send a payment C->B->A, making the forward of this
13721372
// payment from B to A fail due to the paused channel. Finally, we restore the channel monitor
13731373
// updating and claim the payment on B.
1374-
let mut nodes = create_network(3);
1375-
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1376-
create_announced_chan_between_nodes(&nodes, 1, 2);
1374+
let mut nodes = create_network(3, &[None, None, None]);
1375+
let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1376+
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
13771377

13781378
// Rebalance a bit so that we can send backwards from 3 to 2.
13791379
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
@@ -1441,9 +1441,9 @@ fn test_monitor_update_on_pending_forwards() {
14411441
// We do this with a simple 3-node network, sending a payment from A to C and one from C to A.
14421442
// The payment from A to C will be failed by C and pending a back-fail to A, while the payment
14431443
// from C to A will be pending a forward to A.
1444-
let mut nodes = create_network(3);
1445-
create_announced_chan_between_nodes(&nodes, 0, 1);
1446-
create_announced_chan_between_nodes(&nodes, 1, 2);
1444+
let mut nodes = create_network(3, &[None, None, None]);
1445+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1446+
create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
14471447

14481448
// Rebalance a bit so that we can send backwards from 3 to 1.
14491449
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
@@ -1505,8 +1505,8 @@ fn monitor_update_claim_fail_no_response() {
15051505
// to channel being AwaitingRAA).
15061506
// Backported from chanmon_fail_consistency fuzz tests as an unmerged version of the handling
15071507
// code was broken.
1508-
let mut nodes = create_network(2);
1509-
create_announced_chan_between_nodes(&nodes, 0, 1);
1508+
let mut nodes = create_network(2, &[None, None]);
1509+
create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
15101510

15111511
// Forward a payment for B to claim
15121512
let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);

src/ln/channel.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use secp256k1::{Secp256k1,Signature};
1616
use secp256k1;
1717

1818
use ln::msgs;
19-
use ln::msgs::{DecodeError, OptionalField};
19+
use ln::msgs::{DecodeError, OptionalField, LocalFeatures};
2020
use ln::channelmonitor::ChannelMonitor;
2121
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash};
2222
use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
@@ -522,7 +522,7 @@ impl Channel {
522522

523523
/// Creates a new channel from a remote sides' request for one.
524524
/// Assumes chain_hash has already been checked and corresponds with what we expect!
525-
pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, ChannelError> {
525+
pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface>, their_node_id: PublicKey, their_local_features: LocalFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel, ChannelError> {
526526
let chan_keys = keys_provider.get_channel_keys(true);
527527
let mut local_config = (*config).channel_options.clone();
528528

@@ -625,6 +625,27 @@ impl Channel {
625625
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
626626
channel_monitor.set_their_to_self_delay(msg.to_self_delay);
627627

628+
let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
629+
match &msg.shutdown_scriptpubkey {
630+
&OptionalField::Present(ref script) => {
631+
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
632+
if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
633+
Some(script.clone())
634+
// Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
635+
} else if script.len() == 0 {
636+
None
637+
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
638+
} else {
639+
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
640+
}
641+
},
642+
// Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
643+
&OptionalField::Absent => {
644+
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
645+
}
646+
}
647+
} else { None };
648+
628649
let mut chan = Channel {
629650
user_id: user_id,
630651
config: local_config,
@@ -692,7 +713,7 @@ impl Channel {
692713
their_prev_commitment_point: None,
693714
their_node_id: their_node_id,
694715

695-
their_shutdown_scriptpubkey: None,
716+
their_shutdown_scriptpubkey,
696717

697718
channel_monitor: channel_monitor,
698719

@@ -1341,7 +1362,7 @@ impl Channel {
13411362

13421363
// Message handlers:
13431364

1344-
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig) -> Result<(), ChannelError> {
1365+
pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_local_features: LocalFeatures) -> Result<(), ChannelError> {
13451366
// Check sanity of message fields:
13461367
if !self.channel_outbound {
13471368
return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
@@ -1400,6 +1421,27 @@ impl Channel {
14001421
return Err(ChannelError::Close("We consider the minimum depth to be unreasonably large"));
14011422
}
14021423

1424+
let their_shutdown_scriptpubkey = if their_local_features.supports_upfront_shutdown_script() {
1425+
match &msg.shutdown_scriptpubkey {
1426+
&OptionalField::Present(ref script) => {
1427+
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
1428+
if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
1429+
Some(script.clone())
1430+
// Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
1431+
} else if script.len() == 0 {
1432+
None
1433+
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
1434+
} else {
1435+
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format"));
1436+
}
1437+
},
1438+
// Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
1439+
&OptionalField::Absent => {
1440+
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but we don't get any script. Use 0-length script to opt-out"));
1441+
}
1442+
}
1443+
} else { None };
1444+
14031445
self.channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
14041446

14051447
self.their_dust_limit_satoshis = msg.dust_limit_satoshis;
@@ -1415,6 +1457,7 @@ impl Channel {
14151457
self.their_delayed_payment_basepoint = Some(msg.delayed_payment_basepoint);
14161458
self.their_htlc_basepoint = Some(msg.htlc_basepoint);
14171459
self.their_cur_commitment_point = Some(msg.first_per_commitment_point);
1460+
self.their_shutdown_scriptpubkey = their_shutdown_scriptpubkey;
14181461

14191462
let obscure_factor = self.get_commitment_transaction_number_obscure_factor();
14201463
self.channel_monitor.set_commitment_obscure_factor(obscure_factor);
@@ -3038,7 +3081,7 @@ impl Channel {
30383081
htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
30393082
first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
30403083
channel_flags: if self.config.announced_channel {1} else {0},
3041-
shutdown_scriptpubkey: OptionalField::Absent
3084+
shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
30423085
}
30433086
}
30443087

@@ -3070,7 +3113,7 @@ impl Channel {
30703113
delayed_payment_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key),
30713114
htlc_basepoint: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key),
30723115
first_per_commitment_point: PublicKey::from_secret_key(&self.secp_ctx, &local_commitment_secret),
3073-
shutdown_scriptpubkey: OptionalField::Absent
3116+
shutdown_scriptpubkey: OptionalField::Present(if self.config.commit_upfront_shutdown_pubkey { self.get_closing_scriptpubkey() } else { Builder::new().into_script() })
30743117
}
30753118
}
30763119

0 commit comments

Comments
 (0)