@@ -4230,55 +4230,27 @@ where
4230
4230
}
4231
4231
4232
4232
fn internal_funding_created ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingCreated ) -> Result < ( ) , MsgHandleErrInternal > {
4233
+ let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4234
+
4233
4235
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
4234
4236
let peer_state_mutex_opt = per_peer_state. get ( counterparty_node_id) ;
4235
4237
if let None = peer_state_mutex_opt {
4236
4238
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( format ! ( "Can't find a peer matching the passed counterparty node_id {}" , counterparty_node_id) , msg. temporary_channel_id ) )
4237
4239
}
4238
- let ( ( funding_msg , monitor , mut channel_ready ) , mut chan ) = {
4239
- let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4240
- let mut peer_state_lock = peer_state_mutex_opt . unwrap ( ) . lock ( ) . unwrap ( ) ;
4241
- let peer_state = & mut * peer_state_lock ;
4240
+
4241
+ let mut peer_state_lock = peer_state_mutex_opt . unwrap ( ) . lock ( ) . unwrap ( ) ;
4242
+ let peer_state = & mut * peer_state_lock ;
4243
+ let ( ( funding_msg , monitor ) , chan ) =
4242
4244
match peer_state. channel_by_id . entry ( msg. temporary_channel_id ) {
4243
4245
hash_map:: Entry :: Occupied ( mut chan) => {
4244
4246
( try_chan_entry ! ( self , chan. get_mut( ) . funding_created( msg, best_block, & self . keys_manager, & self . logger) , chan) , chan. remove ( ) )
4245
4247
} ,
4246
4248
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( format ! ( "Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}" , counterparty_node_id) , msg. temporary_channel_id ) )
4247
- }
4248
- } ;
4249
- // Because we have exclusive ownership of the channel here we can release the peer_state
4250
- // lock before watch_channel
4251
- match self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) {
4252
- ChannelMonitorUpdateStatus :: Completed => { } ,
4253
- ChannelMonitorUpdateStatus :: PermanentFailure => {
4254
- // Note that we reply with the new channel_id in error messages if we gave up on the
4255
- // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4256
- // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4257
- // any messages referencing a previously-closed channel anyway.
4258
- // We do not propagate the monitor update to the user as it would be for a monitor
4259
- // that we didn't manage to store (and that we don't care about - we don't respond
4260
- // with the funding_signed so the channel can never go on chain).
4261
- let ( _monitor_update, failed_htlcs) = chan. force_shutdown ( false ) ;
4262
- assert ! ( failed_htlcs. is_empty( ) ) ;
4263
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "ChannelMonitor storage failure" . to_owned ( ) , funding_msg. channel_id ) ) ;
4264
- } ,
4265
- ChannelMonitorUpdateStatus :: InProgress => {
4266
- // There's no problem signing a counterparty's funding transaction if our monitor
4267
- // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4268
- // accepted payment from yet. We do, however, need to wait to send our channel_ready
4269
- // until we have persisted our monitor.
4270
- chan. monitor_updating_paused ( false , false , channel_ready. is_some ( ) , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4271
- channel_ready = None ; // Don't send the channel_ready now
4272
- } ,
4273
- }
4274
- // It's safe to unwrap as we've held the `per_peer_state` read lock since checking that the
4275
- // peer exists, despite the inner PeerState potentially having no channels after removing
4276
- // the channel above.
4277
- let mut peer_state_lock = peer_state_mutex_opt. unwrap ( ) . lock ( ) . unwrap ( ) ;
4278
- let peer_state = & mut * peer_state_lock;
4249
+ } ;
4250
+
4279
4251
match peer_state. channel_by_id . entry ( funding_msg. channel_id ) {
4280
4252
hash_map:: Entry :: Occupied ( _) => {
4281
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4253
+ Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4282
4254
} ,
4283
4255
hash_map:: Entry :: Vacant ( e) => {
4284
4256
let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
@@ -4292,17 +4264,34 @@ where
4292
4264
i_e. insert ( chan. get_counterparty_node_id ( ) ) ;
4293
4265
}
4294
4266
}
4267
+
4268
+ // There's no problem signing a counterparty's funding transaction if our monitor
4269
+ // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4270
+ // accepted payment from yet. We do, however, need to wait to send our channel_ready
4271
+ // until we have persisted our monitor.
4295
4272
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
4296
4273
node_id : counterparty_node_id. clone ( ) ,
4297
4274
msg : funding_msg,
4298
4275
} ) ;
4299
- if let Some ( msg) = channel_ready {
4300
- send_channel_ready ! ( self , peer_state. pending_msg_events, chan, msg) ;
4276
+
4277
+ let monitor_res = self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) ;
4278
+
4279
+ let chan = e. insert ( chan) ;
4280
+ let mut res = handle_new_monitor_update ! ( self , monitor_res, 0 , peer_state_lock, peer_state, chan, MANUALLY_REMOVING , 42 ) ;
4281
+
4282
+ // Note that we reply with the new channel_id in error messages if we gave up on the
4283
+ // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4284
+ // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4285
+ // any messages referencing a previously-closed channel anyway.
4286
+ // We do not propagate the monitor update to the user as it would be for a monitor
4287
+ // that we didn't manage to store (and that we don't care about - we don't respond
4288
+ // with the funding_signed so the channel can never go on chain).
4289
+ if let Err ( MsgHandleErrInternal { shutdown_finish : Some ( ( res, _) ) , .. } ) = & mut res {
4290
+ res. 0 = None ;
4301
4291
}
4302
- e . insert ( chan ) ;
4292
+ res
4303
4293
}
4304
4294
}
4305
- Ok ( ( ) )
4306
4295
}
4307
4296
4308
4297
fn internal_funding_signed ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
0 commit comments