Skip to content

Commit d4bd56f

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 cfeb0c9 commit d4bd56f

File tree

3 files changed

+83
-51
lines changed

3 files changed

+83
-51
lines changed

lightning/src/ln/channel.rs

+56-6
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,57 @@ 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::Undefined);
1385+
let result = 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+
Ok(monitor)
1391+
},
1392+
Err((chan, e)) => {
1393+
self.phase = ChannelPhase::UnfundedOutboundV1(chan);
1394+
Err(e)
1395+
},
1396+
}
1397+
} else {
1398+
self.phase = phase;
1399+
Err(ChannelError::SendError("Failed to find corresponding UnfundedOutboundV1 channel".to_owned()))
1400+
};
1401+
1402+
debug_assert!(!matches!(self.phase, ChannelPhase::Undefined));
1403+
result.map(|monitor| (self.as_funded_mut().expect("Channel should be funded"), monitor))
1404+
}
1405+
1406+
pub fn unset_funding_info(&mut self) {
1407+
let phase = core::mem::replace(&mut self.phase, ChannelPhase::Undefined);
1408+
if let ChannelPhase::Funded(mut funded_chan) = phase {
1409+
funded_chan.unset_funding_info();
1410+
1411+
let context = funded_chan.context;
1412+
let unfunded_context = UnfundedChannelContext {
1413+
unfunded_channel_age_ticks: 0,
1414+
holder_commitment_point: HolderCommitmentPoint::new(&context.holder_signer, &context.secp_ctx),
1415+
};
1416+
let unfunded_chan = OutboundV1Channel {
1417+
context,
1418+
unfunded_context,
1419+
signer_pending_open_channel: false,
1420+
};
1421+
self.phase = ChannelPhase::UnfundedOutboundV1(unfunded_chan);
1422+
} else {
1423+
self.phase = phase;
1424+
};
1425+
1426+
debug_assert!(!matches!(self.phase, ChannelPhase::Undefined));
1427+
}
13811428
}
13821429

13831430
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -4956,12 +5003,15 @@ impl<SP: Deref> FundedChannel<SP> where
49565003
///
49575004
/// Further, the channel must be immediately shut down after this with a call to
49585005
/// [`ChannelContext::force_shutdown`].
4959-
pub fn unset_funding_info(&mut self, temporary_channel_id: ChannelId) {
5006+
pub fn unset_funding_info(&mut self) {
49605007
debug_assert!(matches!(
49615008
self.context.channel_state, ChannelState::AwaitingChannelReady(_)
49625009
));
49635010
self.context.channel_transaction_parameters.funding_outpoint = None;
4964-
self.context.channel_id = temporary_channel_id;
5011+
self.context.channel_id = self.context.temporary_channel_id.expect(
5012+
"temporary_channel_id should be set since unset_funding_info is only called on funded \
5013+
channels that were unfunded immediately beforehand"
5014+
);
49655015
}
49665016

49675017
/// Handles a channel_ready message from our peer. If we've already sent our channel_ready

lightning/src/ln/channelmanager.rs

+26-44
Original file line numberDiff line numberDiff line change
@@ -8088,7 +8088,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80888088
// `locked_close_channel`), we'll remove the existing channel from `outpoint_to_peer`.
80898089
// Thus, we must first unset the funding outpoint on the channel.
80908090
let err = ChannelError::close($err.to_owned());
8091-
chan.unset_funding_info(msg.temporary_channel_id);
8091+
chan.unset_funding_info();
80928092
return Err(convert_channel_err!(self, peer_state, err, chan.context, &funded_channel_id, UNFUNDED_CHANNEL).1);
80938093
} } }
80948094

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

lightning/src/ln/functional_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9350,7 +9350,7 @@ fn test_duplicate_conflicting_funding_from_second_peer() {
93509350
check_added_monitors!(nodes[0], 1);
93519351
get_err_msg(&nodes[0], &nodes[1].node.get_our_node_id());
93529352
let err_reason = ClosureReason::ProcessingError { err: "Channel funding outpoint was a duplicate".to_owned() };
9353-
check_closed_events(&nodes[0], &[ExpectedCloseEvent::from_id_reason(funding_signed_msg.channel_id, true, err_reason)]);
9353+
check_closed_events(&nodes[0], &[ExpectedCloseEvent::from_id_reason(temp_chan_id, true, err_reason)]);
93549354
}
93559355

93569356
#[test]

0 commit comments

Comments
 (0)