@@ -247,6 +247,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
247247 #[ cfg( test) ]
248248 pub ( super ) local_keys : ChanSigner ,
249249 shutdown_pubkey : PublicKey ,
250+ destination_script : Script ,
250251
251252 // Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
252253 // generation start at 0 and count up...this simplifies some parts of implementation at the
@@ -351,7 +352,9 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
351352
352353 their_shutdown_scriptpubkey : Option < Script > ,
353354
354- channel_monitor : ChannelMonitor < ChanSigner > ,
355+ /// Used exclusively to broadcast the latest local state, mostly a historical quirk that this
356+ /// is here:
357+ channel_monitor : Option < ChannelMonitor < ChanSigner > > ,
355358 commitment_secrets : CounterpartyCommitmentSecrets ,
356359
357360 network_sync : UpdateStatus ,
@@ -456,26 +459,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
456459
457460 let feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
458461
459- let secp_ctx = Secp256k1 :: new ( ) ;
460- let channel_monitor = ChannelMonitor :: new ( chan_keys. clone ( ) ,
461- chan_keys. funding_key ( ) , chan_keys. revocation_base_key ( ) , chan_keys. delayed_payment_base_key ( ) ,
462- chan_keys. htlc_base_key ( ) , chan_keys. payment_base_key ( ) , & keys_provider. get_shutdown_pubkey ( ) , config. own_channel_config . our_to_self_delay ,
463- keys_provider. get_destination_script ( ) , logger. clone ( ) ) ;
464-
465462 Ok ( Channel {
466463 user_id : user_id,
467464 config : config. channel_options . clone ( ) ,
468465
469466 channel_id : keys_provider. get_channel_id ( ) ,
470467 channel_state : ChannelState :: OurInitSent as u32 ,
471468 channel_outbound : true ,
472- secp_ctx : secp_ctx ,
469+ secp_ctx : Secp256k1 :: new ( ) ,
473470 channel_value_satoshis : channel_value_satoshis,
474471
475472 latest_monitor_update_id : 0 ,
476473
477474 local_keys : chan_keys,
478475 shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
476+ destination_script : keys_provider. get_destination_script ( ) ,
477+
479478 cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
480479 cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
481480 value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
@@ -530,7 +529,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
530529
531530 their_shutdown_scriptpubkey : None ,
532531
533- channel_monitor : channel_monitor ,
532+ channel_monitor : None ,
534533 commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
535534
536535 network_sync : UpdateStatus :: Fresh ,
@@ -659,12 +658,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
659658 return Err ( ChannelError :: Close ( "Insufficient funding amount for initial commitment" ) ) ;
660659 }
661660
662- let secp_ctx = Secp256k1 :: new ( ) ;
663- let channel_monitor = ChannelMonitor :: new ( chan_keys. clone ( ) ,
664- chan_keys. funding_key ( ) , chan_keys. revocation_base_key ( ) , chan_keys. delayed_payment_base_key ( ) ,
665- chan_keys. htlc_base_key ( ) , chan_keys. payment_base_key ( ) , & keys_provider. get_shutdown_pubkey ( ) , config. own_channel_config . our_to_self_delay ,
666- keys_provider. get_destination_script ( ) , logger. clone ( ) ) ;
667-
668661 let their_shutdown_scriptpubkey = if their_features. supports_upfront_shutdown_script ( ) {
669662 match & msg. shutdown_scriptpubkey {
670663 & OptionalField :: Present ( ref script) => {
@@ -693,12 +686,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
693686 channel_id : msg. temporary_channel_id ,
694687 channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
695688 channel_outbound : false ,
696- secp_ctx : secp_ctx ,
689+ secp_ctx : Secp256k1 :: new ( ) ,
697690
698691 latest_monitor_update_id : 0 ,
699692
700693 local_keys : chan_keys,
701694 shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
695+ destination_script : keys_provider. get_destination_script ( ) ,
696+
702697 cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
703698 cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
704699 value_to_self_msat : msg. push_msat ,
@@ -754,7 +749,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
754749
755750 their_shutdown_scriptpubkey,
756751
757- channel_monitor : channel_monitor ,
752+ channel_monitor : None ,
758753 commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
759754
760755 network_sync : UpdateStatus :: Fresh ,
@@ -1193,7 +1188,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
11931188 payment_preimage: payment_preimage_arg. clone( ) ,
11941189 } ] ,
11951190 } ;
1196- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1191+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
11971192
11981193 if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) != 0 {
11991194 for pending_update in self . holding_cell_htlc_updates . iter ( ) {
@@ -1488,17 +1483,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
14881483 }
14891484 } ;
14901485
1486+ // Now that we're past error-generating stuff, update our local state:
1487+
14911488 let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
14921489 let funding_redeemscript = self . get_funding_redeemscript ( ) ;
1493- self . channel_monitor . set_basic_channel_info ( & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint , self . their_to_self_delay , funding_redeemscript. clone ( ) , self . channel_value_satoshis , self . get_commitment_transaction_number_obscure_factor ( ) ) ;
1494-
14951490 let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
1496- self . channel_monitor . set_funding_info ( ( funding_txo, funding_txo_script) ) ;
1497-
1498- // Now that we're past error-generating stuff, update our local state:
1499-
1500- self . channel_monitor . provide_latest_remote_commitment_tx_info ( & remote_initial_commitment_tx, Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
1501- self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx, local_keys, self . feerate_per_kw , Vec :: new ( ) ) . unwrap ( ) ;
1491+ self . channel_monitor = Some ( ChannelMonitor :: new ( self . local_keys . clone ( ) ,
1492+ & self . shutdown_pubkey , self . our_to_self_delay ,
1493+ & self . destination_script , ( funding_txo, funding_txo_script) ,
1494+ & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint ,
1495+ self . their_to_self_delay , funding_redeemscript, self . channel_value_satoshis ,
1496+ self . get_commitment_transaction_number_obscure_factor ( ) ,
1497+ self . logger . clone ( ) ) ) ;
1498+
1499+ self . channel_monitor . as_mut ( ) . unwrap ( ) . provide_latest_remote_commitment_tx_info ( & remote_initial_commitment_tx, Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
1500+ self . channel_monitor . as_mut ( ) . unwrap ( ) . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx, local_keys, self . feerate_per_kw , Vec :: new ( ) ) . unwrap ( ) ;
15021501 self . channel_state = ChannelState :: FundingSent as u32 ;
15031502 self . channel_id = funding_txo. to_channel_id ( ) ;
15041503 self . cur_remote_commitment_transaction_number -= 1 ;
@@ -1507,7 +1506,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15071506 Ok ( ( msgs:: FundingSigned {
15081507 channel_id : self . channel_id ,
15091508 signature : our_signature
1510- } , self . channel_monitor . clone ( ) ) )
1509+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
15111510 }
15121511
15131512 /// Handles a funding_signed message from the remote end.
@@ -1546,7 +1545,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
15461545 local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: Vec :: new( ) ,
15471546 } ]
15481547 } ;
1549- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1548+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
15501549 self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
15511550 self . cur_local_commitment_transaction_number -= 1 ;
15521551
@@ -1857,7 +1856,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
18571856 local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: htlcs_and_sigs
18581857 } ]
18591858 } ;
1860- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1859+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
18611860
18621861 for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
18631862 let new_forward = if let & InboundHTLCState :: RemoteAnnounced ( ref forward_info) = & htlc. state {
@@ -2090,7 +2089,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
20902089 secret: msg. per_commitment_secret,
20912090 } ] ,
20922091 } ;
2093- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2092+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
20942093
20952094 // Update state now that we've passed all the can-fail calls...
20962095 // (note that we may still fail to generate the new commitment_signed message, but that's
@@ -2559,7 +2558,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
25592558 their_current_per_commitment_point: data_loss. my_current_per_commitment_point
25602559 } ]
25612560 } ;
2562- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2561+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
25632562 return Err ( ChannelError :: CloseDelayBroadcast {
25642563 msg : "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting" ,
25652564 update : monitor_update
@@ -2909,7 +2908,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29092908 if self . channel_state < ChannelState :: FundingCreated as u32 {
29102909 panic ! ( "Can't get a channel monitor until funding has been created" ) ;
29112910 }
2912- & mut self . channel_monitor
2911+ self . channel_monitor . as_mut ( ) . unwrap ( )
29132912 }
29142913
29152914 /// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
@@ -3146,7 +3145,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
31463145 }
31473146 if header. bitcoin_hash ( ) != self . last_block_connected {
31483147 self . last_block_connected = header. bitcoin_hash ( ) ;
3149- self . channel_monitor . last_block_hash = self . last_block_connected ;
3148+ if let Some ( channel_monitor) = self . channel_monitor . as_mut ( ) {
3149+ channel_monitor. last_block_hash = self . last_block_connected ;
3150+ }
31503151 if self . funding_tx_confirmations > 0 {
31513152 if self . funding_tx_confirmations == self . minimum_depth as u64 {
31523153 let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
@@ -3206,7 +3207,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
32063207 self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
32073208 }
32083209 self . last_block_connected = header. bitcoin_hash ( ) ;
3209- self . channel_monitor . last_block_hash = self . last_block_connected ;
3210+ if let Some ( channel_monitor) = self . channel_monitor . as_mut ( ) {
3211+ channel_monitor. last_block_hash = self . last_block_connected ;
3212+ }
32103213 false
32113214 }
32123215
@@ -3320,16 +3323,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33203323 }
33213324 } ;
33223325
3323- let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3324- let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3325- self . channel_monitor . set_basic_channel_info ( & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint , self . their_to_self_delay , funding_redeemscript. clone ( ) , self . channel_value_satoshis , self . get_commitment_transaction_number_obscure_factor ( ) ) ;
3326-
3327- let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3328- self . channel_monitor . set_funding_info ( ( funding_txo, funding_txo_script) ) ;
33293326 let temporary_channel_id = self . channel_id ;
33303327
33313328 // Now that we're past error-generating stuff, update our local state:
3332- self . channel_monitor . provide_latest_remote_commitment_tx_info ( & commitment_tx, Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
3329+
3330+ let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3331+ let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3332+ let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3333+ self . channel_monitor = Some ( ChannelMonitor :: new ( self . local_keys . clone ( ) ,
3334+ & self . shutdown_pubkey , self . our_to_self_delay ,
3335+ & self . destination_script , ( funding_txo, funding_txo_script) ,
3336+ & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint ,
3337+ self . their_to_self_delay , funding_redeemscript, self . channel_value_satoshis ,
3338+ self . get_commitment_transaction_number_obscure_factor ( ) ,
3339+ self . logger . clone ( ) ) ) ;
3340+
3341+ self . channel_monitor . as_mut ( ) . unwrap ( ) . provide_latest_remote_commitment_tx_info ( & commitment_tx, Vec :: new ( ) , self . cur_remote_commitment_transaction_number , self . their_cur_commitment_point . unwrap ( ) ) ;
33333342 self . channel_state = ChannelState :: FundingCreated as u32 ;
33343343 self . channel_id = funding_txo. to_channel_id ( ) ;
33353344 self . cur_remote_commitment_transaction_number -= 1 ;
@@ -3339,7 +3348,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
33393348 funding_txid : funding_txo. txid ,
33403349 funding_output_index : funding_txo. index ,
33413350 signature : our_signature
3342- } , self . channel_monitor . clone ( ) ) )
3351+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
33433352 }
33443353
33453354 /// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3584,7 +3593,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
35843593 their_revocation_point: self . their_cur_commitment_point. unwrap( )
35853594 } ]
35863595 } ;
3587- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
3596+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
35883597 self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
35893598 Ok ( ( res, monitor_update) )
35903599 }
@@ -3728,7 +3737,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
37283737
37293738 self . channel_state = ChannelState :: ShutdownComplete as u32 ;
37303739 self . channel_update_count += 1 ;
3731- ( self . channel_monitor . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3740+ if self . channel_monitor . is_some ( ) {
3741+ ( self . channel_monitor . as_mut ( ) . unwrap ( ) . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3742+ } else {
3743+ // We aren't even signed funding yet, so can't broadcast anything
3744+ ( Vec :: new ( ) , dropped_outbound_htlcs)
3745+ }
37323746 }
37333747}
37343748
@@ -3787,6 +3801,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
37873801
37883802 self . local_keys . write ( writer) ?;
37893803 self . shutdown_pubkey . write ( writer) ?;
3804+ self . destination_script . write ( writer) ?;
37903805
37913806 self . cur_local_commitment_transaction_number . write ( writer) ?;
37923807 self . cur_remote_commitment_transaction_number . write ( writer) ?;
@@ -3961,7 +3976,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
39613976
39623977 self . commitment_secrets . write ( writer) ?;
39633978
3964- self . channel_monitor . write_for_disk ( writer) ?;
3979+ self . channel_monitor . as_ref ( ) . unwrap ( ) . write_for_disk ( writer) ?;
39653980 Ok ( ( ) )
39663981 }
39673982}
@@ -3986,6 +4001,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
39864001
39874002 let local_keys = Readable :: read ( reader) ?;
39884003 let shutdown_pubkey = Readable :: read ( reader) ?;
4004+ let destination_script = Readable :: read ( reader) ?;
39894005
39904006 let cur_local_commitment_transaction_number = Readable :: read ( reader) ?;
39914007 let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
@@ -4136,6 +4152,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
41364152
41374153 local_keys,
41384154 shutdown_pubkey,
4155+ destination_script,
41394156
41404157 cur_local_commitment_transaction_number,
41414158 cur_remote_commitment_transaction_number,
@@ -4192,7 +4209,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
41924209
41934210 their_shutdown_scriptpubkey,
41944211
4195- channel_monitor,
4212+ channel_monitor : Some ( channel_monitor ) ,
41964213 commitment_secrets,
41974214
41984215 network_sync : UpdateStatus :: Fresh ,
0 commit comments