@@ -423,7 +423,7 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
423
423
pub ( super ) latest_block_height : AtomicUsize ,
424
424
#[ cfg( not( test) ) ]
425
425
latest_block_height : AtomicUsize ,
426
- last_block_hash : Mutex < BlockHash > ,
426
+ last_block_hash : RwLock < BlockHash > ,
427
427
secp_ctx : Secp256k1 < secp256k1:: All > ,
428
428
429
429
#[ cfg( any( test, feature = "_test_utils" ) ) ]
@@ -803,7 +803,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
803
803
tx_broadcaster,
804
804
805
805
latest_block_height : AtomicUsize :: new ( params. latest_height ) ,
806
- last_block_hash : Mutex :: new ( params. latest_hash ) ,
806
+ last_block_hash : RwLock :: new ( params. latest_hash ) ,
807
807
secp_ctx,
808
808
809
809
channel_state : Mutex :: new ( ChannelHolder {
@@ -2454,14 +2454,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2454
2454
2455
2455
fn internal_funding_created ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingCreated ) -> Result < ( ) , MsgHandleErrInternal > {
2456
2456
let ( ( funding_msg, monitor) , mut chan) = {
2457
+ let last_block_hash = * self . last_block_hash . read ( ) . unwrap ( ) ;
2457
2458
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2458
2459
let channel_state = & mut * channel_lock;
2459
2460
match channel_state. by_id . entry ( msg. temporary_channel_id . clone ( ) ) {
2460
2461
hash_map:: Entry :: Occupied ( mut chan) => {
2461
2462
if chan. get ( ) . get_counterparty_node_id ( ) != * counterparty_node_id {
2462
2463
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. temporary_channel_id ) ) ;
2463
2464
}
2464
- let last_block_hash = * self . last_block_hash . lock ( ) . unwrap ( ) ;
2465
2465
( try_chan_entry ! ( self , chan. get_mut( ) . funding_created( msg, last_block_hash, & self . logger) , channel_state, chan) , chan. remove ( ) )
2466
2466
} ,
2467
2467
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" . to_owned ( ) , msg. temporary_channel_id ) )
@@ -2511,14 +2511,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2511
2511
2512
2512
fn internal_funding_signed ( & self , counterparty_node_id : & PublicKey , msg : & msgs:: FundingSigned ) -> Result < ( ) , MsgHandleErrInternal > {
2513
2513
let ( funding_txo, user_id) = {
2514
+ let last_block_hash = * self . last_block_hash . read ( ) . unwrap ( ) ;
2514
2515
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2515
2516
let channel_state = & mut * channel_lock;
2516
2517
match channel_state. by_id . entry ( msg. channel_id ) {
2517
2518
hash_map:: Entry :: Occupied ( mut chan) => {
2518
2519
if chan. get ( ) . get_counterparty_node_id ( ) != * counterparty_node_id {
2519
2520
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" . to_owned ( ) , msg. channel_id ) ) ;
2520
2521
}
2521
- let last_block_hash = * self . last_block_hash . lock ( ) . unwrap ( ) ;
2522
2522
let monitor = match chan. get_mut ( ) . funding_signed ( & msg, last_block_hash, & self . logger ) {
2523
2523
Ok ( update) => update,
2524
2524
Err ( e) => try_chan_entry ! ( self , Err ( e) , channel_state, chan) ,
@@ -3257,7 +3257,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3257
3257
// See the docs for `ChannelManagerReadArgs` for more.
3258
3258
let block_hash = header. block_hash ( ) ;
3259
3259
log_trace ! ( self . logger, "Block {} at height {} connected" , block_hash, height) ;
3260
+
3260
3261
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3262
+
3263
+ self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
3264
+ * self . last_block_hash . write ( ) . unwrap ( ) = block_hash;
3265
+
3261
3266
let mut failed_channels = Vec :: new ( ) ;
3262
3267
let mut timed_out_htlcs = Vec :: new ( ) ;
3263
3268
{
@@ -3346,8 +3351,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3346
3351
for ( source, payment_hash, reason) in timed_out_htlcs. drain ( ..) {
3347
3352
self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , source, & payment_hash, reason) ;
3348
3353
}
3349
- self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
3350
- * self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = block_hash;
3354
+
3351
3355
loop {
3352
3356
// Update last_node_announcement_serial to be the max of its current value and the
3353
3357
// block timestamp. This should keep us close to the current time without relying on
@@ -3371,6 +3375,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3371
3375
// during initialization prior to the chain_monitor being fully configured in some cases.
3372
3376
// See the docs for `ChannelManagerReadArgs` for more.
3373
3377
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3378
+
3379
+ self . latest_block_height . fetch_sub ( 1 , Ordering :: AcqRel ) ;
3380
+ * self . last_block_hash . write ( ) . unwrap ( ) = header. block_hash ( ) ;
3381
+
3374
3382
let mut failed_channels = Vec :: new ( ) ;
3375
3383
{
3376
3384
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -3394,9 +3402,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3394
3402
}
3395
3403
} ) ;
3396
3404
}
3405
+
3397
3406
self . handle_init_event_channel_failures ( failed_channels) ;
3398
- self . latest_block_height . fetch_sub ( 1 , Ordering :: AcqRel ) ;
3399
- * self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header. block_hash ( ) ;
3400
3407
}
3401
3408
3402
3409
/// Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
@@ -3952,7 +3959,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
3952
3959
3953
3960
self . genesis_hash . write ( writer) ?;
3954
3961
( self . latest_block_height . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3955
- self . last_block_hash . lock ( ) . unwrap ( ) . write ( writer) ?;
3962
+ self . last_block_hash . read ( ) . unwrap ( ) . write ( writer) ?;
3956
3963
3957
3964
let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3958
3965
let mut unfunded_channels = 0 ;
@@ -4254,7 +4261,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4254
4261
tx_broadcaster : args. tx_broadcaster ,
4255
4262
4256
4263
latest_block_height : AtomicUsize :: new ( latest_block_height as usize ) ,
4257
- last_block_hash : Mutex :: new ( last_block_hash) ,
4264
+ last_block_hash : RwLock :: new ( last_block_hash) ,
4258
4265
secp_ctx,
4259
4266
4260
4267
channel_state : Mutex :: new ( ChannelHolder {
0 commit comments