1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
//! Implementation of an in-memory key-value database to represent the
//! Ethereum State Trie.
use eth_types::{geth_types, Address, BigEndianHash, Bytecode, Hash, Word, H256, U256};
use ethers_core::utils::keccak256;
use itertools::Itertools;
use lazy_static::lazy_static;
use std::collections::{HashMap, HashSet};
lazy_static! {
static ref ACCOUNT_ZERO: Account = Account::zero();
/// Hash value for empty code hash.
static ref EMPTY_CODE_HASH: Hash = CodeDB::hash(&[]);
/// bytes of empty code hash, in little endian order.
pub static ref EMPTY_CODE_HASH_LE: [u8; 32] = {
let mut bytes = EMPTY_CODE_HASH.to_fixed_bytes();
bytes.reverse();
bytes
};
}
const VALUE_ZERO: Word = Word::zero();
/// Memory storage for contract code by code hash.
#[derive(Debug, Clone, Default)]
pub struct CodeDB(HashMap<Hash, Vec<u8>>);
impl CodeDB {
/// Insert code indexed by code hash, and return the code hash.
pub fn insert(&mut self, code: Vec<u8>) -> Hash {
let hash = Self::hash(&code);
self.0.insert(hash, code);
hash
}
/// Compute hash of given code.
pub fn hash(code: &[u8]) -> Hash {
H256(keccak256(code))
}
/// Code hash of empty code.
pub fn empty_code_hash() -> Hash {
*EMPTY_CODE_HASH
}
/// Compute number of rows required for bytecode table.
pub fn num_rows_required_for_bytecode_table(&self) -> usize {
self.0.values().map(|bytecode| bytecode.len() + 1).sum()
}
/// Query Bytecode by H256
pub fn get_from_h256(&self, codehash: &H256) -> Option<Bytecode> {
self.0.get(codehash).cloned().map(|code| code.into())
}
/// Query Bytecode by U256
pub fn get_from_u256(&self, codehash: &Word) -> Option<Bytecode> {
self.get_from_h256(&H256::from_uint(codehash))
}
}
impl From<Vec<Vec<u8>>> for CodeDB {
fn from(bytecodes: Vec<Vec<u8>>) -> Self {
Self(HashMap::from_iter(
bytecodes
.iter()
.cloned()
.map(|bytecode| (Self::hash(&bytecode), bytecode)),
))
}
}
impl IntoIterator for CodeDB {
type Item = Bytecode;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0
.values()
.cloned()
.map(Bytecode::from)
.collect_vec()
.into_iter()
}
}
/// Account of the Ethereum State Trie, which contains an in-memory key-value
/// database that represents the Account Storage Trie.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Account {
/// Nonce
pub nonce: u64,
/// Balance
pub balance: Word,
/// Storage key-value map
pub storage: HashMap<Word, Word>,
/// Code hash
pub code_hash: Hash,
}
impl From<geth_types::Account> for Account {
fn from(account: geth_types::Account) -> Self {
Self {
nonce: account.nonce.as_u64(),
balance: account.balance,
storage: account.storage.clone(),
code_hash: CodeDB::hash(&account.code),
}
}
}
impl Account {
/// Return an empty account, with all values set at zero.
pub fn zero() -> Self {
Self {
nonce: 0,
balance: Word::zero(),
storage: HashMap::new(),
code_hash: *EMPTY_CODE_HASH,
}
}
/// Return if account is empty or not.
pub fn is_empty(&self) -> bool {
self.nonce == 0 && self.balance.is_zero() && self.code_hash.eq(&EMPTY_CODE_HASH)
}
}
/// In-memory key-value database that represents the Ethereum State Trie.
#[derive(Debug, Clone, Default)]
pub struct StateDB {
state: HashMap<Address, Account>,
// Fields with transaction lifespan, will be clear in `clear_access_list_and_refund`.
access_list_account: HashSet<Address>,
access_list_account_storage: HashSet<(Address, U256)>,
// `dirty_storage` contains writes during current transaction.
// When current transaction finishes, `dirty_storage` will be committed into `state`.
// The reason why we need this is that EVM needs committed state, namely
// state before current transaction, to calculate gas cost for some opcodes like sstore.
// So both dirty storage and committed storage are needed.
dirty_storage: HashMap<(Address, Word), Word>,
// Transient storage, which is cleared after the transaction.
transient_storage: HashMap<(Address, Word), Word>,
// Accounts that have been through `SELFDESTRUCT` under the situation that `is_persistent` is
// `true`. These accounts will be reset once `commit_tx` is called.
destructed_account: HashSet<Address>,
refund: u64,
}
impl StateDB {
/// Create an empty Self
pub fn new() -> Self {
Self::default()
}
/// Set an [`Account`] at `addr` in the StateDB.
pub fn set_account(&mut self, addr: &Address, acc: Account) {
self.state.insert(*addr, acc);
}
/// Get a reference to the [`Account`] at `addr`. Returns false and a zero
/// [`Account`] when the [`Account`] wasn't found in the state.
pub fn get_account(&self, addr: &Address) -> (bool, &Account) {
match self.state.get(addr) {
Some(acc) => (true, acc),
None => (false, &(*ACCOUNT_ZERO)),
}
}
/// Get a mutable reference to the [`Account`] at `addr`. If the
/// [`Account`] is not found in the state, a zero one will be inserted
/// and returned along with false.
pub fn get_account_mut(&mut self, addr: &Address) -> (bool, &mut Account) {
let found = if self.state.contains_key(addr) {
true
} else {
self.state.insert(*addr, Account::zero());
false
};
(found, self.state.get_mut(addr).expect("addr not inserted"))
}
/// Get a reference to the storage value from [`Account`] at `addr`, at
/// `key`. Returns false and a zero [`Word`] when the [`Account`] or `key`
/// wasn't found in the state.
/// Returns dirty storage state, which includes writes in current tx
pub fn get_storage(&self, addr: &Address, key: &Word) -> (bool, &Word) {
match self.dirty_storage.get(&(*addr, *key)) {
Some(v) => (true, v),
None => self.get_committed_storage(addr, key),
}
}
/// Get a reference to the transient storage value from [`Account`] at `addr`, at
/// `key`. Returns false and a zero [`Word`] when the [`Account`] or `key`
/// wasn't found in the state.
/// Returns transient storage value, which is cleared after current tx
pub fn get_transient_storage(&self, addr: &Address, key: &Word) -> (bool, &Word) {
match self.transient_storage.get(&(*addr, *key)) {
Some(v) => (true, v),
None => (false, &VALUE_ZERO),
}
}
/// Get a reference to the storage value from [`Account`] at `addr`, at
/// `key`. Returns false and a zero [`Word`] when the [`Account`] or `key`
/// wasn't found in the state.
/// Returns committed storage, which is storage state before current tx
pub fn get_committed_storage(&self, addr: &Address, key: &Word) -> (bool, &Word) {
let (_, acc) = self.get_account(addr);
match acc.storage.get(key) {
Some(value) => (true, value),
None => (false, &VALUE_ZERO),
}
}
/// Get a mutable reference to the storage value from [`Account`] at `addr`,
/// at `key`. Returns false when the [`Account`] or `key` wasn't found in
/// the state and it is created. If the [`Account`] or `key` is not found
/// in the state, a zero [`Account`] will be inserted, a zero value will
/// be inserted at `key` in its storage, and the value will be returned
/// along with false.
pub fn get_storage_mut(&mut self, addr: &Address, key: &Word) -> (bool, &mut Word) {
let (_, acc) = self.get_account_mut(addr);
let found = if acc.storage.contains_key(key) {
true
} else {
acc.storage.insert(*key, Word::zero());
false
};
(found, acc.storage.get_mut(key).expect("key not inserted"))
}
/// Set storage value at `addr` and `key`.
/// Writes into dirty_storage during transaction execution.
/// After transaction execution, `dirty_storage` is committed into `storage`
/// in `commit_tx` method.
pub fn set_storage(&mut self, addr: &Address, key: &Word, value: &Word) {
self.dirty_storage.insert((*addr, *key), *value);
}
/// Set transient storage value at `addr` and `key`.
/// Transient storage is cleared after transaction execution.
pub fn set_transient_storage(&mut self, addr: &Address, key: &Word, value: &Word) {
self.transient_storage.insert((*addr, *key), *value);
}
/// Get nonce of account with `addr`.
pub fn get_nonce(&self, addr: &Address) -> u64 {
let (_, account) = self.get_account(addr);
account.nonce
}
/// Get balance of account with the given address.
pub fn get_balance(&self, addr: &Address) -> Word {
let (_, account) = self.get_account(addr);
account.balance
}
/// Increase nonce of account with `addr` and return the previous value.
pub fn increase_nonce(&mut self, addr: &Address) -> u64 {
let (_, account) = self.get_account_mut(addr);
let nonce = account.nonce;
account.nonce += 1;
nonce
}
/// Check whether `addr` exists in account access list.
pub fn check_account_in_access_list(&self, addr: &Address) -> bool {
self.access_list_account.contains(addr)
}
/// Add `addr` into account access list. Returns `true` if it's not in the
/// access list before.
pub fn add_account_to_access_list(&mut self, addr: Address) -> bool {
self.access_list_account.insert(addr)
}
/// Remove `addr` from account access list.
pub fn remove_account_from_access_list(&mut self, addr: &Address) {
let exist = self.access_list_account.remove(addr);
debug_assert!(exist);
}
/// Check whether `(addr, key)` exists in account storage access list.
pub fn check_account_storage_in_access_list(&self, pair: &(Address, Word)) -> bool {
self.access_list_account_storage.contains(pair)
}
/// Add `(addr, key)` into account storage access list. Returns `true` if
/// it's not in the access list before.
pub fn add_account_storage_to_access_list(&mut self, (addr, key): (Address, Word)) -> bool {
self.access_list_account_storage.insert((addr, key))
}
/// Remove `(addr, key)` from account storage access list.
pub fn remove_account_storage_from_access_list(&mut self, pair: &(Address, Word)) {
let exist = self.access_list_account_storage.remove(pair);
debug_assert!(exist);
}
/// Set account as self destructed.
pub fn destruct_account(&mut self, addr: Address) {
self.destructed_account.insert(addr);
}
/// Retrieve refund.
pub fn refund(&self) -> u64 {
self.refund
}
/// Set refund
pub fn set_refund(&mut self, value: u64) {
self.refund = value;
}
/// Clear access list and refund, and commit dirty storage.
/// It should be invoked before processing
/// with new transaction with the same [`StateDB`].
pub fn commit_tx(&mut self) {
self.access_list_account = HashSet::new();
self.access_list_account_storage = HashSet::new();
for ((addr, key), value) in self.dirty_storage.clone() {
let (_, ptr) = self.get_storage_mut(&addr, &key);
*ptr = value;
}
self.dirty_storage = HashMap::new();
for addr in self.destructed_account.clone() {
let (_, account) = self.get_account_mut(&addr);
*account = ACCOUNT_ZERO.clone();
}
self.refund = 0;
}
/// Clear transient storage.
pub fn clear_transient_storage(&mut self) {
self.transient_storage = HashMap::new();
}
}
#[cfg(test)]
mod statedb_tests {
use super::*;
use eth_types::address;
#[test]
fn statedb() {
let addr_a = address!("0x0000000000000000000000000000000000000001");
let addr_b = address!("0x0000000000000000000000000000000000000002");
let mut statedb = StateDB::new();
// Get non-existing account
let (found, acc) = statedb.get_account(&addr_a);
assert!(!found);
assert_eq!(acc, &Account::zero());
// Get non-existing storage key for non-existing account
let (found, value) = statedb.get_storage(&addr_a, &Word::from(2));
assert!(!found);
assert_eq!(value, &Word::zero());
// Get mut non-existing account and set nonce
let (found, acc) = statedb.get_account_mut(&addr_a);
assert!(!found);
assert_eq!(acc, &Account::zero());
acc.nonce = 100;
// Get existing account and check nonce
let (found, acc) = statedb.get_account(&addr_a);
assert!(found);
assert_eq!(acc.nonce, 100);
// Get non-existing storage key for existing account and set value
let (found, value) = statedb.get_storage_mut(&addr_a, &Word::from(2));
assert!(!found);
assert_eq!(value, &Word::zero());
*value = Word::from(101);
// Get existing storage key and check value
let (found, value) = statedb.get_storage(&addr_a, &Word::from(2));
assert!(found);
assert_eq!(value, &Word::from(101));
// Get non-existing storage key for non-existing account and set value
let (found, value) = statedb.get_storage_mut(&addr_b, &Word::from(3));
assert!(!found);
assert_eq!(value, &Word::zero());
*value = Word::from(102);
// Get existing account and check nonce
let (found, acc) = statedb.get_account(&addr_b);
assert!(found);
assert_eq!(acc.nonce, 0);
// Get existing storage key and check value
let (found, value) = statedb.get_storage(&addr_b, &Word::from(3));
assert!(found);
assert_eq!(value, &Word::from(102));
}
}