Skip to content

Commit c209ee7

Browse files
committed
f - Refactor setting of Channel::shutdown_scriptpubkey at shutdown
1 parent cbf5ceb commit c209ee7

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

lightning/src/ln/channel.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,15 +3275,16 @@ impl<Signer: Sign> Channel<Signer> {
32753275
// any further commitment updates after we set LocalShutdownSent.
32763276
let send_shutdown = (self.channel_state & ChannelState::LocalShutdownSent as u32) != ChannelState::LocalShutdownSent as u32;
32773277

3278-
let shutdown_scriptpubkey = match self.shutdown_scriptpubkey {
3279-
Some(_) => None,
3278+
let update_shutdown_script = match self.shutdown_scriptpubkey {
3279+
Some(_) => false,
32803280
None => {
32813281
assert!(send_shutdown);
32823282
let shutdown_scriptpubkey = keys_provider.get_shutdown_scriptpubkey();
32833283
if !shutdown_scriptpubkey.is_compatible(their_features) {
32843284
return Err(ChannelError::Close(format!("Provided a scriptpubkey format not accepted by peer. script: ({})", shutdown_scriptpubkey.clone().into_inner().to_bytes().to_hex())));
32853285
}
3286-
Some(shutdown_scriptpubkey)
3286+
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
3287+
true
32873288
},
32883289
};
32893290

@@ -3307,19 +3308,15 @@ impl<Signer: Sign> Channel<Signer> {
33073308
}
33083309
});
33093310

3310-
let monitor_update = match shutdown_scriptpubkey {
3311-
Some(shutdown_scriptpubkey) => {
3312-
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
3313-
self.latest_monitor_update_id += 1;
3314-
Some(ChannelMonitorUpdate {
3315-
update_id: self.latest_monitor_update_id,
3316-
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
3317-
scriptpubkey: self.get_closing_scriptpubkey(),
3318-
}],
3319-
})
3320-
},
3321-
None => None,
3322-
};
3311+
let monitor_update = if update_shutdown_script {
3312+
self.latest_monitor_update_id += 1;
3313+
Some(ChannelMonitorUpdate {
3314+
update_id: self.latest_monitor_update_id,
3315+
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
3316+
scriptpubkey: self.get_closing_scriptpubkey(),
3317+
}],
3318+
})
3319+
} else { None };
33233320
let shutdown = if send_shutdown {
33243321
Some(msgs::Shutdown {
33253322
channel_id: self.channel_id,
@@ -4471,31 +4468,28 @@ impl<Signer: Sign> Channel<Signer> {
44714468
return Err(APIError::ChannelUnavailable{err: "Cannot begin shutdown while peer is disconnected or we're waiting on a monitor update, maybe force-close instead?".to_owned()});
44724469
}
44734470

4474-
let shutdown_scriptpubkey = match self.shutdown_scriptpubkey {
4475-
Some(_) => None,
4471+
let update_shutdown_script = match self.shutdown_scriptpubkey {
4472+
Some(_) => false,
44764473
None => {
44774474
let shutdown_scriptpubkey = keys_provider.get_shutdown_scriptpubkey();
44784475
if !shutdown_scriptpubkey.is_compatible(their_features) {
44794476
return Err(APIError::APIMisuseError { err: format!("Provided a scriptpubkey format not accepted by peer. script: ({})", shutdown_scriptpubkey.clone().into_inner().to_bytes().to_hex()) });
44804477
}
4481-
Some(shutdown_scriptpubkey)
4482-
},
4483-
};
4484-
4485-
let monitor_update = match shutdown_scriptpubkey {
4486-
Some(shutdown_scriptpubkey) => {
44874478
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
4488-
self.latest_monitor_update_id += 1;
4489-
Some(ChannelMonitorUpdate {
4490-
update_id: self.latest_monitor_update_id,
4491-
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
4492-
scriptpubkey: self.get_closing_scriptpubkey(),
4493-
}],
4494-
})
4479+
true
44954480
},
4496-
None => None,
44974481
};
44984482

4483+
let monitor_update = if update_shutdown_script {
4484+
self.latest_monitor_update_id += 1;
4485+
Some(ChannelMonitorUpdate {
4486+
update_id: self.latest_monitor_update_id,
4487+
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
4488+
scriptpubkey: self.get_closing_scriptpubkey(),
4489+
}],
4490+
})
4491+
} else { None };
4492+
44994493
// From here on out, we may not fail!
45004494
if self.channel_state < ChannelState::FundingSent as u32 {
45014495
self.channel_state = ChannelState::ShutdownComplete as u32;

0 commit comments

Comments
 (0)