Some initial implementation of more complicated methods
This commit is contained in:
parent
915a6050cd
commit
0733214059
@ -12,6 +12,9 @@ pub enum Error {
|
|||||||
/// was invalid. Balance still should be transfered and nonce
|
/// was invalid. Balance still should be transfered and nonce
|
||||||
/// should be increased.
|
/// should be increased.
|
||||||
OutOfGas,
|
OutOfGas,
|
||||||
|
/// `BadJumpDestination` is returned when execution tried to move
|
||||||
|
/// to position that wasn't marked with JUMPDEST instruction
|
||||||
|
BadJumpDestination,
|
||||||
/// Returned on evm internal error. Should never be ignored during development.
|
/// Returned on evm internal error. Should never be ignored during development.
|
||||||
/// Likely to cause consensus issues.
|
/// Likely to cause consensus issues.
|
||||||
Internal,
|
Internal,
|
||||||
|
@ -44,7 +44,7 @@ pub trait Ext {
|
|||||||
fn extcode(&self, address: &Address) -> Vec<u8>;
|
fn extcode(&self, address: &Address) -> Vec<u8>;
|
||||||
|
|
||||||
/// Creates log entry with given topics and data
|
/// Creates log entry with given topics and data
|
||||||
fn log(&mut self, topics: Vec<H256>, data: Bytes);
|
fn log(&mut self, topics: Vec<H256>, data: &[u8]);
|
||||||
|
|
||||||
/// Should be called when transaction calls `RETURN` opcode.
|
/// Should be called when transaction calls `RETURN` opcode.
|
||||||
/// Returns gas_left if cost of returning the data is not too high.
|
/// Returns gas_left if cost of returning the data is not too high.
|
||||||
|
@ -15,7 +15,7 @@ impl Factory {
|
|||||||
/// Returns native rust evm
|
/// Returns native rust evm
|
||||||
#[cfg(not(feature = "jit"))]
|
#[cfg(not(feature = "jit"))]
|
||||||
pub fn create() -> Box<Evm> {
|
pub fn create() -> Box<Evm> {
|
||||||
unimplemented!();
|
Box::new(super::interpreter::Interpreter::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
180
src/evm/instructions.rs
Normal file
180
src/evm/instructions.rs
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
//! VM Instructions list and utility functions
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_push_bytes (i : Instruction) -> usize {
|
||||||
|
// TODO [todr] range checking?
|
||||||
|
(i - PUSH1 + 1) as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_dup_position (i: Instruction) -> usize {
|
||||||
|
// TODO [todr] range checking?
|
||||||
|
(i - DUP1) as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_swap_position (i : Instruction) -> usize {
|
||||||
|
// TODO [todr] range checking?
|
||||||
|
(i - SWAP1 + 1) as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Virtual machine bytecode instruction.
|
||||||
|
pub const STOP: Instruction = 0x00; //< halts execution
|
||||||
|
pub const ADD: Instruction = 0x01; //< addition operation
|
||||||
|
pub const MUL: Instruction = 0x02; //< mulitplication operation
|
||||||
|
pub const SUB: Instruction = 0x03; //< subtraction operation
|
||||||
|
pub const DIV: Instruction = 0x04; //< integer division operation
|
||||||
|
pub const SDIV: Instruction = 0x05; //< signed integer division operation
|
||||||
|
pub const MOD: Instruction = 0x06; //< modulo remainder operation
|
||||||
|
pub const SMOD: Instruction = 0x07; //< signed modulo remainder operation
|
||||||
|
pub const ADDMOD: Instruction = 0x08; //< unsigned modular addition
|
||||||
|
pub const MULMOD: Instruction = 0x09; //< unsigned modular multiplication
|
||||||
|
pub const EXP: Instruction = 0x0a; //< exponential operation
|
||||||
|
pub const SIGNEXTEND: Instruction = 0x0b; //< extend length of signed integer
|
||||||
|
|
||||||
|
pub const LT: Instruction = 0x10; //< less-than comparision
|
||||||
|
pub const GT: Instruction = 0x11; //< greater-than comparision
|
||||||
|
pub const SLT: Instruction = 0x12; //< signed less-than comparision
|
||||||
|
pub const SGT: Instruction = 0x13; //< signed greater-than comparision
|
||||||
|
pub const EQ: Instruction = 0x14; //< equality comparision
|
||||||
|
pub const ISZERO: Instruction = 0x15; //< simple not operator
|
||||||
|
pub const AND: Instruction = 0x16; //< bitwise AND operation
|
||||||
|
pub const OR: Instruction = 0x17; //< bitwise OR operation
|
||||||
|
pub const XOR: Instruction = 0x18; //< bitwise XOR operation
|
||||||
|
pub const NOT: Instruction = 0x19; //< bitwise NOT opertation
|
||||||
|
pub const BYTE: Instruction = 0x1a; //< retrieve single byte from word
|
||||||
|
|
||||||
|
pub const SHA3: Instruction = 0x20; //< compute SHA3-256 hash
|
||||||
|
|
||||||
|
pub const ADDRESS: Instruction = 0x30; //< get address of currently executing account
|
||||||
|
pub const BALANCE: Instruction = 0x31; //< get balance of the given account
|
||||||
|
pub const ORIGIN: Instruction = 0x32; //< get execution origination address
|
||||||
|
pub const CALLER: Instruction = 0x33; //< get caller address
|
||||||
|
pub const CALLVALUE: Instruction = 0x34; //< get deposited value by the instruction/transaction responsible for this execution
|
||||||
|
pub const CALLDATALOAD: Instruction = 0x35; //< get input data of current environment
|
||||||
|
pub const CALLDATASIZE: Instruction = 0x36; //< get size of input data in current environment
|
||||||
|
pub const CALLDATACOPY: Instruction = 0x37; //< copy input data in current environment to memory
|
||||||
|
pub const CODESIZE: Instruction = 0x38; //< get size of code running in current environment
|
||||||
|
pub const CODECOPY: Instruction = 0x39; //< copy code running in current environment to memory
|
||||||
|
pub const GASPRICE: Instruction = 0x3a; //< get price of gas in current environment
|
||||||
|
pub const EXTCODESIZE: Instruction = 0x3b; //< get external code size (from another contract)
|
||||||
|
pub const EXTCODECOPY: Instruction = 0x3c; //< copy external code (from another contract)
|
||||||
|
|
||||||
|
pub const BLOCKHASH: Instruction = 0x40; //< get hash of most recent complete block
|
||||||
|
pub const COINBASE: Instruction = 0x41; //< get the block's coinbase address
|
||||||
|
pub const TIMESTAMP: Instruction = 0x42; //< get the block's timestamp
|
||||||
|
pub const NUMBER: Instruction = 0x43; //< get the block's number
|
||||||
|
pub const DIFFICULTY: Instruction = 0x44; //< get the block's difficulty
|
||||||
|
pub const GASLIMIT: Instruction = 0x45; //< get the block's gas limit
|
||||||
|
|
||||||
|
pub const POP: Instruction = 0x50; //< remove item from stack
|
||||||
|
pub const MLOAD: Instruction = 0x51; //< load word from memory
|
||||||
|
pub const MSTORE: Instruction = 0x52; //< save word to memory
|
||||||
|
pub const MSTORE8: Instruction = 0x53; //< save byte to memory
|
||||||
|
pub const SLOAD: Instruction = 0x54; //< load word from storage
|
||||||
|
pub const SSTORE: Instruction = 0x55; //< save word to storage
|
||||||
|
pub const JUMP: Instruction = 0x56; //< alter the program counter
|
||||||
|
pub const JUMPI: Instruction = 0x57; //< conditionally alter the program counter
|
||||||
|
pub const PC: Instruction = 0x58; //< get the program counter
|
||||||
|
pub const MSIZE: Instruction = 0x59; //< get the size of active memory
|
||||||
|
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
|
||||||
|
pub const PUSH2: Instruction = 0x61; //< place 2 byte item on stack
|
||||||
|
pub const PUSH3: Instruction = 0x62; //< place 3 byte item on stack
|
||||||
|
pub const PUSH4: Instruction = 0x63; //< place 4 byte item on stack
|
||||||
|
pub const PUSH5: Instruction = 0x64; //< place 5 byte item on stack
|
||||||
|
pub const PUSH6: Instruction = 0x65; //< place 6 byte item on stack
|
||||||
|
pub const PUSH7: Instruction = 0x66; //< place 7 byte item on stack
|
||||||
|
pub const PUSH8: Instruction = 0x67; //< place 8 byte item on stack
|
||||||
|
pub const PUSH9: Instruction = 0x68; //< place 9 byte item on stack
|
||||||
|
pub const PUSH10: Instruction = 0x69; //< place 10 byte item on stack
|
||||||
|
pub const PUSH11: Instruction = 0x6a; //< place 11 byte item on stack
|
||||||
|
pub const PUSH12: Instruction = 0x6b; //< place 12 byte item on stack
|
||||||
|
pub const PUSH13: Instruction = 0x6c; //< place 13 byte item on stack
|
||||||
|
pub const PUSH14: Instruction = 0x6d; //< place 14 byte item on stack
|
||||||
|
pub const PUSH15: Instruction = 0x6e; //< place 15 byte item on stack
|
||||||
|
pub const PUSH16: Instruction = 0x6f; //< place 16 byte item on stack
|
||||||
|
pub const PUSH17: Instruction = 0x70; //< place 17 byte item on stack
|
||||||
|
pub const PUSH18: Instruction = 0x71; //< place 18 byte item on stack
|
||||||
|
pub const PUSH19: Instruction = 0x72; //< place 19 byte item on stack
|
||||||
|
pub const PUSH20: Instruction = 0x73; //< place 20 byte item on stack
|
||||||
|
pub const PUSH21: Instruction = 0x74; //< place 21 byte item on stack
|
||||||
|
pub const PUSH22: Instruction = 0x75; //< place 22 byte item on stack
|
||||||
|
pub const PUSH23: Instruction = 0x76; //< place 23 byte item on stack
|
||||||
|
pub const PUSH24: Instruction = 0x77; //< place 24 byte item on stack
|
||||||
|
pub const PUSH25: Instruction = 0x78; //< place 25 byte item on stack
|
||||||
|
pub const PUSH26: Instruction = 0x79; //< place 26 byte item on stack
|
||||||
|
pub const PUSH27: Instruction = 0x7a; //< place 27 byte item on stack
|
||||||
|
pub const PUSH28: Instruction = 0x7b; //< place 28 byte item on stack
|
||||||
|
pub const PUSH29: Instruction = 0x7c; //< place 29 byte item on stack
|
||||||
|
pub const PUSH30: Instruction = 0x7d; //< place 30 byte item on stack
|
||||||
|
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
|
||||||
|
pub const DUP2: Instruction = 0x81; //< copies the second highest item in the stack to the top of the stack
|
||||||
|
pub const DUP3: Instruction = 0x82; //< copies the third highest item in the stack to the top of the stack
|
||||||
|
pub const DUP4: Instruction = 0x83; //< copies the 4th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP5: Instruction = 0x84; //< copies the 5th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP6: Instruction = 0x85; //< copies the 6th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP7: Instruction = 0x86; //< copies the 7th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP8: Instruction = 0x87; //< copies the 8th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP9: Instruction = 0x88; //< copies the 9th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP10: Instruction = 0x89; //< copies the 10th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP11: Instruction = 0x8a; //< copies the 11th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP12: Instruction = 0x8b; //< copies the 12th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP13: Instruction = 0x8c; //< copies the 13th highest item in the stack to the top of the stack
|
||||||
|
pub const DUP14: Instruction = 0x8d; //< copies the 14th highest item in the stack to the top of the stack
|
||||||
|
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
|
||||||
|
pub const SWAP2: Instruction = 0x91; //< swaps the highest and third highest value on the stack
|
||||||
|
pub const SWAP3: Instruction = 0x92; //< swaps the highest and 4th highest value on the stack
|
||||||
|
pub const SWAP4: Instruction = 0x93; //< swaps the highest and 5th highest value on the stack
|
||||||
|
pub const SWAP5: Instruction = 0x94; //< swaps the highest and 6th highest value on the stack
|
||||||
|
pub const SWAP6: Instruction = 0x95; //< swaps the highest and 7th highest value on the stack
|
||||||
|
pub const SWAP7: Instruction = 0x96; //< swaps the highest and 8th highest value on the stack
|
||||||
|
pub const SWAP8: Instruction = 0x97; //< swaps the highest and 9th highest value on the stack
|
||||||
|
pub const SWAP9: Instruction = 0x98; //< swaps the highest and 10th highest value on the stack
|
||||||
|
pub const SWAP10: Instruction = 0x99; //< swaps the highest and 11th highest value on the stack
|
||||||
|
pub const SWAP11: Instruction = 0x9a; //< swaps the highest and 12th highest value on the stack
|
||||||
|
pub const SWAP12: Instruction = 0x9b; //< swaps the highest and 13th highest value on the stack
|
||||||
|
pub const SWAP13: Instruction = 0x9c; //< swaps the highest and 14th highest value on the stack
|
||||||
|
pub const SWAP14: Instruction = 0x9d; //< swaps the highest and 15th highest value on the stack
|
||||||
|
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.
|
||||||
|
pub const LOG1: Instruction = 0xa1; //< Makes a log entry; 1 topic.
|
||||||
|
pub const LOG2: Instruction = 0xa2; //< Makes a log entry; 2 topics.
|
||||||
|
pub const LOG3: Instruction = 0xa3; //< Makes a log entry; 3 topics.
|
||||||
|
pub const LOG4: Instruction = 0xa4; //< Makes a log entry; 4 topics.
|
||||||
|
|
||||||
|
pub const CREATE: Instruction = 0xf0; //< create a new account with associated code
|
||||||
|
pub const CALL: Instruction = 0xf1; //< message-call into an account
|
||||||
|
pub const CALLCODE: Instruction = 0xf2; //< message-call with another account's code only
|
||||||
|
pub const RETURN: Instruction = 0xf3; //< halt execution returning output data
|
||||||
|
pub const DELEGATECALL: Instruction = 0xf4; //< like CALLCODE but keeps caller's value and sender
|
||||||
|
pub const SUICIDE: Instruction = 0xff; //< halt execution and register account for later deletion
|
||||||
|
|
326
src/evm/interpreter.rs
Normal file
326
src/evm/interpreter.rs
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
///! Rust VM implementation
|
||||||
|
|
||||||
|
use common::*;
|
||||||
|
use evm;
|
||||||
|
use super::schedule::Schedule;
|
||||||
|
use super::instructions as instructions;
|
||||||
|
use super::instructions::Instruction;
|
||||||
|
|
||||||
|
type CodePosition = usize;
|
||||||
|
type Gas = U256;
|
||||||
|
type ProgramCounter = usize;
|
||||||
|
|
||||||
|
/// Stack trait with VM-friendly API
|
||||||
|
trait Stack<T> {
|
||||||
|
/// Returns `Stack[len(Stack) - no_from_top]`
|
||||||
|
fn peek(&self, no_from_top: usize) -> &T;
|
||||||
|
/// Swaps Stack[len(Stack)] and Stack[len(Stack) - no_from_top]
|
||||||
|
fn swap_with_top(&mut self, no_from_top: usize);
|
||||||
|
/// Returns true if Stack has at least `no_of_elems` elements
|
||||||
|
fn has(&self, no_of_elems: usize) -> bool;
|
||||||
|
/// Get element from top and remove it from Stack. Panics if stack is empty.
|
||||||
|
fn pop_back(&mut self) -> T;
|
||||||
|
/// Get elements from top and remove them from Stack. Panics if stack is empty.
|
||||||
|
fn pop_n(&mut self, no_of_elems: usize) -> Vec<T>;
|
||||||
|
/// Add element on top of the Stack
|
||||||
|
fn push(&mut self, elem: T);
|
||||||
|
}
|
||||||
|
impl<S> Stack<S> for Vec<S> {
|
||||||
|
fn peek(&self, no_from_top: usize) -> &S {
|
||||||
|
return &self[self.len() - no_from_top - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn swap_with_top(&mut self, no_from_top: usize) {
|
||||||
|
let len = self.len();
|
||||||
|
self.swap(len - no_from_top - 1, len - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has(&self, no_of_elems: usize) -> bool {
|
||||||
|
self.len() >= no_of_elems
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop_back(&mut self) -> S {
|
||||||
|
let val = self.pop();
|
||||||
|
match val {
|
||||||
|
Some(x) => x,
|
||||||
|
None => panic!("Tried to pop from empty stack.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop_n(&mut self, no_of_elems: usize) -> Vec<S> {
|
||||||
|
let mut vec = Vec::new();
|
||||||
|
for i in 1..no_of_elems {
|
||||||
|
vec.push(self.pop_back());
|
||||||
|
}
|
||||||
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn push(&mut self, elem: S) {
|
||||||
|
self.push(elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Abstraction over raw vector of Bytes. Easier state management of PC.
|
||||||
|
struct CodeReader<'a> {
|
||||||
|
position: ProgramCounter,
|
||||||
|
code: &'a Bytes
|
||||||
|
}
|
||||||
|
impl<'a> CodeReader<'a> {
|
||||||
|
/// Get `no_of_bytes` from code and convert to U256. Move PC
|
||||||
|
fn read(&mut self, no_of_bytes: usize) -> U256 {
|
||||||
|
self.position += no_of_bytes;
|
||||||
|
// TODO [todr] READ and return something usefull
|
||||||
|
U256::zero()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieve part of the code described by offset and size
|
||||||
|
fn get_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
|
||||||
|
let init_off = init_off_u.low_u64() as usize;
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = ¶ms.code;
|
||||||
|
let valid_jump_destinations = self.find_jump_destinations(&code);
|
||||||
|
|
||||||
|
let mut gas = params.gas.clone();
|
||||||
|
let mut stack = vec![];
|
||||||
|
let mut reader = CodeReader {
|
||||||
|
position: 0,
|
||||||
|
code: &code
|
||||||
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
// Handle jumps
|
||||||
|
match instruction {
|
||||||
|
instructions::JUMP => {
|
||||||
|
let jump = stack.pop_back();
|
||||||
|
reader.position = try!(self.verify_jump(jump, &valid_jump_destinations));
|
||||||
|
},
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
instructions::JUMPDEST => {
|
||||||
|
// ignore
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
// Execute all other instructions
|
||||||
|
self.exec_instruction(params, ext, gas, instruction, &mut reader, &mut stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader.position += 1;
|
||||||
|
}
|
||||||
|
Ok(gas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Interpreter {
|
||||||
|
|
||||||
|
fn check_and_get_gas_usage(&self, instruction: Instruction/*, schedule: &Schedule*/) -> Gas {
|
||||||
|
U256::zero()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exec_instruction(&self,
|
||||||
|
params: &ActionParams,
|
||||||
|
ext: &mut evm::Ext,
|
||||||
|
gas: Gas,
|
||||||
|
instruction: Instruction,
|
||||||
|
code: &mut CodeReader,
|
||||||
|
stack: &mut Stack<U256>
|
||||||
|
) -> evm::Result {
|
||||||
|
match instruction {
|
||||||
|
instructions::CREATE => {
|
||||||
|
let endowment = stack.pop_back();
|
||||||
|
let init_off = stack.pop_back();
|
||||||
|
let init_size = stack.pop_back();
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
match maybe_address {
|
||||||
|
Some(address) => stack.push(U256::from(address)),
|
||||||
|
None => stack.push(U256::zero())
|
||||||
|
}
|
||||||
|
Ok(U256::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))
|
||||||
|
},
|
||||||
|
instructions::STOP => {
|
||||||
|
code.stop_execution();
|
||||||
|
Ok(gas)
|
||||||
|
},
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
instructions::LOG0...instructions::LOG4 => {
|
||||||
|
let no_of_topics = instructions::get_log_topics(instruction);
|
||||||
|
let topics_data = stack.pop_n(no_of_topics + 2);
|
||||||
|
|
||||||
|
let offset = topics_data[0];
|
||||||
|
let size = topics_data[1];
|
||||||
|
let topics = topics_data
|
||||||
|
.iter()
|
||||||
|
.skip(2)
|
||||||
|
.map(H256::from)
|
||||||
|
.collect();
|
||||||
|
ext.log(topics, code.get_slice(offset, size));
|
||||||
|
Ok(gas)
|
||||||
|
},
|
||||||
|
instructions::PUSH1...instructions::PUSH32 => {
|
||||||
|
// Load to stack
|
||||||
|
let bytes = instructions::get_push_bytes(instruction);
|
||||||
|
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::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)
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
self.exec_stack_instruction(instruction, stack);
|
||||||
|
Ok(gas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_jump(&self, jump_u: U256, valid_jump_destinations: &HashSet<usize>) -> Result<usize, evm::Error> {
|
||||||
|
let jump = jump_u.low_u64() as usize;
|
||||||
|
|
||||||
|
if valid_jump_destinations.contains(&jump) {
|
||||||
|
Ok(jump)
|
||||||
|
} else {
|
||||||
|
Err(evm::Error::BadJumpDestination)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_zero(&self, val: U256) -> bool {
|
||||||
|
U256::zero() == val
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exec_stack_instruction(&self, instruction: Instruction, stack : &mut Stack<U256>) {
|
||||||
|
match instruction {
|
||||||
|
instructions::DUP1...instructions::DUP16 => {
|
||||||
|
let position = instructions::get_dup_position(instruction);
|
||||||
|
let val = stack.peek(position).clone();
|
||||||
|
stack.push(val);
|
||||||
|
},
|
||||||
|
instructions::SWAP1...instructions::SWAP16 => {
|
||||||
|
let position = instructions::get_swap_position(instruction);
|
||||||
|
stack.swap_with_top(position)
|
||||||
|
},
|
||||||
|
instructions::ADD => {
|
||||||
|
let a = stack.pop_back();
|
||||||
|
let b = stack.pop_back();
|
||||||
|
stack.push(a + b);
|
||||||
|
},
|
||||||
|
_ => panic!(format!("Unknown stack instruction: {:x}", instruction))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_jump_destinations(&self, code : &Bytes) -> HashSet<CodePosition> {
|
||||||
|
let mut jump_dests = HashSet::new();
|
||||||
|
let mut position = 0;
|
||||||
|
|
||||||
|
while position < code.len() {
|
||||||
|
let instruction = code[position];
|
||||||
|
|
||||||
|
if instruction == instructions::JUMPDEST {
|
||||||
|
jump_dests.insert(position);
|
||||||
|
} else if instructions::is_push(instruction) {
|
||||||
|
position += instructions::get_push_bytes(instruction);
|
||||||
|
}
|
||||||
|
position += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return jump_dests;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use common::*;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_jump_destinations() {
|
||||||
|
// given
|
||||||
|
let interpreter = Interpreter;
|
||||||
|
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055".from_hex().unwrap();
|
||||||
|
|
||||||
|
// when
|
||||||
|
let valid_jump_destinations = interpreter.find_jump_destinations(&code);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assert!(valid_jump_destinations.contains(&66));
|
||||||
|
}
|
||||||
|
}
|
@ -295,7 +295,7 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bytes_ref: &[u8] = slice::from_raw_parts(beg, size as usize);
|
let bytes_ref: &[u8] = slice::from_raw_parts(beg, size as usize);
|
||||||
self.ext.log(topics, bytes_ref.to_vec());
|
self.ext.log(topics, bytes_ref);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
pub mod ext;
|
pub mod ext;
|
||||||
pub mod evm;
|
pub mod evm;
|
||||||
|
pub mod interpreter;
|
||||||
pub mod factory;
|
pub mod factory;
|
||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
|
mod instructions;
|
||||||
#[cfg(feature = "jit" )]
|
#[cfg(feature = "jit" )]
|
||||||
mod jit;
|
mod jit;
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ impl FakeExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Ext for FakeExt {
|
impl Ext for FakeExt {
|
||||||
fn sload(&self, key: &H256) -> H256 {
|
fn sload(&self, key: &H256) -> H256 {
|
||||||
self.store.get(key).unwrap_or(&H256::new()).clone()
|
self.store.get(key).unwrap_or(&H256::new()).clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +61,10 @@ impl Ext for FakeExt {
|
|||||||
self.codes.get(address).unwrap_or(&Bytes::new()).clone()
|
self.codes.get(address).unwrap_or(&Bytes::new()).clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(&mut self, topics: Vec<H256>, data: Bytes) {
|
fn log(&mut self, topics: Vec<H256>, data: &[u8]) {
|
||||||
self.logs.push(FakeLogEntry {
|
self.logs.push(FakeLogEntry {
|
||||||
topics: topics,
|
topics: topics,
|
||||||
data: data
|
data: data.to_vec()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +86,17 @@ impl Ext for FakeExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add() {
|
#[cfg(feature = "jit")]
|
||||||
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
fn test_add_jit() {
|
||||||
|
test_add(Box::new(super::jit::JitEvm));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_add_interpreter() {
|
||||||
|
test_add(Box::new(super::interpreter::Interpreter));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_add(vm : Box<super::Evm>) {
|
||||||
|
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
||||||
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055".from_hex().unwrap();
|
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01600055".from_hex().unwrap();
|
||||||
|
|
||||||
let mut params = ActionParams::new();
|
let mut params = ActionParams::new();
|
||||||
@ -97,7 +106,6 @@ fn test_add() {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = Factory::create();
|
|
||||||
vm.exec(¶ms, &mut ext).unwrap()
|
vm.exec(¶ms, &mut ext).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -206,7 +206,8 @@ impl<'a> Executive<'a> {
|
|||||||
fn finalize(&mut self, t: &Transaction, substate: Substate, backup: State, result: evm::Result) -> ExecutionResult {
|
fn finalize(&mut self, t: &Transaction, substate: Substate, backup: State, result: evm::Result) -> ExecutionResult {
|
||||||
match result {
|
match result {
|
||||||
Err(evm::Error::Internal) => Err(ExecutionError::Internal),
|
Err(evm::Error::Internal) => Err(ExecutionError::Internal),
|
||||||
Err(evm::Error::OutOfGas) => {
|
// TODO [ToDr] BadJumpDestination @debris - how to handle that?
|
||||||
|
Err(evm::Error::OutOfGas) | Err(evm::Error::BadJumpDestination) => {
|
||||||
*self.state = backup;
|
*self.state = backup;
|
||||||
Ok(Executed {
|
Ok(Executed {
|
||||||
gas: t.gas,
|
gas: t.gas,
|
||||||
@ -426,9 +427,9 @@ impl<'a> Ext for Externalities<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(&mut self, topics: Vec<H256>, data: Bytes) {
|
fn log(&mut self, topics: Vec<H256>, data: &[u8]) {
|
||||||
let address = self.params.address.clone();
|
let address = self.params.address.clone();
|
||||||
self.substate.logs.push(LogEntry::new(address, topics, data));
|
self.substate.logs.push(LogEntry::new(address, topics, data.to_vec()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn suicide(&mut self) {
|
fn suicide(&mut self) {
|
||||||
|
Loading…
Reference in New Issue
Block a user