pub struct Machine<M, P: Patch> { /* private fields */ }
Expand description
A VM state with PC.
Implementations
sourceimpl<M: Memory + Default, P: Patch> Machine<M, P>
impl<M: Memory + Default, P: Patch> Machine<M, P>
Lifecycle of a Machine
When a new non-invoked transaction is created, initialize_call
or initialize_create
should be called. After this, the machine
can be stepped as normal. When the machine meets a CALL/CALLCODE
or CREATE instruction, a sub-machine will be created. This
submachine should first call invoke_call
or
invoke_create
. After the submachine is finished, it should call
apply_sub
. When the non-invoked transaction is finished, it
should first call code_deposit
if it is a contract creation
transaction. After that, it should call finalize
.
sourcepub fn initialize_call(
&mut self,
preclaimed_value: U256
) -> Result<(), RequireError>
pub fn initialize_call(
&mut self,
preclaimed_value: U256
) -> Result<(), RequireError>
sourcepub fn invoke_call(&mut self) -> Result<(), RequireError>
pub fn invoke_call(&mut self) -> Result<(), RequireError>
Initialize the runtime as a call from a CALL or CALLCODE opcode.
Panic
Requires caller of the CALL/CALLCODE opcode to be committed.
sourcepub fn initialize_create(
&mut self,
preclaimed_value: U256
) -> Result<(), RequireError>
pub fn initialize_create(
&mut self,
preclaimed_value: U256
) -> Result<(), RequireError>
Initialize a ContractCreation transaction.
Panic
Requires caller of the transaction to be committed.
sourcepub fn invoke_create(&mut self) -> Result<(), RequireError>
pub fn invoke_create(&mut self) -> Result<(), RequireError>
Initialize the runtime as a call from a CREATE opcode.
Panic
Requires caller of the CREATE opcode to be committed.
sourcepub fn code_deposit(&mut self)
pub fn code_deposit(&mut self)
Deposit code for a ContractCreation transaction or a CREATE opcode.
sourcepub fn finalize_transaction(
&mut self,
beneficiary: Address,
real_used_gas: Gas,
preclaimed_value: U256,
fresh_account_state: &AccountState<P::Account>
) -> Result<(), RequireError>
pub fn finalize_transaction(
&mut self,
beneficiary: Address,
real_used_gas: Gas,
preclaimed_value: U256,
fresh_account_state: &AccountState<P::Account>
) -> Result<(), RequireError>
Finalize a transaction. This should not be used when invoked by an opcode and should only be used in the top level.
Panic
Requires caller of the transaction to be committed.
sourcepub fn finalize_context(
&mut self,
fresh_account_state: &AccountState<P::Account>
)
pub fn finalize_context(
&mut self,
fresh_account_state: &AccountState<P::Account>
)
Finalize a context execution. This should not be used when invoked by an opcode and should only be used in the top level.
Panic
Requires caller of the transaction to be committed.
sourcepub fn apply_sub(&mut self, sub: Machine<M, P>)
pub fn apply_sub(&mut self, sub: Machine<M, P>)
Apply a sub runtime into the current runtime. This sub runtime
should have been created by the current runtime’s derive
function. Depending whether the current runtime is invoking a
ContractCreation or MessageCall instruction, it will apply
various states back.
sourceimpl<M: Memory + Default, P: Patch> Machine<M, P>
impl<M: Memory + Default, P: Patch> Machine<M, P>
sourcepub fn with_states(
context: Context,
depth: usize,
account_state: AccountState<P::Account>
) -> Self
pub fn with_states(
context: Context,
depth: usize,
account_state: AccountState<P::Account>
) -> Self
Create a new runtime with the given states.
sourcepub fn derive(&self, context: Context) -> Self
pub fn derive(&self, context: Context) -> Self
Derive this runtime to create a sub runtime. This will not modify the current runtime, and it will have a chance to review whether it wants to accept the result of this sub runtime afterwards.
sourcepub fn commit_account(
&mut self,
commitment: AccountCommitment
) -> Result<(), CommitError>
pub fn commit_account(
&mut self,
commitment: AccountCommitment
) -> Result<(), CommitError>
Commit a new account into this runtime.
sourcepub fn step_precompiled(&mut self) -> bool
pub fn step_precompiled(&mut self) -> bool
Step a precompiled runtime. This function returns true if the runtime is indeed a precompiled address. Otherwise return false with state unchanged.
sourcepub fn peek(&self) -> Option<Instruction>
pub fn peek(&self) -> Option<Instruction>
Peek the next instruction.
sourcepub fn peek_opcode(&self) -> Option<Opcode>
pub fn peek_opcode(&self) -> Option<Opcode>
Peek the next opcode.
sourcepub fn step(&mut self, runtime: &Runtime) -> Result<(), RequireError>
pub fn step(&mut self, runtime: &Runtime) -> Result<(), RequireError>
Step an instruction in the PC. The eval result is refected by the runtime status, and it will only return an error if there’re accounts or blockhashes to be committed to this runtime for it to run. In that case, the state of the current runtime will not be affected.
sourcepub fn status(&self) -> MachineStatus
pub fn status(&self) -> MachineStatus
Get the current runtime status.