Simple Gas calculation based on instruction gas price tier

This commit is contained in:
Tomusdrw 2016-01-13 15:21:13 +01:00
parent d7176faddc
commit 0a5666f2c0
6 changed files with 388 additions and 145 deletions

View File

@ -15,6 +15,13 @@ pub enum Error {
/// `BadJumpDestination` is returned when execution tried to move
/// to position that wasn't marked with JUMPDEST instruction
BadJumpDestination,
/// `BadInstructions` is returned when given instruction is not supported
BadInstruction,
/// `StackUnderflow` when there is not enough stack elements to execute instruction
/// First parameter says how many elements were needed and the second how many were actually on Stack
StackUnderflow(/*wanted*/usize, /*on_stack*/usize),
/// When execution would exceed defined Stack Limit
OutOfStack(/*wanted*/usize, /*stack_limit*/usize),
/// Returned on evm internal error. Should never be ignored during development.
/// Likely to cause consensus issues.
Internal,

View File

@ -2,7 +2,6 @@
use util::hash::*;
use util::uint::*;
use util::bytes::*;
use evm::{Schedule, Error};
use env_info::*;

View File

@ -2,10 +2,6 @@
pub type Instruction = u8;
pub fn is_jump (i : Instruction) -> bool {
i == JUMP || i == JUMPI || i == JUMPDEST
}
pub fn is_push (i : Instruction) -> bool {
i >= PUSH1 && i <= PUSH32
}
@ -15,25 +11,239 @@ pub fn get_push_bytes (i : Instruction) -> usize {
(i - PUSH1 + 1) as usize
}
#[test]
fn test_get_push_bytes() {
assert_eq!(get_push_bytes(PUSH1), 1);
assert_eq!(get_push_bytes(PUSH3), 3);
assert_eq!(get_push_bytes(PUSH32), 32);
}
pub fn get_dup_position (i: Instruction) -> usize {
// TODO [todr] range checking?
(i - DUP1) as usize
}
#[test]
fn test_get_dup_position() {
assert_eq!(get_dup_position(DUP1), 1);
assert_eq!(get_dup_position(DUP5), 5);
assert_eq!(get_dup_position(DUP10), 10);
}
pub fn get_swap_position (i : Instruction) -> usize {
// TODO [todr] range checking?
(i - SWAP1 + 1) as usize
}
#[test]
fn test_get_swap_position() {
assert_eq!(get_swap_position(SWAP1), 1);
assert_eq!(get_swap_position(SWAP5), 5);
assert_eq!(get_swap_position(SWAP10), 10);
}
pub fn get_log_topics (i: Instruction) -> usize {
(i - LOG0) as usize
}
#[test]
fn test_get_push_bytes() {
assert_eq!(get_push_bytes(PUSH1), 1);
assert_eq!(get_push_bytes(PUSH3), 3);
assert_eq!(get_push_bytes(PUSH32), 32);
fn test_get_log_topics() {
assert_eq!(get_log_topics(LOG0), 0);
assert_eq!(get_log_topics(LOG2), 2);
assert_eq!(get_log_topics(LOG4), 4);
}
#[derive(PartialEq)]
pub enum GasPriceTier {
/// 0 Zero
ZeroTier,
/// 2 Quick
BaseTier,
/// 3 Fastest
VeryLowTier,
/// 5 Fast
LowTier,
/// 8 Mid
MidTier,
/// 10 Slow
HighTier,
/// 20 Ext
ExtTier,
/// Multiparam or otherwise special
SpecialTier,
/// Invalid
InvalidTier
}
pub fn get_tier_idx (tier: GasPriceTier) -> usize {
match tier {
GasPriceTier::ZeroTier => 0,
GasPriceTier::BaseTier => 1,
GasPriceTier::VeryLowTier => 2,
GasPriceTier::LowTier => 3,
GasPriceTier::MidTier => 4,
GasPriceTier::HighTier => 5,
GasPriceTier::ExtTier => 6,
GasPriceTier::SpecialTier => 7,
GasPriceTier::InvalidTier => 8
}
}
pub struct InstructionInfo {
pub name: &'static str,
pub additional: usize,
pub args: usize,
pub ret: usize,
pub side_effects: bool,
pub tier: GasPriceTier
}
impl InstructionInfo {
pub fn new(name: &'static str, additional: usize, args: usize, ret: usize, side_effects: bool, tier: GasPriceTier) -> InstructionInfo {
InstructionInfo {
name: name,
additional: additional,
args: args,
ret: ret,
side_effects: side_effects,
tier: tier
}
}
}
pub fn get_info (instruction: Instruction) -> InstructionInfo {
match instruction {
STOP => InstructionInfo::new("STOP", 0, 0, 0, true, GasPriceTier::ZeroTier),
ADD => InstructionInfo::new("ADD", 0, 2, 1, false, GasPriceTier::VeryLowTier),
SUB => InstructionInfo::new("SUB", 0, 2, 1, false, GasPriceTier::VeryLowTier),
MUL => InstructionInfo::new("MUL", 0, 2, 1, false, GasPriceTier::LowTier),
DIV => InstructionInfo::new("DIV", 0, 2, 1, false, GasPriceTier::LowTier),
SDIV => InstructionInfo::new("SDIV", 0, 2, 1, false, GasPriceTier::LowTier),
MOD => InstructionInfo::new("MOD", 0, 2, 1, false, GasPriceTier::LowTier),
SMOD => InstructionInfo::new("SMOD", 0, 2, 1, false, GasPriceTier::LowTier),
EXP => InstructionInfo::new("EXP", 0, 2, 1, false, GasPriceTier::SpecialTier),
NOT => InstructionInfo::new("NOT", 0, 1, 1, false, GasPriceTier::VeryLowTier),
LT => InstructionInfo::new("LT", 0, 2, 1, false, GasPriceTier::VeryLowTier),
GT => InstructionInfo::new("GT", 0, 2, 1, false, GasPriceTier::VeryLowTier),
SLT => InstructionInfo::new("SLT", 0, 2, 1, false, GasPriceTier::VeryLowTier),
SGT => InstructionInfo::new("SGT", 0, 2, 1, false, GasPriceTier::VeryLowTier),
EQ => InstructionInfo::new("EQ", 0, 2, 1, false, GasPriceTier::VeryLowTier),
ISZERO => InstructionInfo::new("ISZERO", 0, 1, 1, false, GasPriceTier::VeryLowTier),
AND => InstructionInfo::new("AND", 0, 2, 1, false, GasPriceTier::VeryLowTier),
OR => InstructionInfo::new("OR", 0, 2, 1, false, GasPriceTier::VeryLowTier),
XOR => InstructionInfo::new("XOR", 0, 2, 1, false, GasPriceTier::VeryLowTier),
BYTE => InstructionInfo::new("BYTE", 0, 2, 1, false, GasPriceTier::VeryLowTier),
ADDMOD => InstructionInfo::new("ADDMOD", 0, 3, 1, false, GasPriceTier::MidTier),
MULMOD => InstructionInfo::new("MULMOD", 0, 3, 1, false, GasPriceTier::MidTier),
SIGNEXTEND => InstructionInfo::new("SIGNEXTEND", 0, 2, 1, false, GasPriceTier::LowTier),
SHA3 => InstructionInfo::new("SHA3", 0, 2, 1, false, GasPriceTier::SpecialTier),
ADDRESS => InstructionInfo::new("ADDRESS", 0, 0, 1, false, GasPriceTier::BaseTier),
BALANCE => InstructionInfo::new("BALANCE", 0, 1, 1, false, GasPriceTier::ExtTier),
ORIGIN => InstructionInfo::new("ORIGIN", 0, 0, 1, false, GasPriceTier::BaseTier),
CALLER => InstructionInfo::new("CALLER", 0, 0, 1, false, GasPriceTier::BaseTier),
CALLVALUE => InstructionInfo::new("CALLVALUE", 0, 0, 1, false, GasPriceTier::BaseTier),
CALLDATALOAD => InstructionInfo::new("CALLDATALOAD", 0, 1, 1, false, GasPriceTier::VeryLowTier),
CALLDATASIZE => InstructionInfo::new("CALLDATASIZE", 0, 0, 1, false, GasPriceTier::BaseTier),
CALLDATACOPY => InstructionInfo::new("CALLDATACOPY", 0, 3, 0, true, GasPriceTier::VeryLowTier),
CODESIZE => InstructionInfo::new("CODESIZE", 0, 0, 1, false, GasPriceTier::BaseTier),
CODECOPY => InstructionInfo::new("CODECOPY", 0, 3, 0, true, GasPriceTier::VeryLowTier),
GASPRICE => InstructionInfo::new("GASPRICE", 0, 0, 1, false, GasPriceTier::BaseTier),
EXTCODESIZE => InstructionInfo::new("EXTCODESIZE", 0, 1, 1, false, GasPriceTier::ExtTier),
EXTCODECOPY => InstructionInfo::new("EXTCODECOPY", 0, 4, 0, true, GasPriceTier::ExtTier),
BLOCKHASH => InstructionInfo::new("BLOCKHASH", 0, 1, 1, false, GasPriceTier::ExtTier),
COINBASE => InstructionInfo::new("COINBASE", 0, 0, 1, false, GasPriceTier::BaseTier),
TIMESTAMP => InstructionInfo::new("TIMESTAMP", 0, 0, 1, false, GasPriceTier::BaseTier),
NUMBER => InstructionInfo::new("NUMBER", 0, 0, 1, false, GasPriceTier::BaseTier),
DIFFICULTY => InstructionInfo::new("DIFFICULTY", 0, 0, 1, false, GasPriceTier::BaseTier),
GASLIMIT => InstructionInfo::new("GASLIMIT", 0, 0, 1, false, GasPriceTier::BaseTier),
POP => InstructionInfo::new("POP", 0, 1, 0, false, GasPriceTier::BaseTier),
MLOAD => InstructionInfo::new("MLOAD", 0, 1, 1, false, GasPriceTier::VeryLowTier),
MSTORE => InstructionInfo::new("MSTORE", 0, 2, 0, true, GasPriceTier::VeryLowTier),
MSTORE8 => InstructionInfo::new("MSTORE8", 0, 2, 0, true, GasPriceTier::VeryLowTier),
SLOAD => InstructionInfo::new("SLOAD", 0, 1, 1, false, GasPriceTier::SpecialTier),
SSTORE => InstructionInfo::new("SSTORE", 0, 2, 0, true, GasPriceTier::SpecialTier),
JUMP => InstructionInfo::new("JUMP", 0, 1, 0, true, GasPriceTier::MidTier),
JUMPI => InstructionInfo::new("JUMPI", 0, 2, 0, true, GasPriceTier::HighTier),
PC => InstructionInfo::new("PC", 0, 0, 1, false, GasPriceTier::BaseTier),
MSIZE => InstructionInfo::new("MSIZE", 0, 0, 1, false, GasPriceTier::BaseTier),
GAS => InstructionInfo::new("GAS", 0, 0, 1, false, GasPriceTier::BaseTier),
JUMPDEST => InstructionInfo::new("JUMPDEST", 0, 0, 0, true, GasPriceTier::SpecialTier),
PUSH1 => InstructionInfo::new("PUSH1", 1, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH2 => InstructionInfo::new("PUSH2", 2, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH3 => InstructionInfo::new("PUSH3", 3, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH4 => InstructionInfo::new("PUSH4", 4, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH5 => InstructionInfo::new("PUSH5", 5, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH6 => InstructionInfo::new("PUSH6", 6, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH7 => InstructionInfo::new("PUSH7", 7, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH8 => InstructionInfo::new("PUSH8", 8, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH9 => InstructionInfo::new("PUSH9", 9, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH10 => InstructionInfo::new("PUSH10", 10, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH11 => InstructionInfo::new("PUSH11", 11, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH12 => InstructionInfo::new("PUSH12", 12, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH13 => InstructionInfo::new("PUSH13", 13, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH14 => InstructionInfo::new("PUSH14", 14, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH15 => InstructionInfo::new("PUSH15", 15, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH16 => InstructionInfo::new("PUSH16", 16, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH17 => InstructionInfo::new("PUSH17", 17, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH18 => InstructionInfo::new("PUSH18", 18, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH19 => InstructionInfo::new("PUSH19", 19, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH20 => InstructionInfo::new("PUSH20", 20, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH21 => InstructionInfo::new("PUSH21", 21, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH22 => InstructionInfo::new("PUSH22", 22, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH23 => InstructionInfo::new("PUSH23", 23, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH24 => InstructionInfo::new("PUSH24", 24, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH25 => InstructionInfo::new("PUSH25", 25, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH26 => InstructionInfo::new("PUSH26", 26, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH27 => InstructionInfo::new("PUSH27", 27, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH28 => InstructionInfo::new("PUSH28", 28, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH29 => InstructionInfo::new("PUSH29", 29, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH30 => InstructionInfo::new("PUSH30", 30, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH31 => InstructionInfo::new("PUSH31", 31, 0, 1, false, GasPriceTier::VeryLowTier),
PUSH32 => InstructionInfo::new("PUSH32", 32, 0, 1, false, GasPriceTier::VeryLowTier),
DUP1 => InstructionInfo::new("DUP1", 0, 1, 2, false, GasPriceTier::VeryLowTier),
DUP2 => InstructionInfo::new("DUP2", 0, 2, 3, false, GasPriceTier::VeryLowTier),
DUP3 => InstructionInfo::new("DUP3", 0, 3, 4, false, GasPriceTier::VeryLowTier),
DUP4 => InstructionInfo::new("DUP4", 0, 4, 5, false, GasPriceTier::VeryLowTier),
DUP5 => InstructionInfo::new("DUP5", 0, 5, 6, false, GasPriceTier::VeryLowTier),
DUP6 => InstructionInfo::new("DUP6", 0, 6, 7, false, GasPriceTier::VeryLowTier),
DUP7 => InstructionInfo::new("DUP7", 0, 7, 8, false, GasPriceTier::VeryLowTier),
DUP8 => InstructionInfo::new("DUP8", 0, 8, 9, false, GasPriceTier::VeryLowTier),
DUP9 => InstructionInfo::new("DUP9", 0, 9, 10, false, GasPriceTier::VeryLowTier),
DUP10 => InstructionInfo::new("DUP10", 0, 10, 11, false, GasPriceTier::VeryLowTier),
DUP11 => InstructionInfo::new("DUP11", 0, 11, 12, false, GasPriceTier::VeryLowTier),
DUP12 => InstructionInfo::new("DUP12", 0, 12, 13, false, GasPriceTier::VeryLowTier),
DUP13 => InstructionInfo::new("DUP13", 0, 13, 14, false, GasPriceTier::VeryLowTier),
DUP14 => InstructionInfo::new("DUP14", 0, 14, 15, false, GasPriceTier::VeryLowTier),
DUP15 => InstructionInfo::new("DUP15", 0, 15, 16, false, GasPriceTier::VeryLowTier),
DUP16 => InstructionInfo::new("DUP16", 0, 16, 17, false, GasPriceTier::VeryLowTier),
SWAP1 => InstructionInfo::new("SWAP1", 0, 2, 2, false, GasPriceTier::VeryLowTier),
SWAP2 => InstructionInfo::new("SWAP2", 0, 3, 3, false, GasPriceTier::VeryLowTier),
SWAP3 => InstructionInfo::new("SWAP3", 0, 4, 4, false, GasPriceTier::VeryLowTier),
SWAP4 => InstructionInfo::new("SWAP4", 0, 5, 5, false, GasPriceTier::VeryLowTier),
SWAP5 => InstructionInfo::new("SWAP5", 0, 6, 6, false, GasPriceTier::VeryLowTier),
SWAP6 => InstructionInfo::new("SWAP6", 0, 7, 7, false, GasPriceTier::VeryLowTier),
SWAP7 => InstructionInfo::new("SWAP7", 0, 8, 8, false, GasPriceTier::VeryLowTier),
SWAP8 => InstructionInfo::new("SWAP8", 0, 9, 9, false, GasPriceTier::VeryLowTier),
SWAP9 => InstructionInfo::new("SWAP9", 0, 10, 10, false, GasPriceTier::VeryLowTier),
SWAP10 => InstructionInfo::new("SWAP10", 0, 11, 11, false, GasPriceTier::VeryLowTier),
SWAP11 => InstructionInfo::new("SWAP11", 0, 12, 12, false, GasPriceTier::VeryLowTier),
SWAP12 => InstructionInfo::new("SWAP12", 0, 13, 13, false, GasPriceTier::VeryLowTier),
SWAP13 => InstructionInfo::new("SWAP13", 0, 14, 14, false, GasPriceTier::VeryLowTier),
SWAP14 => InstructionInfo::new("SWAP14", 0, 15, 15, false, GasPriceTier::VeryLowTier),
SWAP15 => InstructionInfo::new("SWAP15", 0, 16, 16, false, GasPriceTier::VeryLowTier),
SWAP16 => InstructionInfo::new("SWAP16", 0, 17, 17, false, GasPriceTier::VeryLowTier),
LOG0 => InstructionInfo::new("LOG0", 0, 2, 0, true, GasPriceTier::SpecialTier),
LOG1 => InstructionInfo::new("LOG1", 0, 3, 0, true, GasPriceTier::SpecialTier),
LOG2 => InstructionInfo::new("LOG2", 0, 4, 0, true, GasPriceTier::SpecialTier),
LOG3 => InstructionInfo::new("LOG3", 0, 5, 0, true, GasPriceTier::SpecialTier),
LOG4 => InstructionInfo::new("LOG4", 0, 6, 0, true, GasPriceTier::SpecialTier),
CREATE => InstructionInfo::new("CREATE", 0, 3, 1, true, GasPriceTier::SpecialTier),
CALL => InstructionInfo::new("CALL", 0, 7, 1, true, GasPriceTier::SpecialTier),
CALLCODE => InstructionInfo::new("CALLCODE", 0, 7, 1, true, GasPriceTier::SpecialTier),
RETURN => InstructionInfo::new("RETURN", 0, 2, 0, true, GasPriceTier::ZeroTier),
DELEGATECALL => InstructionInfo::new("DELEGATECALL", 0, 6, 1, true, GasPriceTier::SpecialTier),
SUICIDE => InstructionInfo::new("SUICIDE", 0, 1, 0, true, GasPriceTier::ZeroTier),
_ => panic!(format!("Undefined instruction: {}", instruction))
}
}
// Virtual machine bytecode instruction.
@ -99,136 +309,75 @@ pub const GAS: Instruction = 0x5a; //< get the amount of available gas
pub const JUMPDEST: Instruction = 0x5b; //< set a potential jump destination
pub const PUSH1: Instruction = 0x60; //< place 1 byte item on stack
#[allow(dead_code)]
pub const PUSH2: Instruction = 0x61; //< place 2 byte item on stack
#[allow(dead_code)]
pub const PUSH3: Instruction = 0x62; //< place 3 byte item on stack
#[allow(dead_code)]
pub const PUSH4: Instruction = 0x63; //< place 4 byte item on stack
#[allow(dead_code)]
pub const PUSH5: Instruction = 0x64; //< place 5 byte item on stack
#[allow(dead_code)]
pub const PUSH6: Instruction = 0x65; //< place 6 byte item on stack
#[allow(dead_code)]
pub const PUSH7: Instruction = 0x66; //< place 7 byte item on stack
#[allow(dead_code)]
pub const PUSH8: Instruction = 0x67; //< place 8 byte item on stack
#[allow(dead_code)]
pub const PUSH9: Instruction = 0x68; //< place 9 byte item on stack
#[allow(dead_code)]
pub const PUSH10: Instruction = 0x69; //< place 10 byte item on stack
#[allow(dead_code)]
pub const PUSH11: Instruction = 0x6a; //< place 11 byte item on stack
#[allow(dead_code)]
pub const PUSH12: Instruction = 0x6b; //< place 12 byte item on stack
#[allow(dead_code)]
pub const PUSH13: Instruction = 0x6c; //< place 13 byte item on stack
#[allow(dead_code)]
pub const PUSH14: Instruction = 0x6d; //< place 14 byte item on stack
#[allow(dead_code)]
pub const PUSH15: Instruction = 0x6e; //< place 15 byte item on stack
#[allow(dead_code)]
pub const PUSH16: Instruction = 0x6f; //< place 16 byte item on stack
#[allow(dead_code)]
pub const PUSH17: Instruction = 0x70; //< place 17 byte item on stack
#[allow(dead_code)]
pub const PUSH18: Instruction = 0x71; //< place 18 byte item on stack
#[allow(dead_code)]
pub const PUSH19: Instruction = 0x72; //< place 19 byte item on stack
#[allow(dead_code)]
pub const PUSH20: Instruction = 0x73; //< place 20 byte item on stack
#[allow(dead_code)]
pub const PUSH21: Instruction = 0x74; //< place 21 byte item on stack
#[allow(dead_code)]
pub const PUSH22: Instruction = 0x75; //< place 22 byte item on stack
#[allow(dead_code)]
pub const PUSH23: Instruction = 0x76; //< place 23 byte item on stack
#[allow(dead_code)]
pub const PUSH24: Instruction = 0x77; //< place 24 byte item on stack
#[allow(dead_code)]
pub const PUSH25: Instruction = 0x78; //< place 25 byte item on stack
#[allow(dead_code)]
pub const PUSH26: Instruction = 0x79; //< place 26 byte item on stack
#[allow(dead_code)]
pub const PUSH27: Instruction = 0x7a; //< place 27 byte item on stack
#[allow(dead_code)]
pub const PUSH28: Instruction = 0x7b; //< place 28 byte item on stack
#[allow(dead_code)]
pub const PUSH29: Instruction = 0x7c; //< place 29 byte item on stack
#[allow(dead_code)]
pub const PUSH30: Instruction = 0x7d; //< place 30 byte item on stack
#[allow(dead_code)]
pub const PUSH31: Instruction = 0x7e; //< place 31 byte item on stack
pub const PUSH32: Instruction = 0x7f; //< place 32 byte item on stack
pub const DUP1: Instruction = 0x80; //< copies the highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP2: Instruction = 0x81; //< copies the second highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP3: Instruction = 0x82; //< copies the third highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP4: Instruction = 0x83; //< copies the 4th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP5: Instruction = 0x84; //< copies the 5th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP6: Instruction = 0x85; //< copies the 6th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP7: Instruction = 0x86; //< copies the 7th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP8: Instruction = 0x87; //< copies the 8th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP9: Instruction = 0x88; //< copies the 9th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP10: Instruction = 0x89; //< copies the 10th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP11: Instruction = 0x8a; //< copies the 11th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP12: Instruction = 0x8b; //< copies the 12th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP13: Instruction = 0x8c; //< copies the 13th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP14: Instruction = 0x8d; //< copies the 14th highest item in the stack to the top of the stack
#[allow(dead_code)]
pub const DUP15: Instruction = 0x8e; //< copies the 15th highest item in the stack to the top of the stack
pub const DUP16: Instruction = 0x8f; //< copies the 16th highest item in the stack to the top of the stack
pub const SWAP1: Instruction = 0x90; //< swaps the highest and second highest value on the stack
#[allow(dead_code)]
pub const SWAP2: Instruction = 0x91; //< swaps the highest and third highest value on the stack
#[allow(dead_code)]
pub const SWAP3: Instruction = 0x92; //< swaps the highest and 4th highest value on the stack
#[allow(dead_code)]
pub const SWAP4: Instruction = 0x93; //< swaps the highest and 5th highest value on the stack
#[allow(dead_code)]
pub const SWAP5: Instruction = 0x94; //< swaps the highest and 6th highest value on the stack
#[allow(dead_code)]
pub const SWAP6: Instruction = 0x95; //< swaps the highest and 7th highest value on the stack
#[allow(dead_code)]
pub const SWAP7: Instruction = 0x96; //< swaps the highest and 8th highest value on the stack
#[allow(dead_code)]
pub const SWAP8: Instruction = 0x97; //< swaps the highest and 9th highest value on the stack
#[allow(dead_code)]
pub const SWAP9: Instruction = 0x98; //< swaps the highest and 10th highest value on the stack
#[allow(dead_code)]
pub const SWAP10: Instruction = 0x99; //< swaps the highest and 11th highest value on the stack
#[allow(dead_code)]
pub const SWAP11: Instruction = 0x9a; //< swaps the highest and 12th highest value on the stack
#[allow(dead_code)]
pub const SWAP12: Instruction = 0x9b; //< swaps the highest and 13th highest value on the stack
#[allow(dead_code)]
pub const SWAP13: Instruction = 0x9c; //< swaps the highest and 14th highest value on the stack
#[allow(dead_code)]
pub const SWAP14: Instruction = 0x9d; //< swaps the highest and 15th highest value on the stack
#[allow(dead_code)]
pub const SWAP15: Instruction = 0x9e; //< swaps the highest and 16th highest value on the stack
pub const SWAP16: Instruction = 0x9f; //< swaps the highest and 17th highest value on the stack
pub const LOG0: Instruction = 0xa0; //< Makes a log entry; no topics.
#[allow(dead_code)]
pub const LOG1: Instruction = 0xa1; //< Makes a log entry; 1 topic.
#[allow(dead_code)]
pub const LOG2: Instruction = 0xa2; //< Makes a log entry; 2 topics.
#[allow(dead_code)]
pub const LOG3: Instruction = 0xa3; //< Makes a log entry; 3 topics.
pub const LOG4: Instruction = 0xa4; //< Makes a log entry; 4 topics.

View File

@ -2,7 +2,6 @@
use common::*;
use evm;
use super::schedule::Schedule;
use super::instructions as instructions;
use super::instructions::Instruction;
@ -24,6 +23,8 @@ trait Stack<T> {
fn pop_n(&mut self, no_of_elems: usize) -> Vec<T>;
/// Add element on top of the Stack
fn push(&mut self, elem: T);
/// Get number of elements on Stack
fn size(&self) -> usize;
}
impl<S : fmt::Display> Stack<S> for Vec<S> {
fn peek(&self, no_from_top: usize) -> &S {
@ -55,11 +56,14 @@ impl<S : fmt::Display> Stack<S> for Vec<S> {
vec
}
fn push(&mut self, elem: S) {
println!("Pushing to stack: {}", elem);
self.push(elem);
}
fn size(&self) -> usize {
self.len()
}
}
/// Abstraction over raw vector of Bytes. Easier state management of PC.
struct CodeReader<'a> {
@ -94,25 +98,24 @@ impl<'a> CodeReader<'a> {
let init_size = init_size_u.low_u64() as usize;
&self.code[self.position + init_off..self.position + init_off + init_size]
}
/// Stop any further execution (move PC to the end)
fn stop_execution(&mut self) {
self.position = self.code.len();
}
enum InstructionResult {
AdditionalGasCost(U256),
JumpToPosition(U256),
StopExecutionWithGasCost(U256),
StopExecution
}
pub struct Interpreter;
impl evm::Evm for Interpreter {
fn exec(&self, params: &ActionParams, ext: &mut evm::Ext) -> evm::Result {
// TODO schedule?
// TODO reserve stack
// let schedule = ext.schedule();
let code = &params.code;
let valid_jump_destinations = self.find_jump_destinations(&code);
let mut gas = params.gas.clone();
// TODO reserve stack
let mut current_gas = params.gas.clone();
let mut stack = vec![];
let mut reader = CodeReader {
position: 0,
@ -121,51 +124,110 @@ impl evm::Evm for Interpreter {
while reader.position < code.len() {
let instruction = code[reader.position];
let gas_usage = self.check_and_get_gas_usage(instruction/*, schedule*/);
// TODO check if we have enough
reader.position += 1;
// Handle jumps
match instruction {
instructions::JUMP => {
let jump = stack.pop_back();
reader.position = try!(self.verify_jump(jump, &valid_jump_destinations));
// Calculate gas cost
let gas_cost = try!(self.get_gas_cost(current_gas, params, ext, instruction, &stack));
try!(self.verify_gas(&current_gas, &gas_cost));
current_gas = current_gas - gas_cost;
// Execute instruction
let result = try!(self.exec_instruction(
current_gas, params, ext, instruction, &mut reader, &mut stack
));
// Advance
match result {
InstructionResult::JumpToPosition(position) => {
let pos = try!(self.verify_jump(position, &valid_jump_destinations));
reader.position = pos;
},
instructions::JUMPI => {
let condition = stack.pop_back();
let jump = stack.pop_back();
if !self.is_zero(condition) {
reader.position = try!(self.verify_jump(jump, &valid_jump_destinations));
}
InstructionResult::AdditionalGasCost(gas_cost) => {
current_gas = current_gas - gas_cost;
},
instructions::JUMPDEST => {
// ignore
InstructionResult::StopExecutionWithGasCost(gas_cost) => {
current_gas = current_gas - gas_cost;
reader.position = code.len();
},
_ => {
// Execute all other instructions
self.exec_instruction(params, ext, gas, instruction, &mut reader, &mut stack);
InstructionResult::StopExecution => {
reader.position = code.len();
}
}
}
Ok(U256::from(79_988))
Ok(current_gas)
}
}
impl Interpreter {
fn check_and_get_gas_usage(&self, instruction: Instruction/*, schedule: &Schedule*/) -> Gas {
U256::zero()
fn get_gas_cost(&self,
gas: Gas,
params: &ActionParams,
ext: &evm::Ext,
instruction: Instruction,
stack: &Stack<U256>
) -> evm::Result {
let schedule = ext.schedule();
let info = instructions::get_info(instruction);
if !schedule.have_delegate_call && instruction == instructions::DELEGATECALL {
return Err(evm::Error::BadInstruction);
}
if info.tier == instructions::GasPriceTier::InvalidTier {
return Err(evm::Error::BadInstruction);
}
try!(self.verify_instructions_requirements(&info, schedule.stack_limit, stack));
let tier = instructions::get_tier_idx(info.tier);
let run_gas = schedule.tier_step_gas[tier];
Ok(Gas::from(run_gas))
}
fn verify_instructions_requirements(&self,
info: &instructions::InstructionInfo,
stack_limit: usize,
stack: &Stack<U256>) -> Result<(), evm::Error> {
if !stack.has(info.args) {
Err(evm::Error::StackUnderflow(info.args, stack.size()))
} else if stack.size() - info.args + info.ret > stack_limit {
Err(evm::Error::OutOfStack(info.ret - info.args, stack_limit))
} else {
Ok(())
}
}
fn exec_instruction(&self,
gas: Gas,
params: &ActionParams,
ext: &mut evm::Ext,
gas: Gas,
instruction: Instruction,
code: &mut CodeReader,
stack: &mut Stack<U256>
) -> evm::Result {
) -> Result<InstructionResult, evm::Error> {
match instruction {
instructions::JUMP => {
let jump = stack.pop_back();
return Ok(InstructionResult::JumpToPosition(
jump
));
},
instructions::JUMPI => {
let condition = stack.pop_back();
let jump = stack.pop_back();
if !self.is_zero(condition) {
return Ok(InstructionResult::JumpToPosition(
jump
));
}
},
instructions::JUMPDEST => {
// ignore
},
instructions::CREATE => {
let endowment = stack.pop_back();
let init_off = stack.pop_back();
@ -174,35 +236,38 @@ impl Interpreter {
// TODO [todr] Fix u64 for gas
let contract_code = code.get_slice(init_off, init_size);
// TODO [todr] Fix u64 for gasLeft
let (gas_left, maybe_address) = try!(ext.create(gas.low_u64(), &endowment, &contract_code));
let (gas_left, maybe_address) = try!(
ext.create(gas.low_u64(), &endowment, &contract_code)
);
match maybe_address {
Some(address) => stack.push(address_to_u256(address)),
None => stack.push(U256::zero())
}
Ok(U256::from(gas_left))
return Ok(InstructionResult::AdditionalGasCost(
gas - Gas::from(gas_left)
));
},
// CALL, CALLCODE, DELEGATECALL
instructions::RETURN => {
let init_off = stack.pop_back();
let init_size = stack.pop_back();
code.stop_execution();
let return_code = code.get_slice(init_off, init_size);
// TODO [todr] Fix u64 for gas
let gas_left = try!(ext.ret(gas.low_u64(), &return_code));
// TODO [todr] Fix u64 for gasLeft
Ok(U256::from(gas_left))
return Ok(InstructionResult::StopExecutionWithGasCost(
gas - Gas::from(gas_left)
));
},
instructions::STOP => {
code.stop_execution();
Ok(gas)
return Ok(InstructionResult::StopExecution);
},
instructions::SUICIDE => {
// TODO [todr] Suicide should have argument with address of contract that funds should be transfered to
let address = stack.pop_back();
// ext.suicide(Address::from(address));
ext.suicide();
code.stop_execution();
Ok(gas)
return Ok(InstructionResult::StopExecution);
},
instructions::LOG0...instructions::LOG4 => {
let no_of_topics = instructions::get_log_topics(instruction);
@ -216,7 +281,6 @@ impl Interpreter {
.map(H256::from)
.collect();
ext.log(topics, code.get_slice(offset, size));
Ok(gas)
},
instructions::PUSH1...instructions::PUSH32 => {
// Load to stack
@ -224,87 +288,69 @@ impl Interpreter {
// TODO [todr] move positions management outside of CodeReader
let val = code.read(bytes);
stack.push(val);
Ok(gas)
},
instructions::MLOAD => {
// TODO [ToDr] load word from mem?
Ok(gas)
},
instructions::MSTORE => {
// TODO [ToDr] save word to mem?
Ok(gas)
},
instructions::MSTORE8 => {
// TODO [ToDr] save byte to mem?
Ok(gas)
},
instructions::MSIZE => {
// Size of memry to stack
Ok(gas)
},
instructions::SHA3 => {
let offset = stack.pop_back();
let size = stack.pop_back();
let sha3 = code.get_slice(offset, size).sha3();
stack.push(U256::from(sha3.as_slice()));
Ok(gas)
},
instructions::SLOAD => {
let key = H256::from(&stack.pop_back());
let word = U256::from(ext.sload(&key).as_slice());
stack.push(word);
Ok(gas)
},
instructions::SSTORE => {
let key = H256::from(&stack.pop_back());
let word = H256::from(&stack.pop_back());
ext.sstore(key, word);
Ok(gas)
},
instructions::PC => {
stack.push(U256::from(code.position));
Ok(gas)
},
instructions::GAS => {
stack.push(U256::from(gas));
Ok(gas)
stack.push(gas.clone());
},
instructions::ADDRESS => {
stack.push(address_to_u256(params.address.clone()));
Ok(gas)
},
instructions::ORIGIN => {
stack.push(address_to_u256(params.origin.clone()));
Ok(gas)
},
instructions::BALANCE => {
let address = u256_to_address(&stack.pop_back());
let balance = ext.balance(&address);
stack.push(balance);
Ok(gas)
},
instructions::CALLER => {
stack.push(address_to_u256(params.sender.clone()));
Ok(gas)
},
instructions::CALLVALUE => {
stack.push(params.value.clone());
Ok(gas)
},
// instructions::CALLDATALOAD
instructions::CALLDATASIZE => {
stack.push(U256::from(params.data.len()));
Ok(gas)
},
instructions::CODESIZE => {
stack.push(U256::from(code.len()));
Ok(gas)
},
instructions::EXTCODESIZE => {
let address = u256_to_address(&stack.pop_back());
let len = ext.extcode(&address).len();
stack.push(U256::from(len));
Ok(gas)
},
// instructions::CALLDATACOPY => {},
// instructions::CODECOPY => {},
@ -314,38 +360,39 @@ impl Interpreter {
// },
instructions::GASPRICE => {
stack.push(params.gas_price.clone());
Ok(gas)
},
instructions::BLOCKHASH => {
let block_number = stack.pop_back();
let block_hash = ext.blockhash(&block_number);
stack.push(U256::from(block_hash.as_slice()));
Ok(gas)
},
instructions::COINBASE => {
stack.push(address_to_u256(ext.env_info().author.clone()));
Ok(gas)
},
instructions::TIMESTAMP => {
stack.push(U256::from(ext.env_info().timestamp));
Ok(gas)
},
instructions::NUMBER => {
stack.push(U256::from(ext.env_info().number));
Ok(gas)
},
instructions::DIFFICULTY => {
stack.push(ext.env_info().difficulty.clone());
Ok(gas)
},
instructions::GASLIMIT => {
stack.push(ext.env_info().gas_limit.clone());
Ok(gas)
},
_ => {
self.exec_stack_instruction(instruction, stack);
Ok(gas)
}
};
Ok(InstructionResult::AdditionalGasCost(U256::zero()))
}
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> {
if current_gas < gas_cost {
Err(evm::Error::OutOfGas)
} else {
Ok(())
}
}

View File

@ -18,11 +18,20 @@ struct FakeExt {
codes: HashMap<Address, Bytes>,
logs: Vec<FakeLogEntry>,
_suicides: HashSet<Address>,
info: EnvInfo
info: EnvInfo,
_schedule: Schedule
}
impl FakeExt {
fn new() -> Self { FakeExt::default() }
fn new() -> Self {
FakeExt::default()
}
}
impl Default for Schedule {
fn default() -> Self {
Schedule::new_homestead()
}
}
impl Ext for FakeExt {
@ -77,7 +86,7 @@ impl Ext for FakeExt {
}
fn schedule(&self) -> &Schedule {
unimplemented!();
&self._schedule
}
fn env_info(&self) -> &EnvInfo {
@ -85,6 +94,34 @@ impl Ext for FakeExt {
}
}
#[test]
fn test_stack_underflow() {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let code = "01600055".from_hex().unwrap();
let mut params = ActionParams::new();
params.address = address.clone();
params.gas = U256::from(100_000);
params.code = code;
let mut ext = FakeExt::new();
let err = {
let vm : Box<evm::Evm> = Box::new(super::interpreter::Interpreter);
vm.exec(&params, &mut ext).unwrap_err()
};
match err {
evm::Error::StackUnderflow(wanted, stack) => {
assert_eq!(wanted, 2);
assert_eq!(stack, 0);
}
_ => {
assert!(false, "Expected StackUndeflow")
}
};
}
macro_rules! evm_test(
($name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]

View File

@ -216,7 +216,11 @@ impl<'a> Executive<'a> {
match result {
Err(evm::Error::Internal) => Err(ExecutionError::Internal),
// TODO [ToDr] BadJumpDestination @debris - how to handle that?
Err(evm::Error::OutOfGas) | Err(evm::Error::BadJumpDestination) => {
Err(evm::Error::OutOfGas)
| Err(evm::Error::BadJumpDestination)
| Err(evm::Error::BadInstruction)
| Err(evm::Error::StackUnderflow(_, _))
| Err(evm::Error::OutOfStack(_, _)) => {
*self.state = backup;
Ok(Executed {
gas: t.gas,