@@ -688,30 +688,6 @@ pub(super) struct ChannelHolder<Signer: Sign> {
688
688
pub ( super ) pending_msg_events : Vec < MessageSendEvent > ,
689
689
}
690
690
691
- /// Holds the information required for a user to respond to an open channel request, when the
692
- /// manually_accept_inbound_channels config flag is set to true.
693
- #[ derive( Clone , Debug ) ]
694
- pub struct OpenChannelRequestInfo {
695
- /// The public key of the peer node which is requesting to open a channel.
696
- pub counterparty_node_id : PublicKey ,
697
- /// The init features sent in the init message.
698
- pub their_features : InitFeatures ,
699
- /// The Open channel message sent by the peer when requesting to open the channel.
700
- pub msg : msgs:: OpenChannel
701
- }
702
-
703
- impl Writeable for OpenChannelRequestInfo {
704
- fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
705
- 11u8 . write ( writer) ?;
706
- write_tlv_fields ! ( writer, {
707
- ( 0 , self . counterparty_node_id, required) ,
708
- ( 2 , self . their_features, required) ,
709
- ( 4 , self . msg, required) ,
710
- } ) ;
711
- Ok ( ( ) )
712
- }
713
- }
714
-
715
691
/// Events which we process internally but cannot be procsesed immediately at the generation site
716
692
/// for some reason. They are handled in timer_tick_occurred, so may be processed with
717
693
/// quite some time lag.
@@ -4128,24 +4104,27 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4128
4104
/// Called to respond to a request to open a channel after the
4129
4105
/// [`events::Event::OpenChannelRequest`] has been triggered.
4130
4106
///
4131
- /// If ' accept_channel' is set to true, the requested channel will be opened. If
4132
- /// ' accept_channel' is false, the open channel request will be rejected.
4107
+ /// If ` accept_channel` is set to true, the requested channel will be opened. If
4108
+ /// ` accept_channel` is false, the open channel request will be rejected.
4133
4109
///
4134
- /// The 'open_channel_request_info' parameter in the [`events::Event::OpenChannelRequest`]
4135
- /// event, should be directly passed as input to this function.
4110
+ /// The `temporary_channel_id` parameter indicates which channel the reponse is for.
4136
4111
///
4137
4112
/// [`events::Event::OpenChannelRequest`]: crate::util::events::Event::OpenChannelRequest
4138
- pub fn respond_to_open_channel_request ( & self , accept_channel : bool , open_channel_request_info : OpenChannelRequestInfo ) {
4113
+ pub fn respond_to_open_channel_request ( & self , accept_channel : bool , temporary_channel_id : [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
4139
4114
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
4140
- match accept_channel {
4141
- true => {
4142
- let _ = handle_error ! ( self , self . internal_create_requested_channel( & open_channel_request_info. counterparty_node_id, open_channel_request_info. their_features, & open_channel_request_info. msg) , open_channel_request_info. counterparty_node_id) ;
4143
- }
4144
- false => {
4145
- let err_res: Result < ( ) , MsgHandleErrInternal > = Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "The peer rejected the request to open the channel" . to_owned ( ) , open_channel_request_info. msg . temporary_channel_id . clone ( ) ) ) ;
4146
- let _ = handle_error ! ( self , err_res, open_channel_request_info. counterparty_node_id) ;
4115
+
4116
+ if let Some ( counterparty_node_id) = self . internal_get_counterparty_node_id ( temporary_channel_id) {
4117
+ match accept_channel {
4118
+ true => {
4119
+ return self . accept_requested_channel ( temporary_channel_id) ;
4120
+ }
4121
+ false => {
4122
+ let err_res: Result < ( ) , MsgHandleErrInternal > = Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "The peer rejected the request to open the channel" . to_owned ( ) , temporary_channel_id) ) ;
4123
+ let _ = handle_error ! ( self , err_res, counterparty_node_id) ;
4124
+ return Ok ( ( ) ) ;
4125
+ }
4147
4126
}
4148
- }
4127
+ } else { return Err ( APIError :: ChannelUnavailable { err : "No channel with the inputted temporary_channel_id found" . to_owned ( ) } ) ; }
4149
4128
}
4150
4129
4151
4130
fn internal_open_channel ( & self , counterparty_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: OpenChannel ) -> Result < ( ) , MsgHandleErrInternal > {
@@ -4157,42 +4136,69 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4157
4136
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "No inbound channels accepted" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) ) ;
4158
4137
}
4159
4138
4160
- if self . default_configuration . manually_accept_inbound_channels {
4161
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
4162
- pending_events. push (
4163
- events:: Event :: OpenChannelRequest {
4164
- open_channel_request_info : OpenChannelRequestInfo {
4165
- counterparty_node_id : counterparty_node_id. clone ( ) ,
4166
- their_features : their_features. clone ( ) ,
4167
- msg : msg. clone ( )
4168
- } ,
4169
- }
4170
- ) ;
4171
- return Ok ( ( ) )
4172
- }
4173
-
4174
- return self . internal_create_requested_channel ( counterparty_node_id, their_features, msg) ;
4175
- }
4176
-
4177
- fn internal_create_requested_channel ( & self , counterparty_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: OpenChannel ) -> Result < ( ) , MsgHandleErrInternal > {
4178
- let channel = Channel :: new_from_req ( & self . fee_estimator , & self . keys_manager , counterparty_node_id. clone ( ) ,
4139
+ let mut channel = Channel :: new_from_req ( & self . fee_estimator , & self . keys_manager , counterparty_node_id. clone ( ) ,
4179
4140
& their_features, msg, 0 , & self . default_configuration , self . best_block . read ( ) . unwrap ( ) . height ( ) , & self . logger )
4180
4141
. map_err ( |e| MsgHandleErrInternal :: from_chan_no_close ( e, msg. temporary_channel_id ) ) ?;
4181
4142
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4182
4143
let channel_state = & mut * channel_state_lock;
4183
4144
match channel_state. by_id . entry ( channel. channel_id ( ) ) {
4184
4145
hash_map:: Entry :: Occupied ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "temporary_channel_id collision!" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) ) ,
4185
4146
hash_map:: Entry :: Vacant ( entry) => {
4147
+ if !self . default_configuration . manually_accept_inbound_channels {
4148
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendAcceptChannel {
4149
+ node_id : counterparty_node_id. clone ( ) ,
4150
+ msg : channel. get_accept_channel ( ) ,
4151
+ } ) ;
4152
+ } else {
4153
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
4154
+ pending_events. push (
4155
+ events:: Event :: OpenChannelRequest {
4156
+ temporary_channel_id : msg. temporary_channel_id . clone ( ) ,
4157
+ counterparty_node_id : counterparty_node_id. clone ( ) ,
4158
+ chain_hash : msg. chain_hash . clone ( ) ,
4159
+ funding_satoshis : msg. funding_satoshis ,
4160
+ push_msat : msg. push_msat ,
4161
+ }
4162
+ ) ;
4163
+ }
4164
+
4165
+ entry. insert ( channel) ;
4166
+ }
4167
+ }
4168
+ Ok ( ( ) )
4169
+ }
4170
+
4171
+ fn accept_requested_channel ( & self , temporary_channel_id : [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
4172
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4173
+ let channel_state = & mut * channel_state_lock;
4174
+ match channel_state. by_id . entry ( temporary_channel_id) {
4175
+ hash_map:: Entry :: Occupied ( mut channel) => {
4176
+ if !channel. get ( ) . inbound_is_awaiting_accept ( ) {
4177
+ return Err ( APIError :: APIMisuseError { err : "The channel isn't currently awaiting to be accepted." . to_owned ( ) } ) ;
4178
+ }
4186
4179
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendAcceptChannel {
4187
- node_id : counterparty_node_id . clone ( ) ,
4188
- msg : channel. get_accept_channel ( ) ,
4180
+ node_id : channel . get ( ) . get_counterparty_node_id ( ) ,
4181
+ msg : channel. get_mut ( ) . get_accept_channel ( ) ,
4189
4182
} ) ;
4190
- entry. insert ( channel) ;
4183
+ }
4184
+ hash_map:: Entry :: Vacant ( _) => {
4185
+ return Err ( APIError :: ChannelUnavailable { err : "Can't accept a channel that doesn't exist" . to_owned ( ) } ) ;
4191
4186
}
4192
4187
}
4193
4188
Ok ( ( ) )
4194
4189
}
4195
4190
4191
+ fn internal_get_counterparty_node_id ( & self , channel_id : [ u8 ; 32 ] ) -> Option < PublicKey > {
4192
+ match self . channel_state . lock ( ) . unwrap ( ) . by_id . entry ( channel_id) {
4193
+ hash_map:: Entry :: Occupied ( channel) => {
4194
+ return Some ( channel. get ( ) . get_counterparty_node_id ( ) ) ;
4195
+ }
4196
+ hash_map:: Entry :: Vacant ( _) => {
4197
+ return None ;
4198
+ }
4199
+ }
4200
+ }
4201
+
4196
4202
fn internal_accept_channel ( & self , counterparty_node_id : & PublicKey , their_features : InitFeatures , msg : & msgs:: AcceptChannel ) -> Result < ( ) , MsgHandleErrInternal > {
4197
4203
let ( value, output_script, user_id) = {
4198
4204
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
0 commit comments