Skip to content

Commit 353d295

Browse files
committed
f - Refactor setting of Channel::shutdown_scriptpubkey at shutdown
1 parent 3c699d2 commit 353d295

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
@@ -3276,15 +3276,16 @@ impl<Signer: Sign> Channel<Signer> {
32763276
// any further commitment updates after we set LocalShutdownSent.
32773277
let send_shutdown = (self.channel_state & ChannelState::LocalShutdownSent as u32) != ChannelState::LocalShutdownSent as u32;
32783278

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

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

3311-
let monitor_update = match shutdown_scriptpubkey {
3312-
Some(shutdown_scriptpubkey) => {
3313-
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
3314-
self.latest_monitor_update_id += 1;
3315-
Some(ChannelMonitorUpdate {
3316-
update_id: self.latest_monitor_update_id,
3317-
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
3318-
scriptpubkey: self.get_closing_scriptpubkey(),
3319-
}],
3320-
})
3321-
},
3322-
None => None,
3323-
};
3312+
let monitor_update = if update_shutdown_script {
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+
} else { None };
33243321
let shutdown = if send_shutdown {
33253322
Some(msgs::Shutdown {
33263323
channel_id: self.channel_id,
@@ -4472,31 +4469,28 @@ impl<Signer: Sign> Channel<Signer> {
44724469
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()});
44734470
}
44744471

4475-
let shutdown_scriptpubkey = match self.shutdown_scriptpubkey {
4476-
Some(_) => None,
4472+
let update_shutdown_script = match self.shutdown_scriptpubkey {
4473+
Some(_) => false,
44774474
None => {
44784475
let shutdown_scriptpubkey = keys_provider.get_shutdown_scriptpubkey();
44794476
if !shutdown_scriptpubkey.is_compatible(their_features) {
44804477
return Err(APIError::APIMisuseError { err: format!("Provided a scriptpubkey format not accepted by peer. script: ({})", shutdown_scriptpubkey.clone().into_inner().to_bytes().to_hex()) });
44814478
}
4482-
Some(shutdown_scriptpubkey)
4483-
},
4484-
};
4485-
4486-
let monitor_update = match shutdown_scriptpubkey {
4487-
Some(shutdown_scriptpubkey) => {
44884479
self.shutdown_scriptpubkey = Some(shutdown_scriptpubkey);
4489-
self.latest_monitor_update_id += 1;
4490-
Some(ChannelMonitorUpdate {
4491-
update_id: self.latest_monitor_update_id,
4492-
updates: vec![ChannelMonitorUpdateStep::ShutdownScript {
4493-
scriptpubkey: self.get_closing_scriptpubkey(),
4494-
}],
4495-
})
4480+
true
44964481
},
4497-
None => None,
44984482
};
44994483

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

0 commit comments

Comments
 (0)