Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Extricate PodAccount and state Account to own crates (#10838)
Browse files Browse the repository at this point in the history
* WIP move errors, pod_account and state account to own crates

* Sort out dependencies, fix broken code and tests
Remove botched ethcore-error crate

* remove template line

* fix review feedback

* Remove test-only AccountDBMut::new
  • Loading branch information
dvdplm authored and ordian committed Jul 4, 2019
1 parent bbae075 commit 9f96fa0
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 68 deletions.
57 changes: 57 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ members = [
"util/keccak-hasher",
"util/patricia-trie-ethereum",
"util/fastmap",
"util/time-utils"
"util/time-utils",
]
2 changes: 2 additions & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ parity-bytes = "0.1"
parity-crypto = "0.4.0"
parity-snappy = "0.1"
parking_lot = "0.7"
pod-account = { path = "pod-account" }
trie-db = "0.12.4"
patricia-trie-ethereum = { path = "../util/patricia-trie-ethereum" }
rand = "0.6"
Expand All @@ -61,6 +62,7 @@ rlp_derive = { path = "../util/rlp-derive" }
rustc-hex = "1.0"
serde = "1.0"
serde_derive = "1.0"
state-account = { path = "state-account" }
stats = { path = "../util/stats" }
tempdir = { version = "0.3", optional = true }
time-utils = { path = "../util/time-utils" }
Expand Down
27 changes: 27 additions & 0 deletions ethcore/pod-account/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
description = "Account system expressed in Plain Old Data."
name = "pod-account"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
common-types = { path = "../types" }
ethereum-types = "0.6"
ethjson = { path = "../../json" }
ethtrie = { package = "patricia-trie-ethereum", path = "../../util/patricia-trie-ethereum" }
hash-db = "0.12"
itertools = "0.8"
keccak-hash = "0.2.0"
keccak-hasher = { path = "../../util/keccak-hasher" }
kvdb = "0.1"
log = "0.4"
parity-bytes = "0.1.0"
rlp = "0.4"
rustc-hex = "1"
serde = { version = "1.0", features = ["derive"] }
trie-db = "0.12.4"
triehash = { package = "triehash-ethereum", version = "0.2", path = "../../util/triehash-ethereum" }

[dev-dependencies]
macros = { path = "../../util/macros" }
27 changes: 8 additions & 19 deletions ethcore/src/pod_account.rs → ethcore/pod-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

//! Account system expressed in Plain Old Data.

use log::warn;
use std::collections::BTreeMap;
use itertools::Itertools;
use hash::{keccak};
use keccak_hash::keccak;
use ethereum_types::{H256, U256, BigEndianHash};
use hash_db::HashDB;
use kvdb::DBValue;
use keccak_hasher::KeccakHasher;
use triehash::sec_trie_root;
use bytes::Bytes;
use trie::TrieFactory;
use parity_bytes::Bytes;
use trie_db::TrieFactory;
use ethtrie::RlpCodec;
use state::Account;
use ethjson;
use types::account_diff::*;
use common_types::account_diff::*;
use rlp::{self, RlpStream};
use serde::Serializer;
use serde::{Serializer, Serialize};
use rustc_hex::ToHex;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
Expand All @@ -57,17 +56,6 @@ fn opt_bytes_to_hex<S>(opt_bytes: &Option<Bytes>, serializer: S) -> Result<S::Ok
}

impl PodAccount {
/// Convert Account to a PodAccount.
/// NOTE: This will silently fail unless the account is fully cached.
pub fn from_account(acc: &Account) -> PodAccount {
PodAccount {
balance: *acc.balance(),
nonce: *acc.nonce(),
storage: acc.storage_changes().iter().fold(BTreeMap::new(), |mut m, (k, v)| {m.insert(k.clone(), v.clone()); m}),
code: acc.code().map(|x| x.to_vec()),
}
}

/// Returns the RLP for this account.
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
Expand Down Expand Up @@ -170,9 +158,10 @@ pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<A
#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use types::account_diff::*;
use common_types::account_diff::*;
use super::{PodAccount, diff_pod};
use ethereum_types::H256;
use macros::map;

#[test]
fn existence() {
Expand Down
15 changes: 3 additions & 12 deletions ethcore/src/account_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,12 @@ pub struct AccountDBMut<'db> {
}

impl<'db> AccountDBMut<'db> {
/// Create a new AccountDB from an address.
#[cfg(test)]
pub fn new(db: &'db mut dyn HashDB<KeccakHasher, DBValue>, address: &Address) -> Self {
Self::from_hash(db, keccak(address))
}

/// Create a new AcountDB from an address' hash.
/// Create a new `AccountDBMut` from an address' hash.
pub fn from_hash(db: &'db mut dyn HashDB<KeccakHasher, DBValue>, address_hash: H256) -> Self {
AccountDBMut {
db: db,
address_hash: address_hash,
}
AccountDBMut { db, address_hash }
}

#[cfg(test)]
/// Create an `AccountDB` from an `AccountDBMut` (used in tests).
pub fn immutable(&'db self) -> AccountDB<'db> {
AccountDB { db: self.db, address_hash: self.address_hash.clone() }
}
Expand Down
7 changes: 3 additions & 4 deletions ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern crate parity_bytes as bytes;
extern crate parity_crypto;
extern crate parity_snappy as snappy;
extern crate parking_lot;
extern crate pod_account;
extern crate trie_db as trie;
extern crate patricia_trie_ethereum as ethtrie;
extern crate rand;
Expand All @@ -98,6 +99,7 @@ extern crate parity_util_mem as malloc_size_of;
extern crate rustc_hex;
extern crate serde;
extern crate stats;
extern crate state_account;
extern crate time_utils;
extern crate triehash_ethereum as triehash;
extern crate unexpected;
Expand All @@ -120,8 +122,6 @@ extern crate blooms_db;
#[cfg(any(test, feature = "env_logger"))]
extern crate env_logger;
#[cfg(test)]
extern crate rlp_compress;
#[cfg(test)]
extern crate serde_json;

#[macro_use]
Expand Down Expand Up @@ -162,16 +162,15 @@ pub mod executive;
pub mod machine;
pub mod miner;
pub mod pod_state;
pub mod pod_account;
pub mod snapshot;
pub mod spec;
pub mod state;
pub mod state_db;
pub mod trace;
pub mod transaction_ext;
pub mod verification;
pub mod account_db;

mod account_db;
mod externalities;
mod factory;
mod tx_filter;
Expand Down
20 changes: 10 additions & 10 deletions ethcore/src/snapshot/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests {
let p = Progress::default();
let fat_rlps = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value(), &p).unwrap();
let fat_rlp = Rlp::new(&fat_rlps[0]).at(1).unwrap();
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account);
assert_eq!(from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr)), fat_rlp, H256::zero()).unwrap().0, account);
}

#[test]
Expand All @@ -262,7 +262,7 @@ mod tests {
let addr = Address::random();

let account = {
let acct_db = AccountDBMut::new(db.as_hash_db_mut(), &addr);
let acct_db = AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr));
let mut root = KECCAK_NULL_RLP;
fill_storage(acct_db, &mut root, &mut H256::zero());
BasicAccount {
Expand All @@ -280,7 +280,7 @@ mod tests {

let fat_rlp = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hash_db(), &addr), &mut Default::default(), usize::max_value(), usize::max_value(), &p).unwrap();
let fat_rlp = Rlp::new(&fat_rlp[0]).at(1).unwrap();
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account);
assert_eq!(from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr)), fat_rlp, H256::zero()).unwrap().0, account);
}

#[test]
Expand All @@ -289,7 +289,7 @@ mod tests {
let addr = Address::random();

let account = {
let acct_db = AccountDBMut::new(db.as_hash_db_mut(), &addr);
let acct_db = AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr));
let mut root = KECCAK_NULL_RLP;
fill_storage(acct_db, &mut root, &mut H256::zero());
BasicAccount {
Expand All @@ -309,7 +309,7 @@ mod tests {
let mut restored_account = None;
for rlp in fat_rlps {
let fat_rlp = Rlp::new(&rlp).at(1).unwrap();
restored_account = Some(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr), fat_rlp, root).unwrap().0);
restored_account = Some(from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr)), fat_rlp, root).unwrap().0);
root = restored_account.as_ref().unwrap().storage_root.clone();
}
assert_eq!(restored_account, Some(account));
Expand All @@ -323,12 +323,12 @@ mod tests {
let addr2 = Address::random();

let code_hash = {
let mut acct_db = AccountDBMut::new(db.as_hash_db_mut(), &addr1);
let mut acct_db = AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr1));
acct_db.insert(EMPTY_PREFIX, b"this is definitely code")
};

{
let mut acct_db = AccountDBMut::new(db.as_hash_db_mut(), &addr2);
let mut acct_db = AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr2));
acct_db.emplace(code_hash.clone(), EMPTY_PREFIX, DBValue::from_slice(b"this is definitely code"));
}

Expand Down Expand Up @@ -356,18 +356,18 @@ mod tests {
let fat_rlp1 = Rlp::new(&fat_rlp1[0]).at(1).unwrap();
let fat_rlp2 = Rlp::new(&fat_rlp2[0]).at(1).unwrap();

let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr2), fat_rlp2, H256::zero()).unwrap();
let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr2)), fat_rlp2, H256::zero()).unwrap();
assert!(maybe_code.is_none());
assert_eq!(acc, account2);

let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &addr1), fat_rlp1, H256::zero()).unwrap();
let (acc, maybe_code) = from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(addr1)), fat_rlp1, H256::zero()).unwrap();
assert_eq!(maybe_code, Some(b"this is definitely code".to_vec()));
assert_eq!(acc, account1);
}

#[test]
fn encoding_empty_acc() {
let mut db = get_temp_state_db();
assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hash_db_mut(), &Address::zero()), Rlp::new(&::rlp::NULL_RLP), H256::zero()).unwrap(), (ACC_EMPTY, None));
assert_eq!(from_fat_rlp(&mut AccountDBMut::from_hash(db.as_hash_db_mut(), keccak(Address::zero())), Rlp::new(&::rlp::NULL_RLP), H256::zero()).unwrap(), (ACC_EMPTY, None));
}
}
7 changes: 3 additions & 4 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ use bytes::Bytes;
use trie::{Trie, TrieError, Recorder};
use ethtrie::{TrieDB, Result as TrieResult};

mod account;
mod substate;

pub mod backend;

pub use self::account::Account;
pub use state_account::Account;
pub use self::backend::Backend;
pub use self::substate::Substate;

Expand Down Expand Up @@ -964,7 +963,7 @@ impl<B: Backend> State<B> {
assert!(self.checkpoints.borrow().is_empty());
PodState::from(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
if let Some(ref acc) = opt.account {
m.insert(*add, PodAccount::from_account(acc));
m.insert(*add, acc.to_pod());
}
m
}))
Expand Down Expand Up @@ -1031,7 +1030,7 @@ impl<B: Backend> State<B> {
}
}

let mut pod_account = PodAccount::from_account(&account);
let mut pod_account = account.to_pod();
// cached one first
pod_storage.append(&mut pod_account.storage);
pod_account.storage = pod_storage;
Expand Down
Loading

0 comments on commit 9f96fa0

Please sign in to comment.