@@ -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,7 @@ 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
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . last_block_hash = self . last_block_connected ;
3166
3165
if self . funding_tx_confirmations > 0 {
3167
3166
if self . funding_tx_confirmations == self . minimum_depth as u64 {
3168
3167
let need_commitment_update = if non_shutdown_state == ChannelState :: FundingSent as u32 {
@@ -3222,7 +3221,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3222
3221
self . funding_tx_confirmations = self . minimum_depth as u64 - 1 ;
3223
3222
}
3224
3223
self . last_block_connected = header. bitcoin_hash ( ) ;
3225
- self . channel_monitor . last_block_hash = self . last_block_connected ;
3224
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . last_block_hash = self . last_block_connected ;
3226
3225
false
3227
3226
}
3228
3227
@@ -3336,16 +3335,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3336
3335
}
3337
3336
} ;
3338
3337
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
3338
let temporary_channel_id = self . channel_id ;
3346
3339
3347
3340
// 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 ( ) ) ;
3341
+
3342
+ let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
3343
+ let funding_redeemscript = self . get_funding_redeemscript ( ) ;
3344
+ let funding_txo_script = funding_redeemscript. to_v0_p2wsh ( ) ;
3345
+ self . channel_monitor = Some ( ChannelMonitor :: new ( self . local_keys . clone ( ) ,
3346
+ & self . shutdown_pubkey , self . our_to_self_delay ,
3347
+ & self . destination_script , ( funding_txo, funding_txo_script) ,
3348
+ & their_pubkeys. htlc_basepoint , & their_pubkeys. delayed_payment_basepoint ,
3349
+ self . their_to_self_delay , funding_redeemscript, self . channel_value_satoshis ,
3350
+ self . get_commitment_transaction_number_obscure_factor ( ) ,
3351
+ self . logger . clone ( ) ) ) ;
3352
+
3353
+ 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
3354
self . channel_state = ChannelState :: FundingCreated as u32 ;
3350
3355
self . channel_id = funding_txo. to_channel_id ( ) ;
3351
3356
self . cur_remote_commitment_transaction_number -= 1 ;
@@ -3355,7 +3360,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3355
3360
funding_txid : funding_txo. txid ,
3356
3361
funding_output_index : funding_txo. index ,
3357
3362
signature : our_signature
3358
- } , self . channel_monitor . clone ( ) ) )
3363
+ } , self . channel_monitor . as_ref ( ) . unwrap ( ) . clone ( ) ) )
3359
3364
}
3360
3365
3361
3366
/// Gets an UnsignedChannelAnnouncement, as well as a signature covering it using our
@@ -3600,7 +3605,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3600
3605
their_revocation_point: self . their_cur_commitment_point. unwrap( )
3601
3606
} ]
3602
3607
} ;
3603
- self . channel_monitor . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
3608
+ self . channel_monitor . as_mut ( ) . unwrap ( ) . update_monitor_ooo ( monitor_update. clone ( ) ) . unwrap ( ) ;
3604
3609
self . channel_state |= ChannelState :: AwaitingRemoteRevoke as u32 ;
3605
3610
Ok ( ( res, monitor_update) )
3606
3611
}
@@ -3744,7 +3749,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3744
3749
3745
3750
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
3746
3751
self . channel_update_count += 1 ;
3747
- ( self . channel_monitor . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3752
+ if self . channel_monitor . is_some ( ) {
3753
+ ( self . channel_monitor . as_mut ( ) . unwrap ( ) . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3754
+ } else {
3755
+ // We aren't even signed funding yet, so can't broadcast anything
3756
+ ( Vec :: new ( ) , dropped_outbound_htlcs)
3757
+ }
3748
3758
}
3749
3759
}
3750
3760
@@ -3803,6 +3813,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3803
3813
3804
3814
self . local_keys . write ( writer) ?;
3805
3815
self . shutdown_pubkey . write ( writer) ?;
3816
+ self . destination_script . write ( writer) ?;
3806
3817
3807
3818
self . cur_local_commitment_transaction_number . write ( writer) ?;
3808
3819
self . cur_remote_commitment_transaction_number . write ( writer) ?;
@@ -3974,7 +3985,7 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
3974
3985
3975
3986
self . commitment_secrets . write ( writer) ?;
3976
3987
3977
- self . channel_monitor . write_for_disk ( writer) ?;
3988
+ self . channel_monitor . as_ref ( ) . unwrap ( ) . write_for_disk ( writer) ?;
3978
3989
Ok ( ( ) )
3979
3990
}
3980
3991
}
@@ -3999,6 +4010,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
3999
4010
4000
4011
let local_keys = Readable :: read ( reader) ?;
4001
4012
let shutdown_pubkey = Readable :: read ( reader) ?;
4013
+ let destination_script = Readable :: read ( reader) ?;
4002
4014
4003
4015
let cur_local_commitment_transaction_number = Readable :: read ( reader) ?;
4004
4016
let cur_remote_commitment_transaction_number = Readable :: read ( reader) ?;
@@ -4149,6 +4161,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4149
4161
4150
4162
local_keys,
4151
4163
shutdown_pubkey,
4164
+ destination_script,
4152
4165
4153
4166
cur_local_commitment_transaction_number,
4154
4167
cur_remote_commitment_transaction_number,
@@ -4205,7 +4218,7 @@ impl<R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R,
4205
4218
4206
4219
their_shutdown_scriptpubkey,
4207
4220
4208
- channel_monitor,
4221
+ channel_monitor : Some ( channel_monitor ) ,
4209
4222
commitment_secrets,
4210
4223
4211
4224
network_sync : UpdateStatus :: Fresh ,
0 commit comments