Reformat the source code

This commit is contained in:
Artem Vorotnikov
2020-08-05 07:08:03 +03:00
parent 253ff3f37b
commit 610d9baba4
742 changed files with 175791 additions and 141379 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -21,54 +21,57 @@
//! should become general over time to the point where not even a
//! merkle trie is strictly necessary.
use std::collections::{HashSet, HashMap};
use std::sync::Arc;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use state::Account;
use parking_lot::Mutex;
use ethereum_types::{Address, H256};
use memory_db::MemoryDB;
use hash_db::{AsHashDB, HashDB};
use kvdb::DBValue;
use keccak_hasher::KeccakHasher;
use journaldb::AsKeyedHashDB;
use keccak_hasher::KeccakHasher;
use kvdb::DBValue;
use memory_db::MemoryDB;
use parking_lot::Mutex;
use state::Account;
/// State backend. See module docs for more details.
pub trait Backend: Send {
/// Treat the backend as a read-only hashdb.
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue>;
/// Treat the backend as a read-only hashdb.
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue>;
/// Treat the backend as a writeable hashdb.
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue>;
/// Treat the backend as a writeable hashdb.
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue>;
/// Add an account entry to the cache.
fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool);
/// Add an account entry to the cache.
fn add_to_account_cache(&mut self, addr: Address, data: Option<Account>, modified: bool);
/// Add a global code cache entry. This doesn't need to worry about canonicality because
/// it simply maps hashes to raw code and will always be correct in the absence of
/// hash collisions.
fn cache_code(&self, hash: H256, code: Arc<Vec<u8>>);
/// Add a global code cache entry. This doesn't need to worry about canonicality because
/// it simply maps hashes to raw code and will always be correct in the absence of
/// hash collisions.
fn cache_code(&self, hash: H256, code: Arc<Vec<u8>>);
/// Get basic copy of the cached account. Not required to include storage.
/// Returns 'None' if cache is disabled or if the account is not cached.
fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>>;
/// Get basic copy of the cached account. Not required to include storage.
/// Returns 'None' if cache is disabled or if the account is not cached.
fn get_cached_account(&self, addr: &Address) -> Option<Option<Account>>;
/// Get value from a cached account.
/// `None` is passed to the closure if the account entry cached
/// is known not to exist.
/// `None` is returned if the entry is not cached.
fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U;
/// Get value from a cached account.
/// `None` is passed to the closure if the account entry cached
/// is known not to exist.
/// `None` is returned if the entry is not cached.
fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U>
where
F: FnOnce(Option<&mut Account>) -> U;
/// Get cached code based on hash.
fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>>;
/// Get cached code based on hash.
fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>>;
/// Note that an account with the given address is non-null.
fn note_non_null_account(&self, address: &Address);
/// Note that an account with the given address is non-null.
fn note_non_null_account(&self, address: &Address);
/// Check whether an account is known to be empty. Returns true if known to be
/// empty, false otherwise.
fn is_known_null(&self, address: &Address) -> bool;
/// Check whether an account is known to be empty. Returns true if known to be
/// empty, false otherwise.
fn is_known_null(&self, address: &Address) -> bool;
}
/// A raw backend used to check proofs of execution.
@@ -81,57 +84,76 @@ pub trait Backend: Send {
pub struct ProofCheck(MemoryDB<KeccakHasher, DBValue>);
impl ProofCheck {
/// Create a new `ProofCheck` backend from the given state items.
pub fn new(proof: &[DBValue]) -> Self {
let mut db = journaldb::new_memory_db();
for item in proof { db.insert(item); }
ProofCheck(db)
}
/// Create a new `ProofCheck` backend from the given state items.
pub fn new(proof: &[DBValue]) -> Self {
let mut db = journaldb::new_memory_db();
for item in proof {
db.insert(item);
}
ProofCheck(db)
}
}
impl journaldb::KeyedHashDB for ProofCheck {
fn keys(&self) -> HashMap<H256, i32> { self.0.keys() }
fn keys(&self) -> HashMap<H256, i32> {
self.0.keys()
}
}
impl HashDB<KeccakHasher, DBValue> for ProofCheck {
fn get(&self, key: &H256) -> Option<DBValue> {
self.0.get(key)
}
fn get(&self, key: &H256) -> Option<DBValue> {
self.0.get(key)
}
fn contains(&self, key: &H256) -> bool {
self.0.contains(key)
}
fn contains(&self, key: &H256) -> bool {
self.0.contains(key)
}
fn insert(&mut self, value: &[u8]) -> H256 {
self.0.insert(value)
}
fn insert(&mut self, value: &[u8]) -> H256 {
self.0.insert(value)
}
fn emplace(&mut self, key: H256, value: DBValue) {
self.0.emplace(key, value)
}
fn emplace(&mut self, key: H256, value: DBValue) {
self.0.emplace(key, value)
}
fn remove(&mut self, _key: &H256) { }
fn remove(&mut self, _key: &H256) {}
}
impl AsHashDB<KeccakHasher, DBValue> for ProofCheck {
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self
}
}
impl Backend for ProofCheck {
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> { self }
fn add_to_account_cache(&mut self, _addr: Address, _data: Option<Account>, _modified: bool) {}
fn cache_code(&self, _hash: H256, _code: Arc<Vec<u8>>) {}
fn get_cached_account(&self, _addr: &Address) -> Option<Option<Account>> { None }
fn get_cached<F, U>(&self, _a: &Address, _f: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U
{
None
}
fn get_cached_code(&self, _hash: &H256) -> Option<Arc<Vec<u8>>> { None }
fn note_non_null_account(&self, _address: &Address) {}
fn is_known_null(&self, _address: &Address) -> bool { false }
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self
}
fn add_to_account_cache(&mut self, _addr: Address, _data: Option<Account>, _modified: bool) {}
fn cache_code(&self, _hash: H256, _code: Arc<Vec<u8>>) {}
fn get_cached_account(&self, _addr: &Address) -> Option<Option<Account>> {
None
}
fn get_cached<F, U>(&self, _a: &Address, _f: F) -> Option<U>
where
F: FnOnce(Option<&mut Account>) -> U,
{
None
}
fn get_cached_code(&self, _hash: &H256) -> Option<Arc<Vec<u8>>> {
None
}
fn note_non_null_account(&self, _address: &Address) {}
fn is_known_null(&self, _address: &Address) -> bool {
false
}
}
/// Proving state backend.
@@ -140,107 +162,128 @@ impl Backend for ProofCheck {
///
/// This doesn't cache anything or rely on the canonical state caches.
pub struct Proving<H> {
base: H, // state we're proving values from.
changed: MemoryDB<KeccakHasher, DBValue>, // changed state via insertions.
proof: Mutex<HashSet<DBValue>>,
base: H, // state we're proving values from.
changed: MemoryDB<KeccakHasher, DBValue>, // changed state via insertions.
proof: Mutex<HashSet<DBValue>>,
}
impl<AH: AsKeyedHashDB + Send + Sync> AsKeyedHashDB for Proving<AH> {
fn as_keyed_hash_db(&self) -> &journaldb::KeyedHashDB { self }
fn as_keyed_hash_db(&self) -> &journaldb::KeyedHashDB {
self
}
}
impl<AH: AsHashDB<KeccakHasher, DBValue> + Send + Sync> AsHashDB<KeccakHasher, DBValue> for Proving<AH> {
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> { self }
impl<AH: AsHashDB<KeccakHasher, DBValue> + Send + Sync> AsHashDB<KeccakHasher, DBValue>
for Proving<AH>
{
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self
}
}
impl<H: AsKeyedHashDB + Send + Sync> journaldb::KeyedHashDB for Proving<H> {
fn keys(&self) -> HashMap<H256, i32> {
let mut keys = self.base.as_keyed_hash_db().keys();
keys.extend(self.changed.keys());
keys
}
fn keys(&self) -> HashMap<H256, i32> {
let mut keys = self.base.as_keyed_hash_db().keys();
keys.extend(self.changed.keys());
keys
}
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> HashDB<KeccakHasher, DBValue> for Proving<H> {
fn get(&self, key: &H256) -> Option<DBValue> {
match self.base.as_hash_db().get(key) {
Some(val) => {
self.proof.lock().insert(val.clone());
Some(val)
}
None => self.changed.get(key)
}
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> HashDB<KeccakHasher, DBValue>
for Proving<H>
{
fn get(&self, key: &H256) -> Option<DBValue> {
match self.base.as_hash_db().get(key) {
Some(val) => {
self.proof.lock().insert(val.clone());
Some(val)
}
None => self.changed.get(key),
}
}
fn contains(&self, key: &H256) -> bool {
self.get(key).is_some()
}
fn contains(&self, key: &H256) -> bool {
self.get(key).is_some()
}
fn insert(&mut self, value: &[u8]) -> H256 {
self.changed.insert(value)
}
fn insert(&mut self, value: &[u8]) -> H256 {
self.changed.insert(value)
}
fn emplace(&mut self, key: H256, value: DBValue) {
self.changed.emplace(key, value)
}
fn emplace(&mut self, key: H256, value: DBValue) {
self.changed.emplace(key, value)
}
fn remove(&mut self, key: &H256) {
// only remove from `changed`
if self.changed.contains(key) {
self.changed.remove(key)
}
}
fn remove(&mut self, key: &H256) {
// only remove from `changed`
if self.changed.contains(key) {
self.changed.remove(key)
}
}
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Proving<H> {
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self
}
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) { }
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) {}
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) { }
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) {}
fn get_cached_account(&self, _: &Address) -> Option<Option<Account>> { None }
fn get_cached_account(&self, _: &Address) -> Option<Option<Account>> {
None
}
fn get_cached<F, U>(&self, _: &Address, _: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U
{
None
}
fn get_cached<F, U>(&self, _: &Address, _: F) -> Option<U>
where
F: FnOnce(Option<&mut Account>) -> U,
{
None
}
fn get_cached_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> { None }
fn note_non_null_account(&self, _: &Address) { }
fn is_known_null(&self, _: &Address) -> bool { false }
fn get_cached_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> {
None
}
fn note_non_null_account(&self, _: &Address) {}
fn is_known_null(&self, _: &Address) -> bool {
false
}
}
impl<H: AsHashDB<KeccakHasher, DBValue>> Proving<H> {
/// Create a new `Proving` over a base database.
/// This will store all values ever fetched from that base.
pub fn new(base: H) -> Self {
Proving {
base: base,
changed: journaldb::new_memory_db(),
proof: Mutex::new(HashSet::new()),
}
}
/// Create a new `Proving` over a base database.
/// This will store all values ever fetched from that base.
pub fn new(base: H) -> Self {
Proving {
base: base,
changed: journaldb::new_memory_db(),
proof: Mutex::new(HashSet::new()),
}
}
/// Consume the backend, extracting the gathered proof in lexicographical order
/// by value.
pub fn extract_proof(self) -> Vec<DBValue> {
self.proof.into_inner().into_iter().collect()
}
/// Consume the backend, extracting the gathered proof in lexicographical order
/// by value.
pub fn extract_proof(self) -> Vec<DBValue> {
self.proof.into_inner().into_iter().collect()
}
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Clone> Clone for Proving<H> {
fn clone(&self) -> Self {
Proving {
base: self.base.clone(),
changed: self.changed.clone(),
proof: Mutex::new(self.proof.lock().clone()),
}
}
fn clone(&self) -> Self {
Proving {
base: self.base.clone(),
changed: self.changed.clone(),
proof: Mutex::new(self.proof.lock().clone()),
}
}
}
/// A basic backend. Just wraps the given database, directly inserting into and deleting from
@@ -248,27 +291,34 @@ impl<H: AsHashDB<KeccakHasher, DBValue> + Clone> Clone for Proving<H> {
pub struct Basic<H>(pub H);
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Basic<H> {
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db()
}
fn as_hash_db(&self) -> &HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db()
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db_mut()
}
fn as_hash_db_mut(&mut self) -> &mut HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db_mut()
}
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) { }
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) {}
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) { }
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) {}
fn get_cached_account(&self, _: &Address) -> Option<Option<Account>> { None }
fn get_cached_account(&self, _: &Address) -> Option<Option<Account>> {
None
}
fn get_cached<F, U>(&self, _: &Address, _: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U
{
None
}
fn get_cached<F, U>(&self, _: &Address, _: F) -> Option<U>
where
F: FnOnce(Option<&mut Account>) -> U,
{
None
}
fn get_cached_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> { None }
fn note_non_null_account(&self, _: &Address) { }
fn is_known_null(&self, _: &Address) -> bool { false }
fn get_cached_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> {
None
}
fn note_non_null_account(&self, _: &Address) {}
fn is_known_null(&self, _: &Address) -> bool {
false
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -15,92 +15,96 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
//! Execution environment substate.
use std::collections::HashSet;
use ethereum_types::Address;
use types::log_entry::LogEntry;
use evm::{Schedule, CleanDustMode};
use super::CleanupMode;
use ethereum_types::Address;
use evm::{CleanDustMode, Schedule};
use std::collections::HashSet;
use types::log_entry::LogEntry;
/// State changes which should be applied in finalize,
/// after transaction is fully executed.
#[derive(Debug, Default)]
pub struct Substate {
/// Any accounts that have suicided.
pub suicides: HashSet<Address>,
/// Any accounts that have suicided.
pub suicides: HashSet<Address>,
/// Any accounts that are touched.
pub touched: HashSet<Address>,
/// Any accounts that are touched.
pub touched: HashSet<Address>,
/// Any logs.
pub logs: Vec<LogEntry>,
/// Any logs.
pub logs: Vec<LogEntry>,
/// Refund counter of SSTORE.
pub sstore_clears_refund: i128,
/// Refund counter of SSTORE.
pub sstore_clears_refund: i128,
/// Created contracts.
pub contracts_created: Vec<Address>,
/// Created contracts.
pub contracts_created: Vec<Address>,
}
impl Substate {
/// Creates new substate.
pub fn new() -> Self {
Substate::default()
}
/// Creates new substate.
pub fn new() -> Self {
Substate::default()
}
/// Merge secondary substate `s` into self, accruing each element correspondingly.
pub fn accrue(&mut self, s: Substate) {
self.suicides.extend(s.suicides);
self.touched.extend(s.touched);
self.logs.extend(s.logs);
self.sstore_clears_refund += s.sstore_clears_refund;
self.contracts_created.extend(s.contracts_created);
}
/// Merge secondary substate `s` into self, accruing each element correspondingly.
pub fn accrue(&mut self, s: Substate) {
self.suicides.extend(s.suicides);
self.touched.extend(s.touched);
self.logs.extend(s.logs);
self.sstore_clears_refund += s.sstore_clears_refund;
self.contracts_created.extend(s.contracts_created);
}
/// Get the cleanup mode object from this.
pub fn to_cleanup_mode(&mut self, schedule: &Schedule) -> CleanupMode {
match (schedule.kill_dust != CleanDustMode::Off, schedule.no_empty, schedule.kill_empty) {
(false, false, _) => CleanupMode::ForceCreate,
(false, true, false) => CleanupMode::NoEmpty,
(false, true, true) | (true, _, _,) => CleanupMode::TrackTouched(&mut self.touched),
}
}
/// Get the cleanup mode object from this.
pub fn to_cleanup_mode(&mut self, schedule: &Schedule) -> CleanupMode {
match (
schedule.kill_dust != CleanDustMode::Off,
schedule.no_empty,
schedule.kill_empty,
) {
(false, false, _) => CleanupMode::ForceCreate,
(false, true, false) => CleanupMode::NoEmpty,
(false, true, true) | (true, _, _) => CleanupMode::TrackTouched(&mut self.touched),
}
}
}
#[cfg(test)]
mod tests {
use super::Substate;
use types::log_entry::LogEntry;
use super::Substate;
use types::log_entry::LogEntry;
#[test]
fn created() {
let sub_state = Substate::new();
assert_eq!(sub_state.suicides.len(), 0);
}
#[test]
fn created() {
let sub_state = Substate::new();
assert_eq!(sub_state.suicides.len(), 0);
}
#[test]
fn accrue() {
let mut sub_state = Substate::new();
sub_state.contracts_created.push(1u64.into());
sub_state.logs.push(LogEntry {
address: 1u64.into(),
topics: vec![],
data: vec![]
});
sub_state.sstore_clears_refund = (15000 * 5).into();
sub_state.suicides.insert(10u64.into());
#[test]
fn accrue() {
let mut sub_state = Substate::new();
sub_state.contracts_created.push(1u64.into());
sub_state.logs.push(LogEntry {
address: 1u64.into(),
topics: vec![],
data: vec![],
});
sub_state.sstore_clears_refund = (15000 * 5).into();
sub_state.suicides.insert(10u64.into());
let mut sub_state_2 = Substate::new();
sub_state_2.contracts_created.push(2u64.into());
sub_state_2.logs.push(LogEntry {
address: 1u64.into(),
topics: vec![],
data: vec![]
});
sub_state_2.sstore_clears_refund = (15000 * 7).into();
let mut sub_state_2 = Substate::new();
sub_state_2.contracts_created.push(2u64.into());
sub_state_2.logs.push(LogEntry {
address: 1u64.into(),
topics: vec![],
data: vec![],
});
sub_state_2.sstore_clears_refund = (15000 * 7).into();
sub_state.accrue(sub_state_2);
assert_eq!(sub_state.contracts_created.len(), 2);
assert_eq!(sub_state.sstore_clears_refund, (15000 * 12).into());
assert_eq!(sub_state.suicides.len(), 1);
}
sub_state.accrue(sub_state_2);
assert_eq!(sub_state.contracts_created.len(), 2);
assert_eq!(sub_state.sstore_clears_refund, (15000 * 12).into());
assert_eq!(sub_state.suicides.len(), 1);
}
}