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