[REQUIRES DB UPGRADE] have account_db use address hash

This commit is contained in:
Robert Habermeier 2016-06-13 12:48:19 +02:00
parent d696a66d71
commit e5ca5e0926

View File

@ -8,19 +8,26 @@ static NULL_RLP_STATIC: [u8; 1] = [0x80; 1];
/// Transforms trie node keys for the database /// Transforms trie node keys for the database
pub struct AccountDB<'db> { pub struct AccountDB<'db> {
db: &'db HashDB, db: &'db HashDB,
address: H256, address_hash: H256,
} }
// used to ensure that account storage keys are unique in the database.
#[inline] #[inline]
fn combine_key<'a>(address: &'a H256, key: &'a H256) -> H256 { fn combine_key<'a>(address: &'a H256, key: &'a H256) -> H256 {
address ^ key address ^ key
} }
impl<'db> AccountDB<'db> { impl<'db> AccountDB<'db> {
/// Create an AccountDB from an address.
pub fn new(db: &'db HashDB, address: &Address) -> AccountDB<'db> { pub fn new(db: &'db HashDB, address: &Address) -> AccountDB<'db> {
Self::from_hash(db, address.sha3())
}
/// Create an AccountDB from an address' hash.
pub fn from_hash(db: &'db HashDB, address_hash: H256) -> AccountDB<'db> {
AccountDB { AccountDB {
db: db, db: db,
address: address.into(), address_hash: address_hash
} }
} }
} }
@ -34,14 +41,14 @@ impl<'db> HashDB for AccountDB<'db>{
if key == &SHA3_NULL_RLP { if key == &SHA3_NULL_RLP {
return Some(&NULL_RLP_STATIC); return Some(&NULL_RLP_STATIC);
} }
self.db.lookup(&combine_key(&self.address, key)) self.db.lookup(&combine_key(&self.address_hash, key))
} }
fn exists(&self, key: &H256) -> bool { fn exists(&self, key: &H256) -> bool {
if key == &SHA3_NULL_RLP { if key == &SHA3_NULL_RLP {
return true; return true;
} }
self.db.exists(&combine_key(&self.address, key)) self.db.exists(&combine_key(&self.address_hash, key))
} }
fn insert(&mut self, _value: &[u8]) -> H256 { fn insert(&mut self, _value: &[u8]) -> H256 {
@ -60,20 +67,26 @@ impl<'db> HashDB for AccountDB<'db>{
/// DB backend wrapper for Account trie /// DB backend wrapper for Account trie
pub struct AccountDBMut<'db> { pub struct AccountDBMut<'db> {
db: &'db mut HashDB, db: &'db mut HashDB,
address: H256, address_hash: H256,
} }
impl<'db> AccountDBMut<'db> { impl<'db> AccountDBMut<'db> {
/// Create an AccountDBMut from an address.
pub fn new(db: &'db mut HashDB, address: &Address) -> AccountDBMut<'db> { pub fn new(db: &'db mut HashDB, address: &Address) -> AccountDBMut<'db> {
Self::from_hash(db, address.sha3())
}
/// Create an AccountDBMut from an address' hash.
pub fn from_hash(db: &'db mut HashDB, address_hash: H256) -> AccountDBMut<'db> {
AccountDBMut { AccountDBMut {
db: db, db: db,
address: address.into(), address_hash: address_hash,
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn immutable(&'db self) -> AccountDB<'db> { pub fn immutable(&'db self) -> AccountDB<'db> {
AccountDB { db: self.db, address: self.address.clone() } AccountDB { db: self.db, address_hash: self.address_hash.clone() }
} }
} }
@ -86,14 +99,14 @@ impl<'db> HashDB for AccountDBMut<'db>{
if key == &SHA3_NULL_RLP { if key == &SHA3_NULL_RLP {
return Some(&NULL_RLP_STATIC); return Some(&NULL_RLP_STATIC);
} }
self.db.lookup(&combine_key(&self.address, key)) self.db.lookup(&combine_key(&self.address_hash, key))
} }
fn exists(&self, key: &H256) -> bool { fn exists(&self, key: &H256) -> bool {
if key == &SHA3_NULL_RLP { if key == &SHA3_NULL_RLP {
return true; return true;
} }
self.db.exists(&combine_key(&self.address, key)) self.db.exists(&combine_key(&self.address_hash, key))
} }
fn insert(&mut self, value: &[u8]) -> H256 { fn insert(&mut self, value: &[u8]) -> H256 {
@ -101,7 +114,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
return SHA3_NULL_RLP.clone(); return SHA3_NULL_RLP.clone();
} }
let k = value.sha3(); let k = value.sha3();
let ak = combine_key(&self.address, &k); let ak = combine_key(&self.address_hash, &k);
self.db.emplace(ak, value.to_vec()); self.db.emplace(ak, value.to_vec());
k k
} }
@ -110,7 +123,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
if key == SHA3_NULL_RLP { if key == SHA3_NULL_RLP {
return; return;
} }
let key = combine_key(&self.address, &key); let key = combine_key(&self.address_hash, &key);
self.db.emplace(key, value.to_vec()) self.db.emplace(key, value.to_vec())
} }
@ -118,7 +131,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
if key == &SHA3_NULL_RLP { if key == &SHA3_NULL_RLP {
return; return;
} }
let key = combine_key(&self.address, key); let key = combine_key(&self.address_hash, key);
self.db.kill(&key) self.db.kill(&key)
} }
} }