use std::collections::HashSet;
use std::sync::Arc;
use vapory_types::{Address, H256};
use tetsy_hash_db::{AsHashDB, EMPTY_PREFIX, HashDB, Prefix};
use tetsy_kvdb::DBValue;
use tetsy_memory_db::{HashKey, MemoryDB};
use parking_lot::Mutex;
use tetsy_keccak_hasher::KeccakHasher;
use crate::account::Account;
pub trait Backend: Send {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue>;
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue>;
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>>;
fn get_cached<F, U>(&self, a: &Address, f: F) -> Option<U>
where F: FnOnce(Option<&mut Account>) -> U;
fn get_cached_code(&self, hash: &H256) -> Option<Arc<Vec<u8>>>;
fn note_non_null_account(&self, address: &Address);
fn is_known_null(&self, address: &Address) -> bool;
}
#[derive(Clone, PartialEq)]
pub struct ProofCheck(MemoryDB<KeccakHasher, HashKey<KeccakHasher>, DBValue>);
impl ProofCheck {
pub fn new(proof: &[DBValue]) -> Self {
let mut db = journaldb::new_tetsy_memory_db();
for item in proof { db.insert(EMPTY_PREFIX, item); }
ProofCheck(db)
}
}
impl HashDB<KeccakHasher, DBValue> for ProofCheck {
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
self.0.get(key, prefix)
}
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
self.0.contains(key, prefix)
}
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
self.0.insert(prefix, value)
}
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
self.0.emplace(key, prefix, value)
}
fn remove(&mut self, _key: &H256, _prefix: Prefix) { }
}
impl AsHashDB<KeccakHasher, DBValue> for ProofCheck {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
}
impl Backend for ProofCheck {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut dyn 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 }
}
pub struct Proving<H> {
base: H, changed: MemoryDB<KeccakHasher, HashKey<KeccakHasher>, DBValue>, proof: Mutex<HashSet<DBValue>>,
}
impl<AH: AsHashDB<KeccakHasher, DBValue> + Send + Sync> AsHashDB<KeccakHasher, DBValue> for Proving<AH> {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> HashDB<KeccakHasher, DBValue> for Proving<H> {
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
match self.base.as_hash_db().get(key, prefix) {
Some(val) => {
self.proof.lock().insert(val.clone());
Some(val)
}
None => self.changed.get(key, prefix)
}
}
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
self.get(key, prefix).is_some()
}
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
self.changed.insert(prefix, value)
}
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
self.changed.emplace(key, prefix, value)
}
fn remove(&mut self, key: &H256, prefix: Prefix) {
if self.changed.contains(key, prefix) {
self.changed.remove(key, prefix)
}
}
}
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Proving<H> {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> { self }
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> { self }
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 }
}
impl<H: AsHashDB<KeccakHasher, DBValue>> Proving<H> {
pub fn new(base: H) -> Self {
Proving {
base,
changed: journaldb::new_tetsy_memory_db(),
proof: Mutex::new(HashSet::new()),
}
}
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()),
}
}
}
pub struct Basic<H>(pub H);
impl<H: AsHashDB<KeccakHasher, DBValue> + Send + Sync> Backend for Basic<H> {
fn as_hash_db(&self) -> &dyn HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db()
}
fn as_hash_db_mut(&mut self) -> &mut dyn HashDB<KeccakHasher, DBValue> {
self.0.as_hash_db_mut()
}
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 }
}