@@ -28,15 +28,17 @@ use parking_lot::Mutex;
2828use util_error:: UtilError ;
2929
3030use super :: state:: {
31- self , Account , Asset , AssetAddress , AssetScheme , AssetSchemeAddress , CacheableItem , Shard , ShardAddress ,
31+ self , Account , Asset , AssetAddress , AssetScheme , AssetSchemeAddress , CacheableItem , Metadata , MetadataAddress ,
32+ Shard , ShardAddress ,
3233} ;
3334use super :: types:: BlockNumber ;
3435
3536const STATE_CACHE_BLOCKS : usize = 12 ;
3637
3738// The percentage of supplied cache size to go to accounts.
3839const ACCOUNT_CACHE_RATIO : usize = 40 ;
39- const SHARD_CACHE_RATIO : usize = 10 ;
40+ const METADATA_CACHE_RATIO : usize = 1 ;
41+ const SHARD_CACHE_RATIO : usize = 9 ;
4042const ASSET_SCHEME_CACHE_RATIO : usize = 10 ;
4143const ASSET_CACHE_RATIO : usize = 40 ;
4244
@@ -100,12 +102,14 @@ pub struct StateDB {
100102 db : Box < JournalDB > ,
101103 /// Shared canonical state cache.
102104 account_cache : Arc < Mutex < Cache < Account > > > ,
105+ metadata_cache : Arc < Mutex < Cache < Metadata > > > ,
103106 shard_cache : Arc < Mutex < Cache < Shard > > > ,
104107 asset_scheme_cache : Arc < Mutex < Cache < AssetScheme > > > ,
105108 asset_cache : Arc < Mutex < Cache < Asset > > > ,
106109
107110 /// Local dirty cache.
108111 local_account_cache : Vec < CacheQueueItem < Account > > ,
112+ local_metadata_cache : Vec < CacheQueueItem < Metadata > > ,
109113 local_shard_cache : Vec < CacheQueueItem < Shard > > ,
110114 local_asset_scheme_cache : Vec < CacheQueueItem < AssetScheme > > ,
111115 local_asset_cache : Vec < CacheQueueItem < Asset > > ,
@@ -124,11 +128,18 @@ impl StateDB {
124128 // TODO: make the cache size actually accurate by moving the account storage cache
125129 // into the `AccountCache` structure as its own `LruCache<(Address, H256), H256>`.
126130 pub fn new ( db : Box < JournalDB > , cache_size : usize ) -> StateDB {
127- assert_eq ! ( 100 , ACCOUNT_CACHE_RATIO + SHARD_CACHE_RATIO + ASSET_SCHEME_CACHE_RATIO + ASSET_CACHE_RATIO ) ;
131+ assert_eq ! (
132+ 100 ,
133+ ACCOUNT_CACHE_RATIO + METADATA_CACHE_RATIO + SHARD_CACHE_RATIO + ASSET_SCHEME_CACHE_RATIO
134+ + ASSET_CACHE_RATIO
135+ ) ;
128136
129137 let account_cache_size = cache_size * ACCOUNT_CACHE_RATIO / 100 ;
130138 let account_cache_items = account_cache_size / :: std:: mem:: size_of :: < Option < Account > > ( ) ;
131139
140+ let metadata_cache_size = cache_size * METADATA_CACHE_RATIO / 100 ;
141+ let metadata_cache_items = metadata_cache_size / :: std:: mem:: size_of :: < Option < Metadata > > ( ) ;
142+
132143 let shard_cache_size = cache_size * SHARD_CACHE_RATIO / 100 ;
133144 let shard_cache_items = shard_cache_size / :: std:: mem:: size_of :: < Option < Shard > > ( ) ;
134145
@@ -144,6 +155,10 @@ impl StateDB {
144155 cache : LruCache :: new ( account_cache_items) ,
145156 modifications : VecDeque :: new ( ) ,
146157 } ) ) ,
158+ metadata_cache : Arc :: new ( Mutex :: new ( Cache {
159+ cache : LruCache :: new ( metadata_cache_items) ,
160+ modifications : VecDeque :: new ( ) ,
161+ } ) ) ,
147162 shard_cache : Arc :: new ( Mutex :: new ( Cache {
148163 cache : LruCache :: new ( shard_cache_items) ,
149164 modifications : VecDeque :: new ( ) ,
@@ -158,6 +173,7 @@ impl StateDB {
158173 } ) ) ,
159174
160175 local_account_cache : Vec :: new ( ) ,
176+ local_metadata_cache : Vec :: new ( ) ,
161177 local_shard_cache : Vec :: new ( ) ,
162178 local_asset_scheme_cache : Vec :: new ( ) ,
163179 local_asset_cache : Vec :: new ( ) ,
@@ -362,11 +378,13 @@ impl StateDB {
362378 StateDB {
363379 db : self . db . boxed_clone ( ) ,
364380 account_cache : self . account_cache . clone ( ) ,
381+ metadata_cache : self . metadata_cache . clone ( ) ,
365382 shard_cache : self . shard_cache . clone ( ) ,
366383 asset_scheme_cache : self . asset_scheme_cache . clone ( ) ,
367384 asset_cache : self . asset_cache . clone ( ) ,
368385
369386 local_account_cache : Vec :: new ( ) ,
387+ local_metadata_cache : Vec :: new ( ) ,
370388 local_shard_cache : Vec :: new ( ) ,
371389 local_asset_scheme_cache : Vec :: new ( ) ,
372390 local_asset_cache : Vec :: new ( ) ,
@@ -470,11 +488,13 @@ impl Clone for StateDB {
470488 Self {
471489 db : self . db . boxed_clone ( ) ,
472490 account_cache : self . account_cache . clone ( ) ,
491+ metadata_cache : self . metadata_cache . clone ( ) ,
473492 shard_cache : self . shard_cache . clone ( ) ,
474493 asset_scheme_cache : self . asset_scheme_cache . clone ( ) ,
475494 asset_cache : self . asset_cache . clone ( ) ,
476495
477496 local_account_cache : Vec :: new ( ) ,
497+ local_metadata_cache : Vec :: new ( ) ,
478498 local_shard_cache : Vec :: new ( ) ,
479499 local_asset_scheme_cache : Vec :: new ( ) ,
480500 local_asset_cache : Vec :: new ( ) ,
@@ -505,6 +525,14 @@ impl state::TopBackend for StateDB {
505525 } )
506526 }
507527
528+ fn add_to_metadata_cache ( & mut self , address : MetadataAddress , item : Option < Metadata > , modified : bool ) {
529+ self . local_metadata_cache . push ( CacheQueueItem {
530+ address,
531+ item,
532+ modified,
533+ } )
534+ }
535+
508536 fn add_to_shard_cache ( & mut self , address : ShardAddress , item : Option < Shard > , modified : bool ) {
509537 self . local_shard_cache . push ( CacheQueueItem {
510538 address,
@@ -517,6 +545,10 @@ impl state::TopBackend for StateDB {
517545 self . get_cached ( addr, & self . account_cache )
518546 }
519547
548+ fn get_cached_metadata ( & self , addr : & MetadataAddress ) -> Option < Option < Metadata > > {
549+ self . get_cached ( addr, & self . metadata_cache )
550+ }
551+
520552 fn get_cached_shard ( & self , addr : & ShardAddress ) -> Option < Option < Shard > > {
521553 self . get_cached ( addr, & self . shard_cache )
522554 }
0 commit comments