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