diff --git a/substrate/wasm-runtime/polkadot/src/lib.rs b/substrate/wasm-runtime/polkadot/src/lib.rs index 6a6201945d..500594e31a 100644 --- a/substrate/wasm-runtime/polkadot/src/lib.rs +++ b/substrate/wasm-runtime/polkadot/src/lib.rs @@ -15,15 +15,10 @@ pub use support::{primitives, function, environment, storage}; #[cfg(test)] pub use support::{testing, statichex}; - -#[allow(unused_imports)] // TODO: remove in due course use runtime_support::Vec; use slicable::Slicable; use primitives::{Block, UncheckedTransaction}; -// TODO: add externals for: -// - trie rooting - pub fn execute_block(input: Vec) -> Vec { runtime::system::execute_block(Block::from_slice(&input).unwrap()); Vec::new() diff --git a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs index 0d2531cea4..f8991e8871 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/consensus.rs @@ -2,6 +2,7 @@ use runtime_support::Vec; use keyedvec::KeyedVec; use storage::Storage; use primitives::{AccountID, SessionKey, BlockNumber}; +use runtime::{system, staking}; pub fn set_authority(index: u32, authority: AccountID) { authority.store(&index.to_keyed_vec(b"con\0aut\0")); @@ -32,41 +33,3 @@ pub fn set_authorities(authorities: &[AccountID]) { set_authority_count(authorities.len() as u32); authorities.iter().enumerate().for_each(|(v, &i)| set_authority(v as u32, i)); } - -/// Get the current set of validators. These are the long-term identifiers for the validators -/// and will be mapped to a session key with the most recent `set_next_session_key`. -pub fn validators() -> Vec { - unimplemented!() -} - -/// Set the current set of validators. -/// -/// Called by staking::next_era() only. -pub fn set_validators(_new: &[AccountID]) { - unimplemented!() -} - -/// The number of blocks in each session. -pub fn session_length() -> BlockNumber { - Storage::into(b"con\0bps") -} - -/// Sets the session key of `_transactor` to `_session`. This doesn't take effect until the next -/// session. -pub fn set_session_key(_transactor: &AccountID, _session: &AccountID) { - unimplemented!() -} - -/// Move onto next session: register the new authority set. -pub fn next_session() { - // TODO: Call set_authorities(). - unimplemented!() -} - -/// Hook to be called prior to transaction processing. -pub fn pre_transactions() {} - -/// Hook to be called after to transaction processing. -pub fn post_transactions() { - // TODO: check block number and call next_session if necessary. -} diff --git a/substrate/wasm-runtime/polkadot/src/runtime/mod.rs b/substrate/wasm-runtime/polkadot/src/runtime/mod.rs index c73e338a91..8e60ec431c 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/mod.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/mod.rs @@ -6,3 +6,5 @@ pub mod consensus; pub mod staking; #[allow(unused)] pub mod timestamp; +#[allow(unused)] +pub mod session; diff --git a/substrate/wasm-runtime/polkadot/src/runtime/session.rs b/substrate/wasm-runtime/polkadot/src/runtime/session.rs new file mode 100644 index 0000000000..f72ef0b5c6 --- /dev/null +++ b/substrate/wasm-runtime/polkadot/src/runtime/session.rs @@ -0,0 +1,51 @@ +use runtime_support::Vec; +use keyedvec::KeyedVec; +use storage::Storage; +use primitives::{AccountID, SessionKey, BlockNumber}; +use runtime::{system, staking, consensus}; + +/// Get the current set of validators. These are the long-term identifiers for the validators +/// and will be mapped to a session key with the most recent `set_next_session_key`. +pub fn validators() -> Vec { + consensus::authorities() +} + +/// Set the current set of validators. +/// +/// Called by staking::next_era() only. +pub fn set_validators(new: &[AccountID]) { + consensus::set_authorities(new); +} + +/// The number of blocks in each session. +pub fn length() -> BlockNumber { + Storage::into(b"con\0bps") +} + +/// Sets the session key of `_transactor` to `_session`. This doesn't take effect until the next +/// session. +pub fn set_key(_transactor: &AccountID, _session: &AccountID) { + // TODO: record the new session key for `_transactor`, ready for the next session. +} + +/// Move onto next session: register the new authority set. +pub fn next_session() { + // TODO: Call set_authorities() with any new authorities. +} + +/// Hook to be called prior to transaction processing. +pub fn pre_transactions() { + staking::pre_transactions(); +} + +/// Hook to be called after to transaction processing. +pub fn post_transactions() { + staking::post_transactions(); + + // do this last, after the staking system has had chance to switch out the authorities for the + // new set. + // check block number and call next_session if necessary. + if system::block_number() % length() == 0 { + next_session(); + } +} diff --git a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs index 6e22cfcd5f..366ad0668b 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/staking.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/staking.rs @@ -1,11 +1,11 @@ use keyedvec::KeyedVec; use storage::Storage; use primitives::{BlockNumber, Balance, AccountID}; -use runtime::consensus; +use runtime::{system, session}; /// The length of a staking era in blocks. pub fn era_length() -> BlockNumber { - sessions_per_era() * consensus::session_length() + sessions_per_era() * session::length() } /// The length of a staking era in sessions. @@ -17,7 +17,8 @@ pub fn sessions_per_era() -> BlockNumber { /// /// NOTE: This always happens on a session change. pub fn next_era() { - unimplemented!() + // TODO: evaluate desired staking amounts and nominations and optimise to find the best + // combination of validators, then use session::set_validators(). } /// The balance of a given account. @@ -53,13 +54,14 @@ pub fn unstake(_transactor: &AccountID) { /// Hook to be called prior to transaction processing. pub fn pre_transactions() { - consensus::pre_transactions(); } /// Hook to be called after to transaction processing. pub fn post_transactions() { - // TODO: check block number and call next_era if necessary. - consensus::post_transactions(); + // check block number and call next_era if necessary. + if system::block_number() % era_length() == 0 { + next_era(); + } } diff --git a/substrate/wasm-runtime/polkadot/src/runtime/system.rs b/substrate/wasm-runtime/polkadot/src/runtime/system.rs index fe0e835608..690e1db527 100644 --- a/substrate/wasm-runtime/polkadot/src/runtime/system.rs +++ b/substrate/wasm-runtime/polkadot/src/runtime/system.rs @@ -3,7 +3,7 @@ use runtime_support::{Vec, swap}; use storage::Storage; use keyedvec::KeyedVec; use environment::with_env; -use runtime::staking; +use runtime::session; /// The current block number being processed. Set by `execute_block`. pub fn block_number() -> BlockNumber { @@ -40,20 +40,24 @@ pub fn execute_block(mut block: Block) { ); // TODO: check transaction trie root represents the transactions. + // this requires non-trivial changes to the externals API or compiling trie rooting into wasm + // so will wait until a little later. // store the header hash in storage. let header_hash_key = header.number.to_keyed_vec(b"sys\0old\0"); header.keccak256().store(&header_hash_key); // execute transactions - staking::pre_transactions(); + session::pre_transactions(); block.transactions.iter().for_each(execute_transaction); - staking::post_transactions(); + session::post_transactions(); // any final checks final_checks(&block); - // TODO: check storage root somehow + // TODO: check storage root. + // this requires non-trivial changes to the externals API or compiling trie rooting into wasm + // so will wait until a little later. } fn final_checks(_block: &Block) { diff --git a/substrate/wasm-runtime/polkadot/src/support/function.rs b/substrate/wasm-runtime/polkadot/src/support/function.rs index ff54938d47..995ec3c459 100644 --- a/substrate/wasm-runtime/polkadot/src/support/function.rs +++ b/substrate/wasm-runtime/polkadot/src/support/function.rs @@ -1,4 +1,4 @@ -use runtime::{staking, consensus}; +use runtime::{staking, session}; use primitives::AccountID; use streamreader::StreamReader; @@ -9,7 +9,7 @@ pub enum Function { StakingStake, StakingUnstake, StakingTransferStake, - ConsensusSetSessionKey, + SessionSetKey, } impl Function { @@ -18,7 +18,7 @@ impl Function { x if x == Function::StakingStake as u8 => Some(Function::StakingStake), x if x == Function::StakingUnstake as u8 => Some(Function::StakingUnstake), x if x == Function::StakingTransferStake as u8 => Some(Function::StakingTransferStake), - x if x == Function::ConsensusSetSessionKey as u8 => Some(Function::ConsensusSetSessionKey), + x if x == Function::SessionSetKey as u8 => Some(Function::SessionSetKey), _ => None, } } @@ -40,9 +40,9 @@ impl Function { let value = params.read().unwrap(); staking::transfer_stake(transactor, &dest, value); } - Function::ConsensusSetSessionKey => { + Function::SessionSetKey => { let session = params.read().unwrap(); - consensus::set_session_key(transactor, &session); + session::set_key(transactor, &session); } } }