@@ -4352,60 +4352,31 @@ where
4352
4352
}
4353
4353
4354
4354
fn internal_funding_created ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingCreated ) -> Result < ( ) , MsgHandleErrInternal > {
4355
+ let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4356
+
4355
4357
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
4356
4358
let peer_state_mutex = per_peer_state. get ( counterparty_node_id)
4357
4359
. ok_or_else ( || {
4358
4360
debug_assert ! ( false ) ;
4359
4361
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 )
4360
4362
} ) ?;
4361
- let ( ( funding_msg , monitor , mut channel_ready ) , mut chan ) = {
4362
- let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4363
- let mut peer_state_lock = peer_state_mutex . lock ( ) . unwrap ( ) ;
4364
- let peer_state = & mut * peer_state_lock ;
4363
+
4364
+ let mut peer_state_lock = peer_state_mutex . lock ( ) . unwrap ( ) ;
4365
+ let peer_state = & mut * peer_state_lock ;
4366
+ let ( ( funding_msg , monitor ) , chan ) =
4365
4367
match peer_state. channel_by_id . entry ( msg. temporary_channel_id ) {
4366
4368
hash_map:: Entry :: Occupied ( mut chan) => {
4367
4369
( try_chan_entry ! ( self , chan. get_mut( ) . funding_created( msg, best_block, & self . signer_provider, & self . logger) , chan) , chan. remove ( ) )
4368
4370
} ,
4369
4371
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 ) )
4370
- }
4371
- } ;
4372
- // Because we have exclusive ownership of the channel here we can release the peer_state
4373
- // lock before watch_channel
4374
- match self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) {
4375
- ChannelMonitorUpdateStatus :: Completed => { } ,
4376
- ChannelMonitorUpdateStatus :: PermanentFailure => {
4377
- // Note that we reply with the new channel_id in error messages if we gave up on the
4378
- // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4379
- // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4380
- // any messages referencing a previously-closed channel anyway.
4381
- // We do not propagate the monitor update to the user as it would be for a monitor
4382
- // that we didn't manage to store (and that we don't care about - we don't respond
4383
- // with the funding_signed so the channel can never go on chain).
4384
- let ( _monitor_update, failed_htlcs) = chan. force_shutdown ( false ) ;
4385
- assert ! ( failed_htlcs. is_empty( ) ) ;
4386
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "ChannelMonitor storage failure" . to_owned ( ) , funding_msg. channel_id ) ) ;
4387
- } ,
4388
- ChannelMonitorUpdateStatus :: InProgress => {
4389
- // There's no problem signing a counterparty's funding transaction if our monitor
4390
- // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4391
- // accepted payment from yet. We do, however, need to wait to send our channel_ready
4392
- // until we have persisted our monitor.
4393
- chan. monitor_updating_paused ( false , false , channel_ready. is_some ( ) , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4394
- channel_ready = None ; // Don't send the channel_ready now
4395
- } ,
4396
- }
4397
- // It's safe to unwrap as we've held the `per_peer_state` read lock since checking that the
4398
- // peer exists, despite the inner PeerState potentially having no channels after removing
4399
- // the channel above.
4400
- let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
4401
- let peer_state = & mut * peer_state_lock;
4372
+ } ;
4373
+
4402
4374
match peer_state. channel_by_id . entry ( funding_msg. channel_id ) {
4403
4375
hash_map:: Entry :: Occupied ( _) => {
4404
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4376
+ Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4405
4377
} ,
4406
4378
hash_map:: Entry :: Vacant ( e) => {
4407
- let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
4408
- match id_to_peer. entry ( chan. channel_id ( ) ) {
4379
+ match self . id_to_peer . lock ( ) . unwrap ( ) . entry ( chan. channel_id ( ) ) {
4409
4380
hash_map:: Entry :: Occupied ( _) => {
4410
4381
return Err ( MsgHandleErrInternal :: send_err_msg_no_close (
4411
4382
"The funding_created message had the same funding_txid as an existing channel - funding is not possible" . to_owned ( ) ,
@@ -4415,17 +4386,35 @@ where
4415
4386
i_e. insert ( chan. get_counterparty_node_id ( ) ) ;
4416
4387
}
4417
4388
}
4389
+
4390
+ // There's no problem signing a counterparty's funding transaction if our monitor
4391
+ // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4392
+ // accepted payment from yet. We do, however, need to wait to send our channel_ready
4393
+ // until we have persisted our monitor.
4394
+ let new_channel_id = funding_msg. channel_id ;
4418
4395
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
4419
4396
node_id : counterparty_node_id. clone ( ) ,
4420
4397
msg : funding_msg,
4421
4398
} ) ;
4422
- if let Some ( msg) = channel_ready {
4423
- send_channel_ready ! ( self , peer_state. pending_msg_events, chan, msg) ;
4399
+
4400
+ let monitor_res = self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) ;
4401
+
4402
+ let chan = e. insert ( chan) ;
4403
+ let mut res = handle_new_monitor_update ! ( self , monitor_res, 0 , peer_state_lock, peer_state, chan, MANUALLY_REMOVING , { peer_state. channel_by_id. remove( & new_channel_id) } ) ;
4404
+
4405
+ // Note that we reply with the new channel_id in error messages if we gave up on the
4406
+ // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4407
+ // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4408
+ // any messages referencing a previously-closed channel anyway.
4409
+ // We do not propagate the monitor update to the user as it would be for a monitor
4410
+ // that we didn't manage to store (and that we don't care about - we don't respond
4411
+ // with the funding_signed so the channel can never go on chain).
4412
+ if let Err ( MsgHandleErrInternal { shutdown_finish : Some ( ( res, _) ) , .. } ) = & mut res {
4413
+ res. 0 = None ;
4424
4414
}
4425
- e . insert ( chan ) ;
4415
+ res
4426
4416
}
4427
4417
}
4428
- Ok ( ( ) )
4429
4418
}
4430
4419
4431
4420
fn internal_funding_signed ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
0 commit comments