2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-07-11 09:46:33 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-05 01:49:17 +01:00
|
|
|
//! DB backend wrapper for Account trie
|
|
|
|
use util::*;
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp::NULL_RLP;
|
2016-02-05 01:49:17 +01:00
|
|
|
|
|
|
|
static NULL_RLP_STATIC: [u8; 1] = [0x80; 1];
|
|
|
|
|
2016-07-11 09:46:33 +02:00
|
|
|
// combines a key with an address hash to ensure uniqueness.
|
|
|
|
// 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();
|
|
|
|
{
|
|
|
|
let last_src: &[u8] = &*address_hash;
|
|
|
|
let last_dst: &mut [u8] = &mut *dst;
|
|
|
|
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 {
|
|
|
|
/// Mangle hashes based on address.
|
|
|
|
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.
|
|
|
|
pub fn readonly<'db>(&self, db: &'db HashDB, address_hash: H256) -> Box<HashDB + 'db> {
|
|
|
|
match *self {
|
|
|
|
Factory::Mangled => Box::new(AccountDB::from_hash(db, address_hash)),
|
|
|
|
Factory::Plain => Box::new(Wrapping(db)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new mutable hashdb.
|
|
|
|
pub fn create<'db>(&self, db: &'db mut HashDB, address_hash: H256) -> Box<HashDB + 'db> {
|
|
|
|
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> {
|
|
|
|
db: &'db HashDB,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: H256,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> AccountDB<'db> {
|
2016-07-11 09:46:33 +02:00
|
|
|
/// Create a new AccountDB from an address.
|
2017-05-03 09:00:02 +02:00
|
|
|
#[cfg(test)]
|
2016-07-11 09:46:33 +02:00
|
|
|
pub fn new(db: &'db HashDB, address: &Address) -> Self {
|
|
|
|
Self::from_hash(db, address.sha3())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new AcountDB from an address' hash.
|
|
|
|
pub fn from_hash(db: &'db HashDB, address_hash: H256) -> Self {
|
2016-02-05 01:49:17 +01:00
|
|
|
AccountDB {
|
|
|
|
db: db,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: address_hash,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> HashDB for AccountDB<'db>{
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2016-02-05 01:49:17 +01:00
|
|
|
if key == &SHA3_NULL_RLP {
|
2016-10-26 13:53:47 +02:00
|
|
|
return Some(DBValue::from_slice(&NULL_RLP_STATIC));
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
self.db.get(&combine_key(&self.address_hash, key))
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
2016-02-05 01:49:17 +01:00
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
self.db.contains(&combine_key(&self.address_hash, key))
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, _value: &[u8]) -> H256 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, _key: H256, _value: DBValue) {
|
2016-02-05 01:49:17 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, _key: &H256) {
|
2016-02-05 01:49:17 +01:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DB backend wrapper for Account trie
|
|
|
|
pub struct AccountDBMut<'db> {
|
|
|
|
db: &'db mut HashDB,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: H256,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> AccountDBMut<'db> {
|
2016-07-11 09:46:33 +02:00
|
|
|
/// Create a new AccountDB from an address.
|
2017-05-03 09:00:02 +02:00
|
|
|
#[cfg(test)]
|
2016-07-11 09:46:33 +02:00
|
|
|
pub fn new(db: &'db mut HashDB, address: &Address) -> Self {
|
|
|
|
Self::from_hash(db, address.sha3())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new AcountDB from an address' hash.
|
|
|
|
pub fn from_hash(db: &'db mut HashDB, address_hash: H256) -> Self {
|
2016-02-05 01:49:17 +01:00
|
|
|
AccountDBMut {
|
|
|
|
db: db,
|
2016-07-11 09:46:33 +02:00
|
|
|
address_hash: address_hash,
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:00:02 +02:00
|
|
|
#[cfg(test)]
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'db> HashDB for AccountDBMut<'db>{
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2016-02-05 01:49:17 +01:00
|
|
|
if key == &SHA3_NULL_RLP {
|
2016-10-26 13:53:47 +02:00
|
|
|
return Some(DBValue::from_slice(&NULL_RLP_STATIC));
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
self.db.get(&combine_key(&self.address_hash, key))
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
2016-02-05 01:49:17 +01:00
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
self.db.contains(&combine_key(&self.address_hash, key))
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
2016-03-13 21:28:57 +01:00
|
|
|
if value == &NULL_RLP {
|
|
|
|
return SHA3_NULL_RLP.clone();
|
|
|
|
}
|
2016-02-05 01:49:17 +01:00
|
|
|
let k = value.sha3();
|
2016-07-11 09:46:33 +02:00
|
|
|
let ak = combine_key(&self.address_hash, &k);
|
2016-10-26 13:53:47 +02:00
|
|
|
self.db.emplace(ak, DBValue::from_slice(value));
|
2016-02-05 01:49:17 +01:00
|
|
|
k
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) {
|
2016-03-13 21:28:57 +01:00
|
|
|
if key == SHA3_NULL_RLP {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
let key = combine_key(&self.address_hash, &key);
|
2016-10-26 13:53:47 +02:00
|
|
|
self.db.emplace(key, value)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) {
|
2016-03-13 21:28:57 +01:00
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-11 09:46:33 +02:00
|
|
|
let key = combine_key(&self.address_hash, key);
|
2016-06-23 11:16:11 +02:00
|
|
|
self.db.remove(&key)
|
2016-02-05 01:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 16:53:36 +02:00
|
|
|
struct Wrapping<'db>(&'db HashDB);
|
|
|
|
|
|
|
|
impl<'db> HashDB for Wrapping<'db> {
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2016-08-24 16:53:36 +02:00
|
|
|
if key == &SHA3_NULL_RLP {
|
2016-10-26 13:53:47 +02:00
|
|
|
return Some(DBValue::from_slice(&NULL_RLP_STATIC));
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
self.0.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains(&self, key: &H256) -> bool {
|
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
self.0.contains(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, _value: &[u8]) -> H256 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, _key: H256, _value: DBValue) {
|
2016-08-24 16:53:36 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&mut self, _key: &H256) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WrappingMut<'db>(&'db mut HashDB);
|
|
|
|
|
|
|
|
impl<'db> HashDB for WrappingMut<'db>{
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-02-05 01:49:17 +01:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2016-08-24 16:53:36 +02:00
|
|
|
if key == &SHA3_NULL_RLP {
|
2016-10-26 13:53:47 +02:00
|
|
|
return Some(DBValue::from_slice(&NULL_RLP_STATIC));
|
2016-08-24 16:53:36 +02:00
|
|
|
}
|
|
|
|
self.0.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains(&self, key: &H256) -> bool {
|
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
self.0.contains(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
|
|
|
if value == &NULL_RLP {
|
|
|
|
return SHA3_NULL_RLP.clone();
|
|
|
|
}
|
|
|
|
self.0.insert(value)
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) {
|
2016-08-24 16:53:36 +02:00
|
|
|
if key == SHA3_NULL_RLP {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.0.emplace(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&mut self, key: &H256) {
|
|
|
|
if key == &SHA3_NULL_RLP {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.0.remove(key)
|
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
}
|