Skip to content

Commit 37feaac

Browse files
committed
Split generating AcceptChannel msgs and the accept
1 parent 366593e commit 37feaac

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,15 @@ pub(super) struct Channel<Signer: Sign> {
536536
#[cfg(not(test))]
537537
closing_fee_limits: Option<(u64, u64)>,
538538

539-
/// Flag that ensures that the `get_accept_channel` function must be called before the
539+
/// Flag that ensures that the `accept_inbound_channel` function must be called before the
540540
/// `funding_created` function is executed successfully. The reason for this flag is that
541541
/// when the util::config::UserConfig.manually_accept_inbound_channels is set to true,
542542
/// inbound channels are required to be manually accepted by the node operator before the
543-
/// `SendAcceptChannel` message is created and sent out. The `get_accept_channel` function is
543+
/// `SendAcceptChannel` message is created and sent out. The `accept_inbound_channel` function is
544544
/// called during that process.
545545
/// A counterparty node could theoretically send a `FundingCreated` message before the node
546546
/// operator has accepted the inbound channel. That would would execute the `funding_created`
547-
/// function before the `get_accept_channel` function, and should therefore be rejected.
547+
/// function before the `accept_inbound_channel` function, and should therefore be rejected.
548548
inbound_awaiting_accept: bool,
549549

550550
/// The hash of the block in which the funding transaction was included.
@@ -4526,7 +4526,11 @@ impl<Signer: Sign> Channel<Signer> {
45264526
self.inbound_awaiting_accept
45274527
}
45284528

4529-
pub fn get_accept_channel(&mut self) -> msgs::AcceptChannel {
4529+
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
4530+
/// should be sent back to the counterparty node.
4531+
///
4532+
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4533+
pub fn accept_inbound_channel(&mut self) -> msgs::AcceptChannel {
45304534
if self.is_outbound() {
45314535
panic!("Tried to send accept_channel for an outbound channel?");
45324536
}
@@ -4536,9 +4540,22 @@ impl<Signer: Sign> Channel<Signer> {
45364540
if self.cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
45374541
panic!("Tried to send an accept_channel for a channel that has already advanced");
45384542
}
4543+
if !self.inbound_awaiting_accept {
4544+
panic!("The inbound channel has already been accepted");
4545+
}
45394546

45404547
self.inbound_awaiting_accept = false;
45414548

4549+
return self.get_accept_channel_message();
4550+
}
4551+
4552+
/// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
4553+
/// inbound channel. If the intention is to accept an inbound channel, you should use the
4554+
/// [`Channel::accept_inbound_channel`] function instead.
4555+
///
4556+
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4557+
/// [`Channel::accept_inbound_channel`]: crate::ln::channel::Channel::accept_inbound_channel
4558+
pub fn get_accept_channel_message(&self) -> msgs::AcceptChannel {
45424559
let first_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number, &self.secp_ctx);
45434560
let keys = self.get_holder_pubkeys();
45444561

@@ -6079,7 +6096,7 @@ mod tests {
60796096
let mut node_b_chan = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, &InitFeatures::known(), &open_channel_msg, 7, &config, 0, &&logger).unwrap();
60806097

60816098
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
6082-
let mut accept_channel_msg = node_b_chan.get_accept_channel();
6099+
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
60836100
accept_channel_msg.dust_limit_satoshis = 546;
60846101
node_a_chan.accept_channel(&accept_channel_msg, &config, &InitFeatures::known()).unwrap();
60856102
node_a_chan.holder_dust_limit_satoshis = 1560;
@@ -6197,7 +6214,7 @@ mod tests {
61976214
let mut node_b_chan = Channel::<EnforcingSigner>::new_from_req(&&feeest, &&keys_provider, node_b_node_id, &InitFeatures::known(), &open_channel_msg, 7, &config, 0, &&logger).unwrap();
61986215

61996216
// Node B --> Node A: accept channel
6200-
let accept_channel_msg = node_b_chan.get_accept_channel();
6217+
let accept_channel_msg = node_b_chan.accept_inbound_channel();
62016218
node_a_chan.accept_channel(&accept_channel_msg, &config, &InitFeatures::known()).unwrap();
62026219

62036220
// Node A --> Node B: funding created

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,7 +4119,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41194119
}
41204120
channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
41214121
node_id: channel.get().get_counterparty_node_id(),
4122-
msg: channel.get_mut().get_accept_channel(),
4122+
msg: channel.get_mut().accept_inbound_channel(),
41234123
});
41244124
}
41254125
hash_map::Entry::Vacant(_) => {
@@ -4149,7 +4149,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41494149
if !self.default_configuration.manually_accept_inbound_channels {
41504150
channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
41514151
node_id: counterparty_node_id.clone(),
4152-
msg: channel.get_accept_channel(),
4152+
msg: channel.accept_inbound_channel(),
41534153
});
41544154
} else {
41554155
let mut pending_events = self.pending_events.lock().unwrap();

0 commit comments

Comments
 (0)