@@ -4743,50 +4743,24 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4743
4743
}
4744
4744
4745
4745
fn internal_funding_created ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingCreated ) -> Result < ( ) , MsgHandleErrInternal > {
4746
- let ( ( funding_msg , monitor , mut channel_ready ) , mut chan ) = {
4747
- let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4748
- let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4749
- let channel_state = & mut * channel_lock ;
4746
+ let best_block = * self . best_block . read ( ) . unwrap ( ) ;
4747
+ let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4748
+ let channel_state = & mut * channel_lock ;
4749
+ let ( ( funding_msg , monitor ) , chan ) =
4750
4750
match channel_state. by_id . entry ( msg. temporary_channel_id . clone ( ) ) {
4751
4751
hash_map:: Entry :: Occupied ( mut chan) => {
4752
4752
if chan. get ( ) . get_counterparty_node_id ( ) != * counterparty_node_id {
4753
4753
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. temporary_channel_id ) ) ;
4754
4754
}
4755
4755
( try_chan_entry ! ( self , chan. get_mut( ) . funding_created( msg, best_block, & self . logger) , chan) , chan. remove ( ) )
4756
+
4756
4757
} ,
4757
4758
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. temporary_channel_id ) )
4758
- }
4759
- } ;
4760
- // Because we have exclusive ownership of the channel here we can release the channel_state
4761
- // lock before watch_channel
4762
- match self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) {
4763
- ChannelMonitorUpdateStatus :: Completed => { } ,
4764
- ChannelMonitorUpdateStatus :: PermanentFailure => {
4765
- // Note that we reply with the new channel_id in error messages if we gave up on the
4766
- // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4767
- // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4768
- // any messages referencing a previously-closed channel anyway.
4769
- // We do not propagate the monitor update to the user as it would be for a monitor
4770
- // that we didn't manage to store (and that we don't care about - we don't respond
4771
- // with the funding_signed so the channel can never go on chain).
4772
- let ( _monitor_update, failed_htlcs) = chan. force_shutdown ( false ) ;
4773
- assert ! ( failed_htlcs. is_empty( ) ) ;
4774
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "ChannelMonitor storage failure" . to_owned ( ) , funding_msg. channel_id ) ) ;
4775
- } ,
4776
- ChannelMonitorUpdateStatus :: InProgress => {
4777
- // There's no problem signing a counterparty's funding transaction if our monitor
4778
- // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4779
- // accepted payment from yet. We do, however, need to wait to send our channel_ready
4780
- // until we have persisted our monitor.
4781
- chan. monitor_updating_paused ( false , false , channel_ready. is_some ( ) , Vec :: new ( ) , Vec :: new ( ) , Vec :: new ( ) ) ;
4782
- channel_ready = None ; // Don't send the channel_ready now
4783
- } ,
4784
- }
4785
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4786
- let channel_state = & mut * channel_state_lock;
4759
+ } ;
4760
+
4787
4761
match channel_state. by_id . entry ( funding_msg. channel_id ) {
4788
4762
hash_map:: Entry :: Occupied ( _) => {
4789
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4763
+ Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4790
4764
} ,
4791
4765
hash_map:: Entry :: Vacant ( e) => {
4792
4766
let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
@@ -4800,17 +4774,34 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4800
4774
i_e. insert ( chan. get_counterparty_node_id ( ) ) ;
4801
4775
}
4802
4776
}
4777
+
4778
+ // There's no problem signing a counterparty's funding transaction if our monitor
4779
+ // hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
4780
+ // accepted payment from yet. We do, however, need to wait to send our channel_ready
4781
+ // until we have persisted our monitor.
4803
4782
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
4804
4783
node_id : counterparty_node_id. clone ( ) ,
4805
4784
msg : funding_msg,
4806
4785
} ) ;
4807
- if let Some ( msg) = channel_ready {
4808
- send_channel_ready ! ( self , channel_state. pending_msg_events, chan, msg) ;
4786
+
4787
+ let monitor_res = self . chain_monitor . watch_channel ( monitor. get_funding_txo ( ) . 0 , monitor) ;
4788
+
4789
+ let chan = e. insert ( chan) ;
4790
+ let mut res = handle_new_monitor_update ! ( self , monitor_res, 0 , channel_lock, channel_state. pending_msg_events, chan, MANUALLY_REMOVING , 42 ) ;
4791
+
4792
+ // Note that we reply with the new channel_id in error messages if we gave up on the
4793
+ // channel, not the temporary_channel_id. This is compatible with ourselves, but the
4794
+ // spec is somewhat ambiguous here. Not a huge deal since we'll send error messages for
4795
+ // any messages referencing a previously-closed channel anyway.
4796
+ // We do not propagate the monitor update to the user as it would be for a monitor
4797
+ // that we didn't manage to store (and that we don't care about - we don't respond
4798
+ // with the funding_signed so the channel can never go on chain).
4799
+ if let Err ( MsgHandleErrInternal { shutdown_finish : Some ( ( res, _) ) , .. } ) = & mut res {
4800
+ res. 0 = None ;
4809
4801
}
4810
- e . insert ( chan ) ;
4802
+ res
4811
4803
}
4812
4804
}
4813
- Ok ( ( ) )
4814
4805
}
4815
4806
4816
4807
fn internal_funding_signed ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
0 commit comments