Skip to content

Commit 8d00338

Browse files
committed
Update with review from @wpualino.
- Defer creating outbound_scid_alias for a manually accepted channel until the channel is actually accepted. The obviates the need to haul it around in the InboundChannelRequest struct and to clean it up when the channel is rejected. - Move the InboundChannelRequest struct to channelmanager.rs since it's really just an internal thing to be used there.
1 parent 305e68d commit 8d00338

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

lightning/src/ln/channel.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6025,18 +6025,6 @@ pub(super) struct InboundV1Channel<Signer: ChannelSigner> {
60256025
pub unfunded_context: UnfundedChannelContext,
60266026
}
60276027

6028-
/// A not-yet-accepted inbound (from counterparty) channel. Once
6029-
/// accepted, the parameters will be used to construct a channel.
6030-
pub(super) struct InboundChannelRequest {
6031-
/// The counterparty node id that initiated the channel open.
6032-
pub counterparty_node_id: PublicKey,
6033-
/// The original OpenChannel message.
6034-
pub open_channel_msg: msgs::OpenChannel,
6035-
/// The outbound SCID alias assigned to the channel when we received
6036-
/// the OpenChannel request.
6037-
pub outbound_scid_alias: u64,
6038-
}
6039-
60406028
impl<Signer: WriteableEcdsaChannelSigner> InboundV1Channel<Signer> {
60416029
/// Creates a new channel from a remote sides' request for one.
60426030
/// Assumes chain_hash has already been checked and corresponds with what we expect!

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
4040
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4141
// construct one themselves.
4242
use crate::ln::{inbound_payment, PaymentHash, PaymentPreimage, PaymentSecret};
43-
use crate::ln::channel::{Channel, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundChannelRequest, InboundV1Channel};
43+
use crate::ln::channel::{Channel, ChannelContext, ChannelError, ChannelUpdateStatus, ShutdownResult, UnfundedChannelContext, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel};
4444
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::Bolt11InvoiceFeatures;
@@ -704,10 +704,20 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
704704
fn has_channel(&self, channel_id: &[u8; 32]) -> bool {
705705
self.channel_by_id.contains_key(channel_id) ||
706706
self.outbound_v1_channel_by_id.contains_key(channel_id) ||
707-
self.inbound_v1_channel_by_id.contains_key(channel_id)
707+
self.inbound_v1_channel_by_id.contains_key(channel_id) ||
708+
self.inbound_channel_request_by_id.contains_key(channel_id)
708709
}
709710
}
710711

712+
/// A not-yet-accepted inbound (from counterparty) channel. Once
713+
/// accepted, the parameters will be used to construct a channel.
714+
pub(super) struct InboundChannelRequest {
715+
/// The counterparty node id that initiated the channel open.
716+
pub counterparty_node_id: PublicKey,
717+
/// The original OpenChannel message.
718+
pub open_channel_msg: msgs::OpenChannel,
719+
}
720+
711721
/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
712722
/// actually ours and not some duplicate HTLC sent to us by a node along the route.
713723
///
@@ -2572,9 +2582,6 @@ where
25722582
// N.B. that we don't send any channel close event here: we
25732583
// don't have a user_channel_id, and we never sent any opening
25742584
// events anyway.
2575-
self.id_to_peer.lock().unwrap().remove(channel_id);
2576-
self.outbound_scid_aliases.lock().unwrap().remove(&chan.outbound_scid_alias);
2577-
self.short_to_chan_info.write().unwrap().remove(&chan.outbound_scid_alias);
25782585
(None, chan.counterparty_node_id)
25792586
} else {
25802587
return Err(APIError::ChannelUnavailable{ err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*channel_id), peer_node_id) });
@@ -5205,15 +5212,17 @@ where
52055212
let peer_state = &mut *peer_state_lock;
52065213
let is_only_peer_channel = peer_state.total_channel_count() == 1;
52075214

5208-
// Find (and remove) the channel in the unaccepted table. If it's
5209-
// not there, something weird is happening and return an error.
5215+
// Find (and remove) the channel in the unaccepted table. If it's not there, something weird is
5216+
// happening and return an error. N.B. that we create channel with an outbound SCID of zero so
5217+
// that we can delay allocating the SCID until after we're sure that the checks below will
5218+
// succeed.
52105219
let mut channel = match peer_state.inbound_channel_request_by_id.remove(temporary_channel_id) {
52115220
Some(unaccepted_channel) => {
52125221
let best_block_height = self.best_block.read().unwrap().height();
52135222
InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
52145223
counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features,
52155224
&unaccepted_channel.open_channel_msg, user_channel_id, &self.default_configuration, best_block_height,
5216-
&self.logger, unaccepted_channel.outbound_scid_alias).map_err(|e| APIError::ChannelUnavailable { err: e.to_string() })
5225+
&self.logger, /*outbound_scid_alias=*/0).map_err(|e| APIError::ChannelUnavailable { err: e.to_string() })
52175226
}
52185227
_ => Err(APIError::APIMisuseError { err: "No such channel awaiting to be accepted.".to_owned() })
52195228
}?;
@@ -5245,6 +5254,10 @@ where
52455254
}
52465255
}
52475256

5257+
// Now that we know we have a channel, assign an outbound SCID alias.
5258+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
5259+
channel.context.set_outbound_scid_alias(outbound_scid_alias);
5260+
52485261
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
52495262
node_id: channel.context.get_counterparty_node_id(),
52505263
msg: channel.accept_inbound_channel(user_channel_id),
@@ -5310,8 +5323,6 @@ where
53105323

53115324
let mut random_bytes = [0u8; 16];
53125325
random_bytes.copy_from_slice(&self.entropy_source.get_secure_random_bytes()[..16]);
5313-
let user_channel_id = u128::from_be_bytes(random_bytes);
5314-
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
53155326

53165327
// Get the number of peers with channels, but without funded ones. We don't care too much
53175328
// about peers that never open a channel, so we filter by peers that have at least one
@@ -5350,7 +5361,6 @@ where
53505361
let channel_id = msg.temporary_channel_id;
53515362
let channel_exists = peer_state.has_channel(&channel_id);
53525363
if channel_exists {
5353-
self.outbound_scid_aliases.lock().unwrap().remove(&outbound_scid_alias);
53545364
return Err(MsgHandleErrInternal::send_err_msg_no_close("temporary_channel_id collision for the same peer!".to_owned(), msg.temporary_channel_id.clone()));
53555365
}
53565366

@@ -5367,12 +5377,13 @@ where
53675377
peer_state.inbound_channel_request_by_id.insert(channel_id, InboundChannelRequest {
53685378
counterparty_node_id: *counterparty_node_id,
53695379
open_channel_msg: msg.clone(),
5370-
outbound_scid_alias,
53715380
});
53725381
return Ok(());
53735382
}
53745383

53755384
// Otherwise create the channel right now.
5385+
let user_channel_id = u128::from_be_bytes(random_bytes);
5386+
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
53765387
let mut channel = match InboundV1Channel::new(&self.fee_estimator, &self.entropy_source, &self.signer_provider,
53775388
counterparty_node_id.clone(), &self.channel_type_features(), &peer_state.latest_features, msg, user_channel_id,
53785389
&self.default_configuration, best_block_height, &self.logger, outbound_scid_alias)

0 commit comments

Comments
 (0)