2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
//! Evm interface.
|
2015-12-28 22:37:15 +01:00
|
|
|
|
2016-01-11 16:33:08 +01:00
|
|
|
use common::*;
|
|
|
|
use evm::Ext;
|
2015-12-28 22:37:15 +01:00
|
|
|
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Evm errors.
|
2016-01-11 14:08:03 +01:00
|
|
|
#[derive(Debug)]
|
2016-01-11 17:01:42 +01:00
|
|
|
pub enum Error {
|
2016-01-11 03:13:41 +01:00
|
|
|
/// `OutOfGas` is returned when transaction execution runs out of gas.
|
2016-01-11 02:17:29 +01:00
|
|
|
/// The state should be reverted to the state from before the
|
|
|
|
/// transaction execution. But it does not mean that transaction
|
|
|
|
/// was invalid. Balance still should be transfered and nonce
|
|
|
|
/// should be increased.
|
2015-12-28 22:37:15 +01:00
|
|
|
OutOfGas,
|
2016-01-13 00:13:09 +01:00
|
|
|
/// `BadJumpDestination` is returned when execution tried to move
|
|
|
|
/// to position that wasn't marked with JUMPDEST instruction
|
2016-01-14 02:36:48 +01:00
|
|
|
BadJumpDestination {
|
2016-02-02 18:02:58 +01:00
|
|
|
/// Position the code tried to jump to.
|
2016-01-14 02:36:48 +01:00
|
|
|
destination: usize
|
|
|
|
},
|
2016-01-13 15:21:13 +01:00
|
|
|
/// `BadInstructions` is returned when given instruction is not supported
|
2016-01-14 02:36:48 +01:00
|
|
|
BadInstruction {
|
2016-02-02 18:02:58 +01:00
|
|
|
/// Unrecognized opcode
|
2016-01-14 02:45:16 +01:00
|
|
|
instruction: u8,
|
2016-01-14 02:36:48 +01:00
|
|
|
},
|
2016-01-13 15:21:13 +01:00
|
|
|
/// `StackUnderflow` when there is not enough stack elements to execute instruction
|
2016-01-14 01:31:45 +01:00
|
|
|
StackUnderflow {
|
2016-02-02 18:02:58 +01:00
|
|
|
/// Invoked instruction
|
2016-01-14 01:31:45 +01:00
|
|
|
instruction: &'static str,
|
2016-02-02 18:02:58 +01:00
|
|
|
/// How many stack elements was requested by instruction
|
2016-04-06 10:07:24 +02:00
|
|
|
wanted: usize,
|
2016-02-02 18:02:58 +01:00
|
|
|
/// How many elements were on stack
|
2016-01-14 01:31:45 +01:00
|
|
|
on_stack: usize
|
|
|
|
},
|
2016-01-13 15:21:13 +01:00
|
|
|
/// When execution would exceed defined Stack Limit
|
2016-01-14 01:31:45 +01:00
|
|
|
OutOfStack {
|
2016-02-02 18:02:58 +01:00
|
|
|
/// Invoked instruction
|
2016-01-14 01:31:45 +01:00
|
|
|
instruction: &'static str,
|
2016-02-02 18:02:58 +01:00
|
|
|
/// How many stack elements instruction wanted to push
|
|
|
|
wanted: usize,
|
|
|
|
/// What was the stack limit
|
2016-01-14 01:31:45 +01:00
|
|
|
limit: usize
|
|
|
|
},
|
2016-01-11 02:17:29 +01:00
|
|
|
/// Returned on evm internal error. Should never be ignored during development.
|
|
|
|
/// Likely to cause consensus issues.
|
2016-02-02 15:55:44 +01:00
|
|
|
#[allow(dead_code)] // created only by jit
|
2016-01-11 02:17:29 +01:00
|
|
|
Internal,
|
2015-12-28 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 19:04:15 +02:00
|
|
|
/// A specialized version of Result over EVM errors.
|
|
|
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
/// Gas Left: either it is a known value, or it needs to be computed by processing
|
|
|
|
/// a return instruction.
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum GasLeft<'a> {
|
|
|
|
/// Known gas left
|
|
|
|
Known(U256),
|
|
|
|
/// Return instruction must be processed.
|
|
|
|
NeedsReturn(U256, &'a [u8]),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Types that can be "finalized" using an EVM.
|
2016-04-06 10:07:24 +02:00
|
|
|
///
|
2016-06-02 19:04:15 +02:00
|
|
|
/// In practice, this is just used to define an inherent impl on
|
|
|
|
/// `Reult<GasLeft<'a>>`.
|
|
|
|
pub trait Finalize {
|
|
|
|
/// Consume the externalities, call return if necessary, and produce a final amount of gas left.
|
|
|
|
fn finalize<E: Ext>(self, ext: E) -> Result<U256>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Finalize for Result<GasLeft<'a>> {
|
|
|
|
fn finalize<E: Ext>(self, ext: E) -> Result<U256> {
|
|
|
|
match self {
|
|
|
|
Ok(GasLeft::Known(gas)) => Ok(gas),
|
|
|
|
Ok(GasLeft::NeedsReturn(gas, ret_code)) => ext.ret(&gas, ret_code),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 02:17:29 +01:00
|
|
|
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256
|
2016-07-05 15:15:44 +02:00
|
|
|
pub trait CostType: ops::Mul<Output=Self> + ops::Div<Output=Self> + ops::Add<Output=Self> + ops::Sub<Output=Self> + ops::Shr<usize, Output=Self> + ops::Shl<usize, Output=Self> + cmp::Ord + Sized + From<usize> + Copy {
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Converts this cost into `U256`
|
2016-07-05 15:15:44 +02:00
|
|
|
fn as_u256(&self) -> U256;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Tries to fit `U256` into this `Cost` type
|
2016-07-05 15:15:44 +02:00
|
|
|
fn from_u256(val: U256) -> Result<Self>;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Convert to usize (may panic)
|
2016-07-05 15:15:44 +02:00
|
|
|
fn as_usize(&self) -> usize;
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Add with overflow
|
2016-07-05 15:15:44 +02:00
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool);
|
2016-07-11 09:42:41 +02:00
|
|
|
/// Multiple with overflow
|
2016-07-05 15:15:44 +02:00
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CostType for U256 {
|
|
|
|
fn as_u256(&self) -> U256 {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_u256(val: U256) -> Result<Self> {
|
|
|
|
Ok(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_usize(&self) -> usize {
|
|
|
|
self.as_u64() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool) {
|
|
|
|
Uint::overflowing_add(self, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool) {
|
|
|
|
Uint::overflowing_mul(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CostType for usize {
|
|
|
|
fn as_u256(&self) -> U256 {
|
|
|
|
U256::from(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_u256(val: U256) -> Result<Self> {
|
|
|
|
if U256::from(val.low_u64()) != val {
|
|
|
|
return Err(Error::OutOfGas);
|
|
|
|
}
|
|
|
|
Ok(val.low_u64() as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_usize(&self) -> usize {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_add(self, other: Self) -> (Self, bool) {
|
|
|
|
self.overflowing_add(other)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow_mul(self, other: Self) -> (Self, bool) {
|
|
|
|
self.overflowing_mul(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 19:04:15 +02:00
|
|
|
/// Evm interface
|
2015-12-28 22:37:15 +01:00
|
|
|
pub trait Evm {
|
2016-01-11 02:17:29 +01:00
|
|
|
/// This function should be used to execute transaction.
|
2016-06-02 19:04:15 +02:00
|
|
|
///
|
|
|
|
/// It returns either an error, a known amount of gas left, or parameters to be used
|
|
|
|
/// to compute the final gas left.
|
|
|
|
fn exec(&mut self, params: ActionParams, ext: &mut Ext) -> Result<GasLeft>;
|
2015-12-28 22:37:15 +01:00
|
|
|
}
|