@@ -247,6 +247,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
247
247
#[ cfg( test) ]
248
248
pub ( super ) local_keys : ChanSigner ,
249
249
shutdown_pubkey : PublicKey ,
250
+ destination_script : Script ,
250
251
251
252
// Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
252
253
// 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> {
351
352
352
353
their_shutdown_scriptpubkey : Option < Script > ,
353
354
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 > > ,
355
358
commitment_secrets : CounterpartyCommitmentSecrets ,
356
359
357
360
network_sync : UpdateStatus ,
@@ -456,26 +459,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
456
459
457
460
let feerate = fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: Normal ) ;
458
461
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
-
465
462
Ok ( Channel {
466
463
user_id : user_id,
467
464
config : config. channel_options . clone ( ) ,
468
465
469
466
channel_id : keys_provider. get_channel_id ( ) ,
470
467
channel_state : ChannelState :: OurInitSent as u32 ,
471
468
channel_outbound : true ,
472
- secp_ctx : secp_ctx ,
469
+ secp_ctx : Secp256k1 :: new ( ) ,
473
470
channel_value_satoshis : channel_value_satoshis,
474
471
475
472
latest_monitor_update_id : 0 ,
476
473
477
474
local_keys : chan_keys,
478
475
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
476
+ destination_script : keys_provider. get_destination_script ( ) ,
477
+
479
478
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
480
479
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
481
480
value_to_self_msat : channel_value_satoshis * 1000 - push_msat,
@@ -530,7 +529,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
530
529
531
530
their_shutdown_scriptpubkey : None ,
532
531
533
- channel_monitor : channel_monitor ,
532
+ channel_monitor : None ,
534
533
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
535
534
536
535
network_sync : UpdateStatus :: Fresh ,
@@ -659,12 +658,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
659
658
return Err ( ChannelError :: Close ( "Insufficient funding amount for initial commitment" ) ) ;
660
659
}
661
660
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
-
668
661
let their_shutdown_scriptpubkey = if their_features. supports_upfront_shutdown_script ( ) {
669
662
match & msg. shutdown_scriptpubkey {
670
663
& OptionalField :: Present ( ref script) => {
@@ -693,12 +686,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
693
686
channel_id : msg. temporary_channel_id ,
694
687
channel_state : ( ChannelState :: OurInitSent as u32 ) | ( ChannelState :: TheirInitSent as u32 ) ,
695
688
channel_outbound : false ,
696
- secp_ctx : secp_ctx ,
689
+ secp_ctx : Secp256k1 :: new ( ) ,
697
690
698
691
latest_monitor_update_id : 0 ,
699
692
700
693
local_keys : chan_keys,
701
694
shutdown_pubkey : keys_provider. get_shutdown_pubkey ( ) ,
695
+ destination_script : keys_provider. get_destination_script ( ) ,
696
+
702
697
cur_local_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
703
698
cur_remote_commitment_transaction_number : INITIAL_COMMITMENT_NUMBER ,
704
699
value_to_self_msat : msg. push_msat ,
@@ -754,7 +749,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
754
749
755
750
their_shutdown_scriptpubkey,
756
751
757
- channel_monitor : channel_monitor ,
752
+ channel_monitor : None ,
758
753
commitment_secrets : CounterpartyCommitmentSecrets :: new ( ) ,
759
754
760
755
network_sync : UpdateStatus :: Fresh ,
@@ -1193,7 +1188,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1193
1188
payment_preimage: payment_preimage_arg. clone( ) ,
1194
1189
} ] ,
1195
1190
} ;
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 ( ) ;
1197
1192
1198
1193
if ( self . channel_state & ( ChannelState :: AwaitingRemoteRevoke as u32 | ChannelState :: PeerDisconnected as u32 | ChannelState :: MonitorUpdateFailed as u32 ) ) != 0 {
1199
1194
for pending_update in self . holding_cell_htlc_updates . iter ( ) {
@@ -1488,17 +1483,21 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1488
1483
}
1489
1484
} ;
1490
1485
1486
+ // Now that we're past error-generating stuff, update our local state:
1487
+
1491
1488
let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
1492
1489
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
-
1495
1490
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 ( ) ;
1502
1501
self . channel_state = ChannelState :: FundingSent as u32 ;
1503
1502
self . channel_id = funding_txo. to_channel_id ( ) ;
1504
1503
self . cur_remote_commitment_transaction_number -= 1 ;
@@ -1507,7 +1506,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1507
1506
Ok ( ( msgs:: FundingSigned {
1508
1507
channel_id : self . channel_id ,
1509
1508
signature : our_signature
1510
- } , self . channel_monitor . clone ( ) ) )
1509
+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
1511
1510
}
1512
1511
1513
1512
/// Handles a funding_signed message from the remote end.
@@ -1546,7 +1545,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1546
1545
local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: Vec :: new( ) ,
1547
1546
} ]
1548
1547
} ;
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 ( ) ;
1550
1549
self . channel_state = ChannelState :: FundingSent as u32 | ( self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) ) ;
1551
1550
self . cur_local_commitment_transaction_number -= 1 ;
1552
1551
@@ -1857,7 +1856,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1857
1856
local_keys, feerate_per_kw: self . feerate_per_kw, htlc_outputs: htlcs_and_sigs
1858
1857
} ]
1859
1858
} ;
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 ( ) ;
1861
1860
1862
1861
for htlc in self . pending_inbound_htlcs . iter_mut ( ) {
1863
1862
let new_forward = if let & InboundHTLCState :: RemoteAnnounced ( ref forward_info) = & htlc. state {
@@ -2090,7 +2089,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2090
2089
secret: msg. per_commitment_secret,
2091
2090
} ] ,
2092
2091
} ;
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 ( ) ;
2094
2093
2095
2094
// Update state now that we've passed all the can-fail calls...
2096
2095
// (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> {
2559
2558
their_current_per_commitment_point: data_loss. my_current_per_commitment_point
2560
2559
} ]
2561
2560
} ;
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 ( ) ;
2563
2562
return Err ( ChannelError :: CloseDelayBroadcast {
2564
2563
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" ,
2565
2564
update : monitor_update
@@ -2909,7 +2908,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2909
2908
if self . channel_state < ChannelState :: FundingCreated as u32 {
2910
2909
panic ! ( "Can't get a channel monitor until funding has been created" ) ;
2911
2910
}
2912
- & mut self . channel_monitor
2911
+ self . channel_monitor . as_mut ( ) . unwrap ( )
2913
2912
}
2914
2913
2915
2914
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
@@ -3146,7 +3145,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3146
3145
}
3147
3146
if header. bitcoin_hash ( ) != self . last_block_connected {
3148
3147
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
+ }
3150
3151
if self . funding_tx_confirmations > 0 {
3151
3152
if self . funding_tx_confirmations == self . minimum_depth as u64 {
3152
3153
let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
@@ -3206,7 +3207,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3206
3207
self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
3207
3208
}
3208
3209
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
+ }
3210
3213
false
3211
3214
}
3212
3215
@@ -3320,16 +3323,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3320
3323
}
3321
3324
} ;
3322
3325
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) ) ;
3329
3326
let temporary_channel_id = self . channel_id ;
3330
3327
3331
3328
// 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 ( ) ) ;
3333
3342
self . channel_state = ChannelState :: FundingCreated as u32 ;
3334
3343
self . channel_id = funding_txo. to_channel_id ( ) ;
3335
3344
self . cur_remote_commitment_transaction_number -= 1 ;
@@ -3339,7 +3348,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3339
3348
funding_txid : funding_txo. txid ,
3340
3349
funding_output_index : funding_txo. index ,
3341
3350
signature : our_signature
3342
- } , self . channel_monitor . clone ( ) ) )
3351
+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
3343
3352
}
3344
3353
3345
3354
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3584,7 +3593,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3584
3593
their_revocation_point: self . their_cur_commitment_point. unwrap( )
3585
3594
} ]
3586
3595
} ;
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 ( ) ;
3588
3597
self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
3589
3598
Ok ( ( res, monitor_update) )
3590
3599
}
@@ -3728,7 +3737,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3728
3737
3729
3738
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
3730
3739
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
+ }
3732
3746
}
3733
3747
}
3734
3748
@@ -3787,6 +3801,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3787
3801
3788
3802
self . local_keys . write ( writer) ?;
3789
3803
self . shutdown_pubkey . write ( writer) ?;
3804
+ self . destination_script . write ( writer) ?;
3790
3805
3791
3806
self . cur_local_commitment_transaction_number . write ( writer) ?;
3792
3807
self . cur_remote_commitment_transaction_number . write ( writer) ?;
@@ -3961,7 +3976,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3961
3976
3962
3977
self . commitment_secrets . write ( writer) ?;
3963
3978
3964
- self . channel_monitor . write_for_disk ( writer) ?;
3979
+ self . channel_monitor . as_ref ( ) . unwrap ( ) . write_for_disk ( writer) ?;
3965
3980
Ok ( ( ) )
3966
3981
}
3967
3982
}
@@ -3986,6 +4001,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3986
4001
3987
4002
let local_keys = Readable :: read ( reader) ?;
3988
4003
let shutdown_pubkey = Readable :: read ( reader) ?;
4004
+ let destination_script = Readable :: read ( reader) ?;
3989
4005
3990
4006
let cur_local_commitment_transaction_number = Readable :: read ( reader) ?;
3991
4007
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
@@ -4136,6 +4152,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4136
4152
4137
4153
local_keys,
4138
4154
shutdown_pubkey,
4155
+ destination_script,
4139
4156
4140
4157
cur_local_commitment_transaction_number,
4141
4158
cur_remote_commitment_transaction_number,
@@ -4192,7 +4209,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4192
4209
4193
4210
their_shutdown_scriptpubkey,
4194
4211
4195
- channel_monitor,
4212
+ channel_monitor : Some ( channel_monitor ) ,
4196
4213
commitment_secrets,
4197
4214
4198
4215
network_sync : UpdateStatus :: Fresh ,
0 commit comments