Skip to content

Commit 479661a

Browse files
committed
Handle fallible commitment point when getting accept_channel
1 parent b785712 commit 479661a

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

lightning/src/ln/channel.rs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7917,6 +7917,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
79177917
pub(super) struct InboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
79187918
pub context: ChannelContext<SP>,
79197919
pub unfunded_context: UnfundedChannelContext,
7920+
pub signer_pending_accept_channel: bool,
79207921
}
79217922

79227923
/// Fetches the [`ChannelTypeFeatures`] that will be used for a channel built from a given
@@ -8003,7 +8004,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80038004
msg.push_msat,
80048005
msg.common_fields.clone(),
80058006
)?,
8006-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
8007+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
8008+
signer_pending_accept_channel: false,
80078009
};
80088010
Ok(chan)
80098011
}
@@ -8012,7 +8014,9 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80128014
/// should be sent back to the counterparty node.
80138015
///
80148016
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
8015-
pub fn accept_inbound_channel(&mut self) -> msgs::AcceptChannel {
8017+
pub fn accept_inbound_channel<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
8018+
where L::Target: Logger
8019+
{
80168020
if self.context.is_outbound() {
80178021
panic!("Tried to send accept_channel for an outbound channel?");
80188022
}
@@ -8026,20 +8030,36 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80268030
panic!("Tried to send an accept_channel for a channel that has already advanced");
80278031
}
80288032

8029-
self.generate_accept_channel_message()
8033+
self.generate_accept_channel_message(logger)
80308034
}
80318035

80328036
/// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
80338037
/// inbound channel. If the intention is to accept an inbound channel, use
80348038
/// [`InboundV1Channel::accept_inbound_channel`] instead.
80358039
///
80368040
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
8037-
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
8038-
debug_assert!(self.context.holder_commitment_point.is_available());
8039-
let first_per_commitment_point = self.context.holder_commitment_point.current_point().expect("TODO");
8041+
fn generate_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
8042+
where L::Target: Logger
8043+
{
8044+
// Note: another option here is to make commitment point a parameter of this function
8045+
// and make a helper method get_point_for_open_channel to check + set signer_pending_open_channel
8046+
// and call that right before anytime we call this function, so this function can remain
8047+
// side-effect free.
8048+
let first_per_commitment_point = if let Some(point) = self.context.holder_commitment_point.current_point() {
8049+
point
8050+
} else {
8051+
#[cfg(not(async_signing))] {
8052+
panic!("Failed getting commitment point for accept_channel message");
8053+
}
8054+
#[cfg(async_signing)] {
8055+
log_trace!(logger, "Unable to generate accept_channel message, waiting for commitment point");
8056+
self.signer_pending_accept_channel = true;
8057+
return None;
8058+
}
8059+
};
80408060
let keys = self.context.get_holder_pubkeys();
80418061

8042-
msgs::AcceptChannel {
8062+
Some(msgs::AcceptChannel {
80438063
common_fields: msgs::CommonAcceptChannelFields {
80448064
temporary_channel_id: self.context.channel_id,
80458065
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
@@ -8063,16 +8083,18 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
80638083
channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
80648084
#[cfg(taproot)]
80658085
next_local_nonce: None,
8066-
}
8086+
})
80678087
}
80688088

80698089
/// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
80708090
/// inbound channel without accepting it.
80718091
///
80728092
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80738093
#[cfg(test)]
8074-
pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
8075-
self.generate_accept_channel_message()
8094+
pub fn get_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
8095+
where L::Target: Logger
8096+
{
8097+
self.generate_accept_channel_message(logger)
80768098
}
80778099

80788100
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
@@ -9716,7 +9738,7 @@ mod tests {
97169738
let mut node_b_chan = InboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false).unwrap();
97179739

97189740
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
9719-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
9741+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
97209742
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
97219743
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
97229744
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -9848,7 +9870,7 @@ mod tests {
98489870
let mut node_b_chan = InboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false).unwrap();
98499871

98509872
// Node B --> Node A: accept channel
9851-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
9873+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
98529874
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
98539875

98549876
// Node A --> Node B: funding created
@@ -10035,7 +10057,7 @@ mod tests {
1003510057
let mut node_b_chan = InboundV1Channel::<&TestKeysInterface>::new(&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config), &open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false).unwrap();
1003610058

1003710059
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
10038-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
10060+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
1003910061
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
1004010062
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
1004110063
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -10104,11 +10126,11 @@ mod tests {
1010410126
let mut outbound_chan = OutboundV1Channel::<&TestKeysInterface>::new(
1010510127
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &features, 10000000, 100000, 42, &config, 0, 42, None, &&logger
1010610128
).unwrap();
10107-
let inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
10129+
let mut inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
1010810130
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config),
1010910131
&features, &outbound_chan.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap(), 7, &config, 0, &&logger, false
1011010132
).unwrap();
10111-
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(), &config.channel_handshake_limits, &features).unwrap();
10133+
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(&&logger).unwrap(), &config.channel_handshake_limits, &features).unwrap();
1011210134
let tx = Transaction { version: Version::ONE, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
1011310135
value: Amount::from_sat(10000000), script_pubkey: outbound_chan.context.get_funding_redeemscript(),
1011410136
}]};
@@ -11158,13 +11180,13 @@ mod tests {
1115811180

1115911181
let open_channel_msg = channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
1116011182

11161-
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
11183+
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
1116211184
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
1116311185
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
1116411186
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
1116511187
).unwrap();
1116611188

11167-
let mut accept_channel_msg = channel_b.get_accept_channel_message();
11189+
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
1116811190
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
1116911191

1117011192
let res = channel_a.accept_channel(
@@ -11224,7 +11246,7 @@ mod tests {
1122411246
true, // Allow node b to send a 0conf channel_ready.
1122511247
).unwrap();
1122611248

11227-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
11249+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
1122811250
node_a_chan.accept_channel(
1122911251
&accept_channel_msg,
1123011252
&config.channel_handshake_limits,

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6843,10 +6843,13 @@ where
68436843
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
68446844
channel.context.set_outbound_scid_alias(outbound_scid_alias);
68456845

6846-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6847-
node_id: channel.context.get_counterparty_node_id(),
6848-
msg: channel.accept_inbound_channel(),
6849-
});
6846+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
6847+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
6848+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6849+
node_id: channel.context.get_counterparty_node_id(),
6850+
msg,
6851+
});
6852+
}
68506853

68516854
peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
68526855

@@ -7031,10 +7034,13 @@ where
70317034
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
70327035
channel.context.set_outbound_scid_alias(outbound_scid_alias);
70337036

7034-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
7035-
node_id: counterparty_node_id.clone(),
7036-
msg: channel.accept_inbound_channel(),
7037-
});
7037+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
7038+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
7039+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
7040+
node_id: counterparty_node_id.clone(),
7041+
msg,
7042+
});
7043+
}
70387044
peer_state.channel_by_id.insert(channel_id, ChannelPhase::UnfundedInboundV1(channel));
70397045
Ok(())
70407046
}

0 commit comments

Comments
 (0)