Skip to content

Commit aaf6b0a

Browse files
committed
Move funding_signed phase transition to Channel
Now that ChannelPhase is encapsulated in Channel, phase transitions can be moved from ChannelManager to Channel. Update the funding_signed phase transition accordingly. This allows for simpler logic in ChannelManager since the channel does not need to removed and then readded into the channel_by_id map.
1 parent 61e2291 commit aaf6b0a

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

lightning/src/ln/channel.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,6 @@ impl<SP: Deref> Channel<SP> where
12111211
matches!(self.phase, ChannelPhase::UnfundedOutboundV1(_) | ChannelPhase::UnfundedInboundV1(_))
12121212
}
12131213

1214-
pub fn is_unfunded_outbound_v1(&self) -> bool {
1215-
matches!(self.phase, ChannelPhase::UnfundedOutboundV1(_))
1216-
}
1217-
12181214
pub fn into_unfunded_outbound_v1(self) -> Result<OutboundV1Channel<SP>, Self> {
12191215
if let ChannelPhase::UnfundedOutboundV1(channel) = self.phase {
12201216
Ok(channel)
@@ -1378,6 +1374,32 @@ impl<SP: Deref> Channel<SP> where
13781374
},
13791375
}
13801376
}
1377+
1378+
pub fn funding_signed<L: Deref>(
1379+
&mut self, msg: &msgs::FundingSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
1380+
) -> Result<(&mut FundedChannel<SP>, ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>), ChannelError>
1381+
where
1382+
L::Target: Logger
1383+
{
1384+
let phase = core::mem::replace(&mut self.phase, ChannelPhase::Intermediate);
1385+
if let ChannelPhase::UnfundedOutboundV1(chan) = phase {
1386+
let logger = WithChannelContext::from(logger, &chan.context, None);
1387+
match chan.funding_signed(msg, best_block, signer_provider, &&logger) {
1388+
Ok((chan, monitor)) => {
1389+
self.phase = ChannelPhase::Funded(chan);
1390+
let funded_chan = self.as_funded_mut().expect("Channel should be funded");
1391+
Ok((funded_chan, monitor))
1392+
},
1393+
Err((chan, e)) => {
1394+
self.phase = ChannelPhase::UnfundedOutboundV1(chan);
1395+
Err(e)
1396+
},
1397+
}
1398+
} else {
1399+
self.phase = phase;
1400+
Err(ChannelError::NotFound("Failed to find corresponding channel".to_owned()))
1401+
}
1402+
}
13811403
}
13821404

13831405
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8157,49 +8157,29 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81578157
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
81588158
let peer_state = &mut *peer_state_lock;
81598159
match peer_state.channel_by_id.entry(msg.channel_id) {
8160-
hash_map::Entry::Occupied(chan_entry) => {
8161-
if chan_entry.get().is_unfunded_outbound_v1() {
8162-
let chan = if let Ok(chan) = chan_entry.remove().into_unfunded_outbound_v1() { chan } else { unreachable!() };
8163-
let logger = WithContext::from(
8164-
&self.logger,
8165-
Some(chan.context.get_counterparty_node_id()),
8166-
Some(chan.context.channel_id()),
8167-
None
8168-
);
8169-
let res =
8170-
chan.funding_signed(&msg, best_block, &self.signer_provider, &&logger);
8171-
match res {
8172-
Ok((mut chan, monitor)) => {
8173-
if let Ok(persist_status) = self.chain_monitor.watch_channel(chan.context.get_funding_txo().unwrap(), monitor) {
8174-
// We really should be able to insert here without doing a second
8175-
// lookup, but sadly rust stdlib doesn't currently allow keeping
8176-
// the original Entry around with the value removed.
8177-
let chan = peer_state.channel_by_id.entry(msg.channel_id).or_insert(Channel::from(chan));
8178-
if let Some(funded_chan) = chan.as_funded_mut() {
8179-
handle_new_monitor_update!(self, persist_status, peer_state_lock, peer_state, per_peer_state, funded_chan, INITIAL_MONITOR);
8180-
} else { unreachable!(); }
8181-
Ok(())
8182-
} else {
8183-
let e = ChannelError::close("Channel funding outpoint was a duplicate".to_owned());
8160+
hash_map::Entry::Occupied(mut chan_entry) => {
8161+
let chan = chan_entry.get_mut();
8162+
match chan
8163+
.funding_signed(&msg, best_block, &self.signer_provider, &self.logger)
8164+
.and_then(|(funded_chan, monitor)| {
8165+
self.chain_monitor
8166+
.watch_channel(funded_chan.context.get_funding_txo().unwrap(), monitor)
8167+
.map_err(|()| {
81848168
// We weren't able to watch the channel to begin with, so no
81858169
// updates should be made on it. Previously, full_stack_target
81868170
// found an (unreachable) panic when the monitor update contained
81878171
// within `shutdown_finish` was applied.
8188-
chan.unset_funding_info(msg.channel_id);
8189-
return Err(convert_channel_err!(self, peer_state, e, chan, &msg.channel_id, FUNDED_CHANNEL).1);
8190-
}
8191-
},
8192-
Err((mut chan, e)) => {
8193-
debug_assert!(matches!(e, ChannelError::Close(_)),
8194-
"We don't have a channel anymore, so the error better have expected close");
8195-
// We've already removed this outbound channel from the map in
8196-
// `PeerState` above so at this point we just need to clean up any
8197-
// lingering entries concerning this channel as it is safe to do so.
8198-
return Err(convert_channel_err!(self, peer_state, e, chan.context, &msg.channel_id, UNFUNDED_CHANNEL).1);
8199-
}
8200-
}
8201-
} else {
8202-
return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id));
8172+
funded_chan.unset_funding_info(msg.channel_id);
8173+
ChannelError::close("Channel funding outpoint was a duplicate".to_owned())
8174+
})
8175+
.map(|persist_status| (funded_chan, persist_status))
8176+
})
8177+
{
8178+
Ok((funded_chan, persist_status)) => {
8179+
handle_new_monitor_update!(self, persist_status, peer_state_lock, peer_state, per_peer_state, funded_chan, INITIAL_MONITOR);
8180+
Ok(())
8181+
},
8182+
Err(e) => try_channel_entry!(self, peer_state, Err(e), chan_entry),
82038183
}
82048184
},
82058185
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))

0 commit comments

Comments
 (0)