Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from NikVolf/store_hash
Browse files Browse the repository at this point in the history
Fixed-size data types use
  • Loading branch information
NikVolf authored Sep 7, 2017
2 parents 916ebb6 + e38b886 commit 2096b72
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 661 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ authors = ["NikVolf <nikvolf@gmail.com>"]

[dependencies]
wasm-alloc = { path = "alloc" }
rustc-hex = "1.0.0"

[dependencies.bigint]
version = "4"
default-features = false

[dependencies.byteorder]
version = "1"
default-features = false
default-features = false

[dependencies.parity-hash]
version = "1"
default-features = false
9 changes: 5 additions & 4 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
extern crate pwasm_std;

use pwasm_std::{CallArgs, storage};
use pwasm_std::hash::H256;

#[no_mangle]
pub fn call(descriptor: *mut u8) {
let mut call_args = unsafe { CallArgs::from_raw(descriptor) };

let k = [0u8; 32];
let mut v = [0u8; 32];
storage::read(&k, &mut v);
let _ = storage::write(&H256::from([1u8; 32]), &[1u8; 32]).expect("OK");
let v = storage::read(&H256::from([1u8; 32])).expect("OK");


let mut vec = vec![0u8; 16384];
vec[32..64].copy_from_slice(&v);

*call_args.result_mut() = vec.into_boxed_slice();

unsafe { call_args.save(descriptor); }
}
}
38 changes: 23 additions & 15 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// use hash::H256;
use bigint::U256;
use hash::Address;

#[derive(Debug)]
pub struct Error;

Expand Down Expand Up @@ -66,31 +70,35 @@ mod external {
}
}

pub fn suicide(refund: &[u8; 20]) {
pub fn suicide(refund: &Address) {
unsafe { external::suicide(refund.as_ptr()); }
}

pub fn create(endowment: &[u8; 32], code: &[u8]) -> Result<[u8; 20], Error> {
let mut result = [0u8; 20];
pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> {
let mut endowment_arr = [0u8; 32];
endowment.to_big_endian(&mut endowment_arr);
let mut result = Address::new();
unsafe {
if external::create(endowment.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr()) == 0 {
if external::create(endowment_arr.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr()) == 0 {
Ok(result)
} else {
Err(Error)
}
}
}

pub fn call(address: &[u8; 20], value: &[u8; 32], input: &[u8], result: &mut [u8]) -> Result<(), Error> {
pub fn call(address: &Address, value: U256, input: &[u8], result: &mut [u8]) -> Result<(), Error> {
let mut value_arr = [0u8; 32];
value.to_big_endian(&mut value_arr);
unsafe {
match external::ccall(address.as_ptr(), value.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32) {
match external::ccall(address.as_ptr(), value_arr.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32) {
0 => Ok(()),
_ => Err(Error),
}
}
}

pub fn call_code(address: &[u8; 20], input: &[u8], result: &mut [u8]) -> Result<(), Error> {
pub fn call_code(address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> {
unsafe {
match external::dcall(address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32) {
0 => Ok(()),
Expand All @@ -99,7 +107,7 @@ pub fn call_code(address: &[u8; 20], input: &[u8], result: &mut [u8]) -> Result<
}
}

pub fn static_call(address: &[u8; 20], input: &[u8], result: &mut [u8]) -> Result<(), Error> {
pub fn static_call(address: &Address, input: &[u8], result: &mut [u8]) -> Result<(), Error> {
unsafe {
match external::scall(address.as_ptr(), input.as_ptr(), input.len() as u32, result.as_mut_ptr(), result.len() as u32) {
0 => Ok(()),
Expand All @@ -118,8 +126,8 @@ pub fn block_hash(block_number: u64) -> Result<[u8; 32], Error> {
}
}

pub fn coinbase() -> [u8; 20] {
let mut res = [0u8; 20];
pub fn coinbase() -> Address {
let mut res = Address::new();
unsafe { external::coinbase(res.as_mut_ptr()); }
res
}
Expand All @@ -132,14 +140,14 @@ pub fn block_number() -> u64 {
unsafe { external::blocknumber() as u64 }
}

pub fn difficulty() -> [u8; 32] {
pub fn difficulty() -> U256 {
let mut res = [0u8; 32];
unsafe { external::difficulty(res.as_mut_ptr()); }
res
U256::from_big_endian(&res)
}

pub fn gas_limit() -> [u8; 32] {
pub fn gas_limit() -> U256 {
let mut res = [0u8; 32];
unsafe { external::gaslimit(res.as_mut_ptr()); }
res
}
U256::from_big_endian(&res)
}
Loading

0 comments on commit 2096b72

Please sign in to comment.