-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-run): split storage into a single file
- Loading branch information
1 parent
2503b82
commit 854557c
Showing
7 changed files
with
100 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub(crate) mod extensions; | ||
pub(crate) mod network; | ||
pub(crate) mod storage; | ||
pub(crate) mod system; | ||
|
||
#[cfg(all( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{path::Path, sync::Arc}; | ||
|
||
use common_config_parser::types::{spec::InitialAccount, ConfigRocksDB}; | ||
use core_db::{RocksAdapter, RocksDB}; | ||
use core_executor::{MPTTrie, RocksTrieDB}; | ||
use core_storage::ImplStorage; | ||
use protocol::{ | ||
async_trait, | ||
codec::ProtocolCodec, | ||
traits::{Context, Storage}, | ||
trie::{self, Trie}, | ||
types::{Account, Block, ExecResp, HasherKeccak, RichBlock, NIL_DATA, RLP_NULL}, | ||
ProtocolResult, | ||
}; | ||
|
||
pub(crate) async fn init_storage<P: AsRef<Path>>( | ||
config: &ConfigRocksDB, | ||
rocksdb_path: P, | ||
triedb_cache_size: usize, | ||
) -> ProtocolResult<( | ||
Arc<ImplStorage<RocksAdapter>>, | ||
Arc<RocksTrieDB>, | ||
Arc<RocksDB>, | ||
)> { | ||
let adapter = Arc::new(RocksAdapter::new(rocksdb_path, config.clone())?); | ||
let inner_db = adapter.inner_db(); | ||
let trie_db = Arc::new(RocksTrieDB::new_evm(adapter.inner_db(), triedb_cache_size)); | ||
let storage = Arc::new(ImplStorage::new(adapter, config.cache_size)); | ||
Ok((storage, trie_db, inner_db)) | ||
} | ||
|
||
#[async_trait] | ||
pub(crate) trait StorageExt: Storage { | ||
async fn try_load_genesis(&self) -> ProtocolResult<Option<Block>> { | ||
self.get_block(Context::new(), 0).await.or_else(|e| { | ||
if e.to_string().contains("GetNone") { | ||
Ok(None) | ||
} else { | ||
Err(e) | ||
} | ||
}) | ||
} | ||
|
||
async fn save_block(&self, rich: &RichBlock, resp: &ExecResp) -> ProtocolResult<()> { | ||
self.update_latest_proof(Context::new(), rich.block.header.proof.clone()) | ||
.await?; | ||
self.insert_block(Context::new(), rich.block.clone()) | ||
.await?; | ||
self.insert_transactions(Context::new(), rich.block.header.number, rich.txs.clone()) | ||
.await?; | ||
let (receipts, _logs) = rich.generate_receipts_and_logs(resp); | ||
self.insert_receipts(Context::new(), rich.block.header.number, receipts) | ||
.await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl StorageExt for ImplStorage<RocksAdapter> {} | ||
|
||
pub(crate) trait TrieExt<D, H>: Trie<D, H> + Sized | ||
where | ||
D: trie::DB, | ||
H: trie::Hasher, | ||
{ | ||
fn insert_accounts(mut self, accounts: &[InitialAccount]) -> ProtocolResult<Self> { | ||
for account in accounts { | ||
let raw_account = Account { | ||
nonce: 0u64.into(), | ||
balance: account.balance, | ||
storage_root: RLP_NULL, | ||
code_hash: NIL_DATA, | ||
} | ||
.encode()?; | ||
self.insert(account.address.as_bytes().to_vec(), raw_account.to_vec())?; | ||
} | ||
Ok(self) | ||
} | ||
} | ||
|
||
impl<D> TrieExt<D, HasherKeccak> for MPTTrie<D> where D: trie::DB {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters