@@ -536,6 +536,17 @@ 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
540+ /// `funding_created` function is executed successfully. The reason for need of this flag is
541+ /// that when the util::config::UserConfig.manually_accept_inbound_channels is set to true,
542+ /// 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
544+ /// called during that process.
545+ /// A counterparty node could theoretically send a `FundingCreated` message before the node
546+ /// 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.
548+ inbound_awaiting_accept : bool ,
549+
539550 /// The hash of the block in which the funding transaction was included.
540551 funding_tx_confirmed_in : Option < BlockHash > ,
541552 funding_tx_confirmation_height : u32 ,
@@ -833,6 +844,8 @@ impl<Signer: Sign> Channel<Signer> {
833844 closing_fee_limits : None ,
834845 target_closing_feerate_sats_per_kw : None ,
835846
847+ inbound_awaiting_accept : false ,
848+
836849 funding_tx_confirmed_in : None ,
837850 funding_tx_confirmation_height : 0 ,
838851 short_channel_id : None ,
@@ -1130,6 +1143,8 @@ impl<Signer: Sign> Channel<Signer> {
11301143 closing_fee_limits : None ,
11311144 target_closing_feerate_sats_per_kw : None ,
11321145
1146+ inbound_awaiting_accept : true ,
1147+
11331148 funding_tx_confirmed_in : None ,
11341149 funding_tx_confirmation_height : 0 ,
11351150 short_channel_id : None ,
@@ -1918,6 +1933,10 @@ impl<Signer: Sign> Channel<Signer> {
19181933 // channel.
19191934 return Err ( ChannelError :: Close ( "Received funding_created after we got the channel!" . to_owned ( ) ) ) ;
19201935 }
1936+ if self . inbound_awaiting_accept {
1937+ // The Channel must manually accepted before the FundingCreated is received.
1938+ return Err ( ChannelError :: Close ( "FundingCreated message received before the channel was accepted" . to_owned ( ) ) ) ;
1939+ }
19211940 if self . commitment_secrets . get_min_seen_secret ( ) != ( 1 << 48 ) ||
19221941 self . cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
19231942 self . cur_holder_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER {
@@ -4504,7 +4523,11 @@ impl<Signer: Sign> Channel<Signer> {
45044523 }
45054524 }
45064525
4507- pub fn get_accept_channel ( & self ) -> msgs:: AcceptChannel {
4526+ pub fn inbound_is_awaiting_accept ( & self ) -> bool {
4527+ self . inbound_awaiting_accept
4528+ }
4529+
4530+ pub fn get_accept_channel ( & mut self ) -> msgs:: AcceptChannel {
45084531 if self . is_outbound ( ) {
45094532 panic ! ( "Tried to send accept_channel for an outbound channel?" ) ;
45104533 }
@@ -4515,6 +4538,8 @@ impl<Signer: Sign> Channel<Signer> {
45154538 panic ! ( "Tried to send an accept_channel for a channel that has already advanced" ) ;
45164539 }
45174540
4541+ self . inbound_awaiting_accept = false ;
4542+
45184543 let first_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
45194544 let keys = self . get_holder_pubkeys ( ) ;
45204545
@@ -5840,6 +5865,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
58405865 closing_fee_limits : None ,
58415866 target_closing_feerate_sats_per_kw,
58425867
5868+ inbound_awaiting_accept : false ,
5869+
58435870 funding_tx_confirmed_in,
58445871 funding_tx_confirmation_height,
58455872 short_channel_id,
@@ -6050,7 +6077,7 @@ mod tests {
60506077 // Make sure A's dust limit is as we expect.
60516078 let open_channel_msg = node_a_chan. get_open_channel ( genesis_block ( network) . header . block_hash ( ) ) ;
60526079 let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
6053- 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 ( ) ;
6080+ 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 ( ) ;
60546081
60556082 // Node B --> Node A: accept channel, explicitly setting B's dust limit.
60566083 let mut accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
0 commit comments