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