Skip to content

Commit 1b11d34

Browse files
author
Antoine Riard
committed
Implement delay channel closing in ChannelManager until maturation
Fix tests broken by introduced change
1 parent 7581b8e commit 1b11d34

File tree

2 files changed

+199
-116
lines changed

2 files changed

+199
-116
lines changed

src/ln/channelmanager.rs

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ pub struct ChannelManager {
331331
channel_state: Mutex<ChannelHolder>,
332332
our_network_key: SecretKey,
333333

334+
channel_closing_waiting_threshold_conf: Mutex<HashMap<u32, Vec<[u8; 32]>>>,
335+
334336
pending_events: Mutex<Vec<events::Event>>,
335337
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
336338
/// Essentially just when we're serializing ourselves out.
@@ -555,6 +557,8 @@ impl ChannelManager {
555557
}),
556558
our_network_key: keys_manager.get_node_secret(),
557559

560+
channel_closing_waiting_threshold_conf: Mutex::new(HashMap::new()),
561+
558562
pending_events: Mutex::new(Vec::new()),
559563
total_consistency_lock: RwLock::new(()),
560564

@@ -2399,11 +2403,12 @@ impl ChainListener for ChannelManager {
23992403
let _ = self.total_consistency_lock.read().unwrap();
24002404
let mut failed_channels = Vec::new();
24012405
{
2406+
let mut channel_closing_lock = self.channel_closing_waiting_threshold_conf.lock().unwrap();
24022407
let mut channel_lock = self.channel_state.lock().unwrap();
24032408
let channel_state = channel_lock.borrow_parts();
24042409
let short_to_id = channel_state.short_to_id;
24052410
let pending_msg_events = channel_state.pending_msg_events;
2406-
channel_state.by_id.retain(|_, channel| {
2411+
channel_state.by_id.retain(|channel_id, channel| {
24072412
let chan_res = channel.block_connected(header, height, txn_matched, indexes_of_txn_matched);
24082413
if let Ok(Some(funding_locked)) = chan_res {
24092414
pending_msg_events.push(events::MessageSendEvent::SendFundingLocked {
@@ -2428,20 +2433,24 @@ impl ChainListener for ChannelManager {
24282433
for tx in txn_matched {
24292434
for inp in tx.input.iter() {
24302435
if inp.previous_output == funding_txo.into_bitcoin_outpoint() {
2431-
log_trace!(self, "Detected channel-closing tx {} spending {}:{}, closing channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, log_bytes!(channel.channel_id()));
2432-
if let Some(short_id) = channel.get_short_channel_id() {
2433-
short_to_id.remove(&short_id);
2434-
}
2435-
// It looks like our counterparty went on-chain. We go ahead and
2436-
// broadcast our latest local state as well here, just in case its
2437-
// some kind of SPV attack, though we expect these to be dropped.
2438-
failed_channels.push(channel.force_shutdown());
2439-
if let Ok(update) = self.get_channel_update(&channel) {
2440-
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2441-
msg: update
2442-
});
2436+
log_trace!(self, "Detected channel-closing tx {} spending {}:{}, waiting until {} to close channel {}", tx.txid(), inp.previous_output.txid, inp.previous_output.vout, height + HTLC_FAIL_ANTI_REORG_DELAY - 1, log_bytes!(channel_id[..]));
2437+
match channel_closing_lock.entry(height + HTLC_FAIL_ANTI_REORG_DELAY - 1) {
2438+
hash_map::Entry::Occupied(mut entry) => {
2439+
let mut duplicate = false;
2440+
for id in entry.get().iter() {
2441+
if *id == *channel_id {
2442+
duplicate = true;
2443+
break;
2444+
}
2445+
}
2446+
if !duplicate {
2447+
entry.get_mut().push(*channel_id);
2448+
}
2449+
}
2450+
hash_map::Entry::Vacant(entry) => {
2451+
entry.insert(vec![*channel_id]);
2452+
}
24432453
}
2444-
return false;
24452454
}
24462455
}
24472456
}
@@ -2464,6 +2473,25 @@ impl ChainListener for ChannelManager {
24642473
}
24652474
true
24662475
});
2476+
if let Some(channel_closings) = channel_closing_lock.remove(&height) {
2477+
for channel_id in channel_closings {
2478+
log_trace!(self, "Enough confirmations for a broacast commitment tx, channel {} can be closed", log_bytes!(&channel_id[..]));
2479+
if let Some(mut channel) = channel_state.by_id.remove(&channel_id) {
2480+
if let Some(short_id) = channel.get_short_channel_id() {
2481+
short_to_id.remove(&short_id);
2482+
}
2483+
// It looks like our counterparty went on-chain. We go ahead and
2484+
// broadcast our latest local state as well here, just in case its
2485+
// some kind of SPV attack, though we expect these to be dropped.
2486+
failed_channels.push(channel.force_shutdown());
2487+
if let Ok(update) = self.get_channel_update(&channel) {
2488+
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2489+
msg: update
2490+
});
2491+
}
2492+
}
2493+
}
2494+
}
24672495
}
24682496
for failure in failed_channels.drain(..) {
24692497
self.finish_force_close_channel(failure);
@@ -2935,6 +2963,15 @@ impl Writeable for ChannelManager {
29352963
}
29362964
}
29372965

2966+
let channel_closing_lock = self.channel_closing_waiting_threshold_conf.lock().unwrap();
2967+
(channel_closing_lock.len() as u64).write(writer)?;
2968+
for (confirmation_height, channel_id) in channel_closing_lock.iter() {
2969+
confirmation_height.write(writer)?;
2970+
for id in channel_id {
2971+
id.write(writer)?;
2972+
}
2973+
}
2974+
29382975
Ok(())
29392976
}
29402977
}
@@ -3072,6 +3109,21 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
30723109
claimable_htlcs.insert(payment_hash, previous_hops);
30733110
}
30743111

3112+
let channel_closing_count: u64 = Readable::read(reader)?;
3113+
let mut channel_closing: HashMap<u32, Vec<[u8; 32]>> = HashMap::with_capacity(cmp::min(channel_closing_count as usize, 32));
3114+
for _ in 0..channel_closing_count {
3115+
let confirmation_height: u32 = Readable::read(reader)?;
3116+
let channel_id: [u8; 32] = Readable::read(reader)?;
3117+
match channel_closing.entry(confirmation_height) {
3118+
hash_map::Entry::Occupied(mut entry) => {
3119+
entry.get_mut().push(channel_id);
3120+
}
3121+
hash_map::Entry::Vacant(entry) => {
3122+
entry.insert(vec![channel_id]);
3123+
}
3124+
}
3125+
}
3126+
30753127
let channel_manager = ChannelManager {
30763128
genesis_hash,
30773129
fee_estimator: args.fee_estimator,
@@ -3093,6 +3145,8 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
30933145
}),
30943146
our_network_key: args.keys_manager.get_node_secret(),
30953147

3148+
channel_closing_waiting_threshold_conf: Mutex::new(channel_closing),
3149+
30963150
pending_events: Mutex::new(Vec::new()),
30973151
total_consistency_lock: RwLock::new(()),
30983152
keys_manager: args.keys_manager,

0 commit comments

Comments
 (0)