Skip to content
Snippets Groups Projects
Commit 88d66681 authored by Gav's avatar Gav
Browse files

More fleshing out on runtime.

parent b104f5e6
Branches
No related merge requests found
Showing
with 68 additions and 56 deletions
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
#[macro_use] #[macro_use]
extern crate runtime_support; 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. /// The hash of an ECDSA pub key which is used to identify an external transactor.
pub type AccountID = [u8; 32]; pub type AccountID = [u8; 32];
...@@ -19,20 +20,36 @@ pub type TxOrder = u64; ...@@ -19,20 +20,36 @@ pub type TxOrder = u64;
/// The functions that a transaction can call (and be dispatched to). /// The functions that a transaction can call (and be dispatched to).
pub enum Function { pub enum Function {
StakingStake(), StakingStake,
StakingUnstake(), StakingUnstake,
ConsensusSetSessionKey(SessionKey), ConsensusSetSessionKey,
} }
impl Function { impl Function {
/// Dispatch the function. /// Dispatch the function.
pub fn dispatch(self) -> Vec<u8> { 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(&params[0..size_of::<AccountID>()]);
consensus::set_session_key(transactor, session);
}
}
}
} }
#[derive(Clone)]
pub struct Digest { pub struct Digest {
pub logs: Vec<Vec<u8>>, pub logs: Vec<Vec<u8>>,
} }
#[derive(Clone)]
pub struct Header { pub struct Header {
pub parent_hash: Hash, pub parent_hash: Hash,
pub number: BlockNumber, pub number: BlockNumber,
...@@ -42,7 +59,7 @@ pub struct Header { ...@@ -42,7 +59,7 @@ pub struct Header {
} }
pub struct Transaction { pub struct Transaction {
pub senders: Vec<AccountID>, pub signed: AccountID,
pub function: Function, pub function: Function,
pub input_data: Vec<u8>, pub input_data: Vec<u8>,
pub nonce: TxOrder, pub nonce: TxOrder,
...@@ -71,42 +88,28 @@ impl Block { ...@@ -71,42 +88,28 @@ impl Block {
} }
} }
/*
use std::sync::{rc, RefCell, Once, ONCE_INIT};
use std::mem;
#[derive(Default)] #[derive(Default)]
struct Environment { struct Environment {
header: Option<Header>, block_number: BlockNumber,
current_user: Option<AccountID>,
} }
#[derive(Clone)] fn env() -> Rc<RefCell<Environment>> {
struct EnvironmentHolder {
inner: Rc<RefCell<Environment>>,
}
fn get_environment() -> EnvironmentHolder {
// Initialize it to a null value // Initialize it to a null value
static mut SINGLETON: *const EnvironmentHolder = 0 as *const EnvironmentHolder; static mut SINGLETON: *const Rc<RefCell<Environment>> = 0 as *const Rc<RefCell<Environment>>;
static ONCE: Once = ONCE_INIT;
unsafe { unsafe {
ONCE.call_once(|| { if SINGLETON == 0 as *const Rc<RefCell<Environment>> {
// Make it // Make it
let singleton = EnvironmentHolder { let singleton: Rc<RefCell<Environment>> = Rc::new(RefCell::new(Default::default()));
inner: Rc::new(RefCell::new(Default::default())),
};
// Put it in the heap so it can outlive this call // 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. // Now we give out a copy of the data that is safe to use concurrently.
(*SINGLETON).clone() (*SINGLETON).clone()
} }
} }
*/
// TODO: include RLP implementation // TODO: include RLP implementation
// TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme) // TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme)
...@@ -133,20 +136,26 @@ mod environment { ...@@ -133,20 +136,26 @@ mod environment {
use super::*; use super::*;
/// The current block number being processed. Set by `execute_block`. /// 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. /// Get the block hash of a given block.
pub fn block_hash(_number: BlockNumber) -> Hash { unimplemented!() } 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<u8> { pub fn execute_block(_block: &Block) -> Vec<u8> {
// TODO: populate environment from header. // populate environment from header.
{
let e = env();
e.borrow_mut().block_number = _block.header.number;
}
staking::pre_transactions(); staking::pre_transactions();
// TODO: go through each transaction and use execute_transaction to execute. // TODO: go through each transaction and use execute_transaction to execute.
staking::post_transactions(); staking::post_transactions();
// TODO: ensure digest in header is what we expect from transactions. // TODO: ensure digest in header is what we expect from transactions.
Vec::new() Vec::new()
} }
...@@ -156,9 +165,9 @@ mod environment { ...@@ -156,9 +165,9 @@ mod environment {
// TODO: ensure signature valid and recover id (use authentication::authenticate) // TODO: ensure signature valid and recover id (use authentication::authenticate)
// TODO: ensure target_function valid // TODO: ensure target_function valid
// TODO: decode parameters // TODO: decode parameters
// TODO: set `current_user` to the id
// TODO: make call _tx.function.dispatch(&_tx.signed, &_tx.input_data);
// TODO: reset `current_user`
// TODO: encode any return // TODO: encode any return
Vec::new() Vec::new()
} }
...@@ -235,9 +244,9 @@ mod consensus { ...@@ -235,9 +244,9 @@ mod consensus {
10 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. /// session.
pub fn set_session_key(_validator: AccountID, _session: AccountID) { pub fn set_session_key(_transactor: &AccountID, _session: AccountID) {
unimplemented!() unimplemented!()
} }
...@@ -274,17 +283,17 @@ mod staking { ...@@ -274,17 +283,17 @@ mod staking {
fn balance(_who: AccountID) -> Balance { unimplemented!() } fn balance(_who: AccountID) -> Balance { unimplemented!() }
/// Transfer some unlocked staking balance to another staker. /// 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. /// 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. /// 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. /// Hook to be called prior to transaction processing.
pub fn pre_transactions() { pub fn pre_transactions() {
......
...@@ -7,7 +7,10 @@ ...@@ -7,7 +7,10 @@
//#[macro_use] //#[macro_use]
extern crate alloc; extern crate alloc;
pub use alloc::vec::Vec; 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_libc;
extern crate pwasm_alloc; extern crate pwasm_alloc;
...@@ -37,11 +40,11 @@ pub fn storage(key: &[u8]) -> Vec<u8> { ...@@ -37,11 +40,11 @@ pub fn storage(key: &[u8]) -> Vec<u8> {
pub fn storage_into<T: Sized>(key: &[u8]) -> Option<T> { pub fn storage_into<T: Sized>(key: &[u8]) -> Option<T> {
let mut result: T; let mut result: T;
let size = mem::size_of::<T>(); let size = size_of::<T>();
let written; let written;
unsafe { unsafe {
result = mem::uninitialized(); result = uninitialized();
let result_as_byte_blob = mem::transmute::<*mut T, *mut u8>(&mut result); 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; 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. // Only return a fully written value.
......
d4940d6f62cf958e 6eee55b59b574864
\ No newline at end of file \ 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)",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":[]} {"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 \ No newline at end of file
42f9c1f3676cdc1e ea3ae1eab20002fa
\ No newline at end of file \ 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":[[1515500954,752149165],"/Users/gav/Core/polkadot/wasm-runtime/target/wasm32-unknown-unknown/release/.fingerprint/runtime-support-5482fb51bf4d410e/dep-lib-runtime_support-5482fb51bf4d410e"]}],"rustflags":[]} {"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 \ No newline at end of file
9cf830998a9aef5e 0caae204cc6f3f45
\ No newline at end of file \ 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)",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":[]} {"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 \ No newline at end of file
/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
/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
No preview for this file type
/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
/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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment