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 16, 2018
1 parent 91dbe23 commit 7ac9942
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,36 @@ fn unsafe_alloc_buffer(len: usize) -> 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],
}

type Address = Uint160;
type Hash = Uint256;
type Value = Uint128;

trait EwasmType {
fn new() -> Self;
}

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 {
Expand Down Expand Up @@ -115,20 +125,20 @@ pub fn gas_left() -> u64 {
}
}

pub fn current_address() -> [u8;20] {
let mut ret = [0u8;20];
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);
}
ret
}

pub fn external_balance(address: &[u8;20]) -> [u8;16] {
let mut ret = [0u8;16];
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);
}
ret
}
Expand Down

0 comments on commit 7ac9942

Please sign in to comment.