|
| 1 | +use revm::{ |
| 2 | + db::{states::bundle_state::BundleRetention, BundleState, State}, |
| 3 | + primitives::{Account, Address, B256}, |
| 4 | + Database, DatabaseCommit, |
| 5 | +}; |
| 6 | +use std::{collections::BTreeMap, convert::Infallible, sync::Arc}; |
| 7 | + |
| 8 | +/// Abstraction trait covering types that accumulate state changes into a |
| 9 | +/// [`BundleState`]. The prime example of this is [`State`]. These types are |
| 10 | +/// use to accumulate state changes during the execution of a sequence of |
| 11 | +/// transactions, and then provide access to the net changes in the form of a |
| 12 | +/// [`BundleState`]. |
| 13 | +pub trait StateAcc { |
| 14 | + /// Set the state clear flag. See [`State::set_state_clear_flag`]. |
| 15 | + fn set_state_clear_flag(&mut self, flag: bool); |
| 16 | + |
| 17 | + /// Merge transitions into the bundle. See [`State::merge_transitions`]. |
| 18 | + fn merge_transitions(&mut self, retention: BundleRetention); |
| 19 | + |
| 20 | + /// Take the bundle. See [`State::take_bundle`]. |
| 21 | + fn take_bundle(&mut self) -> BundleState; |
| 22 | + |
| 23 | + /// Set the block hashes, overriding any existing values, and inserting any |
| 24 | + /// absent values. |
| 25 | + fn set_block_hashes(&mut self, block_hashes: &BTreeMap<u64, B256>); |
| 26 | +} |
| 27 | + |
| 28 | +impl<Db: Database> StateAcc for State<Db> { |
| 29 | + fn set_state_clear_flag(&mut self, flag: bool) { |
| 30 | + Self::set_state_clear_flag(self, flag) |
| 31 | + } |
| 32 | + |
| 33 | + fn merge_transitions(&mut self, retention: BundleRetention) { |
| 34 | + Self::merge_transitions(self, retention) |
| 35 | + } |
| 36 | + |
| 37 | + fn take_bundle(&mut self) -> BundleState { |
| 38 | + Self::take_bundle(self) |
| 39 | + } |
| 40 | + |
| 41 | + fn set_block_hashes(&mut self, block_hashes: &BTreeMap<u64, B256>) { |
| 42 | + self.block_hashes.extend(block_hashes) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Fallible version of [`StateAcc`]. |
| 47 | +/// |
| 48 | +/// Abstraction trait covering types that accumulate state changes into a |
| 49 | +/// [`BundleState`]. The prime example of this is [`State`]. These types are |
| 50 | +/// use to accumulate state changes during the execution of a sequence of |
| 51 | +/// transactions, and then provide access to the net changes in the form of a |
| 52 | +/// [`BundleState`]. |
| 53 | +/// |
| 54 | +/// The primary motivator for this trait is to allow for the implementation of |
| 55 | +/// [`StateAcc`] for [`Arc`]-wrapped DBs, which may fail to mutate if the |
| 56 | +/// reference is not unique. |
| 57 | +pub trait TryStateAcc: Sync { |
| 58 | + /// Error type to be thrown when state accumulation fails. |
| 59 | + type Error: core::error::Error; |
| 60 | + |
| 61 | + /// Attempt to set the state clear flag. See [`State::set_state_clear_flag`]. |
| 62 | + fn try_set_state_clear_flag(&mut self, flag: bool) -> Result<(), Self::Error>; |
| 63 | + |
| 64 | + /// Attempt to merge transitions into the bundle. See |
| 65 | + /// [`State::merge_transitions`]. |
| 66 | + fn try_merge_transitions(&mut self, retention: BundleRetention) -> Result<(), Self::Error>; |
| 67 | + |
| 68 | + /// Attempt to take the bundle. See [`State::take_bundle`]. |
| 69 | + fn try_take_bundle(&mut self) -> Result<BundleState, Self::Error>; |
| 70 | + |
| 71 | + /// Attempt to set the block hashes, overriding any existing values, and |
| 72 | + /// inserting any absent values. |
| 73 | + fn try_set_block_hashes( |
| 74 | + &mut self, |
| 75 | + block_hashes: &BTreeMap<u64, B256>, |
| 76 | + ) -> Result<(), Self::Error>; |
| 77 | +} |
| 78 | + |
| 79 | +impl<Db> TryStateAcc for Db |
| 80 | +where |
| 81 | + Db: StateAcc + Sync, |
| 82 | +{ |
| 83 | + type Error = Infallible; |
| 84 | + |
| 85 | + fn try_set_state_clear_flag(&mut self, flag: bool) -> Result<(), Infallible> { |
| 86 | + self.set_state_clear_flag(flag); |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + |
| 90 | + fn try_merge_transitions(&mut self, retention: BundleRetention) -> Result<(), Infallible> { |
| 91 | + self.merge_transitions(retention); |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | + |
| 95 | + fn try_take_bundle(&mut self) -> Result<BundleState, Infallible> { |
| 96 | + Ok(self.take_bundle()) |
| 97 | + } |
| 98 | + |
| 99 | + fn try_set_block_hashes( |
| 100 | + &mut self, |
| 101 | + block_hashes: &BTreeMap<u64, B256>, |
| 102 | + ) -> Result<(), Infallible> { |
| 103 | + self.set_block_hashes(block_hashes); |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Error type for implementation of [`TryStateAcc`] for [`Arc`]-wrapped |
| 109 | +/// DBs. |
| 110 | +#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)] |
| 111 | +pub enum ArcUpgradeError { |
| 112 | + /// Arc reference is not unique. Ensure that all other references are |
| 113 | + /// dropped before attempting to mutate the state. |
| 114 | + #[error("Arc reference is not unique, cannot mutate")] |
| 115 | + NotUnique, |
| 116 | +} |
| 117 | + |
| 118 | +impl<Db> TryStateAcc for Arc<Db> |
| 119 | +where |
| 120 | + Db: StateAcc + Sync + Send, |
| 121 | +{ |
| 122 | + type Error = ArcUpgradeError; |
| 123 | + |
| 124 | + fn try_set_state_clear_flag(&mut self, flag: bool) -> Result<(), ArcUpgradeError> { |
| 125 | + Self::get_mut(self).ok_or(ArcUpgradeError::NotUnique)?.set_state_clear_flag(flag); |
| 126 | + Ok(()) |
| 127 | + } |
| 128 | + |
| 129 | + fn try_merge_transitions(&mut self, retention: BundleRetention) -> Result<(), ArcUpgradeError> { |
| 130 | + Self::get_mut(self).ok_or(ArcUpgradeError::NotUnique)?.merge_transitions(retention); |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | + |
| 134 | + fn try_take_bundle(&mut self) -> Result<BundleState, ArcUpgradeError> { |
| 135 | + Ok(Self::get_mut(self).ok_or(ArcUpgradeError::NotUnique)?.take_bundle()) |
| 136 | + } |
| 137 | + |
| 138 | + fn try_set_block_hashes( |
| 139 | + &mut self, |
| 140 | + block_hashes: &BTreeMap<u64, B256>, |
| 141 | + ) -> Result<(), ArcUpgradeError> { |
| 142 | + Self::get_mut(self).ok_or(ArcUpgradeError::NotUnique)?.set_block_hashes(block_hashes); |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/// A fallible version of [`DatabaseCommit`]. |
| 148 | +pub trait TryDatabaseCommit { |
| 149 | + /// Error type to be thrown when committing changes fails. |
| 150 | + type Error: core::error::Error; |
| 151 | + |
| 152 | + /// Attempt to commit changes to the database. |
| 153 | + fn try_commit( |
| 154 | + &mut self, |
| 155 | + changes: revm::primitives::HashMap<Address, Account>, |
| 156 | + ) -> Result<(), Self::Error>; |
| 157 | +} |
| 158 | + |
| 159 | +impl<Db> TryDatabaseCommit for Arc<Db> |
| 160 | +where |
| 161 | + Db: DatabaseCommit, |
| 162 | +{ |
| 163 | + type Error = ArcUpgradeError; |
| 164 | + |
| 165 | + fn try_commit( |
| 166 | + &mut self, |
| 167 | + changes: revm::primitives::HashMap<Address, Account>, |
| 168 | + ) -> Result<(), Self::Error> { |
| 169 | + Self::get_mut(self).ok_or(ArcUpgradeError::NotUnique)?.commit(changes); |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | +} |
0 commit comments