|
| 1 | +use crate::db::ConcurrentState; |
| 2 | +use revm::{ |
| 3 | + db::{ |
| 4 | + states::{BundleState, TransitionState}, |
| 5 | + EmptyDB, |
| 6 | + }, |
| 7 | + primitives::{db::DatabaseRef, B256}, |
| 8 | +}; |
| 9 | +use std::collections::BTreeMap; |
| 10 | + |
| 11 | +use super::{ConcurrentCacheState, ConcurrentStateInfo}; |
| 12 | + |
| 13 | +/// Allows building of State and initializing it with different options. |
| 14 | +#[derive(Clone, Debug)] |
| 15 | +pub struct ConcurrentStateBuilder<Db> { |
| 16 | + /// Database that we use to fetch data from. |
| 17 | + database: Db, |
| 18 | + /// Enabled state clear flag that is introduced in Spurious Dragon hardfork. |
| 19 | + /// Default is true as spurious dragon happened long time ago. |
| 20 | + with_state_clear: bool, |
| 21 | + /// if there is prestate that we want to use. |
| 22 | + /// This would mean that we have additional state layer between evm and disk/database. |
| 23 | + with_bundle_prestate: Option<BundleState>, |
| 24 | + /// This will initialize cache to this state. |
| 25 | + with_cache_prestate: Option<ConcurrentCacheState>, |
| 26 | + /// Do we want to create reverts and update bundle state. |
| 27 | + /// Default is false. |
| 28 | + with_bundle_update: bool, |
| 29 | + /// Do we want to merge transitions in background. |
| 30 | + /// This will allows evm to continue executing. |
| 31 | + /// Default is false. |
| 32 | + with_background_transition_merge: bool, |
| 33 | + /// If we want to set different block hashes |
| 34 | + with_block_hashes: BTreeMap<u64, B256>, |
| 35 | +} |
| 36 | + |
| 37 | +impl ConcurrentStateBuilder<EmptyDB> { |
| 38 | + /// Create a new builder with an empty database. |
| 39 | + /// |
| 40 | + /// If you want to instantiate it with a specific database, use |
| 41 | + /// [`new_with_database`](Self::new_with_database). |
| 42 | + pub fn new() -> Self { |
| 43 | + Self::default() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<Db: DatabaseRef + Sync + Default> Default for ConcurrentStateBuilder<Db> { |
| 48 | + fn default() -> Self { |
| 49 | + Self::new_with_database(Db::default()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<Db: DatabaseRef + Sync> ConcurrentStateBuilder<Db> { |
| 54 | + /// Create a new builder with the given database. |
| 55 | + pub const fn new_with_database(database: Db) -> Self { |
| 56 | + Self { |
| 57 | + database, |
| 58 | + with_state_clear: true, |
| 59 | + with_cache_prestate: None, |
| 60 | + with_bundle_prestate: None, |
| 61 | + with_bundle_update: false, |
| 62 | + with_background_transition_merge: false, |
| 63 | + with_block_hashes: BTreeMap::new(), |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Set the database. |
| 68 | + pub fn with_database<ODb: DatabaseRef + Sync>( |
| 69 | + self, |
| 70 | + database: ODb, |
| 71 | + ) -> ConcurrentStateBuilder<ODb> { |
| 72 | + // cast to the different database, |
| 73 | + // Note that we return different type depending of the database NewDBError. |
| 74 | + ConcurrentStateBuilder { |
| 75 | + with_state_clear: self.with_state_clear, |
| 76 | + database, |
| 77 | + with_cache_prestate: self.with_cache_prestate, |
| 78 | + with_bundle_prestate: self.with_bundle_prestate, |
| 79 | + with_bundle_update: self.with_bundle_update, |
| 80 | + with_background_transition_merge: self.with_background_transition_merge, |
| 81 | + with_block_hashes: self.with_block_hashes, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Alias for [`Self::with_database`], for revm compat reasons. |
| 86 | + pub fn with_database_ref<ODb: DatabaseRef + Sync>( |
| 87 | + self, |
| 88 | + database: ODb, |
| 89 | + ) -> ConcurrentStateBuilder<ODb> { |
| 90 | + self.with_database(database) |
| 91 | + } |
| 92 | + |
| 93 | + /// By default state clear flag is enabled but for initial sync on mainnet |
| 94 | + /// we want to disable it so proper consensus changes are in place. |
| 95 | + pub fn without_state_clear(self) -> Self { |
| 96 | + Self { with_state_clear: false, ..self } |
| 97 | + } |
| 98 | + |
| 99 | + /// Allows setting prestate that is going to be used for execution. |
| 100 | + /// This bundle state will act as additional layer of cache. |
| 101 | + /// and State after not finding data inside StateCache will try to find it inside BundleState. |
| 102 | + /// |
| 103 | + /// On update Bundle state will be changed and updated. |
| 104 | + pub fn with_bundle_prestate(self, bundle: BundleState) -> Self { |
| 105 | + Self { with_bundle_prestate: Some(bundle), ..self } |
| 106 | + } |
| 107 | + |
| 108 | + /// Make transitions and update bundle state. |
| 109 | + /// |
| 110 | + /// This is needed option if we want to create reverts |
| 111 | + /// and getting output of changed states. |
| 112 | + pub fn with_bundle_update(self) -> Self { |
| 113 | + Self { with_bundle_update: true, ..self } |
| 114 | + } |
| 115 | + |
| 116 | + /// It will use different cache for the state. If set, it will ignore bundle prestate. |
| 117 | + /// and will ignore `without_state_clear` flag as cache contains its own state_clear flag. |
| 118 | + /// |
| 119 | + /// This is useful for testing. |
| 120 | + pub fn with_cached_prestate(self, cache: impl Into<ConcurrentCacheState>) -> Self { |
| 121 | + Self { with_cache_prestate: Some(cache.into()), ..self } |
| 122 | + } |
| 123 | + |
| 124 | + /// Starts the thread that will take transitions and do merge to the bundle state |
| 125 | + /// in the background. |
| 126 | + pub fn with_background_transition_merge(self) -> Self { |
| 127 | + Self { with_background_transition_merge: true, ..self } |
| 128 | + } |
| 129 | + |
| 130 | + /// Add block hashes to the state. |
| 131 | + pub fn with_block_hashes(self, block_hashes: BTreeMap<u64, B256>) -> Self { |
| 132 | + Self { with_block_hashes: block_hashes, ..self } |
| 133 | + } |
| 134 | + |
| 135 | + /// Build the concurrent state. |
| 136 | + pub fn build(mut self) -> ConcurrentState<Db> { |
| 137 | + let use_preloaded_bundle = if self.with_cache_prestate.is_some() { |
| 138 | + self.with_bundle_prestate = None; |
| 139 | + false |
| 140 | + } else { |
| 141 | + self.with_bundle_prestate.is_some() |
| 142 | + }; |
| 143 | + ConcurrentState::new( |
| 144 | + self.database, |
| 145 | + ConcurrentStateInfo { |
| 146 | + cache: self |
| 147 | + .with_cache_prestate |
| 148 | + .unwrap_or_else(|| ConcurrentCacheState::new(self.with_state_clear)), |
| 149 | + transition_state: self.with_bundle_update.then(TransitionState::default), |
| 150 | + bundle_state: self.with_bundle_prestate.unwrap_or_default(), |
| 151 | + use_preloaded_bundle, |
| 152 | + block_hashes: self.with_block_hashes.into(), |
| 153 | + }, |
| 154 | + ) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Some code above and documentation is adapted from the revm crate, and is |
| 159 | +// reproduced here under the terms of the MIT license. |
| 160 | +// |
| 161 | +// MIT License |
| 162 | +// |
| 163 | +// Copyright (c) 2021-2024 draganrakita |
| 164 | +// |
| 165 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 166 | +// of this software and associated documentation files (the "Software"), to deal |
| 167 | +// in the Software without restriction, including without limitation the rights |
| 168 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 169 | +// copies of the Software, and to permit persons to whom the Software is |
| 170 | +// furnished to do so, subject to the following conditions: |
| 171 | +// |
| 172 | +// The above copyright notice and this permission notice shall be included in all |
| 173 | +// copies or substantial portions of the Software. |
| 174 | +// |
| 175 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 176 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 177 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 178 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 179 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 180 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 181 | +// SOFTWARE. |
0 commit comments