revm_database/states/
state_builder.rs1use crate::states::block_hash_cache::BlockHashCache;
2
3use super::{cache::CacheState, state::DBBox, BundleState, State, TransitionState};
4use database_interface::{
5 bal::BalState, DBErrorMarker, Database, DatabaseRef, EmptyDB, WrapDatabaseRef,
6};
7use state::bal::Bal;
8use std::sync::Arc;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct StateBuilder<DB> {
13 database: DB,
15 with_bundle_prestate: Option<BundleState>,
18 with_cache_prestate: Option<CacheState>,
20 with_bundle_update: bool,
24 with_block_hashes: BlockHashCache,
26 bal_state: BalState,
28}
29
30impl StateBuilder<EmptyDB> {
31 pub fn new() -> Self {
36 Self::default()
37 }
38}
39
40impl<DB: Database + Default> Default for StateBuilder<DB> {
41 fn default() -> Self {
42 Self::new_with_database(DB::default())
43 }
44}
45
46impl<DB: Database> StateBuilder<DB> {
47 pub fn new_with_database(database: DB) -> Self {
49 Self {
50 database,
51 with_cache_prestate: None,
52 with_bundle_prestate: None,
53 with_bundle_update: false,
54 with_block_hashes: BlockHashCache::new(),
55 bal_state: BalState::default(),
56 }
57 }
58
59 pub fn with_database<ODB: Database>(self, database: ODB) -> StateBuilder<ODB> {
61 StateBuilder {
64 database,
65 with_cache_prestate: self.with_cache_prestate,
66 with_bundle_prestate: self.with_bundle_prestate,
67 with_bundle_update: self.with_bundle_update,
68 with_block_hashes: self.with_block_hashes,
69 bal_state: self.bal_state,
70 }
71 }
72
73 pub fn with_database_ref<ODB: DatabaseRef>(
75 self,
76 database: ODB,
77 ) -> StateBuilder<WrapDatabaseRef<ODB>> {
78 self.with_database(WrapDatabaseRef(database))
79 }
80
81 pub fn with_database_boxed<Error: DBErrorMarker>(
83 self,
84 database: DBBox<'_, Error>,
85 ) -> StateBuilder<DBBox<'_, Error>> {
86 self.with_database(database)
87 }
88
89 pub fn with_bundle_prestate(self, bundle: BundleState) -> Self {
98 Self {
99 with_bundle_prestate: Some(bundle),
100 ..self
101 }
102 }
103
104 pub fn with_bundle_update(self) -> Self {
109 Self {
110 with_bundle_update: true,
111 ..self
112 }
113 }
114
115 pub fn with_bundle_update_if(self, enable: bool) -> Self {
117 Self {
118 with_bundle_update: enable,
119 ..self
120 }
121 }
122
123 pub fn with_cached_prestate(self, cache: CacheState) -> Self {
129 Self {
130 with_cache_prestate: Some(cache),
131 ..self
132 }
133 }
134
135 pub fn with_block_hashes(self, block_hashes: BlockHashCache) -> Self {
137 Self {
138 with_block_hashes: block_hashes,
139 ..self
140 }
141 }
142
143 pub fn with_bal(mut self, bal: Arc<Bal>) -> Self {
145 self.bal_state.bal = Some(bal);
146 self
147 }
148
149 pub fn with_bal_builder(mut self) -> Self {
151 self.bal_state.bal_builder = Some(Bal::new());
152 self
153 }
154
155 pub fn with_bal_builder_if(mut self, enable: bool) -> Self {
157 if enable {
158 self.bal_state.bal_builder = Some(Bal::new());
159 }
160 self
161 }
162
163 pub fn build(mut self) -> State<DB> {
165 let use_preloaded_bundle = if self.with_cache_prestate.is_some() {
166 self.with_bundle_prestate = None;
167 false
168 } else {
169 self.with_bundle_prestate.is_some()
170 };
171 State {
172 cache: self.with_cache_prestate.unwrap_or_default(),
173 database: self.database,
174 transition_state: self.with_bundle_update.then(TransitionState::default),
175 bundle_state: self.with_bundle_prestate.unwrap_or_default(),
176 use_preloaded_bundle,
177 block_hashes: self.with_block_hashes,
178 bal_state: self.bal_state,
179 state_hook: None,
180 }
181 }
182}