-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
68 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ mod storage; | |
|
||
mod checkpoints; | ||
|
||
mod state_override; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
|
73 changes: 73 additions & 0 deletions
73
crates/cfxcore/executor/src/state/overlay_account/state_override.rs
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,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); | ||
} | ||
} | ||
} |
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