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

Commit

Permalink
tests refactoring to satisfy openethereum/pwasm-std#10
Browse files Browse the repository at this point in the history
  • Loading branch information
lexfrl committed Sep 7, 2017
1 parent 5fd2756 commit fac2fd4
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["NikVolf <nikvolf@gmail.com>"]

[dependencies]
pwasm-std = { git = "https://github.com/nikvolf/pwasm-std" }
pwasm-std = { git = "https://github.com/nikvolf/pwasm-std", branch = "store_hash" }

[[bin]]
name = "call_code"
Expand Down Expand Up @@ -59,4 +59,4 @@ name = "storage_read"
path = "src/storage_read.rs"

[profile.release]
panic = "abort"
panic = "abort"
Binary file modified compiled/call_code.wasm
Binary file not shown.
Binary file modified compiled/call_static.wasm
Binary file not shown.
Binary file modified compiled/creator.wasm
Binary file not shown.
Binary file modified compiled/dispersion.wasm
Binary file not shown.
Binary file modified compiled/empty.wasm
Binary file not shown.
Binary file modified compiled/externs.wasm
Binary file not shown.
Binary file modified compiled/identity.wasm
Binary file not shown.
Binary file modified compiled/logger.wasm
Binary file not shown.
Binary file modified compiled/math.wasm
Binary file not shown.
Binary file modified compiled/realloc.wasm
Binary file not shown.
Binary file modified compiled/rterr.wasm
Binary file not shown.
Binary file modified compiled/storage_read.wasm
Binary file not shown.
Binary file modified compiled/suicidal.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions src/call_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#[macro_use] extern crate pwasm_std;

use pwasm_std::{CallArgs, ext, write_u32, logger};
use pwasm_std::hash::Address;
use core::hash::{SipHasher, Hasher};

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

let addr = [13u8, 19, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let addr = Address::from([13u8, 19, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

let input = [1u8, 2, 3, 5, 7, 11];
let mut result = vec![0u8; 256];
Expand All @@ -35,4 +36,4 @@ pub fn call(desc: *mut u8) {

logger::debug("Exiting...");
unsafe { ctx.save(desc); }
}
}
5 changes: 3 additions & 2 deletions src/call_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use core::hash::{Hasher, SipHasher};
use pwasm_std::{CallArgs, ext, write_u32, logger};
use pwasm_std::hash::Address;

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

let addr = [19u8, 7, 123, 251, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let addr = Address::from([19u8, 7, 123, 251, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

let input = [1u8, 2, 3, 5, 7, 11];
let mut result = vec![0u8; 256];
Expand All @@ -35,4 +36,4 @@ pub fn call(desc: *mut u8) {

logger::debug("Exiting...");
unsafe { ctx.save(desc); }
}
}
4 changes: 2 additions & 2 deletions src/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use pwasm_std::{CallArgs, ext, logger};
pub fn call(desc: *mut u8) {
let mut ctx = unsafe { CallArgs::from_raw(desc) };

if let Ok(addr) = ext::create(&ctx.params().value(), ctx.params().args()) {
if let Ok(addr) = ext::create(ctx.params().value(), ctx.params().args()) {
logger::debug("Created contractwith code");
*ctx.result_mut() = addr.to_vec().into_boxed_slice();
} else {
logger::debug("Error creating contract");
}

unsafe { ctx.save(desc); }
}
}
13 changes: 10 additions & 3 deletions src/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
extern crate pwasm_std;

use pwasm_std::{ext, Vec, CallArgs, write_u64};
use pwasm_std::bigint::U256;

fn push_u64(buf: &mut Vec<u8>, val: u64) {
let mut slc = [0u8; 8];
write_u64(&mut slc, val);
buf.extend(&slc[..]);
}

fn push_u256(buf: &mut Vec<u8>, val: U256) {
let mut slc = [0u8; 32];
val.to_big_endian(&mut slc);
buf.extend(&slc[..]);
}

#[no_mangle]
pub fn call(desc: *mut u8) {
let mut ctx = unsafe { CallArgs::from_raw(desc) };
Expand All @@ -22,10 +29,10 @@ pub fn call(desc: *mut u8) {
output.extend(&ext::coinbase()[..]);
push_u64(&mut output, ext::timestamp());
push_u64(&mut output, ext::block_number());
output.extend(&ext::difficulty()[..]);
output.extend(&ext::gas_limit()[..]);
push_u256(&mut output, ext::difficulty());
push_u256(&mut output, ext::gas_limit());

*ctx.result_mut() = output.into_boxed_slice();

unsafe { ctx.save(desc); }
}
}
14 changes: 8 additions & 6 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@
extern crate pwasm_std;

use pwasm_std::{CallArgs, storage, write_u32};
use pwasm_std::bigint::U256;
use pwasm_std::hash::H256;

fn set_key_from_addr(key: u32, val: &[u8; 20]) {
fn set_key_from_addr(key: u32, val: &[u8]) {
let mut full_key = [0u8; 32];
let mut full_val = [0u8; 32];

write_u32(&mut full_key[0..4], key);
full_val[12..32].copy_from_slice(val);

let _ = storage::write(&full_key, &full_val);
let _ = storage::write(&H256::from(full_key), &full_val);
}

fn set_key_from_u256(key: u32, val: &[u8; 32]) {
fn set_key_from_u256(key: u32, val: U256) {
let mut full_key = [0u8; 32];
write_u32(&mut full_key[0..4], key);

let _ = storage::write(&full_key, val);
let _ = storage::write(&H256::from(full_key), &val.into());
}

#[no_mangle]
Expand All @@ -30,8 +32,8 @@ pub fn call(descriptor: *mut u8) {
set_key_from_addr(1, &ctx.params().address());
set_key_from_addr(2, &ctx.params().sender());
set_key_from_addr(3, &ctx.params().origin());
set_key_from_u256(4, &ctx.params().value());
set_key_from_u256(4, ctx.params().value());

// Saves the wrapper state to commit return stream
unsafe { ctx.save(descriptor); }
}
}
14 changes: 8 additions & 6 deletions src/storage_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
extern crate pwasm_std;

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

fn get_value_from_key(key: u32, val: &mut [u8; 32]) {
let mut val = val;
fn get_value_from_key(key: u32) -> Result<[u8; 32], storage::Error> {
let mut full_key = [0u8; 32];
write_u32(&mut full_key[0..4], key);
let _ = storage::read(&full_key, &mut val);
storage::read(&H256::from(full_key))
}

#[no_mangle]
pub fn call(descriptor: *mut u8) {
let mut ctx = unsafe { CallArgs::from_raw(descriptor) };
let mut val = [0u8; 32];
get_value_from_key(1, &mut val);
let val: [u8; 32] = match get_value_from_key(1) {
Ok(v) => v,
Err(_) => [0u8; 32]
};

*ctx.result_mut() = val.to_vec().into_boxed_slice();

unsafe { ctx.save(descriptor); }
}
}
4 changes: 2 additions & 2 deletions src/suicidal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub fn call(desc: *mut u8) {
if ctx.params().args().len() > 0 && ctx.params().args()[0] == 127 {
let mut addr = [0u8; 20];
addr.copy_from_slice(&ctx.params().args()[1..]);
ext::suicide(&addr);
ext::suicide(&addr.into());
} else {
*ctx.result_mut() = ctx.params().args().to_vec().into_boxed_slice();
unsafe { ctx.save(desc); }
}
}
}

0 comments on commit fac2fd4

Please sign in to comment.