Skip to content

Commit 1f95bd1

Browse files
committed
Breakup CooperativeClosure into Local/Remote initiated
1 parent 5bf58f0 commit 1f95bd1

File tree

7 files changed

+83
-54
lines changed

7 files changed

+83
-54
lines changed

lightning/src/events/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,14 @@ pub enum ClosureReason {
184184
HolderForceClosed,
185185
/// The channel was closed after negotiating a cooperative close and we've now broadcasted
186186
/// the cooperative close transaction. Note the shutdown may have been initiated by us.
187-
//TODO: split between CounterpartyInitiated/LocallyInitiated
188187
CooperativeClosure,
188+
/// The channel was closed after negotiating a cooperative close and we've now broadcasted
189+
/// the cooperative close transaction. This indicates that the shutdown was initiated by our
190+
/// counterparty.
191+
CounterpartyInitiatedCooperativeClosure,
192+
/// The channel was closed after negotiating a cooperative close and we've now broadcasted
193+
/// the cooperative close transaction. This indicates that the shutdown was initiated by us.
194+
LocallyInitiatedCooperativeClosure,
189195
/// A commitment transaction was confirmed on chain, closing the channel. Most likely this
190196
/// commitment transaction came from our counterparty, but it may also have come from
191197
/// a copy of our own `ChannelMonitor`.
@@ -231,6 +237,8 @@ impl core::fmt::Display for ClosureReason {
231237
},
232238
ClosureReason::HolderForceClosed => f.write_str("user manually force-closed the channel"),
233239
ClosureReason::CooperativeClosure => f.write_str("the channel was cooperatively closed"),
240+
ClosureReason::CounterpartyInitiatedCooperativeClosure => f.write_str("the channel was cooperatively closed by our peer"),
241+
ClosureReason::LocallyInitiatedCooperativeClosure => f.write_str("the channel was cooperatively closed by us"),
234242
ClosureReason::CommitmentTxConfirmed => f.write_str("commitment or closing transaction was confirmed on chain."),
235243
ClosureReason::FundingTimedOut => write!(f, "funding transaction failed to confirm within {} blocks", FUNDING_CONF_DEADLINE_BLOCKS),
236244
ClosureReason::ProcessingError { err } => {
@@ -255,7 +263,9 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
255263
(10, DisconnectedPeer) => {},
256264
(12, OutdatedChannelManager) => {},
257265
(13, CounterpartyCoopClosedUnfundedChannel) => {},
258-
(15, FundingBatchClosure) => {}
266+
(15, FundingBatchClosure) => {},
267+
(16, CounterpartyInitiatedCooperativeClosure) => {},
268+
(18, LocallyInitiatedCooperativeClosure) => {},
259269
);
260270

261271
/// Intended destination of a failed HTLC as indicated in [`Event::HTLCHandlingFailed`].

lightning/src/ln/chanmon_update_fail_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,8 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf:
19551955

19561956
send_payment(&nodes[0], &[&nodes[1]], 8000000);
19571957
close_channel(&nodes[0], &nodes[1], &channel_id, funding_tx, true);
1958-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1959-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1958+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1959+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
19601960
}
19611961

19621962
#[test]
@@ -2634,8 +2634,8 @@ fn test_temporary_error_during_shutdown() {
26342634
assert_eq!(txn_a, txn_b);
26352635
assert_eq!(txn_a.len(), 1);
26362636
check_spends!(txn_a[0], funding_tx);
2637-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
2638-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
2637+
check_closed_event!(nodes[1], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
2638+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
26392639
}
26402640

26412641
#[test]

lightning/src/ln/channel.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ macro_rules! define_state_flags {
347347
is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent);
348348
define_state_flags!($flag_type, FundedStateFlags::LOCAL_SHUTDOWN_SENT,
349349
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent);
350+
define_state_flags!($flag_type, FundedStateFlags::LOCAL_INITIATED_SHUTDOWN,
351+
is_local_initiated_shutdown, set_local_initiated_shutdown, clear_local_initiated_shutdown);
350352

351353
impl core::ops::BitOr<FundedStateFlags> for $flag_type {
352354
type Output = Self;
@@ -388,6 +390,7 @@ mod state_flags {
388390
pub const LOCAL_SHUTDOWN_SENT: u32 = 1 << 11;
389391
pub const SHUTDOWN_COMPLETE: u32 = 1 << 12;
390392
pub const WAITING_FOR_BATCH: u32 = 1 << 13;
393+
pub const LOCAL_INITIATED_SHUTDOWN: u32 = 1 << 14;
391394
}
392395

393396
define_state_flags!(
@@ -406,7 +409,10 @@ define_state_flags!(
406409
is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent),
407410
("Indicates we sent a `shutdown` message. At this point, we may not add any new HTLCs to \
408411
the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT,
409-
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent)
412+
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent),
413+
("Indicates we were the original sender of the the `shutdown` message.", LOCAL_INITIATED_SHUTDOWN,
414+
state_flags::LOCAL_INITIATED_SHUTDOWN, is_local_initiated_shutdown, set_local_initiated_shutdown,
415+
clear_local_initiated_shutdown)
410416
]
411417
);
412418

@@ -573,6 +579,7 @@ impl ChannelState {
573579
impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress, FUNDED_STATES);
574580
impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent, FUNDED_STATES);
575581
impl_state_flag!(is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent, FUNDED_STATES);
582+
impl_state_flag!(is_local_initiated_shutdown, set_local_initiated_shutdown, clear_local_initiated_shutdown, FUNDED_STATES);
576583
impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready, AwaitingChannelReady);
577584
impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready, AwaitingChannelReady);
578585
impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch, AwaitingChannelReady);
@@ -4959,11 +4966,17 @@ impl<SP: Deref> Channel<SP> where
49594966
}
49604967
}
49614968

4969+
let closure_reason = if self.initiated_shutdown() {
4970+
ClosureReason::LocallyInitiatedCooperativeClosure
4971+
} else {
4972+
ClosureReason::CounterpartyInitiatedCooperativeClosure
4973+
};
4974+
49624975
assert!(self.context.shutdown_scriptpubkey.is_some());
49634976
if let Some((last_fee, sig)) = self.context.last_sent_closing_fee {
49644977
if last_fee == msg.fee_satoshis {
49654978
let shutdown_result = ShutdownResult {
4966-
closure_reason: ClosureReason::CooperativeClosure,
4979+
closure_reason,
49674980
monitor_update: None,
49684981
dropped_outbound_htlcs: Vec::new(),
49694982
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
@@ -4998,7 +5011,7 @@ impl<SP: Deref> Channel<SP> where
49985011
.map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
49995012
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
50005013
let shutdown_result = ShutdownResult {
5001-
closure_reason: ClosureReason::CooperativeClosure,
5014+
closure_reason,
50025015
monitor_update: None,
50035016
dropped_outbound_htlcs: Vec::new(),
50045017
unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
@@ -5263,6 +5276,11 @@ impl<SP: Deref> Channel<SP> where
52635276
self.context.channel_state.is_local_shutdown_sent()
52645277
}
52655278

5279+
/// Returns true if we either initiated to shut down the channel.
5280+
pub fn initiated_shutdown(&self) -> bool {
5281+
self.context.channel_state.is_local_initiated_shutdown()
5282+
}
5283+
52665284
/// Returns true if this channel is fully shut down. True here implies that no further actions
52675285
/// may/will be taken on this channel, and thus this object should be freed. Any future changes
52685286
/// will be handled appropriately by the chain monitor.
@@ -6177,6 +6195,7 @@ impl<SP: Deref> Channel<SP> where
61776195
// From here on out, we may not fail!
61786196
self.context.target_closing_feerate_sats_per_kw = target_feerate_sats_per_kw;
61796197
self.context.channel_state.set_local_shutdown_sent();
6198+
self.context.channel_state.set_local_initiated_shutdown(); // fixme why does this break shutdown
61806199
self.context.update_time_counter += 1;
61816200

61826201
let monitor_update = if update_shutdown_script {

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11856,8 +11856,8 @@ mod tests {
1185611856
}
1185711857
let (_nodes_1_update, _none) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1185811858

11859-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000);
11860-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000);
11859+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000);
11860+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000);
1186111861
}
1186211862

1186311863
fn check_not_connected_to_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {

lightning/src/ln/functional_tests.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ fn test_update_fee_with_fundee_update_add_htlc() {
871871
send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
872872
send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
873873
close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
874-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
875-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
874+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
875+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
876876
}
877877

878878
#[test]
@@ -985,8 +985,8 @@ fn test_update_fee() {
985985
assert_eq!(get_feerate!(nodes[0], nodes[1], channel_id), feerate + 30);
986986
assert_eq!(get_feerate!(nodes[1], nodes[0], channel_id), feerate + 30);
987987
close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
988-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
989-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
988+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
989+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
990990
}
991991

992992
#[test]
@@ -1104,17 +1104,17 @@ fn fake_network_test() {
11041104

11051105
// Close down the channels...
11061106
close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1107-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1108-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1107+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1108+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
11091109
close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1110-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1111-
check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1110+
check_closed_event!(nodes[1], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1111+
check_closed_event!(nodes[2], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
11121112
close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1113-
check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1114-
check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1113+
check_closed_event!(nodes[2], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1114+
check_closed_event!(nodes[3], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
11151115
close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1116-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1117-
check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1116+
check_closed_event!(nodes[1], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1117+
check_closed_event!(nodes[3], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
11181118
}
11191119

11201120
#[test]
@@ -5619,15 +5619,15 @@ fn test_static_output_closing_tx() {
56195619
let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
56205620

56215621
mine_transaction(&nodes[0], &closing_tx);
5622-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
5622+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
56235623
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
56245624

56255625
let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
56265626
assert_eq!(spend_txn.len(), 1);
56275627
check_spends!(spend_txn[0], closing_tx);
56285628

56295629
mine_transaction(&nodes[1], &closing_tx);
5630-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
5630+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
56315631
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
56325632

56335633
let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);

lightning/src/ln/monitor_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) {
257257
spendable_outputs_b
258258
);
259259

260-
check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000);
261-
check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000);
260+
check_closed_event!(nodes[0], 1, ClosureReason::LocallyInitiatedCooperativeClosure, [nodes[1].node.get_our_node_id()], 1000000);
261+
check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyInitiatedCooperativeClosure, [nodes[0].node.get_our_node_id()], 1000000);
262262
}
263263

264264
#[test]

0 commit comments

Comments
 (0)