2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-21 11:13:02 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-21 11:13:02 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-02-21 11:13:02 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-21 11:13:02 +01:00
|
|
|
|
|
|
|
//! A minimal "state backend" trait: an abstraction over the sources of data
|
|
|
|
//! a blockchain state may draw upon.
|
|
|
|
//!
|
|
|
|
//! Currently assumes a very specific DB + cache structure, but
|
|
|
|
//! should become general over time to the point where not even a
|
|
|
|
//! merkle trie is strictly necessary.
|
|
|
|
|
2019-08-07 01:45:10 +02:00
|
|
|
use std::collections::HashSet;
|
2017-02-21 11:13:02 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{Address, H256};
|
2019-07-08 18:17:48 +02:00
|
|
|
use hash_db::{AsHashDB, EMPTY_PREFIX, HashDB, Prefix};
|
2018-10-09 22:07:25 +02:00
|
|
|
use kvdb::DBValue;
|
2019-07-08 18:17:48 +02:00
|
|
|
use memory_db::{HashKey, MemoryDB};
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use keccak_hasher::KeccakHasher;
|
|
|
|
|
|
|
|
use crate::account::Account;
|
2017-02-21 11:13:02 +01:00
|
|
|
|
|
|
|
/// State backend. See module docs for more details.
|
2017-02-21 12:35:21 +01:00
|
|
|
pub trait Backend: Send {
|
2017-02-21 11:13:02 +01:00
|
|
|
/// Treat the backend as a read-only hashdb.
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue>;
|
2017-02-21 11:13:02 +01:00
|
|
|
|
|
|
|
/// Treat the backend as a writeable hashdb.
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue>;
|
2017-02-21 11:13:02 +01:00
|
|
|
|
|
|
|
/// 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>>);
|
|
|
|
|
2017-02-21 12:35:21 +01:00
|
|
|
/// Get basic copy of the cached account. Not required to include storage.
|
2017-02-21 11:13:02 +01:00
|
|
|
/// 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 cached code based on hash.
|
|
|
|
fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>>;
|
2017-02-21 12:35:21 +01:00
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// A raw backend used to check proofs of execution.
|
|
|
|
///
|
|
|
|
/// This doesn't delete anything since execution proofs won't have mangled keys
|
|
|
|
/// and we want to avoid collisions.
|
|
|
|
// TODO: when account lookup moved into backends, this won't rely as tenuously on intended
|
|
|
|
// usage.
|
|
|
|
#[derive(Clone, PartialEq)]
|
2019-06-19 13:54:05 +02:00
|
|
|
pub struct ProofCheck(MemoryDB<KeccakHasher, HashKey<KeccakHasher>, DBValue>);
|
2017-03-08 14:39:44 +01:00
|
|
|
|
|
|
|
impl ProofCheck {
|
|
|
|
/// Create a new `ProofCheck` backend from the given state items.
|
|
|
|
pub fn new(proof: &[DBValue]) -> Self {
|
2019-02-20 19:09:34 +01:00
|
|
|
let mut db = journaldb::new_memory_db();
|
2019-06-19 13:54:05 +02:00
|
|
|
for item in proof { db.insert(EMPTY_PREFIX, item); }
|
2017-03-08 14:39:44 +01:00
|
|
|
ProofCheck(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
impl HashDB<KeccakHasher, DBValue> for ProofCheck {
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
|
|
|
self.0.get(key, prefix)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
|
|
|
self.0.contains(key, prefix)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
2017-02-21 12:35:21 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
|
|
|
|
self.0.insert(prefix, value)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
|
|
|
|
self.0.emplace(key, prefix, value)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
2017-02-21 12:35:21 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, _key: &H256, _prefix: Prefix) { }
|
2017-02-21 12:35:21 +01:00
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl AsHashDB<KeccakHasher, DBValue> for ProofCheck {
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
|
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
|
2018-07-02 18:50:05 +02:00
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
impl Backend for ProofCheck {
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
|
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
|
2017-02-21 12:35:21 +01:00
|
|
|
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 }
|
2017-02-21 11:13:02 +01:00
|
|
|
}
|
2017-03-08 14:39:44 +01:00
|
|
|
|
|
|
|
/// Proving state backend.
|
|
|
|
/// This keeps track of all state values loaded during usage of this backend.
|
|
|
|
/// The proof-of-execution can be extracted with `extract_proof`.
|
|
|
|
///
|
|
|
|
/// This doesn't cache anything or rely on the canonical state caches.
|
2019-02-20 19:09:34 +01:00
|
|
|
pub struct Proving<H> {
|
2017-03-08 14:39:44 +01:00
|
|
|
base: H, // state we're proving values from.
|
2019-06-19 13:54:05 +02:00
|
|
|
changed: MemoryDB<KeccakHasher, HashKey<KeccakHasher>, DBValue>, // changed state via insertions.
|
2017-03-08 14:39:44 +01:00
|
|
|
proof: Mutex<HashSet<DBValue>>,
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<AH: AsHashDB<KeccakHasher, DBValue> + Send + Sync> AsHashDB<KeccakHasher, DBValue> for Proving<AH> {
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
|
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
|
2018-07-02 18:50:05 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> HashDB<KeccakHasher, DBValue> for Proving<H> {
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
|
|
|
match self.base.as_hash_db().get(key, prefix) {
|
2017-03-08 14:39:44 +01:00
|
|
|
Some(val) => {
|
|
|
|
self.proof.lock().insert(val.clone());
|
|
|
|
Some(val)
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
None => self.changed.get(key, prefix)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
|
|
|
self.get(key, prefix).is_some()
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
|
|
|
|
self.changed.insert(prefix, value)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
|
|
|
|
self.changed.emplace(key, prefix, value)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, key: &H256, prefix: Prefix) {
|
2017-03-08 14:39:44 +01:00
|
|
|
// only remove from `changed`
|
2019-06-19 13:54:05 +02:00
|
|
|
if self.changed.contains(key, prefix) {
|
|
|
|
self.changed.remove(key, prefix)
|
2017-03-08 14:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Proving<H> {
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
|
2017-03-08 14:39:44 +01:00
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
|
2017-03-08 14:39:44 +01:00
|
|
|
|
|
|
|
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) { }
|
|
|
|
|
|
|
|
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) { }
|
|
|
|
|
|
|
|
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_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> { None }
|
|
|
|
fn note_non_null_account(&self, _: &Address) { }
|
|
|
|
fn is_known_null(&self, _: &Address) -> bool { false }
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<H: AsHashDB<KeccakHasher, DBValue>> Proving<H> {
|
2017-03-08 14:39:44 +01:00
|
|
|
/// 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 {
|
2019-07-18 12:27:08 +02:00
|
|
|
base,
|
2019-02-20 19:09:34 +01:00
|
|
|
changed: journaldb::new_memory_db(),
|
2017-03-08 14:39:44 +01:00
|
|
|
proof: Mutex::new(HashSet::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:00:02 +02:00
|
|
|
/// Consume the backend, extracting the gathered proof in lexicographical order
|
|
|
|
/// by value.
|
2017-03-08 14:39:44 +01:00
|
|
|
pub fn extract_proof(self) -> Vec<DBValue> {
|
|
|
|
self.proof.into_inner().into_iter().collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<H: AsHashDB<KeccakHasher, DBValue> + Clone> Clone for Proving<H> {
|
2017-03-08 14:39:44 +01:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Proving {
|
|
|
|
base: self.base.clone(),
|
|
|
|
changed: self.changed.clone(),
|
|
|
|
proof: Mutex::new(self.proof.lock().clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 09:00:02 +02:00
|
|
|
|
|
|
|
/// A basic backend. Just wraps the given database, directly inserting into and deleting from
|
|
|
|
/// it. Doesn't cache anything.
|
|
|
|
pub struct Basic<H>(pub H);
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Basic<H> {
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> {
|
2019-02-20 19:09:34 +01:00
|
|
|
self.0.as_hash_db()
|
2017-05-03 09:00:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> {
|
2019-02-20 19:09:34 +01:00
|
|
|
self.0.as_hash_db_mut()
|
2017-05-03 09:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_to_account_cache(&mut self, _: Address, _: Option<Account>, _: bool) { }
|
|
|
|
|
|
|
|
fn cache_code(&self, _: H256, _: Arc<Vec<u8>>) { }
|
|
|
|
|
|
|
|
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_code(&self, _: &H256) -> Option<Arc<Vec<u8>>> { None }
|
|
|
|
fn note_non_null_account(&self, _: &Address) { }
|
|
|
|
fn is_known_null(&self, _: &Address) -> bool { false }
|
|
|
|
}
|