Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4f5c5b6
Introduce first groundwork for Wasm executor.
gavofyork Dec 5, 2017
6237848
Remove old Rust-runtime code.
gavofyork Dec 6, 2017
293cf5d
Avoid commiting compled files.
gavofyork Dec 6, 2017
66b636e
Add runtime precompile.
gavofyork Dec 6, 2017
c7e7456
Rename so module makes more sense.
gavofyork Dec 6, 2017
0cfd67e
Further renaming.
gavofyork Dec 6, 2017
3011edf
Ensure tests work.
gavofyork Dec 6, 2017
a74b4fa
Allow bringing in of externalities.
gavofyork Dec 7, 2017
6dac275
Nice macros for imports.
gavofyork Dec 9, 2017
08f7b26
Allow passing in of data through allocators.
gavofyork Dec 11, 2017
1ace6b2
Can now pass in bytes to WasmExecutor.
gavofyork Dec 11, 2017
27e5fed
Additional cleanup.
gavofyork Dec 11, 2017
13dcd89
Switch usages of `OutData` to `u64`
gavofyork Dec 11, 2017
84b9f84
convert to safe but extremely verbose type conversion.
gavofyork Dec 11, 2017
7f31899
Remove StaticExternalities distinction.
gavofyork Dec 11, 2017
edf061e
Remove another unused use.
gavofyork Dec 11, 2017
edb6bea
Refactor wasm utils out
gavofyork Dec 11, 2017
9f59f48
Remove extraneous copies that weren't really testing anything.
gavofyork Dec 11, 2017
287b29d
Try to use wasm 0.15
gavofyork Dec 31, 2017
1bd55fe
Make it work!
gavofyork Dec 31, 2017
36e254a
Call-time externalities working.
gavofyork Jan 1, 2018
cd651a3
Add basic externalities.
gavofyork Jan 1, 2018
a8f9cca
Merge branch 'with-wasm-0.15' into with-wasm
gavofyork Jan 1, 2018
4404846
Fix grumbles and note unwraps to be sorted.
gavofyork Jan 1, 2018
b1d963a
Test storage externality.
gavofyork Jan 3, 2018
319d9c0
Fix nits.
gavofyork Jan 3, 2018
7ec9221
Merge branch 'master' into with-wasm
gavofyork Jan 3, 2018
2934d94
Compile collation logic.
gavofyork Jan 3, 2018
5998aa1
Move back to refs. Yey.
gavofyork Jan 3, 2018
3f4085a
Remove "object" id for storage access.
gavofyork Jan 4, 2018
4be0537
Fix test.
gavofyork Jan 4, 2018
01d7019
Fix up rest of tests.
gavofyork Jan 4, 2018
db1adee
remove unwrap.
gavofyork Jan 4, 2018
87c54f7
Expose set/get code in externalities
gavofyork Jan 5, 2018
471ea1e
Add validator set.
gavofyork Jan 5, 2018
fa35993
Introduce validator set into externalities and test.
gavofyork Jan 5, 2018
a0f64df
Add another external function.
gavofyork Jan 6, 2018
e736d46
Remove code and validators; use storage for everything.
gavofyork Jan 6, 2018
234297c
Introduce validators function.
gavofyork Jan 6, 2018
3f8a96d
Tests (and a fix) for the validators getter.
gavofyork Jan 6, 2018
6636520
Allow calls into runtime to return data.
gavofyork Jan 7, 2018
964659e
Remove unneeded trace.
gavofyork Jan 7, 2018
8ca1b7b
Make runtime printing a bit nicer.
gavofyork Jan 7, 2018
74156a2
Create separate runtimes for testing and polkadot.
gavofyork Jan 8, 2018
611a7ac
Remove commented code.
gavofyork Jan 8, 2018
c3afecc
Use new path.
gavofyork Jan 8, 2018
ea4d6c5
Refactor into shared support module.
gavofyork Jan 8, 2018
709693d
Fix warning.
gavofyork Jan 8, 2018
ec1e6b6
Remove unwraps.
gavofyork Jan 8, 2018
5c0ec3d
Make macro a little less unhygenic.
gavofyork Jan 8, 2018
79ab46f
Add wasm files.
gavofyork Jan 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add validator set.
  • Loading branch information
gavofyork committed Jan 5, 2018
commit 471ea1e9d0f501fa905eeaf8bca3f19073a203cc
6 changes: 6 additions & 0 deletions executor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ error_chain! {
display("Externalities error: {}", e),
}

/// Invalid index.
InvalidIndex {
description("index given was not in range"),
display("Invalid index provided"),
}

/// Invalid return type.
InvalidReturn {
description("u64 was not returned"),
Expand Down
56 changes: 49 additions & 7 deletions executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,33 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
} else { (0, 0) };
this.memory.write_primitive(written_out, written);
offset as u32
},
get_validator_count() -> i32 => {
this.ext.validator_count() as i32
},
get_allocated_validator(index: i32, written_out: *mut i32) -> *mut u8 => {
let (offset, written) = if let Ok(v) = this.ext.validator(index as usize) {
let offset = this.heap.allocate(v.len() as u32) as u32;
let _ = this.memory.set(offset, &v);
(offset, v.len() as u32)
} else { (0, 0) };
this.memory.write_primitive(written_out, written);
offset as u32
},
set_validator_count(validator_count: i32) => {
this.ext.set_validator_count(validator_count as usize);
},
set_validator(index: i32, validator_data: *const u8, validator_len: i32) => {
if let Ok(validator) = this.memory.get(validator_data, validator_len as usize) {
this.ext.set_validator(index as usize, validator);
}
}
=> <'e, E: Externalities + 'e>
);

/// Dummy rust executor for contracts.
/// Wasm rust executor for contracts.
///
/// Instead of actually executing the provided code it just
/// dispatches the calls to pre-defined hardcoded implementations in rust.
/// Executes the provided code in a sandboxed wasm runtime.
#[derive(Debug, Default)]
pub struct WasmExecutor;

Expand Down Expand Up @@ -183,6 +202,7 @@ mod tests {
struct TestExternalities {
data: HashMap<Vec<u8>, Vec<u8>>,
code: Vec<u8>,
validators: Vec<Vec<u8>>,
}
impl Externalities for TestExternalities {
type Error = Error;
Expand All @@ -195,12 +215,34 @@ mod tests {
Ok(self.data.get(&key.to_vec()).map_or(&[] as &[u8], Vec::as_slice))
}

fn set_code(&mut self, _code: Vec<u8>) {
self.code = _code;
fn validator(&self, index: usize) -> Result<&[u8]> {
if index < self.validators.len() {
Ok(self.validators[index].as_slice())
} else {
Err(ErrorKind::InvalidIndex.into())
}
}

fn validator_count(&self) -> usize {
self.validators.len()
}

fn set_code(&mut self, code: Vec<u8>) {
self.code = code;
}

fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.data.insert(key, value);
}

fn set_validator(&mut self, index: usize, value: Vec<u8>) {
if index < self.validators.len() {
self.validators[index] = value;
}
}

fn set_storage(&mut self, _key: Vec<u8>, _value: Vec<u8>) {
self.data.insert(_key, _value);
fn set_validator_count(&mut self, count: usize) {
self.validators.resize(count, vec![]);
}
}

Expand Down
19 changes: 16 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ extern "C" {
fn get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8;
fn set_code(code_data: *const u8, code_len: i32);
fn get_allocated_code(written_out: *mut i32) -> *mut u8;
fn get_validator_count() -> i32;
fn get_allocated_validator(index: i32, written_out: *mut i32) -> *mut u8;
fn set_validator_count(validator_count: i32);
fn set_validator(index: i32, validator_data: *const u8, validator_len: i32);
}

mod state {
Expand All @@ -39,7 +43,12 @@ mod state {
}

pub fn set_storage(key: &[u8], value: &[u8]) {
unsafe { super_set_storage(&key[0] as *const u8, key.len() as i32, &value[0] as *const u8, value.len() as i32); }
unsafe {
super_set_storage(
&key[0] as *const u8, key.len() as i32,
&value[0] as *const u8, value.len() as i32
);
}
}

pub fn code() -> Vec<u8> {
Expand All @@ -51,7 +60,9 @@ mod state {
}

pub fn set_code(new: &[u8]) {
unsafe { super_set_code(&new[0] as *const u8, new.len() as i32); }
unsafe {
super_set_code(&new[0] as *const u8, new.len() as i32);
}
}
}

Expand All @@ -69,7 +80,9 @@ pub fn test(value: u64) -> u64 {
/// Test passing of data.
#[no_mangle]
pub fn test_data_in(input_data: *mut u8, input_len: usize) {
let input = unsafe { Vec::from_raw_parts(input_data, input_len, input_len) };
let input = unsafe {
Vec::from_raw_parts(input_data, input_len, input_len)
};

state::set_storage(b"input", &input);
state::set_storage(b"code", &state::code());
Expand Down
16 changes: 14 additions & 2 deletions state_machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,23 @@ pub trait Externalities {
/// Read storage of current contract being called.
fn storage(&self, key: &[u8]) -> Result<&[u8], Self::Error>;

/// Set the new runtime.
/// Read the current validator set.
fn validator(&self, _index: usize) -> Result<&[u8], Self::Error> { Ok(&[]) }

/// How many validators are there?
fn validator_count(&self) -> usize { 0 }

/// Set the new runtime (effective from the next block).
fn set_code(&mut self, code: Vec<u8>);

/// Set storage of current contract being called.
/// Set storage of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>);

/// Set a new validator set (effective from the next block).
fn set_validator(&mut self, _index: usize, _value: Vec<u8>) {}

/// Resize the validators array - extra validators will be empty vectors.
fn set_validator_count(&mut self, _count: usize) {}
}

/// Code execution engine.
Expand Down