forked from bluealloy/revm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.rs
127 lines (101 loc) · 3.51 KB
/
db.rs
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
use crate::AccountInfo;
use crate::U256;
use crate::{Account, Bytecode};
use crate::{B160, B256};
use auto_impl::auto_impl;
use hashbrown::HashMap as Map;
pub mod components;
pub use components::{
BlockHash, BlockHashRef, DatabaseComponentError, DatabaseComponents, State, StateRef,
};
/// EVM database interface.
#[auto_impl(&mut, Box)]
pub trait Database {
type Error;
/// Get basic account information.
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error>;
/// Get account code by its hash.
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>;
/// Get storage value of address at index.
fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error>;
/// Get block hash by block number.
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error>;
}
impl<F: DatabaseRef> From<F> for WrapDatabaseRef<F> {
fn from(f: F) -> Self {
WrapDatabaseRef(f)
}
}
#[auto_impl(&mut, Box)]
pub trait DatabaseCommit {
fn commit(&mut self, changes: Map<B160, Account>);
}
/// Same as [Database], but uses immutable references.
#[auto_impl(&, Box, Arc)]
pub trait DatabaseRef {
type Error;
/// Get basic account information.
fn basic(&self, address: B160) -> Result<Option<AccountInfo>, Self::Error>;
/// Get account code by its hash.
fn code_by_hash(&self, code_hash: B256) -> Result<Bytecode, Self::Error>;
/// Get storage value of address at index.
fn storage(&self, address: B160, index: U256) -> Result<U256, Self::Error>;
/// Get block hash by block number.
fn block_hash(&self, number: U256) -> Result<B256, Self::Error>;
}
/// Wraps a [`DatabaseRef`] to provide a [`Database`] implementation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WrapDatabaseRef<T: DatabaseRef>(pub T);
impl<T: DatabaseRef> Database for WrapDatabaseRef<T> {
type Error = T::Error;
#[inline]
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> {
self.0.basic(address)
}
#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.0.code_by_hash(code_hash)
}
#[inline]
fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> {
self.0.storage(address, index)
}
#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.0.block_hash(number)
}
}
/// Wraps a `dyn DatabaseRef` to provide a [`Database`] implementation.
#[doc(hidden)]
#[deprecated = "use `WrapDatabaseRef` instead"]
#[allow(missing_debug_implementations)]
pub struct RefDBWrapper<'a, E> {
pub db: &'a dyn DatabaseRef<Error = E>,
}
#[allow(deprecated)]
impl<'a, E> RefDBWrapper<'a, E> {
#[inline]
pub fn new(db: &'a dyn DatabaseRef<Error = E>) -> Self {
Self { db }
}
}
#[allow(deprecated)]
impl<'a, E> Database for RefDBWrapper<'a, E> {
type Error = E;
#[inline]
fn basic(&mut self, address: B160) -> Result<Option<AccountInfo>, Self::Error> {
self.db.basic(address)
}
#[inline]
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
self.db.code_by_hash(code_hash)
}
#[inline]
fn storage(&mut self, address: B160, index: U256) -> Result<U256, Self::Error> {
self.db.storage(address, index)
}
#[inline]
fn block_hash(&mut self, number: U256) -> Result<B256, Self::Error> {
self.db.block_hash(number)
}
}