Skip to content

Commit 0bdb607

Browse files
committed
Handle fallible commitment point when getting accept_channel
1 parent e092228 commit 0bdb607

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

lightning/src/ln/channel.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7761,6 +7761,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
77617761
pub(super) struct InboundV1Channel<SP: Deref> where SP::Target: SignerProvider {
77627762
pub context: ChannelContext<SP>,
77637763
pub unfunded_context: UnfundedChannelContext,
7764+
pub signer_pending_accept_channel: bool,
77647765
}
77657766

77667767
/// Fetches the [`ChannelTypeFeatures`] that will be used for a channel built from a given
@@ -7847,7 +7848,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
78477848
msg.push_msat,
78487849
msg.common_fields.clone(),
78497850
)?,
7850-
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 }
7851+
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
7852+
signer_pending_accept_channel: false,
78517853
};
78527854
Ok(chan)
78537855
}
@@ -7856,7 +7858,9 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
78567858
/// should be sent back to the counterparty node.
78577859
///
78587860
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
7859-
pub fn accept_inbound_channel(&mut self) -> msgs::AcceptChannel {
7861+
pub fn accept_inbound_channel<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
7862+
where L::Target: Logger
7863+
{
78607864
if self.context.is_outbound() {
78617865
panic!("Tried to send accept_channel for an outbound channel?");
78627866
}
@@ -7870,20 +7874,31 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
78707874
panic!("Tried to send an accept_channel for a channel that has already advanced");
78717875
}
78727876

7873-
self.generate_accept_channel_message()
7877+
self.generate_accept_channel_message(logger)
78747878
}
78757879

78767880
/// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
78777881
/// inbound channel. If the intention is to accept an inbound channel, use
78787882
/// [`InboundV1Channel::accept_inbound_channel`] instead.
78797883
///
78807884
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
7881-
fn generate_accept_channel_message(&self) -> msgs::AcceptChannel {
7882-
debug_assert!(self.context.holder_commitment_point.is_available());
7883-
let first_per_commitment_point = self.context.holder_commitment_point.current_point().expect("TODO");
7885+
fn generate_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
7886+
where L::Target: Logger
7887+
{
7888+
// Note: another option here is to make commitment point a parameter of this function
7889+
// and make a helper method get_point_for_open_channel to check + set signer_pending_open_channel
7890+
// and call that right before anytime we call this function, so this function can remain
7891+
// side-effect free.
7892+
let first_per_commitment_point = if let Some(point) = self.context.holder_commitment_point.current_point() {
7893+
point
7894+
} else {
7895+
log_trace!(logger, "Unable to generate accept_channel message, waiting for commitment point");
7896+
self.signer_pending_accept_channel = true;
7897+
return None;
7898+
};
78847899
let keys = self.context.get_holder_pubkeys();
78857900

7886-
msgs::AcceptChannel {
7901+
Some(msgs::AcceptChannel {
78877902
common_fields: msgs::CommonAcceptChannelFields {
78887903
temporary_channel_id: self.context.channel_id,
78897904
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
@@ -7907,16 +7922,18 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
79077922
channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
79087923
#[cfg(taproot)]
79097924
next_local_nonce: None,
7910-
}
7925+
})
79117926
}
79127927

79137928
/// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
79147929
/// inbound channel without accepting it.
79157930
///
79167931
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
79177932
#[cfg(test)]
7918-
pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
7919-
self.generate_accept_channel_message()
7933+
pub fn get_accept_channel_message<L: Deref>(&mut self, logger: &L) -> Option<msgs::AcceptChannel>
7934+
where L::Target: Logger
7935+
{
7936+
self.generate_accept_channel_message(logger)
79207937
}
79217938

79227939
fn check_funding_created_signature<L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
@@ -9557,7 +9574,7 @@ mod tests {
95579574
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();
95589575

95599576
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
9560-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
9577+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
95619578
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
95629579
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
95639580
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -9689,7 +9706,7 @@ mod tests {
96899706
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();
96909707

96919708
// Node B --> Node A: accept channel
9692-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
9709+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
96939710
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
96949711

96959712
// Node A --> Node B: funding created
@@ -9876,7 +9893,7 @@ mod tests {
98769893
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();
98779894

98789895
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
9879-
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
9896+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
98809897
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
98819898
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
98829899
node_a_chan.context.holder_dust_limit_satoshis = 1560;
@@ -9945,11 +9962,11 @@ mod tests {
99459962
let mut outbound_chan = OutboundV1Channel::<&TestKeysInterface>::new(
99469963
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &features, 10000000, 100000, 42, &config, 0, 42, None, &&logger
99479964
).unwrap();
9948-
let inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
9965+
let mut inbound_chan = InboundV1Channel::<&TestKeysInterface>::new(
99499966
&feeest, &&keys_provider, &&keys_provider, node_b_node_id, &channelmanager::provided_channel_type_features(&config),
99509967
&features, &outbound_chan.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap(), 7, &config, 0, &&logger, false
99519968
).unwrap();
9952-
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(), &config.channel_handshake_limits, &features).unwrap();
9969+
outbound_chan.accept_channel(&inbound_chan.get_accept_channel_message(&&logger).unwrap(), &config.channel_handshake_limits, &features).unwrap();
99539970
let tx = Transaction { version: Version::ONE, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
99549971
value: Amount::from_sat(10000000), script_pubkey: outbound_chan.context.get_funding_redeemscript(),
99559972
}]};
@@ -10999,13 +11016,13 @@ mod tests {
1099911016

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

11002-
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
11019+
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
1100311020
&fee_estimator, &&keys_provider, &&keys_provider, node_id_a,
1100411021
&channelmanager::provided_channel_type_features(&config), &channelmanager::provided_init_features(&config),
1100511022
&open_channel_msg, 7, &config, 0, &&logger, /*is_0conf=*/false
1100611023
).unwrap();
1100711024

11008-
let mut accept_channel_msg = channel_b.get_accept_channel_message();
11025+
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
1100911026
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
1101011027

1101111028
let res = channel_a.accept_channel(
@@ -11065,7 +11082,7 @@ mod tests {
1106511082
true, // Allow node b to send a 0conf channel_ready.
1106611083
).unwrap();
1106711084

11068-
let accept_channel_msg = node_b_chan.accept_inbound_channel();
11085+
let accept_channel_msg = node_b_chan.accept_inbound_channel(&&logger).unwrap();
1106911086
node_a_chan.accept_channel(
1107011087
&accept_channel_msg,
1107111088
&config.channel_handshake_limits,

lightning/src/ln/channelmanager.rs

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

6756-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6757-
node_id: channel.context.get_counterparty_node_id(),
6758-
msg: channel.accept_inbound_channel(),
6759-
});
6756+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
6757+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
6758+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6759+
node_id: channel.context.get_counterparty_node_id(),
6760+
msg,
6761+
});
6762+
}
67606763

67616764
peer_state.channel_by_id.insert(temporary_channel_id.clone(), ChannelPhase::UnfundedInboundV1(channel));
67626765

@@ -6941,10 +6944,13 @@ where
69416944
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
69426945
channel.context.set_outbound_scid_alias(outbound_scid_alias);
69436946

6944-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6945-
node_id: counterparty_node_id.clone(),
6946-
msg: channel.accept_inbound_channel(),
6947-
});
6947+
let logger = WithChannelContext::from(&self.logger, &channel.context, None);
6948+
if let Some(msg) = channel.accept_inbound_channel(&&logger) {
6949+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
6950+
node_id: counterparty_node_id.clone(),
6951+
msg,
6952+
});
6953+
}
69486954
peer_state.channel_by_id.insert(channel_id, ChannelPhase::UnfundedInboundV1(channel));
69496955
Ok(())
69506956
}

0 commit comments

Comments
 (0)