Skip to content

Commit e847ec4

Browse files
committed
f swap check order
1 parent 7221708 commit e847ec4

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5708,53 +5708,55 @@ where
57085708
Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
57095709
},
57105710
hash_map::Entry::Vacant(e) => {
5711-
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
5712-
if let Ok(persist_state) = monitor_res {
5713-
match self.id_to_peer.lock().unwrap().entry(chan.context.channel_id()) {
5714-
hash_map::Entry::Occupied(_) => {
5715-
return Err(MsgHandleErrInternal::send_err_msg_no_close(
5716-
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
5717-
funding_msg.channel_id))
5718-
},
5719-
hash_map::Entry::Vacant(i_e) => {
5711+
let mut id_to_peer_lock = self.id_to_peer.lock().unwrap();
5712+
match id_to_peer_lock.entry(chan.context.channel_id()) {
5713+
hash_map::Entry::Occupied(_) => {
5714+
return Err(MsgHandleErrInternal::send_err_msg_no_close(
5715+
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
5716+
funding_msg.channel_id))
5717+
},
5718+
hash_map::Entry::Vacant(i_e) => {
5719+
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
5720+
if let Ok(persist_state) = monitor_res {
57205721
i_e.insert(chan.context.get_counterparty_node_id());
5721-
}
5722-
}
5723-
5724-
// There's no problem signing a counterparty's funding transaction if our monitor
5725-
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
5726-
// accepted payment from yet. We do, however, need to wait to send our channel_ready
5727-
// until we have persisted our monitor.
5728-
let new_channel_id = funding_msg.channel_id;
5729-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
5730-
node_id: counterparty_node_id.clone(),
5731-
msg: funding_msg,
5732-
});
5722+
mem::drop(id_to_peer_lock);
5723+
5724+
// There's no problem signing a counterparty's funding transaction if our monitor
5725+
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
5726+
// accepted payment from yet. We do, however, need to wait to send our channel_ready
5727+
// until we have persisted our monitor.
5728+
let new_channel_id = funding_msg.channel_id;
5729+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
5730+
node_id: counterparty_node_id.clone(),
5731+
msg: funding_msg,
5732+
});
57335733

5734-
if let ChannelPhase::Funded(chan) = e.insert(ChannelPhase::Funded(chan)) {
5735-
let mut res = handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
5736-
per_peer_state, chan, MANUALLY_REMOVING_INITIAL_MONITOR,
5737-
{ peer_state.channel_by_id.remove(&new_channel_id) });
5738-
5739-
// Note that we reply with the new channel_id in error messages if we gave up on the
5740-
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
5741-
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
5742-
// any messages referencing a previously-closed channel anyway.
5743-
// We do not propagate the monitor update to the user as it would be for a monitor
5744-
// that we didn't manage to store (and that we don't care about - we don't respond
5745-
// with the funding_signed so the channel can never go on chain).
5746-
if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
5747-
res.0 = None;
5734+
if let ChannelPhase::Funded(chan) = e.insert(ChannelPhase::Funded(chan)) {
5735+
let mut res = handle_new_monitor_update!(self, persist_state, peer_state_lock, peer_state,
5736+
per_peer_state, chan, MANUALLY_REMOVING_INITIAL_MONITOR,
5737+
{ peer_state.channel_by_id.remove(&new_channel_id) });
5738+
5739+
// Note that we reply with the new channel_id in error messages if we gave up on the
5740+
// channel, not the temporary_channel_id. This is compatible with ourselves, but the
5741+
// spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
5742+
// any messages referencing a previously-closed channel anyway.
5743+
// We do not propagate the monitor update to the user as it would be for a monitor
5744+
// that we didn't manage to store (and that we don't care about - we don't respond
5745+
// with the funding_signed so the channel can never go on chain).
5746+
if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
5747+
res.0 = None;
5748+
}
5749+
res.map(|_| ())
5750+
} else {
5751+
unreachable!("This must be a funded channel as we just inserted it.");
5752+
}
5753+
} else {
5754+
log_error!(self.logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
5755+
return Err(MsgHandleErrInternal::send_err_msg_no_close(
5756+
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
5757+
funding_msg.channel_id));
57485758
}
5749-
res.map(|_| ())
5750-
} else {
5751-
unreachable!("This must be a funded channel as we just inserted it.");
57525759
}
5753-
} else {
5754-
log_error!(self.logger, "Persisting initial ChannelMonitor failed, implying the funding outpoint was duplicated");
5755-
return Err(MsgHandleErrInternal::send_err_msg_no_close(
5756-
"The funding_created message had the same funding_txid as an existing channel - funding is not possible".to_owned(),
5757-
funding_msg.channel_id));
57585760
}
57595761
}
57605762
}

0 commit comments

Comments
 (0)