2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// This file is part of Parity Ethereum.
|
2016-07-11 09:46:33 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-07-11 09:46:33 +02: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,
|
2016-07-11 09:46:33 +02: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/>.
|
2016-07-11 09:46:33 +02:00
|
|
|
|
2016-02-05 01:49:17 +01:00
|
|
|
//! DB backend wrapper for Account trie
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2019-07-04 17:50:31 +02:00
|
|
|
use keccak_hash::{KECCAK_NULL_RLP, keccak};
|
2019-06-19 13:54:05 +02:00
|
|
|
use hash_db::{HashDB, AsHashDB, Prefix};
|
2018-07-02 18:50:05 +02:00
|
|
|
use keccak_hasher::KeccakHasher;
|
2018-01-10 13:35:18 +01:00
|
|
|
use kvdb::DBValue;
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp::NULL_RLP;
|
2016-02-05 01:49:17 +01:00
|
|
|
|
2019-07-04 17:50:31 +02:00
|
|
|
// Combines a key with an address hash to ensure uniqueness.
|
2016-07-11 09:46:33 +02:00
|
|
|
// leaves the first 96 bits untouched in order to support partial key lookup.
|
|
|
|
#[inline]
|
|
|
|
fn combine_key<'a>(address_hash: &'a H256, key: &'a H256) -> H256 {
|
|
|
|
let mut dst = key.clone();
|
|
|
|
{
|
2019-06-03 15:36:21 +02:00
|
|
|
let last_src: &[u8] = address_hash.as_bytes();
|
|
|
|
let last_dst: &mut [u8] = dst.as_bytes_mut();
|
2016-07-11 09:46:33 +02:00
|
|
|
for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) {
|
|
|
|
*k ^= *a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst
|
|
|
|
}
|
|
|
|
|
2016-08-24 16:53:36 +02:00
|
|
|
/// A factory for different kinds of account dbs.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Factory {
|
2018-07-02 18:50:05 +02:00
|
|
|
/// Mangle hashes based on address. This is the default.
|
2016-08-24 16:53:36 +02:00
|
|
|
Mangled,
|
|
|
|
/// Don't mangle hashes.
|
|
|
|
Plain,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Factory {
|
|
|
|
fn default() -> Self { Factory::Mangled }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Factory {
|
|
|
|
/// Create a read-only accountdb.
|
|
|
|
/// This will panic when write operations are called.
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn readonly<'db>(&self, db: &'db dyn HashDB<KeccakHasher, DBValue>, address_hash: H256) -> Box<dyn HashDB<KeccakHasher, DBValue> + 'db> {
|
2016-08-24 16:53:36 +02:00
|
|
|
match *self {
|
|
|
|
Factory::Mangled => Box::new(AccountDB::from_hash(db, address_hash)),
|
|
|
|
Factory::Plain => Box::new(Wrapping(db)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new mutable hashdb.
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn create<'db>(&self, db: &'db mut dyn HashDB<KeccakHasher, DBValue>, address_hash: H256) -> Box<dyn HashDB<KeccakHasher, DBValue> + 'db> {
|
2016-08-24 16:53:36 +02:00
|
|
|
match *self {
|
|
|
|
Factory::Mangled => Box::new(AccountDBMut::from_hash(db, address_hash)),
|
|
|
|
Factory::Plain => Box::new(WrappingMut(db)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 01:49:17 +01:00
|
|
|
// TODO: introduce HashDBMut?
|
|
|
|
/// DB backend wrapper for Account trie
|
|
|
|
/// Transforms trie node keys for the database
|
|
|
|
pub struct AccountDB<'db> {
|
2019-06-14 18:48:35 +02:00
|
|
|
db: &'db dyn HashDB<KeccakHasher, DBValue>,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: H256,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> AccountDB<'db> {
|
2019-07-04 17:50:31 +02:00
|
|
|
/// Create a new AccountDB from an address' hash.
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn from_hash(db: &'db dyn HashDB<KeccakHasher, DBValue>, address_hash: H256) -> Self {
|
2019-07-04 17:50:31 +02:00
|
|
|
AccountDB { db, address_hash }
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> AsHashDB<KeccakHasher, DBValue> for AccountDB<'db> {
|
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
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> HashDB<KeccakHasher, DBValue> for AccountDB<'db> {
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2019-12-20 12:27:38 +01:00
|
|
|
return Some(NULL_RLP.to_vec());
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.get(&combine_key(&self.address_hash, key), prefix)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-02-05 01:49:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.contains(&combine_key(&self.address_hash, key), prefix)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, _prefix: Prefix, _value: &[u8]) -> H256 {
|
2016-02-05 01:49:17 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, _key: H256, _prefix: Prefix, _value: DBValue) {
|
2016-02-05 01:49:17 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, _key: &H256, _prefix: Prefix) {
|
2016-02-05 01:49:17 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DB backend wrapper for Account trie
|
|
|
|
pub struct AccountDBMut<'db> {
|
2019-06-14 18:48:35 +02:00
|
|
|
db: &'db mut dyn HashDB<KeccakHasher, DBValue>,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: H256,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> AccountDBMut<'db> {
|
2019-07-04 15:20:44 +02:00
|
|
|
/// Create a new `AccountDBMut` from an address' hash.
|
2019-06-14 18:48:35 +02:00
|
|
|
pub fn from_hash(db: &'db mut dyn HashDB<KeccakHasher, DBValue>, address_hash: H256) -> Self {
|
2019-07-04 15:20:44 +02:00
|
|
|
AccountDBMut { db, address_hash }
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:44 +02:00
|
|
|
/// Create an `AccountDB` from an `AccountDBMut` (used in tests).
|
2016-02-05 01:49:17 +01:00
|
|
|
pub fn immutable(&'db self) -> AccountDB<'db> {
|
2016-07-11 09:46:33 +02:00
|
|
|
AccountDB { db: self.db, address_hash: self.address_hash.clone() }
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> HashDB<KeccakHasher, DBValue> for AccountDBMut<'db>{
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2019-12-20 12:27:38 +01:00
|
|
|
return Some(NULL_RLP.to_vec());
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.get(&combine_key(&self.address_hash, key), prefix)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-02-05 01:49:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.contains(&combine_key(&self.address_hash, key), prefix)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
|
2016-03-13 21:28:57 +01:00
|
|
|
if value == &NULL_RLP {
|
2017-08-30 19:18:28 +02:00
|
|
|
return KECCAK_NULL_RLP.clone();
|
2016-03-13 21:28:57 +01:00
|
|
|
}
|
2017-08-30 19:18:28 +02:00
|
|
|
let k = keccak(value);
|
2016-07-11 09:46:33 +02:00
|
|
|
let ak = combine_key(&self.address_hash, &k);
|
2019-12-20 12:27:38 +01:00
|
|
|
self.db.emplace(ak, prefix, value.to_vec());
|
2016-02-05 01:49:17 +01:00
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == KECCAK_NULL_RLP {
|
2016-03-13 21:28:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
let key = combine_key(&self.address_hash, &key);
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.emplace(key, prefix, value)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, key: &H256, prefix: Prefix) {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-03-13 21:28:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
let key = combine_key(&self.address_hash, key);
|
2019-06-19 13:54:05 +02:00
|
|
|
self.db.remove(&key, prefix)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> AsHashDB<KeccakHasher, DBValue> for AccountDBMut<'db> {
|
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-06-14 18:48:35 +02:00
|
|
|
struct Wrapping<'db>(&'db dyn HashDB<KeccakHasher, DBValue>);
|
2016-08-24 16:53:36 +02:00
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> AsHashDB<KeccakHasher, DBValue> for Wrapping<'db> {
|
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
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> HashDB<KeccakHasher, DBValue> for Wrapping<'db> {
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2019-12-20 12:27:38 +01:00
|
|
|
return Some(NULL_RLP.to_vec());
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.get(key, prefix)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-08-24 16:53:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.contains(key, prefix)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, _prefix: Prefix, _value: &[u8]) -> H256 {
|
2016-08-24 16:53:36 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, _key: H256, _prefix: Prefix, _value: DBValue) {
|
2016-08-24 16:53:36 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, _key: &H256, _prefix: Prefix) {
|
2016-08-24 16:53:36 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 18:48:35 +02:00
|
|
|
struct WrappingMut<'db>(&'db mut dyn HashDB<KeccakHasher, DBValue>);
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> AsHashDB<KeccakHasher, DBValue> for WrappingMut<'db> {
|
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
|
|
|
}
|
2016-08-24 16:53:36 +02:00
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl<'db> HashDB<KeccakHasher, DBValue> for WrappingMut<'db>{
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2019-12-20 12:27:38 +01:00
|
|
|
return Some(NULL_RLP.to_vec());
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.get(key, prefix)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-08-24 16:53:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.contains(key, prefix)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 {
|
2016-08-24 16:53:36 +02:00
|
|
|
if value == &NULL_RLP {
|
2017-08-30 19:18:28 +02:00
|
|
|
return KECCAK_NULL_RLP.clone();
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.insert(prefix, value)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == KECCAK_NULL_RLP {
|
2016-08-24 16:53:36 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.emplace(key, prefix, value)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn remove(&mut self, key: &H256, prefix: Prefix) {
|
2017-08-30 19:18:28 +02:00
|
|
|
if key == &KECCAK_NULL_RLP {
|
2016-08-24 16:53:36 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-06-19 13:54:05 +02:00
|
|
|
self.0.remove(key, prefix)
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
}
|