Skip to content

Commit

Permalink
Merge pull request #6 from rust-util-collections/master
Browse files Browse the repository at this point in the history
remove trie cache for better stability
  • Loading branch information
ktmlm authored Jun 27, 2023
2 parents a4c1bd2 + 7a404c7 commit 4a3b0f8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 43 deletions.
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ rug = "1.19"
sha2 = "0.10"
ripemd = "0.1"

ruc = { version = "4.1.0", features = ["crypto"] }

vsdb = { version = "0.55.0", features = ["extra_types"] }
# vsdb = { path = "../vsdb/wrappers", features = ["extra_types"] }

vsdb_trie_db = "0.7.0"
# vsdb_trie_db = { path = "../vsdb/utils/trie_db" }
ruc = { version = "5.0.10", features = ["crypto"] }
vsdb = { version = "0.60.0", default-features = false, features = ["extra_types"] }
vsdb_trie_db = "0.14.0"

####################################################################
####################################################################
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl APIAdapter for DefaultAPIAdapter {
) -> Result<Vec<u8>> {
let state_trie_tree = self
.trie_db
.trie_restore(&WORLD_STATE_META_KEY, None, state_root.into())
.trie_restore(&WORLD_STATE_META_KEY, state_root.into())
.c(d!())?;

let raw_account = state_trie_tree
Expand All @@ -181,7 +181,7 @@ impl APIAdapter for DefaultAPIAdapter {

let storage_trie_tree = self
.trie_db
.trie_restore(address.as_bytes(), None, account.storage_root.into())
.trie_restore(address.as_bytes(), account.storage_root.into())
.c(d!())?;

let hash: Hash = BigEndianHash::from_uint(&position);
Expand Down
23 changes: 10 additions & 13 deletions crates/executor/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a> Backend for RTEvmExecutorAdapter<'a> {
Ok(H256::default())
} else {
self.trie_db
.trie_restore(address.as_bytes(), None, storage_root.into())
.trie_restore(address.as_bytes(), storage_root.into())
.map(|trie| match trie.get(index.as_bytes()) {
Ok(Some(res)) => H256::from_slice(res.as_ref()),
_ => H256::default(),
Expand Down Expand Up @@ -223,11 +223,8 @@ impl<'a> RTEvmExecutorAdapter<'a> {
trie_db: &'a MptStore,
storage: &'a Storage,
exec_ctx: ExecutorContext,
world_state_cache_size: Option<usize>,
) -> Result<Self> {
let state = trie_db
.trie_create(&WORLD_STATE_META_KEY, world_state_cache_size, false)
.c(d!())?;
let state = trie_db.trie_create(&WORLD_STATE_META_KEY, false).c(d!())?;
Ok(RTEvmExecutorAdapter {
state,
trie_db,
Expand All @@ -243,7 +240,7 @@ impl<'a> RTEvmExecutorAdapter<'a> {
exec_ctx: ExecutorContext,
) -> Result<Self> {
let state = trie_db
.trie_restore(&WORLD_STATE_META_KEY, None, state_root.into())
.trie_restore(&WORLD_STATE_META_KEY, state_root.into())
.c(d!())?;

Ok(RTEvmExecutorAdapter {
Expand Down Expand Up @@ -276,19 +273,19 @@ impl<'a> RTEvmExecutorAdapter<'a> {
};

let storage_trie = if reset_storage {
self.trie_db
.trie_create(address.as_bytes(), None, true)
.c(d!())
self.trie_db.trie_create(address.as_bytes(), true).c(d!())
} else if existing {
self.trie_db
.trie_restore(address.as_bytes(), None, old_account.storage_root.into())
.trie_restore(address.as_bytes(), old_account.storage_root.into())
.c(d!())
} else {
// this address does not exist in the world state,
// so we reset it in the trie backend db also.
self.trie_db
.trie_create(address.as_bytes(), None, true)
.c(d!("{}, {:?}", address, address.as_bytes()))
self.trie_db.trie_create(address.as_bytes(), true).c(d!(
"{}, {:?}",
address,
address.as_bytes()
))
};

let mut storage_trie = pnk!(storage_trie);
Expand Down
2 changes: 1 addition & 1 deletion crates/model/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Hasher;

impl Hasher {
pub fn digest(data: impl AsRef<[u8]>) -> Hash {
crypto::hash(data.as_ref()).into()
crypto::keccak_hash(data.as_ref()).into()
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/model/src/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,7 @@ impl SignatureComponents {
}

pub fn extract_chain_id(v: u64) -> Option<u64> {
if v >= 35 {
Some((v - 35) / 2u64)
} else {
None
}
if v >= 35 { Some((v - 35) / 2u64) } else { None }
}

#[allow(clippy::len_without_is_empty)]
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ pub fn get_account_by_backend(
};

let state = trie_db
.trie_restore(&WORLD_STATE_META_KEY, None, header.state_root.into())
.trie_restore(&WORLD_STATE_META_KEY, header.state_root.into())
.c(d!())?;

get_account_by_state(&state, address).c(d!())
Expand All @@ -416,7 +416,7 @@ pub fn save_account_by_backend(
) -> Result<()> {
let header = storage.get_latest_block_header().c(d!())?;
let mut state = trie_db
.trie_restore(&WORLD_STATE_META_KEY, None, header.state_root.into())
.trie_restore(&WORLD_STATE_META_KEY, header.state_root.into())
.c(d!())?;

save_account_by_state(&mut state, address, account).c(d!())
Expand Down
1 change: 0 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl Config {
let rt = EvmRuntime::restore_or_create(
self.chain_id,
&self.genesis_token_distributions,
None,
)
.c(d!())?;

Expand Down
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,13 @@ impl EvmRuntime {
pub fn create(
chain_id: u64,
token_distributions: &[TokenDistributon],
world_state_cache_size: Option<usize>,
) -> Result<Self> {
let r = Self::new(chain_id, MptStore::new(), Storage::default());

{
let mut exector_adapter = RTEvmExecutorAdapter::new(
&r.trie_db,
&r.storage,
Default::default(),
world_state_cache_size,
)
.c(d!())?;
let mut exector_adapter =
RTEvmExecutorAdapter::new(&r.trie_db, &r.storage, Default::default())
.c(d!())?;

token_distributions
.iter()
Expand Down Expand Up @@ -171,12 +166,11 @@ impl EvmRuntime {
pub fn restore_or_create(
chain_id: u64,
token_distributions: &[TokenDistributon],
world_state_cache_size: Option<usize>,
) -> Result<Self> {
if let Some(rt) = Self::restore().c(d!())? {
Ok(rt)
} else {
Self::create(chain_id, token_distributions, world_state_cache_size).c(d!())
Self::create(chain_id, token_distributions).c(d!())
}
}

Expand Down Expand Up @@ -255,11 +249,11 @@ impl EvmRuntime {
.c(d!())?;

if let Some(hdr) = http_hdr {
tokio::spawn(async { hdr.await });
tokio::spawn(hdr);
}

if let Some(hdr) = ws_hdr {
tokio::spawn(async { hdr.await });
tokio::spawn(hdr);
}

Ok(())
Expand Down

0 comments on commit 4a3b0f8

Please sign in to comment.