Skip to content

Commit

Permalink
Merge pull request zcash-hackworks#14 from sorpaas/master
Browse files Browse the repository at this point in the history
0x50 to 0x5f, 0x80 to 0x9f opcode and a simple Account implementation
  • Loading branch information
sjmackenzie authored Apr 10, 2017
2 parents 3f4e881 + ac6ba0c commit b75c773
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 111 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ path = "./src/bin/gaslighter/mod.rs"
clap = "2.22"
log = "0.3"
capnp = "0.8"
ring = "^0.6.0"
merkle = "1.1.0"
23 changes: 23 additions & 0 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use utils::u256::U256;
use ring::digest::{Algorithm, Context, SHA256};
use merkle::{MerkleTree};

pub struct Account {
nonce: usize,
balance: U256,
pub storageRoot: MerkleTree<U256>,
codeHash: U256,
}

static digest: &'static Algorithm = &SHA256;

impl Default for Account {
fn default() -> Account {
Account {
nonce: 0,
balance: 0.into(),
storageRoot: MerkleTree::from_vec(digest, Vec::new()),
codeHash: 0.into()
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[macro_use]
extern crate log;
extern crate ring;
extern crate merkle;

pub mod vm;
pub mod account;
mod utils;
10 changes: 9 additions & 1 deletion src/utils/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// #![no_std]

use ::std::convert::{From, Into};
use ::std::convert::{From, Into, AsRef};
use ::std::ops::{Add, Sub, Not, Mul, Div, Shr, Shl, BitAnd, BitOr, BitXor};
use ::std::cmp::Ordering;

Expand Down Expand Up @@ -112,6 +112,14 @@ impl U256 {
}
}

impl AsRef<[u8]> for U256 {
fn as_ref(&self) -> &[u8] {
use std::mem::transmute;
let r: &[u8; 32] = unsafe { transmute(&self.0) };
r
}
}

impl From<u64> for U256 {
fn from(val: u64) -> U256 {
U256([val, 0, 0, 0])
Expand Down
8 changes: 8 additions & 0 deletions src/vm/gas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Machine, Memory, Stack};
use std::ops::{Add, AddAssign, Sub, SubAssign};
use utils::u256::U256;

#[derive(Clone, Copy, Debug)]
pub struct Gas(isize);
Expand All @@ -13,6 +14,13 @@ impl From<isize> for Gas {
fn from(val: isize) -> Gas { Gas(val) }
}

impl Into<U256> for Gas {
fn into(self) -> U256 {
assert!(self.is_valid());
(self.0 as u64).into()
}
}

impl Add for Gas {
type Output = Gas;

Expand Down
3 changes: 3 additions & 0 deletions src/vm/machine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Memory, VectorMemory, Stack, VectorStack, PC, Result, Gas};
use ::account::Account;

pub type VectorMachine<'p> = Machine<'p, VectorMemory, VectorStack>;

Expand All @@ -7,6 +8,7 @@ pub struct Machine<'p, M, S> {
pub pc: PC<'p>,
pub memory: M, // Contains i
pub stack: S,
pub account: Account,
}

impl<'p, M, S> Machine<'p, M, S> where M: Memory, S: Stack {
Expand All @@ -16,6 +18,7 @@ impl<'p, M, S> Machine<'p, M, S> where M: Memory, S: Stack {
pc: PC::new(code),
memory: M::new(),
stack: S::new(),
account: Account::default(),
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/vm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use utils::u256::U256;
pub trait Memory {
fn new() -> Self;
fn write(&mut self, index: U256, value: U256);
fn read(&self, index: U256) -> U256;
fn read(&mut self, index: U256) -> U256;
fn active_len(&self) -> usize;
}

Expand Down Expand Up @@ -31,10 +31,16 @@ impl Memory for VectorMemory {
self.memory[index] = value;
}

fn read(&self, index: U256) -> U256 {
fn read(&mut self, index: U256) -> U256 {
// This is required to be &mut self because a read might modify u_i

let index_u64: u64 = index.into();
let index = index_u64 as usize;

if self.memory.len() <= index {
self.memory.resize(index, 0.into());
}

match self.memory.get(index) {
Some(&v) => v,
None => 0.into()
Expand Down
Loading

0 comments on commit b75c773

Please sign in to comment.