pub struct State<B> { /* private fields */ }
Expand description
Representation of the entire state of all accounts in the system.
State
can work together with StateDB
to share account cache.
Local cache contains changes made locally and changes accumulated locally from previous commits. Global cache reflects the database state and never contains any changes.
Cache items contains account data, or the flag that account does not exist
and modification state (see AccountState
)
Account data can be in the following cache states:
- In global but not local - something that was queried from the database, but never modified
- In local but not global - something that was just added (e.g. new account)
- In both with the same value - something that was changed to a new value, but changed back to a previous block in the same block (same State instance)
- In both with different values - something that was overwritten with a new value.
All read-only state queries check local cache/modifications first, then global state cache. If data is not found in any of the caches it is loaded from the DB to the local cache.
**** IMPORTANT *************************************************************
All the modifications to the account data must set the Dirty
state in the
AccountEntry
. This is done in require
and require_or_from
. So just
use that.
Upon destruction all the local cache data propagated into the global cache. Propagated items might be rejected if current state is non-canonical.
State checkpointing.
A new checkpoint can be created with checkpoint()
. checkpoints can be
created in a hierarchy.
When a checkpoint is active all changes are applied directly into
cache
and the original value is copied into an active checkpoint.
Reverting a checkpoint with revert_to_checkpoint
involves copying
original values from the latest checkpoint back into cache
. The code
takes care not to overwrite cached storage while doing that.
checkpoint can be discarded with discard_checkpoint
. All of the orignal
backed-up values are moved into a parent checkpoint (if any).
Implementations
sourceimpl<B: Backend> State<B>
impl<B: Backend> State<B>
sourcepub fn new(db: B, account_start_nonce: U256, factories: Factories) -> State<B>
pub fn new(db: B, account_start_nonce: U256, factories: Factories) -> State<B>
Creates new state with empty state root Used for tests.
sourcepub fn from_existing(
db: B,
root: H256,
account_start_nonce: U256,
factories: Factories
) -> TrieResult<State<B>>
pub fn from_existing(
db: B,
root: H256,
account_start_nonce: U256,
factories: Factories
) -> TrieResult<State<B>>
Creates new state with existing state root
sourcepub fn vm_factory(&self) -> VmFactory
pub fn vm_factory(&self) -> VmFactory
Get a VM factory that can execute on this state.
sourcepub fn checkpoint(&mut self) -> usize
pub fn checkpoint(&mut self) -> usize
Create a recoverable checkpoint of this state. Return the checkpoint index.
sourcepub fn discard_checkpoint(&mut self)
pub fn discard_checkpoint(&mut self)
Merge last checkpoint with previous.
sourcepub fn revert_to_checkpoint(&mut self)
pub fn revert_to_checkpoint(&mut self)
Revert to the last checkpoint and discard it.
sourcepub fn into_account(
self,
account: &Address
) -> TrieResult<(Option<Arc<Bytes>>, HashMap<H256, H256>)>
pub fn into_account(
self,
account: &Address
) -> TrieResult<(Option<Arc<Bytes>>, HashMap<H256, H256>)>
Destroy the current object and return single account data.
sourcepub fn new_contract(
&mut self,
contract: &Address,
balance: U256,
nonce_offset: U256
) -> TrieResult<()>
pub fn new_contract(
&mut self,
contract: &Address,
balance: U256,
nonce_offset: U256
) -> TrieResult<()>
Create a new contract at address contract
. If there is already an account at the address
it will have its code reset, ready for init_code()
.
sourcepub fn kill_account(&mut self, account: &Address)
pub fn kill_account(&mut self, account: &Address)
Remove an existing account.
sourcepub fn exists(&self, a: &Address) -> TrieResult<bool>
pub fn exists(&self, a: &Address) -> TrieResult<bool>
Determine whether an account exists.
sourcepub fn exists_and_not_null(&self, a: &Address) -> TrieResult<bool>
pub fn exists_and_not_null(&self, a: &Address) -> TrieResult<bool>
Determine whether an account exists and if not empty.
sourcepub fn exists_and_has_code_or_nonce(&self, a: &Address) -> TrieResult<bool>
pub fn exists_and_has_code_or_nonce(&self, a: &Address) -> TrieResult<bool>
Determine whether an account exists and has code or non-zero nonce.
sourcepub fn balance(&self, a: &Address) -> TrieResult<U256>
pub fn balance(&self, a: &Address) -> TrieResult<U256>
Get the balance of account a
.
sourcepub fn nonce(&self, a: &Address) -> TrieResult<U256>
pub fn nonce(&self, a: &Address) -> TrieResult<U256>
Get the nonce of account a
.
sourcepub fn is_base_storage_root_unchanged(&self, a: &Address) -> TrieResult<bool>
pub fn is_base_storage_root_unchanged(&self, a: &Address) -> TrieResult<bool>
Whether the base storage root of an account remains unchanged.
sourcepub fn storage_root(&self, a: &Address) -> TrieResult<Option<H256>>
pub fn storage_root(&self, a: &Address) -> TrieResult<Option<H256>>
Get the storage root of account a
.
sourcepub fn original_storage_root(&self, a: &Address) -> TrieResult<H256>
pub fn original_storage_root(&self, a: &Address) -> TrieResult<H256>
Get the original storage root since last commit of account a
.
sourcepub fn checkpoint_storage_at(
&self,
start_checkpoint_index: usize,
address: &Address,
key: &H256
) -> TrieResult<Option<H256>>
pub fn checkpoint_storage_at(
&self,
start_checkpoint_index: usize,
address: &Address,
key: &H256
) -> TrieResult<Option<H256>>
Get the value of storage at a specific checkpoint.
sourcepub fn storage_at(&self, address: &Address, key: &H256) -> TrieResult<H256>
pub fn storage_at(&self, address: &Address, key: &H256) -> TrieResult<H256>
Mutate storage of account address
so that it is value
for key
.
sourcepub fn original_storage_at(
&self,
address: &Address,
key: &H256
) -> TrieResult<H256>
pub fn original_storage_at(
&self,
address: &Address,
key: &H256
) -> TrieResult<H256>
Get the value of storage after last state commitment.
sourcepub fn add_balance(
&mut self,
a: &Address,
incr: &U256,
cleanup_mode: CleanupMode<'_>
) -> TrieResult<()>
pub fn add_balance(
&mut self,
a: &Address,
incr: &U256,
cleanup_mode: CleanupMode<'_>
) -> TrieResult<()>
Add incr
to the balance of account a
.
sourcepub fn sub_balance(
&mut self,
a: &Address,
decr: &U256,
cleanup_mode: &mut CleanupMode<'_>
) -> TrieResult<()>
pub fn sub_balance(
&mut self,
a: &Address,
decr: &U256,
cleanup_mode: &mut CleanupMode<'_>
) -> TrieResult<()>
Subtract decr
from the balance of account a
.
sourcepub fn transfer_balance(
&mut self,
from: &Address,
to: &Address,
by: &U256,
cleanup_mode: CleanupMode<'_>
) -> TrieResult<()>
pub fn transfer_balance(
&mut self,
from: &Address,
to: &Address,
by: &U256,
cleanup_mode: CleanupMode<'_>
) -> TrieResult<()>
Subtracts by
from the balance of from
and adds it to that of to
.
sourcepub fn inc_nonce(&mut self, a: &Address) -> TrieResult<()>
pub fn inc_nonce(&mut self, a: &Address) -> TrieResult<()>
Increment the nonce of account a
by 1.
sourcepub fn set_storage(
&mut self,
a: &Address,
key: H256,
value: H256
) -> TrieResult<()>
pub fn set_storage(
&mut self,
a: &Address,
key: H256,
value: H256
) -> TrieResult<()>
Mutate storage of account a
so that it is value
for key
.
sourcepub fn init_code(&mut self, a: &Address, code: Bytes) -> TrieResult<()>
pub fn init_code(&mut self, a: &Address, code: Bytes) -> TrieResult<()>
Initialise the code of account a
so that it is code
.
NOTE: Account should have been created with new_contract
.
sourcepub fn reset_code(&mut self, a: &Address, code: Bytes) -> TrieResult<()>
pub fn reset_code(&mut self, a: &Address, code: Bytes) -> TrieResult<()>
Reset the code of account a
so that it is code
.
sourcepub fn apply(
&mut self,
env_info: &EnvInfo,
machine: &Machine,
t: &SignedTransaction,
tracing: bool
) -> ApplyResult<FlatTrace, VMTrace>
pub fn apply(
&mut self,
env_info: &EnvInfo,
machine: &Machine,
t: &SignedTransaction,
tracing: bool
) -> ApplyResult<FlatTrace, VMTrace>
Execute a given transaction, producing a receipt and an optional trace. This will change the state accordingly.
sourcepub fn apply_with_tracing<V, T>(
&mut self,
env_info: &EnvInfo,
machine: &Machine,
t: &SignedTransaction,
tracer: T,
vm_tracer: V
) -> ApplyResult<T::Output, V::Output> where
T: Tracer,
V: VMTracer,
pub fn apply_with_tracing<V, T>(
&mut self,
env_info: &EnvInfo,
machine: &Machine,
t: &SignedTransaction,
tracer: T,
vm_tracer: V
) -> ApplyResult<T::Output, V::Output> where
T: Tracer,
V: VMTracer,
Execute a given transaction with given tracer and VM tracer producing a receipt and an optional trace. This will change the state accordingly.
sourcepub fn commit(&mut self) -> Result<(), Error>
pub fn commit(&mut self) -> Result<(), Error>
t_nb 8.5.2 Commits our cached account changes into the trie.
sourcepub fn kill_garbage(
&mut self,
touched: &HashSet<Address>,
remove_empty_touched: bool,
min_balance: &Option<U256>,
kill_contracts: bool
) -> TrieResult<()>
pub fn kill_garbage(
&mut self,
touched: &HashSet<Address>,
remove_empty_touched: bool,
min_balance: &Option<U256>,
kill_contracts: bool
) -> TrieResult<()>
Remove any touched empty or dust accounts.
sourcepub fn populate_from(&mut self, accounts: PodState)
pub fn populate_from(&mut self, accounts: PodState)
Populate the state from accounts
.
Used for tests.
sourcepub fn diff_from<X: Backend>(&self, orig: State<X>) -> TrieResult<StateDiff>
pub fn diff_from<X: Backend>(&self, orig: State<X>) -> TrieResult<StateDiff>
Returns a StateDiff
describing the difference from orig
to self
.
Consumes self.
sourcepub fn patch_account(
&self,
a: &Address,
code: Arc<Bytes>,
storage: HashMap<H256, H256>
) -> TrieResult<()>
pub fn patch_account(
&self,
a: &Address,
code: Arc<Bytes>,
storage: HashMap<H256, H256>
) -> TrieResult<()>
Replace account code and storage. Creates account if it does not exist.
sourceimpl<B: Backend> State<B>
impl<B: Backend> State<B>
sourcepub fn prove_account(
&self,
account_key: H256
) -> TrieResult<(Vec<Bytes>, BasicAccount)>
pub fn prove_account(
&self,
account_key: H256
) -> TrieResult<(Vec<Bytes>, BasicAccount)>
Prove an account’s existence or nonexistence in the state trie.
Returns a merkle proof of the account’s trie node omitted or an encountered trie error.
If the account doesn’t exist in the trie, prove that and return defaults.
Requires a secure trie to be used for accurate results.
account_key
== keccak(address)
sourcepub fn prove_storage(
&self,
account_key: H256,
storage_key: H256
) -> TrieResult<(Vec<Bytes>, H256)>
pub fn prove_storage(
&self,
account_key: H256,
storage_key: H256
) -> TrieResult<(Vec<Bytes>, H256)>
Prove an account’s storage key’s existence or nonexistence in the state.
Returns a merkle proof of the account’s storage trie.
Requires a secure trie to be used for correctness.
account_key
== keccak(address)
storage_key
== keccak(key)
Trait Implementations
sourceimpl<B: Backend> StateInfo for State<B>
impl<B: Backend> StateInfo for State<B>
sourcefn nonce(&self, a: &Address) -> TrieResult<U256>
fn nonce(&self, a: &Address) -> TrieResult<U256>
Get the nonce of account a
.
sourcefn balance(&self, a: &Address) -> TrieResult<U256>
fn balance(&self, a: &Address) -> TrieResult<U256>
Get the balance of account a
.
sourcefn storage_at(&self, address: &Address, key: &H256) -> TrieResult<H256>
fn storage_at(&self, address: &Address, key: &H256) -> TrieResult<H256>
Mutate storage of account address
so that it is value
for key
.
Auto Trait Implementations
impl<B> !RefUnwindSafe for State<B>
impl<B> Send for State<B> where
B: Send,
impl<B> !Sync for State<B>
impl<B> Unpin for State<B> where
B: Unpin,
impl<B> !UnwindSafe for State<B>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more