@@ -584,6 +584,19 @@ pub(super) struct Channel<Signer: Sign> {
584
584
#[ cfg( not( test) ) ]
585
585
closing_fee_limits : Option < ( u64 , u64 ) > ,
586
586
587
+ /// Flag that ensures that `accept_inbound_channel` must be called before `funding_created`
588
+ /// is executed successfully. The reason for this flag is that when the
589
+ /// `UserConfig::manually_accept_inbound_channels` config flag is set to true, inbound channels
590
+ /// are required to be manually accepted by the node operator before the `msgs::AcceptChannel`
591
+ /// message is created and sent out. During the manual accept process, `accept_inbound_channel`
592
+ /// is called by `ChannelManager::accept_inbound_channel`.
593
+ ///
594
+ /// The flag counteracts that a counterparty node could theoretically send a
595
+ /// `msgs::FundingCreated` message before the node operator has manually accepted an inbound
596
+ /// channel request made by the counterparty node. That would execute `funding_created` before
597
+ /// `accept_inbound_channel`, and `funding_created` should therefore not execute successfully.
598
+ inbound_awaiting_accept : bool ,
599
+
587
600
/// The hash of the block in which the funding transaction was included.
588
601
funding_tx_confirmed_in : Option < BlockHash > ,
589
602
funding_tx_confirmation_height : u32 ,
@@ -883,6 +896,8 @@ impl<Signer: Sign> Channel<Signer> {
883
896
closing_fee_limits : None ,
884
897
target_closing_feerate_sats_per_kw : None ,
885
898
899
+ inbound_awaiting_accept : false ,
900
+
886
901
funding_tx_confirmed_in : None ,
887
902
funding_tx_confirmation_height : 0 ,
888
903
short_channel_id : None ,
@@ -1182,6 +1197,8 @@ impl<Signer: Sign> Channel<Signer> {
1182
1197
closing_fee_limits : None ,
1183
1198
target_closing_feerate_sats_per_kw : None ,
1184
1199
1200
+ inbound_awaiting_accept : true ,
1201
+
1185
1202
funding_tx_confirmed_in : None ,
1186
1203
funding_tx_confirmation_height : 0 ,
1187
1204
short_channel_id : None ,
@@ -1973,6 +1990,9 @@ impl<Signer: Sign> Channel<Signer> {
1973
1990
// channel.
1974
1991
return Err ( ChannelError :: Close ( "Received funding_created after we got the channel!" . to_owned ( ) ) ) ;
1975
1992
}
1993
+ if self . inbound_awaiting_accept {
1994
+ return Err ( ChannelError :: Close ( "FundingCreated message received before the channel was accepted" . to_owned ( ) ) ) ;
1995
+ }
1976
1996
if self . commitment_secrets . get_min_seen_secret ( ) != ( 1 << 48 ) ||
1977
1997
self . cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
1978
1998
self . cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
@@ -4645,7 +4665,15 @@ impl<Signer: Sign> Channel<Signer> {
4645
4665
}
4646
4666
}
4647
4667
4648
- pub fn get_accept_channel ( & self ) -> msgs:: AcceptChannel {
4668
+ pub fn inbound_is_awaiting_accept ( & self ) -> bool {
4669
+ self . inbound_awaiting_accept
4670
+ }
4671
+
4672
+ /// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
4673
+ /// should be sent back to the counterparty node.
4674
+ ///
4675
+ /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4676
+ pub fn accept_inbound_channel ( & mut self ) -> msgs:: AcceptChannel {
4649
4677
if self . is_outbound ( ) {
4650
4678
panic ! ( "Tried to send accept_channel for an outbound channel?" ) ;
4651
4679
}
@@ -4655,7 +4683,21 @@ impl<Signer: Sign> Channel<Signer> {
4655
4683
if self . cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
4656
4684
panic ! ( "Tried to send an accept_channel for a channel that has already advanced" ) ;
4657
4685
}
4686
+ if !self . inbound_awaiting_accept {
4687
+ panic ! ( "The inbound channel has already been accepted" ) ;
4688
+ }
4689
+
4690
+ self . inbound_awaiting_accept = false ;
4658
4691
4692
+ self . generate_accept_channel_message ( )
4693
+ }
4694
+
4695
+ /// This function is used to explicitly generate a [`msgs::AcceptChannel`] message for an
4696
+ /// inbound channel. If the intention is to accept an inbound channel, use
4697
+ /// [`Channel::accept_inbound_channel`] instead.
4698
+ ///
4699
+ /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4700
+ fn generate_accept_channel_message ( & self ) -> msgs:: AcceptChannel {
4659
4701
let first_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
4660
4702
let keys = self . get_holder_pubkeys ( ) ;
4661
4703
@@ -4681,6 +4723,15 @@ impl<Signer: Sign> Channel<Signer> {
4681
4723
}
4682
4724
}
4683
4725
4726
+ /// Enables the possibility for tests to extract a [`msgs::AcceptChannel`] message for an
4727
+ /// inbound channel without accepting it.
4728
+ ///
4729
+ /// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
4730
+ #[ cfg( test) ]
4731
+ pub fn get_accept_channel_message ( & self ) -> msgs:: AcceptChannel {
4732
+ self . generate_accept_channel_message ( )
4733
+ }
4734
+
4684
4735
/// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
4685
4736
fn get_outbound_funding_created_signature < L : Deref > ( & mut self , logger : & L ) -> Result < Signature , ChannelError > where L :: Target : Logger {
4686
4737
let counterparty_keys = self . build_remote_transaction_keys ( ) ?;
@@ -6064,6 +6115,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
6064
6115
closing_fee_limits : None ,
6065
6116
target_closing_feerate_sats_per_kw,
6066
6117
6118
+ inbound_awaiting_accept : false ,
6119
+
6067
6120
funding_tx_confirmed_in,
6068
6121
funding_tx_confirmation_height,
6069
6122
short_channel_id,
@@ -6281,10 +6334,10 @@ mod tests {
6281
6334
// Make sure A's dust limit is as we expect.
6282
6335
let open_channel_msg = node_a_chan. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
6283
6336
let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
6284
- let 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 ( ) ;
6337
+ 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 ( ) ;
6285
6338
6286
6339
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
6287
- let mut accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
6340
+ let mut accept_channel_msg = node_b_chan. accept_inbound_channel ( ) ;
6288
6341
accept_channel_msg. dust_limit_satoshis = 546 ;
6289
6342
node_a_chan. accept_channel ( & accept_channel_msg, & config. peer_channel_config_limits , & InitFeatures :: known ( ) ) . unwrap ( ) ;
6290
6343
node_a_chan. holder_dust_limit_satoshis = 1560 ;
@@ -6402,7 +6455,7 @@ mod tests {
6402
6455
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 ( ) ;
6403
6456
6404
6457
// Node B --> Node A: accept channel
6405
- let accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
6458
+ let accept_channel_msg = node_b_chan. accept_inbound_channel ( ) ;
6406
6459
node_a_chan. accept_channel ( & accept_channel_msg, & config. peer_channel_config_limits , & InitFeatures :: known ( ) ) . unwrap ( ) ;
6407
6460
6408
6461
// Node A --> Node B: funding created
0 commit comments