@@ -3,35 +3,30 @@ use std::{borrow::ToOwned, num::NonZeroUsize, sync::RwLock};
33use ethereum:: { BlockAny , TransactionV2 } ;
44use ethereum_types:: { H256 , U256 } ;
55use lru:: LruCache ;
6- use parking_lot:: Mutex ;
76
87use super :: traits:: { BlockStorage , Rollback , TransactionStorage } ;
98use crate :: Result ;
109
1110#[ derive( Debug ) ]
1211pub struct Cache {
13- transactions : Mutex < LruCache < H256 , TransactionV2 > > ,
14- blocks : Mutex < LruCache < U256 , BlockAny > > ,
15- block_hashes : Mutex < LruCache < H256 , U256 > > ,
12+ transactions : RwLock < LruCache < H256 , TransactionV2 > > ,
13+ blocks : RwLock < LruCache < U256 , BlockAny > > ,
14+ block_hashes : RwLock < LruCache < H256 , U256 > > ,
1615 latest_block : RwLock < Option < BlockAny > > ,
17- contract_code : Mutex < LruCache < H256 , Vec < u8 > > > ,
1816}
1917
2018impl Cache {
2119 const DEFAULT_CACHE_SIZE : usize = 1000 ;
2220
2321 pub fn new ( cache_size : Option < usize > ) -> Self {
2422 Cache {
25- transactions : Mutex :: new ( LruCache :: new (
23+ transactions : RwLock :: new ( LruCache :: new (
2624 NonZeroUsize :: new ( cache_size. unwrap_or ( Self :: DEFAULT_CACHE_SIZE ) ) . unwrap ( ) ,
2725 ) ) ,
28- blocks : Mutex :: new ( LruCache :: new (
26+ blocks : RwLock :: new ( LruCache :: new (
2927 NonZeroUsize :: new ( cache_size. unwrap_or ( Self :: DEFAULT_CACHE_SIZE ) ) . unwrap ( ) ,
3028 ) ) ,
31- block_hashes : Mutex :: new ( LruCache :: new (
32- NonZeroUsize :: new ( cache_size. unwrap_or ( Self :: DEFAULT_CACHE_SIZE ) ) . unwrap ( ) ,
33- ) ) ,
34- contract_code : Mutex :: new ( LruCache :: new (
29+ block_hashes : RwLock :: new ( LruCache :: new (
3530 NonZeroUsize :: new ( cache_size. unwrap_or ( Self :: DEFAULT_CACHE_SIZE ) ) . unwrap ( ) ,
3631 ) ) ,
3732 latest_block : RwLock :: new ( None ) ,
@@ -41,13 +36,19 @@ impl Cache {
4136
4237impl BlockStorage for Cache {
4338 fn get_block_by_number ( & self , number : & U256 ) -> Result < Option < BlockAny > > {
44- let block = self . blocks . lock ( ) . get ( number) . map ( ToOwned :: to_owned) ;
39+ let block = self
40+ . blocks
41+ . write ( )
42+ . unwrap ( )
43+ . get ( number)
44+ . map ( ToOwned :: to_owned) ;
4545 Ok ( block)
4646 }
4747
4848 fn get_block_by_hash ( & self , block_hash : & H256 ) -> Result < Option < BlockAny > > {
4949 self . block_hashes
50- . lock ( )
50+ . write ( )
51+ . unwrap ( )
5152 . get ( block_hash)
5253 . map_or ( Ok ( None ) , |block_number| {
5354 self . get_block_by_number ( block_number)
@@ -59,8 +60,11 @@ impl BlockStorage for Cache {
5960
6061 let block_number = block. header . number ;
6162 let hash = block. header . hash ( ) ;
62- self . blocks . lock ( ) . put ( block_number, block. clone ( ) ) ;
63- self . block_hashes . lock ( ) . put ( hash, block_number) ;
63+ self . blocks
64+ . write ( )
65+ . unwrap ( )
66+ . put ( block_number, block. clone ( ) ) ;
67+ self . block_hashes . write ( ) . unwrap ( ) . put ( hash, block_number) ;
6468 Ok ( ( ) )
6569 }
6670
@@ -83,7 +87,7 @@ impl BlockStorage for Cache {
8387
8488impl TransactionStorage for Cache {
8589 fn extend_transactions_from_block ( & self , block : & BlockAny ) -> Result < ( ) > {
86- let mut cache = self . transactions . lock ( ) ;
90+ let mut cache = self . transactions . write ( ) . unwrap ( ) ;
8791
8892 for transaction in & block. transactions {
8993 let hash = transaction. hash ( ) ;
@@ -93,7 +97,12 @@ impl TransactionStorage for Cache {
9397 }
9498
9599 fn get_transaction_by_hash ( & self , hash : & H256 ) -> Result < Option < TransactionV2 > > {
96- let transaction = self . transactions . lock ( ) . get ( hash) . map ( ToOwned :: to_owned) ;
100+ let transaction = self
101+ . transactions
102+ . write ( )
103+ . unwrap ( )
104+ . get ( hash)
105+ . map ( ToOwned :: to_owned) ;
97106 Ok ( transaction)
98107 }
99108
@@ -103,7 +112,8 @@ impl TransactionStorage for Cache {
103112 index : usize ,
104113 ) -> Result < Option < TransactionV2 > > {
105114 self . block_hashes
106- . lock ( )
115+ . write ( )
116+ . unwrap ( )
107117 . get ( block_hash)
108118 . map_or ( Ok ( None ) , |block_number| {
109119 self . get_transaction_by_block_number_and_index ( block_number, index)
@@ -117,15 +127,17 @@ impl TransactionStorage for Cache {
117127 ) -> Result < Option < TransactionV2 > > {
118128 let transaction = self
119129 . blocks
120- . lock ( )
130+ . write ( )
131+ . unwrap ( )
121132 . get ( block_number)
122133 . and_then ( |block| block. transactions . get ( index) . map ( ToOwned :: to_owned) ) ;
123134 Ok ( transaction)
124135 }
125136
126137 fn put_transaction ( & self , transaction : & TransactionV2 ) -> Result < ( ) > {
127138 self . transactions
128- . lock ( )
139+ . write ( )
140+ . unwrap ( )
129141 . put ( transaction. hash ( ) , transaction. clone ( ) ) ;
130142 Ok ( ( ) )
131143 }
@@ -134,28 +146,17 @@ impl TransactionStorage for Cache {
134146impl Rollback for Cache {
135147 fn disconnect_latest_block ( & self ) -> Result < ( ) > {
136148 if let Some ( block) = self . get_latest_block ( ) ? {
137- let mut transaction_cache = self . transactions . lock ( ) ;
149+ let mut transaction_cache = self . transactions . write ( ) . unwrap ( ) ;
138150 for tx in & block. transactions {
139151 transaction_cache. pop ( & tx. hash ( ) ) ;
140152 }
141153
142- self . block_hashes . lock ( ) . pop ( & block. header . hash ( ) ) ;
143- self . blocks . lock ( ) . pop ( & block. header . number ) ;
154+ self . block_hashes . write ( ) . unwrap ( ) . pop ( & block. header . hash ( ) ) ;
155+ self . blocks . write ( ) . unwrap ( ) . pop ( & block. header . number ) ;
144156
145157 let previous_block = self . get_block_by_hash ( & block. header . parent_hash ) ?;
146158 self . put_latest_block ( previous_block. as_ref ( ) ) ?;
147159 }
148160 Ok ( ( ) )
149161 }
150162}
151-
152- impl Cache {
153- pub fn get_code_by_hash ( & self , hash : & H256 ) -> Result < Option < Vec < u8 > > > {
154- Ok ( self . contract_code . lock ( ) . get ( hash) . map ( ToOwned :: to_owned) )
155- }
156-
157- pub fn put_code ( & self , hash : H256 , code : & [ u8 ] ) -> Result < ( ) > {
158- self . contract_code . lock ( ) . put ( hash, code. to_vec ( ) ) ;
159- Ok ( ( ) )
160- }
161- }
0 commit comments