pub struct State<DB> {
pub cache: CacheState,
pub database: DB,
pub transition_state: Option<TransitionState>,
pub bundle_state: BundleState,
pub use_preloaded_bundle: bool,
pub block_hashes: BTreeMap<u64, B256>,
}
Expand description
State of blockchain.
State clear flag is set inside CacheState and by default it is enabled.
If you want to disable it use set_state_clear_flag
function.
Fields§
§cache: CacheState
Cached state contains both changed from evm execution and cached/loaded account/storages from database. This allows us to have only one layer of cache where we can fetch data. Additionally we can introduce some preloading of data from database.
database: DB
Optional database that we use to fetch data from. If database is not present, we will return not existing account and storage.
Note: It is marked as Send so database can be shared between threads.
transition_state: Option<TransitionState>
Block state, it aggregates transactions transitions into one state.
Build reverts and state that gets applied to the state.
bundle_state: BundleState
After block is finishes we merge those changes inside bundle. Bundle is used to update database and create changesets. Bundle state can be set on initialization if we want to use preloaded bundle.
use_preloaded_bundle: bool
Addition layer that is going to be used to fetched values before fetching values from database.
Bundle is the main output of the state execution and this allows setting previous bundle and using its values for execution.
block_hashes: BTreeMap<u64, B256>
If EVM asks for block hash we will first check if they are found here. and then ask the database.
This map can be used to give different values for block hashes if in case The fork block is different or some blocks are not saved inside database.
Implementations§
Source§impl State<EmptyDB>
impl State<EmptyDB>
Sourcepub fn builder() -> StateBuilder<EmptyDB>
pub fn builder() -> StateBuilder<EmptyDB>
Return the builder that build the State.
Source§impl<DB: Database> State<DB>
impl<DB: Database> State<DB>
Sourcepub fn bundle_size_hint(&self) -> usize
pub fn bundle_size_hint(&self) -> usize
Returns the size hint for the inner bundle state. See BundleState::size_hint for more info.
Sourcepub fn increment_balances(
&mut self,
balances: impl IntoIterator<Item = (Address, u128)>,
) -> Result<(), DB::Error>
pub fn increment_balances( &mut self, balances: impl IntoIterator<Item = (Address, u128)>, ) -> Result<(), DB::Error>
Iterate over received balances and increment all account balances. If account is not found inside cache state it will be loaded from database.
Update will create transitions for all accounts that are updated.
Like CacheAccount::increment_balance, this assumes that incremented balances are not zero, and will not overflow once incremented. If using this to implement withdrawals, zero balances must be filtered out before calling this function.
Sourcepub fn drain_balances(
&mut self,
addresses: impl IntoIterator<Item = Address>,
) -> Result<Vec<u128>, DB::Error>
pub fn drain_balances( &mut self, addresses: impl IntoIterator<Item = Address>, ) -> Result<Vec<u128>, DB::Error>
Drain balances from given account and return those values.
It is used for DAO hardfork state change to move values from given accounts.
Sourcepub fn set_state_clear_flag(&mut self, has_state_clear: bool)
pub fn set_state_clear_flag(&mut self, has_state_clear: bool)
State clear EIP-161 is enabled in Spurious Dragon hardfork.
pub fn insert_not_existing(&mut self, address: Address)
pub fn insert_account(&mut self, address: Address, info: AccountInfo)
pub fn insert_account_with_storage( &mut self, address: Address, info: AccountInfo, storage: PlainStorage, )
Sourcepub fn apply_transition(
&mut self,
transitions: Vec<(Address, TransitionAccount)>,
)
pub fn apply_transition( &mut self, transitions: Vec<(Address, TransitionAccount)>, )
Apply evm transitions to transition state.
Sourcepub fn merge_transitions(&mut self, retention: BundleRetention)
pub fn merge_transitions(&mut self, retention: BundleRetention)
Take all transitions and merge them inside bundle state. This action will create final post state and all reverts so that we at any time revert state of bundle to the state before transition is applied.
Sourcepub fn load_cache_account(
&mut self,
address: Address,
) -> Result<&mut CacheAccount, DB::Error>
pub fn load_cache_account( &mut self, address: Address, ) -> Result<&mut CacheAccount, DB::Error>
Get a mutable reference to the CacheAccount
for the given address.
If the account is not found in the cache, it will be loaded from the
database and inserted into the cache.
Sourcepub fn take_bundle(&mut self) -> BundleState
pub fn take_bundle(&mut self) -> BundleState
Takes the BundleState
changeset from the State
, replacing it
with an empty one.
This will not apply any pending TransitionState
. It is recommended
to call State::merge_transitions
before taking the bundle.
If the State
has been built with the
StateBuilder::with_bundle_prestate
option, the pre-state will be
taken along with any changes made by State::merge_transitions
.
Trait Implementations§
Source§impl<DB: Database> Database for State<DB>
impl<DB: Database> Database for State<DB>
Source§fn basic(
&mut self,
address: Address,
) -> Result<Option<AccountInfo>, Self::Error>
fn basic( &mut self, address: Address, ) -> Result<Option<AccountInfo>, Self::Error>
Source§fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>
fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error>
Source§fn storage(
&mut self,
address: Address,
index: U256,
) -> Result<U256, Self::Error>
fn storage( &mut self, address: Address, index: U256, ) -> Result<U256, Self::Error>
Source§fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>
fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>
Auto Trait Implementations§
impl<DB> Freeze for State<DB>where
DB: Freeze,
impl<DB> RefUnwindSafe for State<DB>where
DB: RefUnwindSafe,
impl<DB> Send for State<DB>where
DB: Send,
impl<DB> Sync for State<DB>where
DB: Sync,
impl<DB> Unpin for State<DB>where
DB: Unpin,
impl<DB> UnwindSafe for State<DB>where
DB: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.