Skip to content

Commit ace0660

Browse files
committed
Use the new monitor persistence flow for funding_created handling
Building on the previous commits, this finishes our transition to doing all message-sending in the monitor update completion pipeline, unifying our immediate- and async- `ChannelMonitor` update and persistence flows.
1 parent 5491193 commit ace0660

File tree

3 files changed

+40
-51
lines changed

3 files changed

+40
-51
lines changed

lightning/src/ln/channel.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -2230,13 +2230,9 @@ impl<Signer: Sign> Channel<Signer> {
22302230
&self.get_counterparty_pubkeys().funding_pubkey
22312231
}
22322232

2233-
pub fn funding_created<K: Deref, L: Deref>(
2234-
&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, keys_source: &K, logger: &L
2235-
) -> Result<(msgs::FundingSigned, ChannelMonitor<<K::Target as KeysInterface>::Signer>, Option<msgs::ChannelReady>), ChannelError>
2236-
where
2237-
K::Target: KeysInterface,
2238-
L::Target: Logger
2239-
{
2233+
pub fn funding_created<L: Deref>(
2234+
&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L
2235+
) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
22402236
if self.is_outbound() {
22412237
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
22422238
}
@@ -2310,10 +2306,13 @@ impl<Signer: Sign> Channel<Signer> {
23102306

23112307
log_info!(logger, "Generated funding_signed for peer for channel {}", log_bytes!(self.channel_id()));
23122308

2309+
let need_channel_ready = self.check_get_channel_ready(0).is_some();
2310+
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
2311+
23132312
Ok((msgs::FundingSigned {
23142313
channel_id: self.channel_id,
23152314
signature
2316-
}, channel_monitor, self.check_get_channel_ready(0)))
2315+
}, channel_monitor))
23172316
}
23182317

23192318
/// Handles a funding_signed message from the remote end.
@@ -3701,7 +3700,7 @@ impl<Signer: Sign> Channel<Signer> {
37013700
///
37023701
/// [`chain::Watch`]: crate::chain::Watch
37033702
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
3704-
pub fn monitor_updating_paused(&mut self, resend_raa: bool, resend_commitment: bool,
3703+
fn monitor_updating_paused(&mut self, resend_raa: bool, resend_commitment: bool,
37053704
resend_channel_ready: bool, mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
37063705
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
37073706
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>
@@ -7088,7 +7087,7 @@ mod tests {
70887087
}]};
70897088
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
70907089
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
7091-
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).unwrap();
7090+
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&keys_provider, &&logger).unwrap();
70927091

70937092
// Node B --> Node A: funding signed
70947093
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&keys_provider, &&logger);

lightning/src/ln/channelmanager.rs

+28-38
Original file line numberDiff line numberDiff line change
@@ -4695,10 +4695,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
46954695
}
46964696

46974697
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
4698-
let ((funding_msg, monitor, mut channel_ready), mut chan) = {
4699-
let best_block = *self.best_block.read().unwrap();
4700-
let mut channel_lock = self.channel_state.lock().unwrap();
4701-
let channel_state = &mut *channel_lock;
4698+
let best_block = *self.best_block.read().unwrap();
4699+
let mut channel_lock = self.channel_state.lock().unwrap();
4700+
let channel_state = &mut *channel_lock;
4701+
let ((funding_msg, monitor), chan) =
47024702
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
47034703
hash_map::Entry::Occupied(mut chan) => {
47044704
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
@@ -4707,38 +4707,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
47074707
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.keys_manager, &self.logger), chan), chan.remove())
47084708
},
47094709
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
4710-
}
4711-
};
4712-
// Because we have exclusive ownership of the channel here we can release the channel_state
4713-
// lock before watch_channel
4714-
match self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor) {
4715-
ChannelMonitorUpdateStatus::Completed => {},
4716-
ChannelMonitorUpdateStatus::PermanentFailure => {
4717-
// Note that we reply with the new channel_id in error messages if we gave up on the
4718-
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
4719-
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4720-
// any messages referencing a previously-closed channel anyway.
4721-
// We do not propagate the monitor update to the user as it would be for a monitor
4722-
// that we didn't manage to store (and that we don't care about - we don't respond
4723-
// with the funding_signed so the channel can never go on chain).
4724-
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);
4725-
assert!(failed_htlcs.is_empty());
4726-
return Err(MsgHandleErrInternal::send_err_msg_no_close("ChannelMonitor storage failure".to_owned(), funding_msg.channel_id));
4727-
},
4728-
ChannelMonitorUpdateStatus::InProgress => {
4729-
// There's no problem signing a counterparty's funding transaction if our monitor
4730-
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4731-
// accepted payment from yet. We do, however, need to wait to send our channel_ready
4732-
// until we have persisted our monitor.
4733-
chan.monitor_updating_paused(false, false, channel_ready.is_some(), Vec::new(), Vec::new(), Vec::new());
4734-
channel_ready = None; // Don't send the channel_ready now
4735-
},
4736-
}
4737-
let mut channel_state_lock = self.channel_state.lock().unwrap();
4738-
let channel_state = &mut *channel_state_lock;
4710+
};
4711+
47394712
match channel_state.by_id.entry(funding_msg.channel_id) {
47404713
hash_map::Entry::Occupied(_) => {
4741-
return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
4714+
Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
47424715
},
47434716
hash_map::Entry::Vacant(e) => {
47444717
let mut id_to_peer = self.id_to_peer.lock().unwrap();
@@ -4752,17 +4725,34 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
47524725
i_e.insert(chan.get_counterparty_node_id());
47534726
}
47544727
}
4728+
4729+
// There's no problem signing a counterparty's funding transaction if our monitor
4730+
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4731+
// accepted payment from yet. We do, however, need to wait to send our channel_ready
4732+
// until we have persisted our monitor.
47554733
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
47564734
node_id: counterparty_node_id.clone(),
47574735
msg: funding_msg,
47584736
});
4759-
if let Some(msg) = channel_ready {
4760-
send_channel_ready!(self, channel_state.pending_msg_events, chan, msg);
4737+
4738+
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
4739+
4740+
let chan = e.insert(chan);
4741+
let mut res = handle_new_monitor_update!(self, monitor_res, 0, channel_lock, channel_state.pending_msg_events, chan, MANUALLY_REMOVING, 42);
4742+
4743+
// Note that we reply with the new channel_id in error messages if we gave up on the
4744+
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
4745+
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4746+
// any messages referencing a previously-closed channel anyway.
4747+
// We do not propagate the monitor update to the user as it would be for a monitor
4748+
// that we didn't manage to store (and that we don't care about - we don't respond
4749+
// with the funding_signed so the channel can never go on chain).
4750+
if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
4751+
res.0 = None;
47614752
}
4762-
e.insert(chan);
4753+
res
47634754
}
47644755
}
4765-
Ok(())
47664756
}
47674757

47684758
fn internal_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {

lightning/src/ln/functional_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8586,9 +8586,9 @@ fn test_duplicate_chan_id() {
85868586
};
85878587
check_added_monitors!(nodes[0], 0);
85888588
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
8589-
// At this point we'll try to add a duplicate channel monitor, which will be rejected, but
8590-
// still needs to be cleared here.
8591-
check_added_monitors!(nodes[1], 1);
8589+
// At this point we'll look up if the channel_id is present and immediately fail the channel
8590+
// without trying to persist the `ChannelMonitor.
8591+
check_added_monitors!(nodes[1], 0);
85928592

85938593
// ...still, nodes[1] will reject the duplicate channel.
85948594
{

0 commit comments

Comments
 (0)