Fix up wasm runtime.

This commit is contained in:
Gav
2018-01-16 18:56:57 +01:00
parent b62a8f9587
commit dc6ebf71cf
12 changed files with 45 additions and 28 deletions
+2 -2
View File
@@ -136,8 +136,8 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
} else { 0 } } else { 0 }
} else { 0 } } else { 0 }
}, },
ext_deposit_log(_log_data: *const u8, _log_len: u32) => { ext_chain_id() -> u64 => {
// TODO 42u64
} }
=> <'e, E: Externalities + 'e> => <'e, E: Externalities + 'e>
); );
@@ -68,6 +68,12 @@ pub fn set_storage(_key: &[u8], _value: &[u8]) {
); );
} }
/// The current relay chain identifier.
pub fn chain_id() -> u64 {
// TODO: fetch from Externalities.
42u64
}
/// Execute the given closure with global function available whose functionality routes into the /// Execute the given closure with global function available whose functionality routes into the
/// externalities `ext`. Forwards the value that the closure returns. /// externalities `ext`. Forwards the value that the closure returns.
pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Error=NoError>, f: F) -> R { pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Error=NoError>, f: F) -> R {
@@ -1,3 +1,4 @@
use runtime_support::Vec;
use keyedvec::KeyedVec; use keyedvec::KeyedVec;
use storage::Storage; use storage::Storage;
use primitives::{AccountID, SessionKey, BlockNumber}; use primitives::{AccountID, SessionKey, BlockNumber};
+8 -9
View File
@@ -6,7 +6,9 @@ extern crate runtime_support;
mod support; mod support;
pub use support::{endiansensitive, streamreader, joiner, slicable, primitives, keyedvec, function, pub use support::{endiansensitive, streamreader, joiner, slicable, primitives, keyedvec, function,
environment, storage, testing}; environment, storage};
#[cfg(test)]
pub use support::testing;
#[allow(unused)] #[allow(unused)]
mod system; mod system;
@@ -19,9 +21,12 @@ mod timestamp;
use runtime_support::Vec; use runtime_support::Vec;
use slicable::Slicable; use slicable::Slicable;
use primitives::{ChainID, Block, Transaction}; use primitives::{Block, Transaction};
// TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme) // TODO: add externals for:
// - keccak256 (or some better hashing scheme)
// - trie rooting
// - ECDSA-recover (or some better sig scheme)
pub fn execute_block(input: Vec<u8>) -> Vec<u8> { pub fn execute_block(input: Vec<u8>) -> Vec<u8> {
system::execute_block(Block::from_slice(&input).unwrap()) system::execute_block(Block::from_slice(&input).unwrap())
@@ -32,9 +37,3 @@ pub fn execute_transaction(input: Vec<u8>) -> Vec<u8> {
} }
impl_stubs!(execute_block, execute_transaction); impl_stubs!(execute_block, execute_transaction);
/// The current relay chain identifier.
pub fn chain_id() -> ChainID {
// TODO: retrieve from external
unimplemented!()
}
@@ -1,3 +1,4 @@
use runtime_support::Vec;
use slicable::Slicable; use slicable::Slicable;
pub trait Joiner { pub trait Joiner {
@@ -1,3 +1,4 @@
use runtime_support::Vec;
use primitives::AccountID; use primitives::AccountID;
use slicable::Slicable; use slicable::Slicable;
@@ -7,4 +7,6 @@ pub mod keyedvec;
pub mod function; pub mod function;
pub mod environment; pub mod environment;
pub mod storage; pub mod storage;
#[cfg(test)]
pub mod testing; pub mod testing;
@@ -1,3 +1,4 @@
use runtime_support::Vec;
use streamreader::StreamReader; use streamreader::StreamReader;
use joiner::Joiner; use joiner::Joiner;
use slicable::{Slicable, NonTrivialSlicable}; use slicable::{Slicable, NonTrivialSlicable};
@@ -62,7 +63,7 @@ impl Slicable for Transaction {
} }
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
vec![] Vec::new()
.join(&self.signed) .join(&self.signed)
.join(&(self.function as u8)) .join(&(self.function as u8))
.join(&self.nonce) .join(&self.nonce)
@@ -84,7 +85,7 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
fn from_slice(value: &[u8]) -> Option<Self> { fn from_slice(value: &[u8]) -> Option<Self> {
let len = Self::size_of(&value[0..4])?; let len = Self::size_of(&value[0..4])?;
let mut off = 4; let mut off = 4;
let mut r = vec![]; let mut r = Vec::new();
while off < len { while off < len {
let element_len = T::size_of(&value[off..])?; let element_len = T::size_of(&value[off..])?;
r.push(T::from_slice(&value[off..off + element_len])?); r.push(T::from_slice(&value[off..off + element_len])?);
@@ -100,7 +101,7 @@ impl<T: NonTrivialSlicable> Slicable for Vec<T> {
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
let vecs = self.iter().map(Slicable::to_vec).collect::<Vec<_>>(); let vecs = self.iter().map(Slicable::to_vec).collect::<Vec<_>>();
let len = vecs.iter().fold(0, |mut a, v| {a += v.len(); a}); let len = vecs.iter().fold(0, |mut a, v| {a += v.len(); a});
let mut r = vec![].join(&(len as u32)); let mut r = Vec::new().join(&(len as u32));
vecs.iter().for_each(|v| r.extend_from_slice(v)); vecs.iter().for_each(|v| r.extend_from_slice(v));
r r
} }
@@ -127,7 +128,7 @@ impl Slicable for Header {
} }
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
vec![] Vec::new()
.join(&self.parent_hash) .join(&self.parent_hash)
.join(&self.number) .join(&self.number)
.join(&self.state_root) .join(&self.state_root)
@@ -156,7 +157,7 @@ impl Slicable for Block {
} }
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
vec![] Vec::new()
.join(&self.header) .join(&self.header)
.join(&self.transactions) .join(&self.transactions)
} }
@@ -181,7 +182,7 @@ mod tests {
let tx = Transaction { let tx = Transaction {
signed: one.clone(), signed: one.clone(),
function: Function::StakingTransferStake, function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64), input_data: Vec::new().join(&two).join(&69u64),
nonce: 69, nonce: 69,
}; };
let serialised = tx.to_vec(); let serialised = tx.to_vec();
@@ -202,7 +203,7 @@ mod tests {
let tx = Transaction { let tx = Transaction {
signed: one.clone(), signed: one.clone(),
function: Function::StakingTransferStake, function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64), input_data: Vec::new().join(&two).join(&69u64),
nonce: 69, nonce: 69,
}; };
let data = [ let data = [
@@ -271,13 +272,13 @@ mod tests {
let tx1 = Transaction { let tx1 = Transaction {
signed: one.clone(), signed: one.clone(),
function: Function::StakingTransferStake, function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64), input_data: Vec::new().join(&two).join(&69u64),
nonce: 69, nonce: 69,
}; };
let tx2 = Transaction { let tx2 = Transaction {
signed: two.clone(), signed: two.clone(),
function: Function::StakingStake, function: Function::StakingStake,
input_data: vec![], input_data: Vec::new(),
nonce: 42, nonce: 42,
}; };
let h = Header { let h = Header {
@@ -327,13 +328,13 @@ mod tests {
let tx1 = Transaction { let tx1 = Transaction {
signed: one.clone(), signed: one.clone(),
function: Function::StakingTransferStake, function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64), input_data: Vec::new().join(&two).join(&69u64),
nonce: 69, nonce: 69,
}; };
let tx2 = Transaction { let tx2 = Transaction {
signed: two.clone(), signed: two.clone(),
function: Function::StakingStake, function: Function::StakingStake,
input_data: vec![], input_data: Vec::new(),
nonce: 42, nonce: 42,
}; };
let h = Header { let h = Header {
@@ -59,7 +59,7 @@ impl Slicable for Vec<u8> {
unimplemented!(); unimplemented!();
} }
fn to_vec(&self) -> Vec<u8> { fn to_vec(&self) -> Vec<u8> {
let mut r: Vec<u8> = vec![].join(&(self.len() as u32)); let mut r: Vec<u8> = Vec::new().join(&(self.len() as u32));
r.extend_from_slice(&self); r.extend_from_slice(&self);
r r
} }
+11 -5
View File
@@ -10,6 +10,7 @@ pub use alloc::vec::Vec;
pub use alloc::boxed::Box; pub use alloc::boxed::Box;
pub use alloc::rc::Rc; pub use alloc::rc::Rc;
pub use core::mem::{transmute, size_of, uninitialized, swap}; pub use core::mem::{transmute, size_of, uninitialized, swap};
pub use core::slice;
pub use core::cell::{RefCell, Ref, RefMut}; pub use core::cell::{RefCell, Ref, RefMut};
extern crate pwasm_libc; extern crate pwasm_libc;
@@ -27,7 +28,7 @@ extern "C" {
fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32); fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32);
fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8; fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8;
fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32) -> u32; fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32) -> u32;
fn ext_deposit_log(log_data: *const u8, log_len: u32); fn ext_chain_id() -> u64;
} }
pub fn storage(key: &[u8]) -> Vec<u8> { pub fn storage(key: &[u8]) -> Vec<u8> {
@@ -64,11 +65,16 @@ pub fn set_storage(key: &[u8], value: &[u8]) {
} }
} }
pub fn deposit_log(log: &[u8]) { pub fn read_storage(key: &[u8], value_out: &mut [u8]) -> usize {
unsafe { unsafe {
ext_deposit_log( ext_get_storage_into(&key[0], key.len() as u32, &mut value_out[0], value_out.len() as u32) as usize
&log[0] as *const u8, log.len() as u32, }
) }
/// The current relay chain identifier.
pub fn chain_id() -> u64 {
unsafe {
ext_chain_id()
} }
} }