Skip to content

Commit

Permalink
refactor two EEI methods to use high-level ewasm types
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelang committed Aug 11, 2018
1 parent 0b022ae commit 84c014e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ fn alloc_hash() -> Vec<u8> {

#[derive(Copy, Clone)]
pub struct Uint160 {
bytes: [u8; 20],
pub bytes: [u8; 20],
}

#[derive(Copy, Clone)]
pub struct Uint256 {
bytes: [u8; 32],
pub bytes: [u8; 32],
}

#[derive(Copy, Clone)]
pub struct Uint128 {
bytes: [u8; 16],
pub bytes: [u8; 16],
}

trait EwasmType {
Expand All @@ -79,6 +79,16 @@ type Address = Uint160;
type Hash = Uint256;
type Value = Uint128;

trait RawPtr {
fn as_raw_ptr(&self) -> *const Self;
}

impl<T> RawPtr for T {
fn as_raw_ptr(&self) -> *const T {
self as *const T
}
}

impl EwasmType for Address {
fn new() -> Self {
Address {
Expand Down Expand Up @@ -127,23 +137,21 @@ pub fn gas_left() -> u64 {
}
}

pub fn current_address() -> Vec<u8> {
let mut ret: Vec<u8> = alloc_address();
pub fn current_address() -> Address {
let mut ret = Address::new();

unsafe {
ethereum_getAddress(ret.as_mut_ptr() as *const u32);
ethereum_getAddress(ret.as_raw_ptr() as *const u32);
}

return ret;
}

pub fn external_balance(address: &Vec<u8>) -> Vec<u8> {
assert!(address.len() == 20);

let mut ret: Vec<u8> = alloc_value();
pub fn external_balance(address: Address) -> Value {
let mut ret = Value::new();

unsafe {
ethereum_getBalance(address.as_ptr() as *const u32, ret.as_mut_ptr() as *const u32);
ethereum_getBalance(address.as_raw_ptr() as *const u32, ret.as_raw_ptr() as *const u32);
}

return ret;
Expand Down

0 comments on commit 84c014e

Please sign in to comment.