[REQUIRES DB UPGRADE] have account_db use address hash
This commit is contained in:
parent
d696a66d71
commit
e5ca5e0926
@ -8,19 +8,26 @@ static NULL_RLP_STATIC: [u8; 1] = [0x80; 1];
|
||||
/// Transforms trie node keys for the database
|
||||
pub struct AccountDB<'db> {
|
||||
db: &'db HashDB,
|
||||
address: H256,
|
||||
address_hash: H256,
|
||||
}
|
||||
|
||||
// used to ensure that account storage keys are unique in the database.
|
||||
#[inline]
|
||||
fn combine_key<'a>(address: &'a H256, key: &'a H256) -> H256 {
|
||||
address ^ key
|
||||
}
|
||||
|
||||
impl<'db> AccountDB<'db> {
|
||||
/// Create an AccountDB from an address.
|
||||
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 {
|
||||
db: db,
|
||||
address: address.into(),
|
||||
address_hash: address_hash
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -34,14 +41,14 @@ impl<'db> HashDB for AccountDB<'db>{
|
||||
if key == &SHA3_NULL_RLP {
|
||||
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 {
|
||||
if key == &SHA3_NULL_RLP {
|
||||
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 {
|
||||
@ -60,20 +67,26 @@ impl<'db> HashDB for AccountDB<'db>{
|
||||
/// DB backend wrapper for Account trie
|
||||
pub struct AccountDBMut<'db> {
|
||||
db: &'db mut HashDB,
|
||||
address: H256,
|
||||
address_hash: H256,
|
||||
}
|
||||
|
||||
impl<'db> AccountDBMut<'db> {
|
||||
/// Create an AccountDBMut from an address.
|
||||
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 {
|
||||
db: db,
|
||||
address: address.into(),
|
||||
address_hash: address_hash,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
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 {
|
||||
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 {
|
||||
if key == &SHA3_NULL_RLP {
|
||||
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 {
|
||||
@ -101,7 +114,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
|
||||
return SHA3_NULL_RLP.clone();
|
||||
}
|
||||
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());
|
||||
k
|
||||
}
|
||||
@ -110,7 +123,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
|
||||
if key == SHA3_NULL_RLP {
|
||||
return;
|
||||
}
|
||||
let key = combine_key(&self.address, &key);
|
||||
let key = combine_key(&self.address_hash, &key);
|
||||
self.db.emplace(key, value.to_vec())
|
||||
}
|
||||
|
||||
@ -118,7 +131,7 @@ impl<'db> HashDB for AccountDBMut<'db>{
|
||||
if key == &SHA3_NULL_RLP {
|
||||
return;
|
||||
}
|
||||
let key = combine_key(&self.address, key);
|
||||
let key = combine_key(&self.address_hash, key);
|
||||
self.db.kill(&key)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user