Skip to content

Commit 881f868

Browse files
committed
Handle fallible commitment point when getting accept_channel
1 parent b453e03 commit 881f868

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
@@ -7851,6 +7851,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78517851
pub(super) struct InboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
78527852
pub context: ChannelContext<SP>,
78537853
pub unfunded_context: UnfundedChannelContext,
7854+
pub signer_pending_accept_channel: bool,
78547855
}
78557856

78567857
/// Fetches the [`ChannelTypeFeatures`] that will be used for a channel built from a given
@@ -7937,7 +7938,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79377938
msg.push_msat,
79387939
msg.common_fields.clone(),
79397940
)?,
7940-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
7941+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
7942+
signer_pending_accept_channel: false,
79417943
};
79427944
Ok(chan)
79437945
}
@@ -7946,7 +7948,9 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79467948
/// should be sent back to the counterparty node.
79477949
///
79487950
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
7949-
pub fn accept_inbound_channel(&mut self) -> msgs::AcceptChannel {
7951+
pub fn accept_inbound_channel<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
7952+
where L::Target: Logger
7953+
{
79507954
if self.context.is_outbound() {
79517955
panic!("Tried to send accept_channel for an outbound channel?");
79527956
}
@@ -7960,20 +7964,36 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79607964
panic!("Tried to send an accept_channel for a channel that has already advanced");
79617965
}
79627966

7963-
self.generate_accept_channel_message()
7967+
self.generate_accept_channel_message(logger)
79647968
}
79657969

79667970
/// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
79677971
/// inbound channel. If the intention is to accept an inbound channel, use
79687972
/// [`InboundV1Channel::accept_inbound_channel`] instead.
79697973
///
79707974
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
7971-
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
7972-
debug_assert!(self.context.holder_commitment_point.is_available());
7973-
let first_per_commitment_point = self.context.holder_commitment_point.current_point().expect("TODO");
7975+
fn generate_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
7976+
where L::Target: Logger
7977+
{
7978+
// Note: another option here is to make commitment point a parameter of this function
7979+
// and make a helper method get_point_for_open_channel to check + set signer_pending_open_channel
7980+
// and call that right before anytime we call this function, so this function can remain
7981+
// side-effect free.
7982+
let first_per_commitment_point = if let Some(point) = self.context.holder_commitment_point.current_point() {
7983+
point
7984+
} else {
7985+
#[cfg(not(async_signing))] {
7986+
panic!("Failed getting commitment point for accept_channel message");
7987+
}
7988+
#[cfg(async_signing)] {
7989+
log_trace!(logger, "Unable to generate accept_channel message, waiting for commitment point");
7990+
self.signer_pending_accept_channel = true;
7991+
return None;
7992+
}
7993+
};
79747994
let keys = self.context.get_holder_pubkeys();
79757995

7976-
msgs::AcceptChannel {
7996+
Some(msgs::AcceptChannel {
79777997
common_fields: msgs::CommonAcceptChannelFields {
79787998
temporary_channel_id: self.context.channel_id,
79797999
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
@@ -7997,16 +8017,18 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79978017
channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
79988018
#[cfg(taproot)]
79998019
next_local_nonce: None,
8000-
}
8020+
})
80018021
}
80028022

80038023
/// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
80048024
/// inbound channel without accepting it.
80058025
///
80068026
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
80078027
#[cfg(test)]
8008-
pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
8009-
self.generate_accept_channel_message()
8028+
pub fn get_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
8029+
where L::Target: Logger
8030+
{
8031+
self.generate_accept_channel_message(logger)
80108032
}
80118033

80128034
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
@@ -9649,7 +9671,7 @@ mod tests {
96499671
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();
96509672

96519673
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
9652-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
9674+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
96539675
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
96549676
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
96559677
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -9781,7 +9803,7 @@ mod tests {
97819803
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();
97829804

97839805
// Node B --> Node A: accept channel
9784-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
9806+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
97859807
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
97869808

97879809
// Node A --> Node B: funding created
@@ -9968,7 +9990,7 @@ mod tests {
99689990
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();
99699991

99709992
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
9971-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
9993+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
99729994
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
99739995
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
99749996
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -10037,11 +10059,11 @@ mod tests {
1003710059
let mut outbound_chan = OutboundV1Channel::<&TestKeysInterface>::new(
1003810060
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &features, 10000000, 100000, 42, &config, 0, 42, None, &&logger
1003910061
).unwrap();
10040-
let inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
10062+
let mut inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
1004110063
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config),
1004210064
&features, &outbound_chan.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap(), 7, &config, 0, &&logger, false
1004310065
).unwrap();
10044-
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(), &config.channel_handshake_limits, &features).unwrap();
10066+
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(&&logger).unwrap(), &config.channel_handshake_limits, &features).unwrap();
1004510067
let tx = Transaction { version: Version::ONE, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
1004610068
value: Amount::from_sat(10000000), script_pubkey: outbound_chan.context.get_funding_redeemscript(),
1004710069
}]};
@@ -11091,13 +11113,13 @@ mod tests {
1109111113

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

11094-
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
11116+
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
1109511117
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
1109611118
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
1109711119
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
1109811120
).unwrap();
1109911121

11100-
let mut accept_channel_msg = channel_b.get_accept_channel_message();
11122+
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
1110111123
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
1110211124

1110311125
let res = channel_a.accept_channel(
@@ -11157,7 +11179,7 @@ mod tests {
1115711179
true, // Allow node b to send a 0conf channel_ready.
1115811180
).unwrap();
1115911181

11160-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
11182+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
1116111183
node_a_chan.accept_channel(
1116211184
&accept_channel_msg,
1116311185
&config.channel_handshake_limits,

lightning/src/ln/channelmanager.rs

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

6793-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6794-
node_id: channel.context.get_counterparty_node_id(),
6795-
msg: channel.accept_inbound_channel(),
6796-
});
6793+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
6794+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
6795+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6796+
node_id: channel.context.get_counterparty_node_id(),
6797+
msg,
6798+
});
6799+
}
67976800

67986801
peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
67996802

@@ -6978,10 +6981,13 @@ where
69786981
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
69796982
channel.context.set_outbound_scid_alias(outbound_scid_alias);
69806983

6981-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6982-
node_id: counterparty_node_id.clone(),
6983-
msg: channel.accept_inbound_channel(),
6984-
});
6984+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
6985+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
6986+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6987+
node_id: counterparty_node_id.clone(),
6988+
msg,
6989+
});
6990+
}
69856991
peer_state.channel_by_id.insert(channel_id, ChannelPhase::UnfundedInboundV1(channel));
69866992
Ok(())
69876993
}

0 commit comments

Comments
 (0)