Skip to content

Commit 4b348e6

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 49be063 commit 4b348e6

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

lightning/src/ln/channel.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ impl<Signer: Sign> Channel<Signer> {
22212221
&self.get_counterparty_pubkeys().funding_pubkey
22222222
}
22232223

2224-
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>, Option<msgs::ChannelReady>), ChannelError> where L::Target: Logger {
2224+
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
22252225
if self.is_outbound() {
22262226
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
22272227
}
@@ -2293,10 +2293,13 @@ impl<Signer: Sign> Channel<Signer> {
22932293

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

2296+
let need_channel_ready = self.check_get_channel_ready(0).is_some();
2297+
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
2298+
22962299
Ok((msgs::FundingSigned {
22972300
channel_id: self.channel_id,
22982301
signature
2299-
}, channel_monitor, self.check_get_channel_ready(0)))
2302+
}, channel_monitor))
23002303
}
23012304

23022305
/// Handles a funding_signed message from the remote end.
@@ -3680,7 +3683,7 @@ impl<Signer: Sign> Channel<Signer> {
36803683
///
36813684
/// [`chain::Watch`]: crate::chain::Watch
36823685
/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
3683-
pub fn monitor_updating_paused(&mut self, resend_raa: bool, resend_commitment: bool,
3686+
fn monitor_updating_paused(&mut self, resend_raa: bool, resend_commitment: bool,
36843687
resend_channel_ready: bool, mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
36853688
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
36863689
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>
@@ -7036,7 +7039,7 @@ mod tests {
70367039
}]};
70377040
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
70387041
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
7039-
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
7042+
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
70407043

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

lightning/src/ln/channelmanager.rs

+29-38
Original file line numberDiff line numberDiff line change
@@ -4743,50 +4743,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
47434743
}
47444744

47454745
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
4746-
let ((funding_msg, monitor, mut channel_ready), mut chan) = {
4747-
let best_block = *self.best_block.read().unwrap();
4748-
let mut channel_lock = self.channel_state.lock().unwrap();
4749-
let channel_state = &mut *channel_lock;
4746+
let best_block = *self.best_block.read().unwrap();
4747+
let mut channel_lock = self.channel_state.lock().unwrap();
4748+
let channel_state = &mut *channel_lock;
4749+
let ((funding_msg, monitor), chan) =
47504750
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
47514751
hash_map::Entry::Occupied(mut chan) => {
47524752
if chan.get().get_counterparty_node_id() != *counterparty_node_id {
47534753
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!".to_owned(), msg.temporary_channel_id));
47544754
}
47554755
(try_chan_entry!(self, chan.get_mut().funding_created(msg, best_block, &self.logger), chan), chan.remove())
4756+
47564757
},
47574758
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.temporary_channel_id))
4758-
}
4759-
};
4760-
// Because we have exclusive ownership of the channel here we can release the channel_state
4761-
// lock before watch_channel
4762-
match self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor) {
4763-
ChannelMonitorUpdateStatus::Completed => {},
4764-
ChannelMonitorUpdateStatus::PermanentFailure => {
4765-
// Note that we reply with the new channel_id in error messages if we gave up on the
4766-
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
4767-
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4768-
// any messages referencing a previously-closed channel anyway.
4769-
// We do not propagate the monitor update to the user as it would be for a monitor
4770-
// that we didn't manage to store (and that we don't care about - we don't respond
4771-
// with the funding_signed so the channel can never go on chain).
4772-
let (_monitor_update, failed_htlcs) = chan.force_shutdown(false);
4773-
assert!(failed_htlcs.is_empty());
4774-
return Err(MsgHandleErrInternal::send_err_msg_no_close("ChannelMonitor storage failure".to_owned(), funding_msg.channel_id));
4775-
},
4776-
ChannelMonitorUpdateStatus::InProgress => {
4777-
// There's no problem signing a counterparty's funding transaction if our monitor
4778-
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4779-
// accepted payment from yet. We do, however, need to wait to send our channel_ready
4780-
// until we have persisted our monitor.
4781-
chan.monitor_updating_paused(false, false, channel_ready.is_some(), Vec::new(), Vec::new(), Vec::new());
4782-
channel_ready = None; // Don't send the channel_ready now
4783-
},
4784-
}
4785-
let mut channel_state_lock = self.channel_state.lock().unwrap();
4786-
let channel_state = &mut *channel_state_lock;
4759+
};
4760+
47874761
match channel_state.by_id.entry(funding_msg.channel_id) {
47884762
hash_map::Entry::Occupied(_) => {
4789-
return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
4763+
Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
47904764
},
47914765
hash_map::Entry::Vacant(e) => {
47924766
let mut id_to_peer = self.id_to_peer.lock().unwrap();
@@ -4800,17 +4774,34 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48004774
i_e.insert(chan.get_counterparty_node_id());
48014775
}
48024776
}
4777+
4778+
// There's no problem signing a counterparty's funding transaction if our monitor
4779+
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4780+
// accepted payment from yet. We do, however, need to wait to send our channel_ready
4781+
// until we have persisted our monitor.
48034782
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
48044783
node_id: counterparty_node_id.clone(),
48054784
msg: funding_msg,
48064785
});
4807-
if let Some(msg) = channel_ready {
4808-
send_channel_ready!(self, channel_state.pending_msg_events, chan, msg);
4786+
4787+
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
4788+
4789+
let chan = e.insert(chan);
4790+
let mut res = handle_new_monitor_update!(self, monitor_res, 0, channel_lock, channel_state.pending_msg_events, chan, MANUALLY_REMOVING, 42);
4791+
4792+
// Note that we reply with the new channel_id in error messages if we gave up on the
4793+
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
4794+
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4795+
// any messages referencing a previously-closed channel anyway.
4796+
// We do not propagate the monitor update to the user as it would be for a monitor
4797+
// that we didn't manage to store (and that we don't care about - we don't respond
4798+
// with the funding_signed so the channel can never go on chain).
4799+
if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
4800+
res.0 = None;
48094801
}
4810-
e.insert(chan);
4802+
res
48114803
}
48124804
}
4813-
Ok(())
48144805
}
48154806

48164807
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
@@ -8659,9 +8659,9 @@ fn test_duplicate_chan_id() {
86598659
};
86608660
check_added_monitors!(nodes[0], 0);
86618661
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
8662-
// At this point we'll try to add a duplicate channel monitor, which will be rejected, but
8663-
// still needs to be cleared here.
8664-
check_added_monitors!(nodes[1], 1);
8662+
// At this point we'll look up if the channel_id is present and immediately fail the channel
8663+
// without trying to persist the `ChannelMonitor.
8664+
check_added_monitors!(nodes[1], 0);
86658665

86668666
// ...still, nodes[1] will reject the duplicate channel.
86678667
{

0 commit comments

Comments
 (0)