@@ -250,6 +250,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
250
250
#[ cfg( test) ]
251
251
pub ( super ) local_keys : ChanSigner ,
252
252
shutdown_pubkey : PublicKey ,
253
+ destination_script : Script ,
253
254
254
255
// Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
255
256
// generation start at 0 and count up...this simplifies some parts of implementation at the
@@ -354,7 +355,9 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
354
355
355
356
their_shutdown_scriptpubkey : Option < Script > ,
356
357
357
- channel_monitor : ChannelMonitor < ChanSigner > ,
358
+ /// Used exclusively to broadcast the latest local state, mostly a historical quirk that this
359
+ /// is here:
360
+ channel_monitor : Option < ChannelMonitor < ChanSigner > > ,
358
361
commitment_secrets : CounterpartyCommitmentSecrets ,
359
362
360
363
network_sync : UpdateStatus ,
@@ -459,26 +462,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
459
462
460
463
let feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
461
464
462
- let secp_ctx = Secp256k1 :: new ( ) ;
463
- let channel_monitor = ChannelMonitor :: new ( chan_keys. clone ( ) ,
464
- chan_keys. funding_key ( ) , chan_keys. revocation_base_key ( ) , chan_keys. delayed_payment_base_key ( ) ,
465
- chan_keys. htlc_base_key ( ) , chan_keys. payment_base_key ( ) , & keys_provider. get_shutdown_pubkey ( ) , config. own_channel_config . our_to_self_delay ,
466
- keys_provider. get_destination_script ( ) , logger. clone ( ) ) ;
467
-
468
465
Ok ( Channel {
469
466
user_id : user_id,
470
467
config : config. channel_options . clone ( ) ,
471
468
472
469
channel_id : keys_provider. get_channel_id ( ) ,
473
470
channel_state : ChannelState :: OurInitSent as u32 ,
474
471
channel_outbound : true ,
475
- secp_ctx : secp_ctx ,
472
+ secp_ctx : Secp256k1 :: new ( ) ,
476
473
channel_value_satoshis : channel_value_satoshis,
477
474
478
475
latest_monitor_update_id : 0 ,
479
476
480
477
local_keys : chan_keys,
481
478
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
479
+ destination_script : keys_provider. get_destination_script ( ) ,
480
+
482
481
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
483
482
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
484
483
value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
@@ -533,7 +532,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
533
532
534
533
their_shutdown_scriptpubkey : None ,
535
534
536
- channel_monitor : channel_monitor ,
535
+ channel_monitor : None ,
537
536
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
538
537
539
538
network_sync : UpdateStatus :: Fresh ,
@@ -662,12 +661,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
662
661
return Err ( ChannelError :: Close ( "Insufficient funding amount for initial commitment" ) ) ;
663
662
}
664
663
665
- let secp_ctx = Secp256k1 :: new ( ) ;
666
- let channel_monitor = ChannelMonitor :: new ( chan_keys. clone ( ) ,
667
- chan_keys. funding_key ( ) , chan_keys. revocation_base_key ( ) , chan_keys. delayed_payment_base_key ( ) ,
668
- chan_keys. htlc_base_key ( ) , chan_keys. payment_base_key ( ) , & keys_provider. get_shutdown_pubkey ( ) , config. own_channel_config . our_to_self_delay ,
669
- keys_provider. get_destination_script ( ) , logger. clone ( ) ) ;
670
-
671
664
let their_shutdown_scriptpubkey = if their_features. supports_upfront_shutdown_script ( ) {
672
665
match & msg. shutdown_scriptpubkey {
673
666
& OptionalField :: Present ( ref script) => {
@@ -696,12 +689,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
696
689
channel_id : msg. temporary_channel_id ,
697
690
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
698
691
channel_outbound : false ,
699
- secp_ctx : secp_ctx ,
692
+ secp_ctx : Secp256k1 :: new ( ) ,
700
693
701
694
latest_monitor_update_id : 0 ,
702
695
703
696
local_keys : chan_keys,
704
697
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
698
+ destination_script : keys_provider. get_destination_script ( ) ,
699
+
705
700
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
706
701
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
707
702
value_to_self_msat : msg. push_msat ,
@@ -757,7 +752,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
757
752
758
753
their_shutdown_scriptpubkey,
759
754
760
- channel_monitor : channel_monitor ,
755
+ channel_monitor : None ,
761
756
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
762
757
763
758
network_sync : UpdateStatus :: Fresh ,
@@ -1196,7 +1191,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1196
1191
payment_preimage: payment_preimage_arg. clone( ) ,
1197
1192
} ] ,
1198
1193
} ;
1199
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1194
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1200
1195
1201
1196
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) != 0 {
1202
1197
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
@@ -1491,17 +1486,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1491
1486
}
1492
1487
} ;
1493
1488
1489
+ // Now that we're past error-generating stuff, update our local state:
1490
+
1494
1491
let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
1495
1492
let funding_redeemscript = self . get_funding_redeemscript ( ) ;
1496
- 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 ( ) ) ;
1497
-
1498
1493
let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
1499
- self . channel_monitor . set_funding_info ( ( funding_txo, funding_txo_script) ) ;
1500
-
1501
- // Now that we're past error-generating stuff, update our local state:
1502
-
1503
- 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 ( ) ) ;
1504
- self . channel_monitor . provide_latest_local_commitment_tx_info ( local_initial_commitment_tx, local_keys, self . feerate_per_kw , Vec :: new ( ) ) . unwrap ( ) ;
1494
+ self . channel_monitor = Some ( ChannelMonitor :: new ( self . local_keys . clone ( ) ,
1495
+ & self . shutdown_pubkey , self . our_to_self_delay ,
1496
+ & self . destination_script , ( funding_txo, funding_txo_script) ,
1497
+ & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint ,
1498
+ self . their_to_self_delay , funding_redeemscript, self . channel_value_satoshis ,
1499
+ self . get_commitment_transaction_number_obscure_factor ( ) ,
1500
+ self . logger . clone ( ) ) ) ;
1501
+
1502
+ 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 ( ) ) ;
1503
+ 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 ( ) ;
1505
1504
self . channel_state = ChannelState :: FundingSent as u32 ;
1506
1505
self . channel_id = funding_txo. to_channel_id ( ) ;
1507
1506
self . cur_remote_commitment_transaction_number -= 1 ;
@@ -1510,7 +1509,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1510
1509
Ok ( ( msgs:: FundingSigned {
1511
1510
channel_id : self . channel_id ,
1512
1511
signature : our_signature
1513
- } , self . channel_monitor . clone ( ) ) )
1512
+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
1514
1513
}
1515
1514
1516
1515
/// Handles a funding_signed message from the remote end.
@@ -1549,7 +1548,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1549
1548
local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: Vec :: new( ) ,
1550
1549
} ]
1551
1550
} ;
1552
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1551
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1553
1552
self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
1554
1553
self . cur_local_commitment_transaction_number -= 1 ;
1555
1554
@@ -1860,7 +1859,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1860
1859
local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: htlcs_and_sigs
1861
1860
} ]
1862
1861
} ;
1863
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1862
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
1864
1863
1865
1864
for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
1866
1865
let new_forward = if let & InboundHTLCState :: RemoteAnnounced ( ref forward_info) = & htlc. state {
@@ -2093,7 +2092,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2093
2092
secret: msg. per_commitment_secret,
2094
2093
} ] ,
2095
2094
} ;
2096
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2095
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2097
2096
2098
2097
// Update state now that we've passed all the can-fail calls...
2099
2098
// (note that we may still fail to generate the new commitment_signed message, but that's
@@ -2563,7 +2562,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2563
2562
their_current_per_commitment_point: data_loss. my_current_per_commitment_point
2564
2563
} ]
2565
2564
} ;
2566
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2565
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
2567
2566
return Err ( ChannelError :: CloseDelayBroadcast {
2568
2567
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" ,
2569
2568
update : monitor_update
@@ -2913,7 +2912,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2913
2912
if self . channel_state < ChannelState :: FundingCreated as u32 {
2914
2913
panic ! ( "Can't get a channel monitor until funding has been created" ) ;
2915
2914
}
2916
- & mut self . channel_monitor
2915
+ self . channel_monitor . as_mut ( ) . unwrap ( )
2917
2916
}
2918
2917
2919
2918
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
@@ -3162,7 +3161,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3162
3161
}
3163
3162
if header. bitcoin_hash ( ) != self . last_block_connected {
3164
3163
self . last_block_connected = header. bitcoin_hash ( ) ;
3165
- self . channel_monitor . last_block_hash = self . last_block_connected ;
3164
+ if let Some ( channel_monitor) = self . channel_monitor . as_mut ( ) {
3165
+ channel_monitor. last_block_hash = self . last_block_connected ;
3166
+ }
3166
3167
if self . funding_tx_confirmations > 0 {
3167
3168
if self . funding_tx_confirmations == self . minimum_depth as u64 {
3168
3169
let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
@@ -3222,7 +3223,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3222
3223
self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
3223
3224
}
3224
3225
self . last_block_connected = header. bitcoin_hash ( ) ;
3225
- self . channel_monitor . last_block_hash = self . last_block_connected ;
3226
+ if let Some ( channel_monitor) = self . channel_monitor . as_mut ( ) {
3227
+ channel_monitor. last_block_hash = self . last_block_connected ;
3228
+ }
3226
3229
false
3227
3230
}
3228
3231
@@ -3336,16 +3339,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3336
3339
}
3337
3340
} ;
3338
3341
3339
- let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3340
- let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3341
- 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 ( ) ) ;
3342
-
3343
- let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3344
- self . channel_monitor . set_funding_info ( ( funding_txo, funding_txo_script) ) ;
3345
3342
let temporary_channel_id = self . channel_id ;
3346
3343
3347
3344
// Now that we're past error-generating stuff, update our local state:
3348
- 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 ( ) ) ;
3345
+
3346
+ let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3347
+ let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3348
+ let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3349
+ self . channel_monitor = Some ( ChannelMonitor :: new ( self . local_keys . clone ( ) ,
3350
+ & self . shutdown_pubkey , self . our_to_self_delay ,
3351
+ & self . destination_script , ( funding_txo, funding_txo_script) ,
3352
+ & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint ,
3353
+ self . their_to_self_delay , funding_redeemscript, self . channel_value_satoshis ,
3354
+ self . get_commitment_transaction_number_obscure_factor ( ) ,
3355
+ self . logger . clone ( ) ) ) ;
3356
+
3357
+ 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 ( ) ) ;
3349
3358
self . channel_state = ChannelState :: FundingCreated as u32 ;
3350
3359
self . channel_id = funding_txo. to_channel_id ( ) ;
3351
3360
self . cur_remote_commitment_transaction_number -= 1 ;
@@ -3355,7 +3364,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3355
3364
funding_txid : funding_txo. txid ,
3356
3365
funding_output_index : funding_txo. index ,
3357
3366
signature : our_signature
3358
- } , self . channel_monitor . clone ( ) ) )
3367
+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
3359
3368
}
3360
3369
3361
3370
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3600,7 +3609,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3600
3609
their_revocation_point: self . their_cur_commitment_point. unwrap( )
3601
3610
} ]
3602
3611
} ;
3603
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
3612
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
3604
3613
self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
3605
3614
Ok ( ( res, monitor_update) )
3606
3615
}
@@ -3744,7 +3753,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3744
3753
3745
3754
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
3746
3755
self . channel_update_count += 1 ;
3747
- ( self . channel_monitor . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3756
+ if self . channel_monitor . is_some ( ) {
3757
+ ( self . channel_monitor . as_mut ( ) . unwrap ( ) . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3758
+ } else {
3759
+ // We aren't even signed funding yet, so can't broadcast anything
3760
+ ( Vec :: new ( ) , dropped_outbound_htlcs)
3761
+ }
3748
3762
}
3749
3763
}
3750
3764
@@ -3803,6 +3817,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3803
3817
3804
3818
self . local_keys . write ( writer) ?;
3805
3819
self . shutdown_pubkey . write ( writer) ?;
3820
+ self . destination_script . write ( writer) ?;
3806
3821
3807
3822
self . cur_local_commitment_transaction_number . write ( writer) ?;
3808
3823
self . cur_remote_commitment_transaction_number . write ( writer) ?;
@@ -3977,7 +3992,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3977
3992
3978
3993
self . commitment_secrets . write ( writer) ?;
3979
3994
3980
- self . channel_monitor . write_for_disk ( writer) ?;
3995
+ self . channel_monitor . as_ref ( ) . unwrap ( ) . write_for_disk ( writer) ?;
3981
3996
Ok ( ( ) )
3982
3997
}
3983
3998
}
@@ -4002,6 +4017,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4002
4017
4003
4018
let local_keys = Readable :: read ( reader) ?;
4004
4019
let shutdown_pubkey = Readable :: read ( reader) ?;
4020
+ let destination_script = Readable :: read ( reader) ?;
4005
4021
4006
4022
let cur_local_commitment_transaction_number = Readable :: read ( reader) ?;
4007
4023
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
@@ -4152,6 +4168,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4152
4168
4153
4169
local_keys,
4154
4170
shutdown_pubkey,
4171
+ destination_script,
4155
4172
4156
4173
cur_local_commitment_transaction_number,
4157
4174
cur_remote_commitment_transaction_number,
@@ -4208,7 +4225,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4208
4225
4209
4226
their_shutdown_scriptpubkey,
4210
4227
4211
- channel_monitor,
4228
+ channel_monitor : Some ( channel_monitor ) ,
4212
4229
commitment_secrets,
4213
4230
4214
4231
network_sync : UpdateStatus :: Fresh ,
0 commit comments