diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs index 24529d4380..fb8e5b86f8 100644 --- a/substrate/wasm-runtime/polkadot/src/lib.rs +++ b/substrate/wasm-runtime/polkadot/src/lib.rs @@ -3,7 +3,8 @@ #[macro_use] extern crate runtime_support; -use runtime_support::{set_storage, storage, storage_into, Vec}; +use runtime_support::{set_storage, storage, storage_into, Vec, size_of}; +use runtime_support::{Rc, RefCell, transmute, Box}; /// The hash of an ECDSA pub key which is used to identify an external transactor. pub type AccountID = [u8; 32]; @@ -19,20 +20,36 @@ pub type TxOrder = u64; /// The functions that a transaction can call (and be dispatched to). pub enum Function { - StakingStake(), - StakingUnstake(), - ConsensusSetSessionKey(SessionKey), + StakingStake, + StakingUnstake, + ConsensusSetSessionKey, } impl Function { /// Dispatch the function. - pub fn dispatch(self) -> Vec { unimplemented!() } + pub fn dispatch(&self, transactor: &AccountID, params: &[u8]) { + match *self { + Function::StakingStake => { + staking::stake(transactor); + } + Function::StakingUnstake => { + staking::unstake(transactor); + } + Function::ConsensusSetSessionKey => { + let mut session = AccountID::default(); + session.copy_from_slice(¶ms[0..size_of::()]); + consensus::set_session_key(transactor, session); + } + } + } } +#[derive(Clone)] pub struct Digest { pub logs: Vec>, } +#[derive(Clone)] pub struct Header { pub parent_hash: Hash, pub number: BlockNumber, @@ -42,7 +59,7 @@ pub struct Header { } pub struct Transaction { - pub senders: Vec, + pub signed: AccountID, pub function: Function, pub input_data: Vec, pub nonce: TxOrder, @@ -71,42 +88,28 @@ impl Block { } } -/* -use std::sync::{rc, RefCell, Once, ONCE_INIT}; -use std::mem; - #[derive(Default)] struct Environment { - header: Option
, - current_user: Option, + block_number: BlockNumber, } -#[derive(Clone)] -struct EnvironmentHolder { - inner: Rc>, -} - -fn get_environment() -> EnvironmentHolder { +fn env() -> Rc> { // Initialize it to a null value - static mut SINGLETON: *const EnvironmentHolder = 0 as *const EnvironmentHolder; - static ONCE: Once = ONCE_INIT; + static mut SINGLETON: *const Rc> = 0 as *const Rc>; unsafe { - ONCE.call_once(|| { + if SINGLETON == 0 as *const Rc> { // Make it - let singleton = EnvironmentHolder { - inner: Rc::new(RefCell::new(Default::default())), - }; + let singleton: Rc> = Rc::new(RefCell::new(Default::default())); // Put it in the heap so it can outlive this call - SINGLETON = mem::transmute(Box::new(singleton)); - }); + SINGLETON = transmute(Box::new(singleton)); + } // Now we give out a copy of the data that is safe to use concurrently. (*SINGLETON).clone() } } -*/ // TODO: include RLP implementation // TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme) @@ -133,20 +136,26 @@ mod environment { use super::*; /// The current block number being processed. Set by `execute_block`. - pub fn block_number() -> BlockNumber { unimplemented!() } + pub fn block_number() -> BlockNumber { let e = env(); let eb = e.borrow(); eb.block_number } /// Get the block hash of a given block. pub fn block_hash(_number: BlockNumber) -> Hash { unimplemented!() } - /// Get the current user's ID - pub fn current_user() -> AccountID { unimplemented!() } - pub fn execute_block(_block: &Block) -> Vec { - // TODO: populate environment from header. + // populate environment from header. + { + let e = env(); + e.borrow_mut().block_number = _block.header.number; + } + staking::pre_transactions(); + // TODO: go through each transaction and use execute_transaction to execute. + staking::post_transactions(); + // TODO: ensure digest in header is what we expect from transactions. + Vec::new() } @@ -156,9 +165,9 @@ mod environment { // TODO: ensure signature valid and recover id (use authentication::authenticate) // TODO: ensure target_function valid // TODO: decode parameters - // TODO: set `current_user` to the id - // TODO: make call - // TODO: reset `current_user` + + _tx.function.dispatch(&_tx.signed, &_tx.input_data); + // TODO: encode any return Vec::new() } @@ -235,9 +244,9 @@ mod consensus { 10 } - /// Sets the session key of `_validator` to `_session`. This doesn't take effect until the next + /// Sets the session key of `_transactor` to `_session`. This doesn't take effect until the next /// session. - pub fn set_session_key(_validator: AccountID, _session: AccountID) { + pub fn set_session_key(_transactor: &AccountID, _session: AccountID) { unimplemented!() } @@ -274,17 +283,17 @@ mod staking { fn balance(_who: AccountID) -> Balance { unimplemented!() } /// Transfer some unlocked staking balance to another staker. - fn transfer_stake(_who: AccountID, _dest: AccountID, _value: Balance) { unimplemented!() } + pub fn transfer_stake(_transactor: AccountID, _dest: AccountID, _value: Balance) { unimplemented!() } - /// Declare the desire to stake. + /// Declare the desire to stake for the transactor. /// /// Effects will be felt at the beginning of the next era. - fn stake() { unimplemented!() } + pub fn stake(_transactor: &AccountID) { unimplemented!() } - /// Retract the desire to stake. + /// Retract the desire to stake for the transactor. /// /// Effects will be felt at the beginning of the next era. - fn unstake() { unimplemented!() } + pub fn unstake(_transactor: &AccountID) { unimplemented!() } /// Hook to be called prior to transaction processing. pub fn pre_transactions() { diff --git a/substrate/wasm-runtime/support/src/lib.rs b/substrate/wasm-runtime/support/src/lib.rs index aaab93e960..3acad5237e 100644 --- a/substrate/wasm-runtime/support/src/lib.rs +++ b/substrate/wasm-runtime/support/src/lib.rs @@ -7,7 +7,10 @@ //#[macro_use] extern crate alloc; pub use alloc::vec::Vec; -use core::mem; +pub use alloc::boxed::Box; +pub use alloc::rc::Rc; +pub use core::mem::{transmute, size_of, uninitialized}; +pub use core::cell::{RefCell, Ref, RefMut}; extern crate pwasm_libc; extern crate pwasm_alloc; @@ -37,11 +40,11 @@ pub fn storage(key: &[u8]) -> Vec { pub fn storage_into(key: &[u8]) -> Option { let mut result: T; - let size = mem::size_of::(); + let size = size_of::(); let written; unsafe { - result = mem::uninitialized(); - let result_as_byte_blob = mem::transmute::<*mut T, *mut u8>(&mut result); + result = uninitialized(); + let result_as_byte_blob = transmute::<*mut T, *mut u8>(&mut result); written = ext_get_storage_into(&key[0], key.len() as u32, result_as_byte_blob, size as u32) as usize; } // Only return a fully written value. diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot index 3e7282f285..77c1ca3667 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot @@ -1 +1 @@ -d4940d6f62cf958e \ No newline at end of file +6eee55b59b574864 \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot.json b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot.json index ae6a4a3c0f..39174c4da4 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot.json +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/lib-runtime_polkadot.json @@ -1 +1 @@ -{"rustc":8294656847287967537,"features":"[\"default\", \"without-std\"]","target":15371597068611496627,"profile":42358739494345872,"deps":[["runtime-support v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/support)",2223771509741189442]],"local":[{"MtimeBased":[[1515501953,507863132],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/dep-lib-runtime_polkadot"]}],"rustflags":[]} \ No newline at end of file +{"rustc":8294656847287967537,"features":"[\"default\", \"without-std\"]","target":15371597068611496627,"profile":42358739494345872,"deps":[["runtime-support v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/support)",18014962227880213226]],"local":[{"MtimeBased":[[1515506687,785085370],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-polkadot-1e4c8740d04ba868/dep-lib-runtime_polkadot"]}],"rustflags":[]} \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e index c062c60974..7d69f92972 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e @@ -1 +1 @@ -42f9c1f3676cdc1e \ No newline at end of file +ea3ae1eab20002fa \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e.json b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e.json index 497c7e5b56..1e4859a996 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e.json +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/lib-runtime_support-5482fb51bf4d410e.json @@ -1 +1 @@ -{"rustc":8294656847287967537,"features":"[]","target":14982045766639954252,"profile":42358739494345872,"deps":[["pwasm-alloc v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc)",1843871105590971886],["pwasm-libc v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/pwasm-libc)",6197225601014249845]],"local":[{"MtimeBased":[[1515500954,752149165],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/dep-lib-runtime_support-5482fb51bf4d410e"]}],"rustflags":[]} \ No newline at end of file +{"rustc":8294656847287967537,"features":"[]","target":14982045766639954252,"profile":42358739494345872,"deps":[["pwasm-alloc v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc)",1843871105590971886],["pwasm-libc v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/pwasm-libc)",6197225601014249845]],"local":[{"MtimeBased":[[1515504344,57881737],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/dep-lib-runtime_support-5482fb51bf4d410e"]}],"rustflags":[]} \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test index dc062b33e3..fb66643a92 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test @@ -1 +1 @@ -9cf830998a9aef5e \ No newline at end of file +0caae204cc6f3f45 \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test.json b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test.json index 6040d1fe18..1003d6c5db 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test.json +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/lib-runtime_test.json @@ -1 +1 @@ -{"rustc":8294656847287967537,"features":"[]","target":11385551307513482501,"profile":42358739494345872,"deps":[["runtime-support v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/support)",2223771509741189442]],"local":[{"MtimeBased":[[1515500955,389693545],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/dep-lib-runtime_test"]}],"rustflags":[]} \ No newline at end of file +{"rustc":8294656847287967537,"features":"[]","target":11385551307513482501,"profile":42358739494345872,"deps":[["runtime-support v0.1.0 (file:///Users/gav/Core/polkadot/wasm-runtime/support)",18014962227880213226]],"local":[{"MtimeBased":[[1515504344,719487568],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-test-0ee9f37942e84b82/dep-lib-runtime_test"]}],"rustflags":[]} \ No newline at end of file diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/deps/libruntime_support-5482fb51bf4d410e.rlib b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/deps/libruntime_support-5482fb51bf4d410e.rlib index 2e0626d070..c74a550523 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/deps/libruntime_support-5482fb51bf4d410e.rlib and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/deps/libruntime_support-5482fb51bf4d410e.rlib differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.d b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.d index f368ff5620..4bcb36f425 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.d +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.d @@ -1 +1 @@ -/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.rlib: /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs +/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/libpwasm_alloc.rlib: /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.d b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.d index 43fe058cb3..6ea9ca2b93 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.d +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.d @@ -1 +1 @@ -/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib: /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs +/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib: /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib index 2e0626d070..c74a550523 100644 Binary files a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib and b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/libruntime_support.rlib differ diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.d b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.d index 078cbb91de..f8c6798856 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.d +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.d @@ -1 +1 @@ -/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm: /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/polkadot/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs +/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_polkadot.wasm: /Users/gav/Core/polkadot/wasm-runtime/polkadot/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs diff --git a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.d b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.d index 7c40f4df98..148293caf3 100644 --- a/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.d +++ b/substrate/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.d @@ -1 +1 @@ -/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm: /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/test/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs +/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.wasm: /Users/gav/Core/polkadot/wasm-runtime/pwasm-libc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/pwasm-alloc/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/support/src/lib.rs /Users/gav/Core/polkadot/wasm-runtime/test/src/lib.rs