@@ -19,8 +19,11 @@ use governor::{clock::DefaultClock, state::keyed::DefaultKeyedStateStore, Quota,
1919
2020use super :: prelude:: * ;
2121use crate :: {
22+ mutex_lock,
2223 protocols:: { Status , StatusCode , BAD_MESSAGE_ALLOWED_EACH_HOUR , MESSAGE_TIMEOUT } ,
24+ read_lock,
2325 types:: { Mutex , RwLock } ,
26+ write_lock,
2427} ;
2528
2629pub type BadMessageRateLimiter < T > = RateLimiter < T , DefaultKeyedStateStore < T > , DefaultClock > ;
@@ -1299,10 +1302,7 @@ impl Peers {
12991302 self . mark_fetching_headers_timeout ( index) ;
13001303 self . mark_fetching_txs_timeout ( index) ;
13011304 self . inner . remove ( & index) ;
1302- #[ cfg( target_arch = "wasm32" ) ]
1303- self . rate_limiter . lock ( ) . await . retain_recent ( ) ;
1304- #[ cfg( not( target_arch = "wasm32" ) ) ]
1305- let _ignore_error = self . rate_limiter . lock ( ) . map ( |inner| inner. retain_recent ( ) ) ;
1305+ mutex_lock ! ( self . rate_limiter) . retain_recent ( ) ;
13061306 }
13071307
13081308 pub ( crate ) fn get_peers_index ( & self ) -> Vec < PeerIndex > {
@@ -1644,64 +1644,33 @@ impl Peers {
16441644 Err ( StatusCode :: PeerIsNotFound . into ( ) )
16451645 }
16461646 }
1647- #[ cfg( target_arch = "wasm32" ) ]
16481647 pub ( crate ) async fn update_min_filtered_block_number (
16491648 & self ,
16501649 min_filtered_block_number : BlockNumber ,
16511650 ) {
16521651 let should_cached_check_point_index =
16531652 self . calc_cached_check_point_index_when_sync_at ( min_filtered_block_number + 1 ) ;
1654- let current_cached_check_point_index = self . cached_block_filter_hashes . read ( ) . await . 0 ;
1653+ let current_cached_check_point_index = read_lock ! ( self . cached_block_filter_hashes) . 0 ;
16551654 if current_cached_check_point_index != should_cached_check_point_index {
1656- let mut tmp = self . cached_block_filter_hashes . write ( ) . await ;
1657- tmp. 0 = should_cached_check_point_index;
1658- tmp. 1 . clear ( ) ;
1659- }
1660- }
1661- #[ cfg( not( target_arch = "wasm32" ) ) ]
1662- pub ( crate ) fn update_min_filtered_block_number ( & self , min_filtered_block_number : BlockNumber ) {
1663- let should_cached_check_point_index =
1664- self . calc_cached_check_point_index_when_sync_at ( min_filtered_block_number + 1 ) ;
1665- let current_cached_check_point_index =
1666- self . cached_block_filter_hashes . read ( ) . expect ( "poisoned" ) . 0 ;
1667- if current_cached_check_point_index != should_cached_check_point_index {
1668- let mut tmp = self . cached_block_filter_hashes . write ( ) . expect ( "poisoned" ) ;
1655+ let mut tmp = write_lock ! ( self . cached_block_filter_hashes) ;
16691656 tmp. 0 = should_cached_check_point_index;
16701657 tmp. 1 . clear ( ) ;
16711658 }
16721659 }
16731660
1674- #[ cfg( target_arch = "wasm32" ) ]
16751661 pub ( crate ) async fn get_cached_block_filter_hashes ( & self ) -> ( u32 , Vec < packed:: Byte32 > ) {
1676- self . cached_block_filter_hashes . read ( ) . await . clone ( )
1677- }
1678- #[ cfg( not( target_arch = "wasm32" ) ) ]
1679- pub ( crate ) fn get_cached_block_filter_hashes ( & self ) -> ( u32 , Vec < packed:: Byte32 > ) {
1680- self . cached_block_filter_hashes
1681- . read ( )
1682- . expect ( "poisoned" )
1683- . clone ( )
1662+ read_lock ! ( self . cached_block_filter_hashes) . clone ( )
16841663 }
16851664 pub ( crate ) async fn update_cached_block_filter_hashes ( & self , hashes : Vec < packed:: Byte32 > ) {
1686- #[ cfg( target_arch = "wasm32" ) ]
1687- {
1688- self . cached_block_filter_hashes . write ( ) . await . 1 = hashes;
1689- }
1690- #[ cfg( not( target_arch = "wasm32" ) ) ]
1691- {
1692- self . cached_block_filter_hashes . write ( ) . expect ( "poisoned" ) . 1 = hashes;
1693- }
1665+ write_lock ! ( self . cached_block_filter_hashes) . 1 = hashes;
16941666 }
16951667
16961668 pub ( crate ) async fn if_cached_block_filter_hashes_require_update (
16971669 & self ,
16981670 finalized_check_point_index : u32 ,
16991671 ) -> Option < BlockNumber > {
17001672 let ( cached_index, cached_length) = {
1701- #[ cfg( target_arch = "wasm32" ) ]
1702- let tmp = self . cached_block_filter_hashes . read ( ) . await ;
1703- #[ cfg( not( target_arch = "wasm32" ) ) ]
1704- let tmp = self . cached_block_filter_hashes . read ( ) . expect ( "poisoned" ) ;
1673+ let tmp = read_lock ! ( self . cached_block_filter_hashes) ;
17051674 ( tmp. 0 , tmp. 1 . len ( ) )
17061675 } ;
17071676 if cached_index >= finalized_check_point_index {
@@ -1884,10 +1853,7 @@ impl Peers {
18841853 // Check:
18851854 // - If cached block filter hashes is same check point as the required,
18861855 // - If all block filter hashes in that check point are downloaded.
1887- #[ cfg( target_arch = "wasm32" ) ]
18881856 let cached_data = self . get_cached_block_filter_hashes ( ) . await ;
1889- #[ cfg( not( target_arch = "wasm32" ) ) ]
1890- let cached_data = self . get_cached_block_filter_hashes ( ) ;
18911857
18921858 let current_cached_check_point_index = cached_data. 0 ;
18931859 should_cached_check_point_index == current_cached_check_point_index
@@ -2026,22 +1992,10 @@ impl Peers {
20261992 if self . bad_message_allowed_each_hour == 0 {
20271993 return true ;
20281994 }
2029- #[ cfg( target_arch = "wasm32" ) ]
2030- let result = self
2031- . rate_limiter
2032- . lock ( )
2033- . await
1995+ mutex_lock ! ( self . rate_limiter)
20341996 . check_key ( & peer_index)
20351997 . map_err ( |_| ( ) )
2036- . is_err ( ) ;
2037- #[ cfg( not( target_arch = "wasm32" ) ) ]
2038- let result = self
2039- . rate_limiter
2040- . lock ( )
2041- . map_err ( |_| ( ) )
2042- . and_then ( |inner| inner. check_key ( & peer_index) . map_err ( |_| ( ) ) )
2043- . is_err ( ) ;
2044- result
1998+ . is_err ( )
20451999 }
20462000}
20472001
0 commit comments