Skip to content

Commit e22ef56

Browse files
Seulgi Kimsgkim126
authored andcommitted
Make DB store metadata
1 parent 14e5a77 commit e22ef56

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

core/src/state/backend.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
use ctypes::Address;
4141
use hashdb::{AsHashDB, HashDB};
4242

43-
use super::{Account, Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Shard, ShardAddress};
43+
use super::{
44+
Account, Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Metadata, MetadataAddress, Shard, ShardAddress,
45+
};
4446

4547
/// State backend. See module docs for more details.
4648
pub trait Backend: Send {
@@ -54,11 +56,13 @@ pub trait Backend: Send {
5456
pub trait TopBackend: Send {
5557
/// Add an account entry to the cache.
5658
fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool);
59+
fn add_to_metadata_cache(&mut self, address: MetadataAddress, item: Option<Metadata>, modified: bool);
5760
fn add_to_shard_cache(&mut self, address: ShardAddress, item: Option<Shard>, modified: bool);
5861

5962
/// Get basic copy of the cached account. Not required to include storage.
6063
/// Returns 'None' if cache is disabled or if the account is not cached.
6164
fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>>;
65+
fn get_cached_metadata(&self, addr: &MetadataAddress) -> Option<Option<Metadata>>;
6266
fn get_cached_shard(&self, addr: &ShardAddress) -> Option<Option<Shard>>;
6367

6468
/// Get value from a cached account.
@@ -100,12 +104,18 @@ impl<H: AsHashDB + Send + Sync> Backend for Basic<H> {
100104
impl<H: AsHashDB + Send + Sync> TopBackend for Basic<H> {
101105
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) {}
102106

107+
fn add_to_metadata_cache(&mut self, _: MetadataAddress, _: Option<Metadata>, _: bool) {}
108+
103109
fn add_to_shard_cache(&mut self, _: ShardAddress, _: Option<Shard>, _modified: bool) {}
104110

105111
fn get_cached_account(&self, _: &Address) -> Option<Option<Account>> {
106112
None
107113
}
108114

115+
fn get_cached_metadata(&self, _: &MetadataAddress) -> Option<Option<Metadata>> {
116+
None
117+
}
118+
109119
fn get_cached_shard(&self, _: &ShardAddress) -> Option<Option<Shard>> {
110120
None
111121
}

core/src/state/top_level.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use super::asset_scheme::{AssetScheme, AssetSchemeAddress};
5454
use super::backend::{Backend, ShardBackend, TopBackend};
5555
use super::cache::{Cache, CacheableItem};
5656
use super::info::{ShardStateInfo, TopStateInfo};
57+
use super::metadata::{Metadata, MetadataAddress};
5758
use super::shard::{Shard, ShardAddress};
5859
use super::shard_level::ShardLevelState;
5960
use super::shard_state::{ShardState, TransactionOutcome};
@@ -118,6 +119,7 @@ pub struct TopLevelState<B> {
118119
db: B,
119120
root: H256,
120121
account: Cache<Account>,
122+
metadata: Cache<Metadata>,
121123
shard: Cache<Shard>,
122124
id_of_checkpoints: Vec<CheckpointId>,
123125
trie_factory: TrieFactory,
@@ -175,6 +177,7 @@ impl<B: Backend + TopBackend> StateWithCheckpoint for TopLevelState<B> {
175177
fn create_checkpoint(&mut self, id: CheckpointId) {
176178
self.id_of_checkpoints.push(id);
177179
self.account.checkpoint();
180+
self.metadata.checkpoint();
178181
self.shard.checkpoint();
179182
}
180183

@@ -183,6 +186,7 @@ impl<B: Backend + TopBackend> StateWithCheckpoint for TopLevelState<B> {
183186
assert_eq!(expected, id);
184187

185188
self.account.discard_checkpoint();
189+
self.metadata.discard_checkpoint();
186190
self.shard.discard_checkpoint();
187191
}
188192

@@ -191,6 +195,7 @@ impl<B: Backend + TopBackend> StateWithCheckpoint for TopLevelState<B> {
191195
assert_eq!(expected, id);
192196

193197
self.account.revert_to_checkpoint();
198+
self.metadata.revert_to_checkpoint();
194199
self.shard.revert_to_checkpoint();
195200
}
196201
}
@@ -199,6 +204,7 @@ impl<B: Backend + TopBackend + ShardBackend> StateWithCache for TopLevelState<B>
199204
fn commit(&mut self) -> TrieResult<()> {
200205
let mut trie = self.trie_factory.from_existing(self.db.as_hashdb_mut(), &mut self.root)?;
201206
self.account.commit(&mut trie)?;
207+
self.metadata.commit(&mut trie)?;
202208
self.shard.commit(&mut trie)?;
203209
Ok(())
204210
}
@@ -208,13 +214,17 @@ impl<B: Backend + TopBackend + ShardBackend> StateWithCache for TopLevelState<B>
208214
self.account.propagate_to_global_cache(|address, item, modified| {
209215
db.add_to_account_cache(address, item, modified);
210216
});
217+
self.metadata.propagate_to_global_cache(|address, item, modified| {
218+
db.add_to_metadata_cache(address, item, modified);
219+
});
211220
self.shard.propagate_to_global_cache(|address, item, modified| {
212221
db.add_to_shard_cache(address, item, modified);
213222
});
214223
}
215224

216225
fn clear(&mut self) {
217226
self.account.clear();
227+
self.metadata.clear();
218228
self.shard.clear();
219229
}
220230
}
@@ -233,6 +243,7 @@ impl<B: Backend + TopBackend + ShardBackend + Clone> TopLevelState<B> {
233243
db,
234244
root,
235245
account: Cache::new(),
246+
metadata: Cache::new(),
236247
shard: Cache::new(),
237248
id_of_checkpoints: Default::default(),
238249
trie_factory,
@@ -249,6 +260,7 @@ impl<B: Backend + TopBackend + ShardBackend + Clone> TopLevelState<B> {
249260
db,
250261
root,
251262
account: Cache::new(),
263+
metadata: Cache::new(),
252264
shard: Cache::new(),
253265
id_of_checkpoints: Default::default(),
254266
trie_factory,
@@ -400,6 +412,8 @@ trait TopStateInternal<B: Backend + TopBackend> {
400412
/// Populates local cache if nothing found.
401413
fn require_account<'a>(&'a self, a: &Address) -> TrieResult<RefMut<'a, Account>>;
402414

415+
fn require_metadata<'a>(&'a self) -> TrieResult<RefMut<'a, Metadata>>;
416+
403417
fn require_shard<'a>(&'a self, shard_id: u32) -> TrieResult<RefMut<'a, Shard>>;
404418
}
405419

@@ -422,6 +436,14 @@ impl<B: Backend + TopBackend> TopStateInternal<B> for TopLevelState<B> {
422436
self.account.require_item_or_from(a, default, db, from_db)
423437
}
424438

439+
fn require_metadata<'a>(&'a self) -> TrieResult<RefMut<'a, Metadata>> {
440+
let default = || Metadata::new(0);
441+
let db = self.trie_factory.readonly(self.db.as_hashdb(), &self.root)?;
442+
let address = MetadataAddress::new();
443+
let from_db = || self.db.get_cached_metadata(&address);
444+
self.metadata.require_item_or_from(&address, default, db, from_db)
445+
}
446+
425447
fn require_shard<'a>(&'a self, shard_id: u32) -> TrieResult<RefMut<'a, Shard>> {
426448
let default = || Shard::new(BLAKE_NULL_RLP);
427449
let db = self.trie_factory.readonly(self.db.as_hashdb(), &self.root)?;
@@ -433,7 +455,10 @@ impl<B: Backend + TopBackend> TopStateInternal<B> for TopLevelState<B> {
433455

434456
impl<B: TopBackend + ShardBackend> fmt::Debug for TopLevelState<B> {
435457
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436-
write!(f, "account: {:?} shard: {:?}", self.account, self.shard)
458+
writeln!(f, "account: {:?}", self.account)?;
459+
writeln!(f, "metadata: {:?}", self.metadata)?;
460+
writeln!(f, "shard: {:?}", self.shard)?;
461+
Ok(())
437462
}
438463
}
439464

@@ -446,6 +471,7 @@ impl Clone for TopLevelState<StateDB> {
446471
root: self.root.clone(),
447472
id_of_checkpoints: self.id_of_checkpoints.clone(),
448473
account: self.account.clone(),
474+
metadata: self.metadata.clone(),
449475
shard: self.shard.clone(),
450476
trie_factory: self.trie_factory.clone(),
451477
}

core/src/state_db.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ use parking_lot::Mutex;
2828
use util_error::UtilError;
2929

3030
use 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
};
3334
use super::types::BlockNumber;
3435

3536
const STATE_CACHE_BLOCKS: usize = 12;
3637

3738
// The percentage of supplied cache size to go to accounts.
3839
const 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;
4042
const ASSET_SCHEME_CACHE_RATIO: usize = 10;
4143
const 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

Comments
 (0)