Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pana committed Jan 10, 2025
1 parent af8e06a commit f45f264
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 68 deletions.
43 changes: 0 additions & 43 deletions crates/cfxcore/executor/src/state/overlay_account/factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use cfx_rpc_eth_types::AccountOverride;
use cfx_types::{Address, AddressSpaceUtil, AddressWithSpace, Space, U256};
use keccak_hash::KECCAK_EMPTY;
use parking_lot::RwLock;
Expand Down Expand Up @@ -55,48 +54,6 @@ impl OverlayAccount {
overlay_account
}

pub fn from_loaded_with_override(
address: &AddressWithSpace, account: Account,
acc_overrides: &AccountOverride,
) -> Self {
let mut acc = Self::from_loaded(address, account);

if let Some(balance) = acc_overrides.balance {
let curr_balance = *acc.balance();
if curr_balance > U256::zero() {
acc.sub_balance(&curr_balance);
}
acc.add_balance(&balance);
}

if let Some(nonce) = acc_overrides.nonce {
acc.set_nonce(&U256::from(nonce));
}

if let Some(code) = acc_overrides.code.as_ref() {
acc.init_code(code.clone(), address.address);
}

match (
acc_overrides.state.as_ref(),
acc_overrides.state_diff.as_ref(),
) {
(Some(state_override), None) => {
acc.override_storage_read_cache(state_override, true);
}
(None, Some(diff)) => {
acc.override_storage_read_cache(diff, false);
}
(_, _) => {}
}

if acc_overrides.move_precompile_to.is_some() {
// TODO: impl move precompile to logic
}

acc
}

/// Create an OverlayAccount of basic account when the account doesn't exist
/// before.
pub fn new_basic(address: &AddressWithSpace, balance: U256) -> Self {
Expand Down
2 changes: 2 additions & 0 deletions crates/cfxcore/executor/src/state/overlay_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ mod storage;

mod checkpoints;

mod state_override;

#[cfg(test)]
mod tests;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::OverlayAccount;
use cfx_rpc_eth_types::AccountOverride;
use cfx_types::{AddressWithSpace, Space, H256, U256};
use primitives::{Account, StorageValue};
use std::{collections::HashMap, sync::Arc};

impl OverlayAccount {
pub fn from_loaded_with_override(
address: &AddressWithSpace, account: Account,
acc_overrides: &AccountOverride,
) -> Self {
let mut acc = Self::from_loaded(address, account);

if let Some(balance) = acc_overrides.balance {
let curr_balance = *acc.balance();
if curr_balance > U256::zero() {
acc.sub_balance(&curr_balance);
}
acc.add_balance(&balance);
}

if let Some(nonce) = acc_overrides.nonce {
acc.set_nonce(&U256::from(nonce));
}

if let Some(code) = acc_overrides.code.as_ref() {
acc.init_code(code.clone(), address.address);
}

match (
acc_overrides.state.as_ref(),
acc_overrides.state_diff.as_ref(),
) {
(Some(state_override), None) => {
acc.override_storage_read_cache(state_override, true);
}
(None, Some(diff)) => {
acc.override_storage_read_cache(diff, false);
}
(_, _) => {}
}

if acc_overrides.move_precompile_to.is_some() {
// TODO: impl move precompile to logic
}

acc
}

fn override_storage_read_cache(
&mut self, account_storage: &HashMap<H256, H256>,
complete_override: bool,
) {
if complete_override {
self.storage_overrided = true;
}
assert!(self.storage_write_checkpoint.is_none());
let read_cache = Arc::get_mut(&mut self.storage_read_cache)
.expect("override should happen when no checkpoint")
.get_mut();
for (key, value) in account_storage {
let key = key.as_bytes().to_vec();
let value = U256::from_big_endian(value.as_bytes());
let owner = if self.address.space == Space::Native {
Some(self.address.address)
} else {
None
};
let storage_value = StorageValue { owner, value };
read_cache.insert(key, storage_value);
}
}
}
27 changes: 2 additions & 25 deletions crates/cfxcore/executor/src/state/overlay_account/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use cfx_parameters::{
staking::COLLATERAL_UNITS_PER_STORAGE_KEY,
};
use cfx_statedb::{Result as DbResult, StateDbExt, StateDbGeneric};
use cfx_types::{Address, Space, H256, U256};
use cfx_types::{Address, Space, U256};

use primitives::{
SkipInputCheck, StorageKey, StorageKeyWithSpace, StorageValue,
};
use std::collections::{hash_map::Entry::*, HashMap};
use std::collections::hash_map::Entry::*;

use super::OverlayAccount;

Expand Down Expand Up @@ -240,29 +240,6 @@ impl OverlayAccount {
&& self.address.address != SYSTEM_STORAGE_ADDRESS
}

pub fn override_storage_read_cache(
&mut self, account_storage: &HashMap<H256, H256>,
complete_override: bool,
) {
if complete_override {
self.storage_read_cache.write().clear();
self.storage_overrided = true;
}
assert!(self.storage_write_checkpoint.is_none());
for (key, value) in account_storage {
let key = key.as_bytes().to_vec();
let value = U256::from_big_endian(value.as_bytes());
// self.update_storage_read_cache(key, value);
let owner = if self.address.space == Space::Native {
Some(self.address.address)
} else {
None
};
let storage_value = StorageValue { owner, value };
self.storage_read_cache.write().insert(key, storage_value);
}
}

pub fn change_storage_value(
&mut self, db: &StateDbGeneric, key: &[u8], value: U256,
) -> DbResult<()> {
Expand Down

0 comments on commit f45f264

Please sign in to comment.