mod call_inputs;
mod call_outcome;
mod create_inputs;
mod create_outcome;
pub use call_inputs::{CallInput, CallInputs, CallScheme, CallValue};
pub use call_outcome::CallOutcome;
pub use create_inputs::CreateInputs;
pub use create_outcome::CreateOutcome;
use primitives::Bytes;
use crate::{Gas, InstructionResult, InterpreterResult, SharedMemory};
use std::boxed::Box;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FrameInput {
Empty,
Call(Box<CallInputs>),
Create(Box<CreateInputs>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FrameInit {
pub depth: usize,
pub memory: SharedMemory,
pub frame_input: FrameInput,
}
impl FrameInput {
pub fn reduce_gas_limit(&mut self, amount: u64) {
match self {
FrameInput::Call(inputs) => {
inputs.gas_limit = inputs.gas_limit.saturating_sub(amount);
}
FrameInput::Create(inputs) => {
inputs.set_gas_limit(inputs.gas_limit().saturating_sub(amount));
}
FrameInput::Empty => {}
}
}
pub fn reservoir(&self) -> u64 {
match self {
FrameInput::Call(inputs) => inputs.reservoir,
FrameInput::Create(inputs) => inputs.reservoir(),
FrameInput::Empty => 0,
}
}
pub fn set_reservoir(&mut self, reservoir: u64) {
match self {
FrameInput::Call(inputs) => inputs.reservoir = reservoir,
FrameInput::Create(inputs) => inputs.set_reservoir(reservoir),
FrameInput::Empty => {}
}
}
}
impl AsMut<Self> for FrameInput {
fn as_mut(&mut self) -> &mut Self {
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InterpreterAction {
NewFrame(FrameInput),
Return(InterpreterResult),
}
impl InterpreterAction {
#[inline]
pub fn is_call(&self) -> bool {
matches!(self, InterpreterAction::NewFrame(FrameInput::Call(..)))
}
#[inline]
pub fn is_create(&self) -> bool {
matches!(self, InterpreterAction::NewFrame(FrameInput::Create(..)))
}
#[inline]
pub fn is_return(&self) -> bool {
matches!(self, InterpreterAction::Return { .. })
}
#[inline]
pub fn gas_mut(&mut self) -> Option<&mut Gas> {
match self {
InterpreterAction::Return(result) => Some(&mut result.gas),
_ => None,
}
}
#[inline]
pub fn into_result_return(self) -> Option<InterpreterResult> {
match self {
InterpreterAction::Return(result) => Some(result),
_ => None,
}
}
#[inline]
pub fn instruction_result(&self) -> Option<InstructionResult> {
match self {
InterpreterAction::Return(result) => Some(result.result),
_ => None,
}
}
#[inline]
pub fn new_frame(frame_input: FrameInput) -> Self {
Self::NewFrame(frame_input)
}
#[inline]
pub fn new_halt(result: InstructionResult, gas: Gas) -> Self {
Self::Return(InterpreterResult::new(result, Bytes::new(), gas))
}
#[inline]
pub fn new_return(result: InstructionResult, output: Bytes, gas: Gas) -> Self {
Self::Return(InterpreterResult::new(result, output, gas))
}
#[inline]
pub fn new_stop() -> Self {
Self::Return(InterpreterResult::new(
InstructionResult::Stop,
Bytes::new(),
Gas::new(0),
))
}
}