diff --git a/substrate/executor/src/wasm_executor.rs b/substrate/executor/src/wasm_executor.rs index cb2df774d9..41029424fa 100644 --- a/substrate/executor/src/wasm_executor.rs +++ b/substrate/executor/src/wasm_executor.rs @@ -136,8 +136,8 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, } else { 0 } } else { 0 } }, - ext_deposit_log(_log_data: *const u8, _log_len: u32) => { - // TODO + ext_chain_id() -> u64 => { + 42u64 } => <'e, E: Externalities + 'e> ); diff --git a/substrate/native-runtime/support/src/lib.rs b/substrate/native-runtime/support/src/lib.rs index 795cae2ffd..9ec5f77bdd 100644 --- a/substrate/native-runtime/support/src/lib.rs +++ b/substrate/native-runtime/support/src/lib.rs @@ -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 /// externalities `ext`. Forwards the value that the closure returns. pub fn with_externalities R>(ext: &mut Externalities, f: F) -> R { diff --git a/substrate/wasm-runtime/polkadot/src/consensus.rs b/substrate/wasm-runtime/polkadot/src/consensus.rs index ef4d5a9856..add87a1808 100644 --- a/substrate/wasm-runtime/polkadot/src/consensus.rs +++ b/substrate/wasm-runtime/polkadot/src/consensus.rs @@ -1,3 +1,4 @@ +use runtime_support::Vec; use keyedvec::KeyedVec; use storage::Storage; use primitives::{AccountID, SessionKey, BlockNumber}; diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs index cc48d8caa6..680116b3d9 100644 --- a/substrate/wasm-runtime/polkadot/src/lib.rs +++ b/substrate/wasm-runtime/polkadot/src/lib.rs @@ -6,7 +6,9 @@ extern crate runtime_support; mod support; pub use support::{endiansensitive, streamreader, joiner, slicable, primitives, keyedvec, function, - environment, storage, testing}; + environment, storage}; +#[cfg(test)] +pub use support::testing; #[allow(unused)] mod system; @@ -19,9 +21,12 @@ mod timestamp; use runtime_support::Vec; 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) -> Vec { system::execute_block(Block::from_slice(&input).unwrap()) @@ -32,9 +37,3 @@ pub fn execute_transaction(input: Vec) -> Vec { } impl_stubs!(execute_block, execute_transaction); - -/// The current relay chain identifier. -pub fn chain_id() -> ChainID { - // TODO: retrieve from external - unimplemented!() -} diff --git a/substrate/wasm-runtime/polkadot/src/support/joiner.rs b/substrate/wasm-runtime/polkadot/src/support/joiner.rs index 99b38dec57..817df8e4cb 100644 --- a/substrate/wasm-runtime/polkadot/src/support/joiner.rs +++ b/substrate/wasm-runtime/polkadot/src/support/joiner.rs @@ -1,3 +1,4 @@ +use runtime_support::Vec; use slicable::Slicable; pub trait Joiner { diff --git a/substrate/wasm-runtime/polkadot/src/support/keyedvec.rs b/substrate/wasm-runtime/polkadot/src/support/keyedvec.rs index 3b0e595628..fcc410e582 100644 --- a/substrate/wasm-runtime/polkadot/src/support/keyedvec.rs +++ b/substrate/wasm-runtime/polkadot/src/support/keyedvec.rs @@ -1,3 +1,4 @@ +use runtime_support::Vec; use primitives::AccountID; use slicable::Slicable; diff --git a/substrate/wasm-runtime/polkadot/src/support/mod.rs b/substrate/wasm-runtime/polkadot/src/support/mod.rs index dd381b1ad2..47b29194dc 100644 --- a/substrate/wasm-runtime/polkadot/src/support/mod.rs +++ b/substrate/wasm-runtime/polkadot/src/support/mod.rs @@ -7,4 +7,6 @@ pub mod keyedvec; pub mod function; pub mod environment; pub mod storage; + +#[cfg(test)] pub mod testing; diff --git a/substrate/wasm-runtime/polkadot/src/support/primitives.rs b/substrate/wasm-runtime/polkadot/src/support/primitives.rs index 44d3bc4739..f1bd31cc66 100644 --- a/substrate/wasm-runtime/polkadot/src/support/primitives.rs +++ b/substrate/wasm-runtime/polkadot/src/support/primitives.rs @@ -1,3 +1,4 @@ +use runtime_support::Vec; use streamreader::StreamReader; use joiner::Joiner; use slicable::{Slicable, NonTrivialSlicable}; @@ -62,7 +63,7 @@ impl Slicable for Transaction { } fn to_vec(&self) -> Vec { - vec![] + Vec::new() .join(&self.signed) .join(&(self.function as u8)) .join(&self.nonce) @@ -84,7 +85,7 @@ impl Slicable for Vec { fn from_slice(value: &[u8]) -> Option { let len = Self::size_of(&value[0..4])?; let mut off = 4; - let mut r = vec![]; + let mut r = Vec::new(); while off < len { let element_len = T::size_of(&value[off..])?; r.push(T::from_slice(&value[off..off + element_len])?); @@ -100,7 +101,7 @@ impl Slicable for Vec { fn to_vec(&self) -> Vec { let vecs = self.iter().map(Slicable::to_vec).collect::>(); 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)); r } @@ -127,7 +128,7 @@ impl Slicable for Header { } fn to_vec(&self) -> Vec { - vec![] + Vec::new() .join(&self.parent_hash) .join(&self.number) .join(&self.state_root) @@ -156,7 +157,7 @@ impl Slicable for Block { } fn to_vec(&self) -> Vec { - vec![] + Vec::new() .join(&self.header) .join(&self.transactions) } @@ -181,7 +182,7 @@ mod tests { let tx = Transaction { signed: one.clone(), function: Function::StakingTransferStake, - input_data: vec![].join(&two).join(&69u64), + input_data: Vec::new().join(&two).join(&69u64), nonce: 69, }; let serialised = tx.to_vec(); @@ -202,7 +203,7 @@ mod tests { let tx = Transaction { signed: one.clone(), function: Function::StakingTransferStake, - input_data: vec![].join(&two).join(&69u64), + input_data: Vec::new().join(&two).join(&69u64), nonce: 69, }; let data = [ @@ -271,13 +272,13 @@ mod tests { let tx1 = Transaction { signed: one.clone(), function: Function::StakingTransferStake, - input_data: vec![].join(&two).join(&69u64), + input_data: Vec::new().join(&two).join(&69u64), nonce: 69, }; let tx2 = Transaction { signed: two.clone(), function: Function::StakingStake, - input_data: vec![], + input_data: Vec::new(), nonce: 42, }; let h = Header { @@ -327,13 +328,13 @@ mod tests { let tx1 = Transaction { signed: one.clone(), function: Function::StakingTransferStake, - input_data: vec![].join(&two).join(&69u64), + input_data: Vec::new().join(&two).join(&69u64), nonce: 69, }; let tx2 = Transaction { signed: two.clone(), function: Function::StakingStake, - input_data: vec![], + input_data: Vec::new(), nonce: 42, }; let h = Header { diff --git a/substrate/wasm-runtime/polkadot/src/support/slicable.rs b/substrate/wasm-runtime/polkadot/src/support/slicable.rs index e220eb0f7d..939d1591ca 100644 --- a/substrate/wasm-runtime/polkadot/src/support/slicable.rs +++ b/substrate/wasm-runtime/polkadot/src/support/slicable.rs @@ -59,7 +59,7 @@ impl Slicable for Vec { unimplemented!(); } fn to_vec(&self) -> Vec { - let mut r: Vec = vec![].join(&(self.len() as u32)); + let mut r: Vec = Vec::new().join(&(self.len() as u32)); r.extend_from_slice(&self); r } diff --git a/substrate/wasm-runtime/support/src/lib.rs b/substrate/wasm-runtime/support/src/lib.rs index 9c64f1a72b..308ab6dd9e 100644 --- a/substrate/wasm-runtime/support/src/lib.rs +++ b/substrate/wasm-runtime/support/src/lib.rs @@ -10,6 +10,7 @@ pub use alloc::vec::Vec; pub use alloc::boxed::Box; pub use alloc::rc::Rc; pub use core::mem::{transmute, size_of, uninitialized, swap}; +pub use core::slice; pub use core::cell::{RefCell, Ref, RefMut}; 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_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_deposit_log(log_data: *const u8, log_len: u32); + fn ext_chain_id() -> u64; } pub fn storage(key: &[u8]) -> Vec { @@ -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 { - ext_deposit_log( - &log[0] as *const u8, log.len() as u32, - ) + ext_get_storage_into(&key[0], key.len() as u32, &mut value_out[0], value_out.len() as u32) as usize + } +} + +/// The current relay chain identifier. +pub fn chain_id() -> u64 { + unsafe { + ext_chain_id() } } diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm index be2ae9b30e..658efc3738 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.compact.wasm differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm index 228d1aaf0c..1d1c937b70 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm differ