Track dirty accounts in the state (#2461)
* State to track dirty accounts * Removed clone_for_snapshot * Renaming stuff * Documentation and other minor fixes * Replaced MaybeAccount with Option
This commit is contained in:
parent
33abb47222
commit
ecf098e9a4
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
//! Single account in the system.
|
//! Single account in the system.
|
||||||
|
|
||||||
use std::collections::hash_map::Entry;
|
|
||||||
use util::*;
|
use util::*;
|
||||||
use pod_account::*;
|
use pod_account::*;
|
||||||
use rlp::*;
|
use rlp::*;
|
||||||
@ -24,9 +23,11 @@ use lru_cache::LruCache;
|
|||||||
|
|
||||||
use std::cell::{RefCell, Cell};
|
use std::cell::{RefCell, Cell};
|
||||||
|
|
||||||
const STORAGE_CACHE_ITEMS: usize = 4096;
|
const STORAGE_CACHE_ITEMS: usize = 8192;
|
||||||
|
|
||||||
/// Single account in the system.
|
/// Single account in the system.
|
||||||
|
/// Keeps track of changes to the code and storage.
|
||||||
|
/// The changes are applied in `commit_storage` and `commit_code`
|
||||||
pub struct Account {
|
pub struct Account {
|
||||||
// Balance of the account.
|
// Balance of the account.
|
||||||
balance: U256,
|
balance: U256,
|
||||||
@ -46,8 +47,6 @@ pub struct Account {
|
|||||||
code_size: Option<usize>,
|
code_size: Option<usize>,
|
||||||
// Code cache of the account.
|
// Code cache of the account.
|
||||||
code_cache: Arc<Bytes>,
|
code_cache: Arc<Bytes>,
|
||||||
// Account is new or has been modified.
|
|
||||||
filth: Filth,
|
|
||||||
// Account code new or has been modified.
|
// Account code new or has been modified.
|
||||||
code_filth: Filth,
|
code_filth: Filth,
|
||||||
// Cached address hash.
|
// Cached address hash.
|
||||||
@ -67,7 +66,6 @@ impl Account {
|
|||||||
code_hash: code.sha3(),
|
code_hash: code.sha3(),
|
||||||
code_size: Some(code.len()),
|
code_size: Some(code.len()),
|
||||||
code_cache: Arc::new(code),
|
code_cache: Arc::new(code),
|
||||||
filth: Filth::Dirty,
|
|
||||||
code_filth: Filth::Dirty,
|
code_filth: Filth::Dirty,
|
||||||
address_hash: Cell::new(None),
|
address_hash: Cell::new(None),
|
||||||
}
|
}
|
||||||
@ -89,7 +87,6 @@ impl Account {
|
|||||||
code_filth: Filth::Dirty,
|
code_filth: Filth::Dirty,
|
||||||
code_size: Some(pod.code.as_ref().map_or(0, |c| c.len())),
|
code_size: Some(pod.code.as_ref().map_or(0, |c| c.len())),
|
||||||
code_cache: Arc::new(pod.code.map_or_else(|| { warn!("POD account with unknown code is being created! Assuming no code."); vec![] }, |c| c)),
|
code_cache: Arc::new(pod.code.map_or_else(|| { warn!("POD account with unknown code is being created! Assuming no code."); vec![] }, |c| c)),
|
||||||
filth: Filth::Dirty,
|
|
||||||
address_hash: Cell::new(None),
|
address_hash: Cell::new(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,7 +102,6 @@ impl Account {
|
|||||||
code_hash: SHA3_EMPTY,
|
code_hash: SHA3_EMPTY,
|
||||||
code_cache: Arc::new(vec![]),
|
code_cache: Arc::new(vec![]),
|
||||||
code_size: Some(0),
|
code_size: Some(0),
|
||||||
filth: Filth::Dirty,
|
|
||||||
code_filth: Filth::Clean,
|
code_filth: Filth::Clean,
|
||||||
address_hash: Cell::new(None),
|
address_hash: Cell::new(None),
|
||||||
}
|
}
|
||||||
@ -123,7 +119,6 @@ impl Account {
|
|||||||
code_hash: r.val_at(3),
|
code_hash: r.val_at(3),
|
||||||
code_cache: Arc::new(vec![]),
|
code_cache: Arc::new(vec![]),
|
||||||
code_size: None,
|
code_size: None,
|
||||||
filth: Filth::Clean,
|
|
||||||
code_filth: Filth::Clean,
|
code_filth: Filth::Clean,
|
||||||
address_hash: Cell::new(None),
|
address_hash: Cell::new(None),
|
||||||
}
|
}
|
||||||
@ -141,7 +136,6 @@ impl Account {
|
|||||||
code_hash: SHA3_EMPTY,
|
code_hash: SHA3_EMPTY,
|
||||||
code_cache: Arc::new(vec![]),
|
code_cache: Arc::new(vec![]),
|
||||||
code_size: None,
|
code_size: None,
|
||||||
filth: Filth::Dirty,
|
|
||||||
code_filth: Filth::Clean,
|
code_filth: Filth::Clean,
|
||||||
address_hash: Cell::new(None),
|
address_hash: Cell::new(None),
|
||||||
}
|
}
|
||||||
@ -153,7 +147,6 @@ impl Account {
|
|||||||
self.code_hash = code.sha3();
|
self.code_hash = code.sha3();
|
||||||
self.code_cache = Arc::new(code);
|
self.code_cache = Arc::new(code);
|
||||||
self.code_size = Some(self.code_cache.len());
|
self.code_size = Some(self.code_cache.len());
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
self.code_filth = Filth::Dirty;
|
self.code_filth = Filth::Dirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,17 +157,7 @@ impl Account {
|
|||||||
|
|
||||||
/// Set (and cache) the contents of the trie's storage at `key` to `value`.
|
/// Set (and cache) the contents of the trie's storage at `key` to `value`.
|
||||||
pub fn set_storage(&mut self, key: H256, value: H256) {
|
pub fn set_storage(&mut self, key: H256, value: H256) {
|
||||||
match self.storage_changes.entry(key) {
|
self.storage_changes.insert(key, value);
|
||||||
Entry::Occupied(ref mut entry) if entry.get() != &value => {
|
|
||||||
entry.insert(value);
|
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
},
|
|
||||||
Entry::Vacant(entry) => {
|
|
||||||
entry.insert(value);
|
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
},
|
|
||||||
_ => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get (and cache) the contents of the trie's storage at `key`.
|
/// Get (and cache) the contents of the trie's storage at `key`.
|
||||||
@ -263,17 +246,6 @@ impl Account {
|
|||||||
!self.code_cache.is_empty() || (self.code_cache.is_empty() && self.code_hash == SHA3_EMPTY)
|
!self.code_cache.is_empty() || (self.code_cache.is_empty() && self.code_hash == SHA3_EMPTY)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Is this a new or modified account?
|
|
||||||
pub fn is_dirty(&self) -> bool {
|
|
||||||
self.filth == Filth::Dirty || self.code_filth == Filth::Dirty || !self.storage_is_clean()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Mark account as clean.
|
|
||||||
pub fn set_clean(&mut self) {
|
|
||||||
assert!(self.storage_is_clean());
|
|
||||||
self.filth = Filth::Clean
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Provide a database to get `code_hash`. Should not be called if it is a contract without code.
|
/// Provide a database to get `code_hash`. Should not be called if it is a contract without code.
|
||||||
pub fn cache_code(&mut self, db: &HashDB) -> bool {
|
pub fn cache_code(&mut self, db: &HashDB) -> bool {
|
||||||
// TODO: fill out self.code_cache;
|
// TODO: fill out self.code_cache;
|
||||||
@ -326,25 +298,18 @@ impl Account {
|
|||||||
/// Increment the nonce of the account by one.
|
/// Increment the nonce of the account by one.
|
||||||
pub fn inc_nonce(&mut self) {
|
pub fn inc_nonce(&mut self) {
|
||||||
self.nonce = self.nonce + U256::from(1u8);
|
self.nonce = self.nonce + U256::from(1u8);
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increment the nonce of the account by one.
|
/// Increase account balance.
|
||||||
pub fn add_balance(&mut self, x: &U256) {
|
pub fn add_balance(&mut self, x: &U256) {
|
||||||
if !x.is_zero() {
|
self.balance = self.balance + *x;
|
||||||
self.balance = self.balance + *x;
|
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increment the nonce of the account by one.
|
/// Decrease account balance.
|
||||||
/// Panics if balance is less than `x`
|
/// Panics if balance is less than `x`
|
||||||
pub fn sub_balance(&mut self, x: &U256) {
|
pub fn sub_balance(&mut self, x: &U256) {
|
||||||
if !x.is_zero() {
|
assert!(self.balance >= *x);
|
||||||
assert!(self.balance >= *x);
|
self.balance = self.balance - *x;
|
||||||
self.balance = self.balance - *x;
|
|
||||||
self.filth = Filth::Dirty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit the `storage_changes` to the backing DB and update `storage_root`.
|
/// Commit the `storage_changes` to the backing DB and update `storage_root`.
|
||||||
@ -406,7 +371,6 @@ impl Account {
|
|||||||
code_hash: self.code_hash.clone(),
|
code_hash: self.code_hash.clone(),
|
||||||
code_size: self.code_size.clone(),
|
code_size: self.code_size.clone(),
|
||||||
code_cache: self.code_cache.clone(),
|
code_cache: self.code_cache.clone(),
|
||||||
filth: self.filth,
|
|
||||||
code_filth: self.code_filth,
|
code_filth: self.code_filth,
|
||||||
address_hash: self.address_hash.clone(),
|
address_hash: self.address_hash.clone(),
|
||||||
}
|
}
|
||||||
@ -427,10 +391,10 @@ impl Account {
|
|||||||
account
|
account
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replace self with the data from other account merging storage cache
|
/// Replace self with the data from other account merging storage cache.
|
||||||
|
/// Basic account data and all modifications are overwritten
|
||||||
|
/// with new values.
|
||||||
pub fn merge_with(&mut self, other: Account) {
|
pub fn merge_with(&mut self, other: Account) {
|
||||||
assert!(self.storage_is_clean());
|
|
||||||
assert!(other.storage_is_clean());
|
|
||||||
self.balance = other.balance;
|
self.balance = other.balance;
|
||||||
self.nonce = other.nonce;
|
self.nonce = other.nonce;
|
||||||
self.storage_root = other.storage_root;
|
self.storage_root = other.storage_root;
|
||||||
@ -443,6 +407,7 @@ impl Account {
|
|||||||
for (k, v) in other.storage_cache.into_inner().into_iter() {
|
for (k, v) in other.storage_cache.into_inner().into_iter() {
|
||||||
cache.insert(k.clone() , v.clone()); //TODO: cloning should not be required here
|
cache.insert(k.clone() , v.clone()); //TODO: cloning should not be required here
|
||||||
}
|
}
|
||||||
|
self.storage_changes = other.storage_changes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::cell::{RefCell, RefMut};
|
use std::cell::{RefCell, RefMut};
|
||||||
|
use std::collections::hash_map::Entry;
|
||||||
use common::*;
|
use common::*;
|
||||||
use engines::Engine;
|
use engines::Engine;
|
||||||
use executive::{Executive, TransactOptions};
|
use executive::{Executive, TransactOptions};
|
||||||
@ -42,42 +43,69 @@ pub struct ApplyOutcome {
|
|||||||
/// Result type for the execution ("application") of a transaction.
|
/// Result type for the execution ("application") of a transaction.
|
||||||
pub type ApplyResult = Result<ApplyOutcome, Error>;
|
pub type ApplyResult = Result<ApplyOutcome, Error>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
|
||||||
enum AccountEntry {
|
/// Account modification state. Used to check if the account was
|
||||||
/// Contains account data.
|
/// Modified in between commits and overall.
|
||||||
Cached(Account),
|
enum AccountState {
|
||||||
/// Account has been deleted.
|
/// Account was never modified in this state object.
|
||||||
Killed,
|
Clean,
|
||||||
/// Account does not exist.
|
/// Account has been modified and is not committed to the trie yet.
|
||||||
Missing,
|
/// This is set than any of the account data is changed, including
|
||||||
|
/// storage and code.
|
||||||
|
Dirty,
|
||||||
|
/// Account was modified and committed to the trie.
|
||||||
|
Commited,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
/// In-memory copy of the account data. Holds the optional account
|
||||||
|
/// and the modification status.
|
||||||
|
/// Account entry can contain existing (`Some`) or non-existing
|
||||||
|
/// account (`None`)
|
||||||
|
struct AccountEntry {
|
||||||
|
account: Option<Account>,
|
||||||
|
state: AccountState,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account cache item. Contains account data and
|
||||||
|
// modification state
|
||||||
impl AccountEntry {
|
impl AccountEntry {
|
||||||
fn is_dirty(&self) -> bool {
|
fn is_dirty(&self) -> bool {
|
||||||
match *self {
|
self.state == AccountState::Dirty
|
||||||
AccountEntry::Cached(ref a) => a.is_dirty(),
|
|
||||||
AccountEntry::Killed => true,
|
|
||||||
AccountEntry::Missing => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clone dirty data into new `AccountEntry`.
|
/// Clone dirty data into new `AccountEntry`. This includes
|
||||||
|
/// basic account data and modified storage keys.
|
||||||
/// Returns None if clean.
|
/// Returns None if clean.
|
||||||
fn clone_dirty(&self) -> Option<AccountEntry> {
|
fn clone_if_dirty(&self) -> Option<AccountEntry> {
|
||||||
match *self {
|
match self.is_dirty() {
|
||||||
AccountEntry::Cached(ref acc) if acc.is_dirty() => Some(AccountEntry::Cached(acc.clone_dirty())),
|
true => Some(self.clone_dirty()),
|
||||||
AccountEntry::Killed => Some(AccountEntry::Killed),
|
false => None,
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clone account entry data that needs to be saved in the snapshot.
|
/// Clone dirty data into new `AccountEntry`. This includes
|
||||||
/// This includes basic account information and all locally cached storage keys
|
/// basic account data and modified storage keys.
|
||||||
fn clone_for_snapshot(&self) -> AccountEntry {
|
fn clone_dirty(&self) -> AccountEntry {
|
||||||
match *self {
|
AccountEntry {
|
||||||
AccountEntry::Cached(ref acc) => AccountEntry::Cached(acc.clone_all()),
|
account: self.account.as_ref().map(Account::clone_dirty),
|
||||||
AccountEntry::Killed => AccountEntry::Killed,
|
state: self.state,
|
||||||
AccountEntry::Missing => AccountEntry::Missing,
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new account entry and mark it as dirty
|
||||||
|
fn new_dirty(account: Option<Account>) -> AccountEntry {
|
||||||
|
AccountEntry {
|
||||||
|
account: account,
|
||||||
|
state: AccountState::Dirty,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new account entry and mark it as clean
|
||||||
|
fn new_clean(account: Option<Account>) -> AccountEntry {
|
||||||
|
AccountEntry {
|
||||||
|
account: account,
|
||||||
|
state: AccountState::Clean,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,6 +118,9 @@ impl AccountEntry {
|
|||||||
/// locally from previous commits. Global cache reflects the database
|
/// locally from previous commits. Global cache reflects the database
|
||||||
/// state and never contains any changes.
|
/// state and never contains any changes.
|
||||||
///
|
///
|
||||||
|
/// Cache items contains account data, or the flag that account does not exist
|
||||||
|
/// and modification state (see `AccountState`)
|
||||||
|
///
|
||||||
/// Account data can be in the following cache states:
|
/// Account data can be in the following cache states:
|
||||||
/// * In global but not local - something that was queried from the database,
|
/// * In global but not local - something that was queried from the database,
|
||||||
/// but never modified
|
/// but never modified
|
||||||
@ -103,6 +134,12 @@ impl AccountEntry {
|
|||||||
/// then global state cache. If data is not found in any of the caches
|
/// then global state cache. If data is not found in any of the caches
|
||||||
/// it is loaded from the DB to the local cache.
|
/// it is loaded from the DB to the local cache.
|
||||||
///
|
///
|
||||||
|
/// **** IMPORTANT *************************************************************
|
||||||
|
/// All the modifications to the account data must set the `Dirty` state in the
|
||||||
|
/// `AccountEntry`. This is done in `require` and `require_or_from`. So just
|
||||||
|
/// use that.
|
||||||
|
/// ****************************************************************************
|
||||||
|
///
|
||||||
/// Upon destruction all the local cache data merged into the global cache.
|
/// Upon destruction all the local cache data merged into the global cache.
|
||||||
/// The merge might be rejected if current state is non-canonical.
|
/// The merge might be rejected if current state is non-canonical.
|
||||||
pub struct State {
|
pub struct State {
|
||||||
@ -190,7 +227,7 @@ impl State {
|
|||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
match self.cache.borrow_mut().entry(k) {
|
match self.cache.borrow_mut().entry(k) {
|
||||||
::std::collections::hash_map::Entry::Occupied(e) => {
|
Entry::Occupied(e) => {
|
||||||
if e.get().is_dirty() {
|
if e.get().is_dirty() {
|
||||||
e.remove();
|
e.remove();
|
||||||
}
|
}
|
||||||
@ -204,10 +241,17 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert_cache(&self, address: &Address, account: AccountEntry) {
|
fn insert_cache(&self, address: &Address, account: AccountEntry) {
|
||||||
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
|
// Dirty account which is not in the cache means this is a new account.
|
||||||
if !snapshot.contains_key(address) {
|
// It goes directly into the snapshot as there's nothing to rever to.
|
||||||
snapshot.insert(address.clone(), self.cache.borrow_mut().insert(address.clone(), account));
|
//
|
||||||
return;
|
// In all other cases account is read as clean first, and after that made
|
||||||
|
// dirty in and added to the snapshot with `note_cache`.
|
||||||
|
if account.is_dirty() {
|
||||||
|
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
|
||||||
|
if !snapshot.contains_key(address) {
|
||||||
|
snapshot.insert(address.clone(), self.cache.borrow_mut().insert(address.clone(), account));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.cache.borrow_mut().insert(address.clone(), account);
|
self.cache.borrow_mut().insert(address.clone(), account);
|
||||||
@ -216,7 +260,7 @@ impl State {
|
|||||||
fn note_cache(&self, address: &Address) {
|
fn note_cache(&self, address: &Address) {
|
||||||
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
|
if let Some(ref mut snapshot) = self.snapshots.borrow_mut().last_mut() {
|
||||||
if !snapshot.contains_key(address) {
|
if !snapshot.contains_key(address) {
|
||||||
snapshot.insert(address.clone(), self.cache.borrow().get(address).map(AccountEntry::clone_for_snapshot));
|
snapshot.insert(address.clone(), self.cache.borrow().get(address).map(AccountEntry::clone_dirty));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,12 +279,12 @@ impl State {
|
|||||||
/// Create a new contract at address `contract`. If there is already an account at the address
|
/// Create a new contract at address `contract`. If there is already an account at the address
|
||||||
/// it will have its code reset, ready for `init_code()`.
|
/// it will have its code reset, ready for `init_code()`.
|
||||||
pub fn new_contract(&mut self, contract: &Address, balance: U256) {
|
pub fn new_contract(&mut self, contract: &Address, balance: U256) {
|
||||||
self.insert_cache(contract, AccountEntry::Cached(Account::new_contract(balance, self.account_start_nonce)));
|
self.insert_cache(contract, AccountEntry::new_dirty(Some(Account::new_contract(balance, self.account_start_nonce))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove an existing account.
|
/// Remove an existing account.
|
||||||
pub fn kill_account(&mut self, account: &Address) {
|
pub fn kill_account(&mut self, account: &Address) {
|
||||||
self.insert_cache(account, AccountEntry::Killed);
|
self.insert_cache(account, AccountEntry::new_dirty(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine whether an account exists.
|
/// Determine whether an account exists.
|
||||||
@ -272,8 +316,8 @@ impl State {
|
|||||||
let local_cache = self.cache.borrow_mut();
|
let local_cache = self.cache.borrow_mut();
|
||||||
let mut local_account = None;
|
let mut local_account = None;
|
||||||
if let Some(maybe_acc) = local_cache.get(address) {
|
if let Some(maybe_acc) = local_cache.get(address) {
|
||||||
match *maybe_acc {
|
match maybe_acc.account {
|
||||||
AccountEntry::Cached(ref account) => {
|
Some(ref account) => {
|
||||||
if let Some(value) = account.cached_storage_at(key) {
|
if let Some(value) = account.cached_storage_at(key) {
|
||||||
return value;
|
return value;
|
||||||
} else {
|
} else {
|
||||||
@ -292,7 +336,7 @@ impl State {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if let Some(ref mut acc) = local_account {
|
if let Some(ref mut acc) = local_account {
|
||||||
if let AccountEntry::Cached(ref account) = **acc {
|
if let Some(ref account) = acc.account {
|
||||||
let account_db = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(address));
|
let account_db = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(address));
|
||||||
return account.storage_at(account_db.as_hashdb(), key)
|
return account.storage_at(account_db.as_hashdb(), key)
|
||||||
} else {
|
} else {
|
||||||
@ -314,10 +358,7 @@ impl State {
|
|||||||
let account_db = self.factories.accountdb.readonly(self.db.as_hashdb(), a.address_hash(address));
|
let account_db = self.factories.accountdb.readonly(self.db.as_hashdb(), a.address_hash(address));
|
||||||
a.storage_at(account_db.as_hashdb(), key)
|
a.storage_at(account_db.as_hashdb(), key)
|
||||||
});
|
});
|
||||||
match maybe_acc {
|
self.insert_cache(address, AccountEntry::new_clean(maybe_acc));
|
||||||
Some(account) => self.insert_cache(address, AccountEntry::Cached(account)),
|
|
||||||
None => self.insert_cache(address, AccountEntry::Missing),
|
|
||||||
}
|
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,13 +382,17 @@ impl State {
|
|||||||
/// Add `incr` to the balance of account `a`.
|
/// Add `incr` to the balance of account `a`.
|
||||||
pub fn add_balance(&mut self, a: &Address, incr: &U256) {
|
pub fn add_balance(&mut self, a: &Address, incr: &U256) {
|
||||||
trace!(target: "state", "add_balance({}, {}): {}", a, incr, self.balance(a));
|
trace!(target: "state", "add_balance({}, {}): {}", a, incr, self.balance(a));
|
||||||
self.require(a, false).add_balance(incr);
|
if !incr.is_zero() || !self.exists(a) {
|
||||||
|
self.require(a, false).add_balance(incr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtract `decr` from the balance of account `a`.
|
/// Subtract `decr` from the balance of account `a`.
|
||||||
pub fn sub_balance(&mut self, a: &Address, decr: &U256) {
|
pub fn sub_balance(&mut self, a: &Address, decr: &U256) {
|
||||||
trace!(target: "state", "sub_balance({}, {}): {}", a, decr, self.balance(a));
|
trace!(target: "state", "sub_balance({}, {}): {}", a, decr, self.balance(a));
|
||||||
self.require(a, false).sub_balance(decr);
|
if !decr.is_zero() || !self.exists(a) {
|
||||||
|
self.require(a, false).sub_balance(decr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtracts `by` from the balance of `from` and adds it to that of `to`.
|
/// Subtracts `by` from the balance of `from` and adds it to that of `to`.
|
||||||
@ -363,7 +408,9 @@ impl State {
|
|||||||
|
|
||||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||||
pub fn set_storage(&mut self, a: &Address, key: H256, value: H256) {
|
pub fn set_storage(&mut self, a: &Address, key: H256, value: H256) {
|
||||||
self.require(a, false).set_storage(key, value)
|
if self.storage_at(a, &key) != value {
|
||||||
|
self.require(a, false).set_storage(key, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialise the code of account `a` so that it is `code`.
|
/// Initialise the code of account `a` so that it is `code`.
|
||||||
@ -404,10 +451,9 @@ impl State {
|
|||||||
accounts: &mut HashMap<Address, AccountEntry>
|
accounts: &mut HashMap<Address, AccountEntry>
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
// first, commit the sub trees.
|
// first, commit the sub trees.
|
||||||
// TODO: is this necessary or can we dispense with the `ref mut a` for just `a`?
|
for (address, ref mut a) in accounts.iter_mut().filter(|&(_, ref a)| a.is_dirty()) {
|
||||||
for (address, ref mut a) in accounts.iter_mut() {
|
match a.account {
|
||||||
match a {
|
Some(ref mut account) => {
|
||||||
&mut&mut AccountEntry::Cached(ref mut account) if account.is_dirty() => {
|
|
||||||
db.note_account_bloom(&address);
|
db.note_account_bloom(&address);
|
||||||
let addr_hash = account.address_hash(address);
|
let addr_hash = account.address_hash(address);
|
||||||
let mut account_db = factories.accountdb.create(db.as_hashdb_mut(), addr_hash);
|
let mut account_db = factories.accountdb.create(db.as_hashdb_mut(), addr_hash);
|
||||||
@ -420,17 +466,15 @@ impl State {
|
|||||||
|
|
||||||
{
|
{
|
||||||
let mut trie = factories.trie.from_existing(db.as_hashdb_mut(), root).unwrap();
|
let mut trie = factories.trie.from_existing(db.as_hashdb_mut(), root).unwrap();
|
||||||
for (address, ref mut a) in accounts.iter_mut() {
|
for (address, ref mut a) in accounts.iter_mut().filter(|&(_, ref a)| a.is_dirty()) {
|
||||||
match **a {
|
a.state = AccountState::Commited;
|
||||||
AccountEntry::Cached(ref mut account) if account.is_dirty() => {
|
match a.account {
|
||||||
account.set_clean();
|
Some(ref mut account) => {
|
||||||
try!(trie.insert(address, &account.rlp()));
|
try!(trie.insert(address, &account.rlp()));
|
||||||
},
|
},
|
||||||
AccountEntry::Killed => {
|
None => {
|
||||||
try!(trie.remove(address));
|
try!(trie.remove(address));
|
||||||
**a = AccountEntry::Missing;
|
|
||||||
},
|
},
|
||||||
_ => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -440,18 +484,9 @@ impl State {
|
|||||||
|
|
||||||
fn commit_cache(&mut self) {
|
fn commit_cache(&mut self) {
|
||||||
let mut addresses = self.cache.borrow_mut();
|
let mut addresses = self.cache.borrow_mut();
|
||||||
for (address, a) in addresses.drain() {
|
trace!("Committing cache {:?} entries", addresses.len());
|
||||||
match a {
|
for (address, a) in addresses.drain().filter(|&(_, ref a)| !a.is_dirty()) {
|
||||||
AccountEntry::Cached(account) => {
|
self.db.cache_account(address, a.account);
|
||||||
if !account.is_dirty() {
|
|
||||||
self.db.cache_account(address, Some(account));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
AccountEntry::Missing => {
|
|
||||||
self.db.cache_account(address, None);
|
|
||||||
},
|
|
||||||
_ => {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -473,7 +508,7 @@ impl State {
|
|||||||
assert!(self.snapshots.borrow().is_empty());
|
assert!(self.snapshots.borrow().is_empty());
|
||||||
for (add, acc) in accounts.drain().into_iter() {
|
for (add, acc) in accounts.drain().into_iter() {
|
||||||
self.db.note_account_bloom(&add);
|
self.db.note_account_bloom(&add);
|
||||||
self.cache.borrow_mut().insert(add, AccountEntry::Cached(Account::from_pod(acc)));
|
self.cache.borrow_mut().insert(add, AccountEntry::new_dirty(Some(Account::from_pod(acc))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -483,7 +518,7 @@ impl State {
|
|||||||
// TODO: handle database rather than just the cache.
|
// TODO: handle database rather than just the cache.
|
||||||
// will need fat db.
|
// will need fat db.
|
||||||
PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
|
PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
|
||||||
if let AccountEntry::Cached(ref acc) = *opt {
|
if let Some(ref acc) = opt.account {
|
||||||
m.insert(add.clone(), PodAccount::from_account(acc));
|
m.insert(add.clone(), PodAccount::from_account(acc));
|
||||||
}
|
}
|
||||||
m
|
m
|
||||||
@ -530,7 +565,7 @@ impl State {
|
|||||||
where F: Fn(Option<&Account>) -> U {
|
where F: Fn(Option<&Account>) -> U {
|
||||||
// check local cache first
|
// check local cache first
|
||||||
if let Some(ref mut maybe_acc) = self.cache.borrow_mut().get_mut(a) {
|
if let Some(ref mut maybe_acc) = self.cache.borrow_mut().get_mut(a) {
|
||||||
if let AccountEntry::Cached(ref mut account) = **maybe_acc {
|
if let Some(ref mut account) = maybe_acc.account {
|
||||||
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(a));
|
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(a));
|
||||||
Self::update_account_cache(require, account, accountdb.as_hashdb());
|
Self::update_account_cache(require, account, accountdb.as_hashdb());
|
||||||
return f(Some(account));
|
return f(Some(account));
|
||||||
@ -563,8 +598,8 @@ impl State {
|
|||||||
}
|
}
|
||||||
let r = f(maybe_acc.as_ref());
|
let r = f(maybe_acc.as_ref());
|
||||||
match maybe_acc {
|
match maybe_acc {
|
||||||
Some(account) => self.insert_cache(a, AccountEntry::Cached(account)),
|
Some(account) => self.insert_cache(a, AccountEntry::new_clean(Some(account))),
|
||||||
None => self.insert_cache(a, AccountEntry::Missing),
|
None => self.insert_cache(a, AccountEntry::new_clean(None)),
|
||||||
}
|
}
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
@ -584,36 +619,39 @@ impl State {
|
|||||||
let contains_key = self.cache.borrow().contains_key(a);
|
let contains_key = self.cache.borrow().contains_key(a);
|
||||||
if !contains_key {
|
if !contains_key {
|
||||||
match self.db.get_cached_account(a) {
|
match self.db.get_cached_account(a) {
|
||||||
Some(Some(acc)) => self.insert_cache(a, AccountEntry::Cached(acc)),
|
Some(Some(acc)) => self.insert_cache(a, AccountEntry::new_clean(Some(acc))),
|
||||||
Some(None) => self.insert_cache(a, AccountEntry::Missing),
|
Some(None) => self.insert_cache(a, AccountEntry::new_clean(None)),
|
||||||
None => {
|
None => {
|
||||||
let maybe_acc = if self.db.check_account_bloom(a) {
|
let maybe_acc = if self.db.check_account_bloom(a) {
|
||||||
let db = self.factories.trie.readonly(self.db.as_hashdb(), &self.root).expect(SEC_TRIE_DB_UNWRAP_STR);
|
let db = self.factories.trie.readonly(self.db.as_hashdb(), &self.root).expect(SEC_TRIE_DB_UNWRAP_STR);
|
||||||
let maybe_acc = match db.get(a) {
|
let maybe_acc = match db.get(a) {
|
||||||
Ok(Some(acc)) => AccountEntry::Cached(Account::from_rlp(acc)),
|
Ok(Some(acc)) => AccountEntry::new_clean(Some(Account::from_rlp(acc))),
|
||||||
Ok(None) => AccountEntry::Missing,
|
Ok(None) => AccountEntry::new_clean(None),
|
||||||
Err(e) => panic!("Potential DB corruption encountered: {}", e),
|
Err(e) => panic!("Potential DB corruption encountered: {}", e),
|
||||||
};
|
};
|
||||||
maybe_acc
|
maybe_acc
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
AccountEntry::Missing
|
AccountEntry::new_clean(None)
|
||||||
};
|
};
|
||||||
self.insert_cache(a, maybe_acc);
|
self.insert_cache(a, maybe_acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
self.note_cache(a);
|
self.note_cache(a);
|
||||||
}
|
|
||||||
|
match &mut self.cache.borrow_mut().get_mut(a).unwrap().account {
|
||||||
match self.cache.borrow_mut().get_mut(a).unwrap() {
|
&mut Some(ref mut acc) => not_default(acc),
|
||||||
&mut AccountEntry::Cached(ref mut acc) => not_default(acc),
|
slot => *slot = Some(default()),
|
||||||
slot => *slot = AccountEntry::Cached(default()),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// at this point the account is guaranteed to be in the cache.
|
||||||
RefMut::map(self.cache.borrow_mut(), |c| {
|
RefMut::map(self.cache.borrow_mut(), |c| {
|
||||||
match c.get_mut(a).unwrap() {
|
let mut entry = c.get_mut(a).unwrap();
|
||||||
&mut AccountEntry::Cached(ref mut account) => {
|
// set the dirty flag after changing account data.
|
||||||
|
entry.state = AccountState::Dirty;
|
||||||
|
match entry.account {
|
||||||
|
Some(ref mut account) => {
|
||||||
if require_code {
|
if require_code {
|
||||||
let addr_hash = account.address_hash(a);
|
let addr_hash = account.address_hash(a);
|
||||||
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), addr_hash);
|
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), addr_hash);
|
||||||
@ -638,7 +676,7 @@ impl Clone for State {
|
|||||||
let cache = {
|
let cache = {
|
||||||
let mut cache: HashMap<Address, AccountEntry> = HashMap::new();
|
let mut cache: HashMap<Address, AccountEntry> = HashMap::new();
|
||||||
for (key, val) in self.cache.borrow().iter() {
|
for (key, val) in self.cache.borrow().iter() {
|
||||||
if let Some(entry) = val.clone_dirty() {
|
if let Some(entry) = val.clone_if_dirty() {
|
||||||
cache.insert(key.clone(), entry);
|
cache.insert(key.clone(), entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user