Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a83059a
Completely rework dispatch mechanism into something modular.
gavofyork Mar 13, 2018
82a178d
Council vote tests.
gavofyork Mar 13, 2018
1d28d9a
Fix tests.
gavofyork Mar 13, 2018
69c88c3
whitespace.
gavofyork Mar 13, 2018
b12a708
Fix demo runtime tests.
gavofyork Mar 14, 2018
e48e86d
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
8e3cc51
Fix up tests.
gavofyork Mar 14, 2018
53e2fdf
Merge branch 'gav-demo' into gav-dispatch
gavofyork Mar 14, 2018
27ecd6f
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
5eca74a
Remove dead code.
gavofyork Mar 14, 2018
7cece3b
Merge branch 'master' into gav-dispatch
gavofyork Mar 14, 2018
8c2396d
Timestamp uses new storage API.
gavofyork Mar 14, 2018
6b6c240
Move over system module to new API.
gavofyork Mar 14, 2018
a79dab2
Much nicer storage API, moved over staking module.
gavofyork Mar 15, 2018
1fd6b3e
More refactoring.
gavofyork Mar 15, 2018
51b4a8c
Democracy uses new storage API.
gavofyork Mar 15, 2018
8ada9f7
Council uses new RPC.
gavofyork Mar 16, 2018
c4f5f42
Fix more tests.
gavofyork Mar 16, 2018
d11f5ca
Use match for Id
gavofyork Mar 16, 2018
faa0a44
Use match for Id
gavofyork Mar 16, 2018
7912de1
Make PrivPass better protected.
gavofyork Mar 19, 2018
aac3899
Address other grumbles.
gavofyork Mar 19, 2018
26519c6
Give PrivPass a private member.
gavofyork Mar 19, 2018
9f32ea7
Testing PrivPass.
gavofyork Mar 19, 2018
8966951
Add docs.
gavofyork Mar 19, 2018
91d5e75
Merge branch 'gav-dispatch' into gav-storage-revamp
gavofyork Mar 19, 2018
7ee81e6
Recompile binaries after merge.
gavofyork Mar 19, 2018
33215e4
Merge branch 'master' into gav-storage-revamp
gavofyork Mar 19, 2018
cf9659e
Remove duplicated code.
gavofyork Mar 19, 2018
b8c0832
New binaries.
gavofyork Mar 19, 2018
57a9a79
Docs
gavofyork Mar 19, 2018
59c7a1a
Docs
gavofyork Mar 19, 2018
6f8f456
avoid use of (arguably) confusing terminology.
gavofyork Mar 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More refactoring.
  • Loading branch information
gavofyork committed Mar 15, 2018
commit 1fd6b3e5d27ae4dfbbb64bc880853661aa1cfe27
13 changes: 5 additions & 8 deletions demo/runtime/src/genesismap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ impl GenesisConfig {
pub fn genesis_map(&self) -> HashMap<Vec<u8>, Vec<u8>> {
let wasm_runtime = include_bytes!("../wasm/genesis.wasm").to_vec();
vec![
(&session::SESSION_LENGTH[..], vec![].and(&self.session_length)),
(&session::VALIDATOR_COUNT[..], vec![].and(&(self.validators.len() as u32))),
(session::SessionLength::key(), vec![].and(&self.session_length)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 🎊

(session::Validators::key(), vec![].and(&self.validators)),

(&staking::Intention::len_key()[..], vec![].and(&0u32)),
(&staking::SessionsPerEra::key()[..], vec![].and(&self.sessions_per_era)),
Expand All @@ -105,11 +105,8 @@ impl GenesisConfig {
(&council_vote::VOTING_PERIOD[..], vec![].and(&self.council_proposal_voting_period))
].into_iter()
.map(|(k, v)| (k.into(), v))
.chain(self.validators.iter()
.enumerate()
.map(|(i, account)| ((i as u32).to_keyed_vec(session::VALIDATOR_AT), vec![].and(account)))
).chain(self.balances.iter()
.map(|&(account, balance)| (account.to_keyed_vec(staking::BALANCE_OF), vec![].and(&balance)))
.chain(self.balances.iter()
.map(|&(account, balance)| (staking::FreeBalanceOf::key_for(&account), vec![].and(&balance)))
)
.map(|(k, v)| (twox_128(&k[..])[..].to_vec(), v.to_vec()))
.chain(vec![
Expand All @@ -127,6 +124,6 @@ impl GenesisConfig {
pub fn additional_storage_with_genesis(genesis_block: &Block) -> HashMap<Vec<u8>, Vec<u8>> {
use codec::Slicable;
map![
system::BlockHash::key_for(&0) => genesis_block.header.blake2_256().encode()
system::BlockHashAt::key_for(&0) => genesis_block.header.blake2_256().encode()
]
}
22 changes: 9 additions & 13 deletions demo/runtime/src/runtime/democracy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub mod internal {
// tally up votes for any expiring referenda.
for (index, _, proposal, vote_threshold) in maturing_referendums_at(now) {
let (approve, against) = tally(index);
let total_stake = staking::TotalStake::get();
let total_stake = staking::total_stake();
clear_referendum(index);
if vote_threshold.approved(approve, against, total_stake) {
proposal.dispatch(PrivPass);
Expand Down Expand Up @@ -341,11 +341,8 @@ pub mod testing {

pub fn externalities() -> TestExternalities {
map![
twox_128(session::SESSION_LENGTH).to_vec() => vec![].and(&1u64),
twox_128(session::VALIDATOR_COUNT).to_vec() => vec![].and(&3u32),
twox_128(&0u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => Alice.to_raw_public_vec(),
twox_128(&1u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => Bob.to_raw_public_vec(),
twox_128(&2u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => Charlie.to_raw_public_vec(),
twox_128(session::SessionLength::key()).to_vec() => vec![].and(&1u64),
twox_128(session::Validators::key()).to_vec() => vec![].and(&vec![Alice.to_raw_public(), Bob.into(), Charlie.into()]),
twox_128(&staking::Intention::len_key()).to_vec() => vec![].and(&3u32),
twox_128(&staking::Intention::key_for(0)).to_vec() => Alice.to_raw_public_vec(),
twox_128(&staking::Intention::key_for(1)).to_vec() => Bob.to_raw_public_vec(),
Expand Down Expand Up @@ -374,7 +371,6 @@ pub mod testing {
mod tests {
use super::*;
use runtime_io::{with_externalities, twox_128, TestExternalities};
use runtime_support::{StorageValue, StorageMap};
use codec::{KeyedVec, Joiner};
use keyring::Keyring::*;
use environment::with_env;
Expand All @@ -396,8 +392,8 @@ mod tests {
assert_eq!(voting_period(), 1u64);
assert_eq!(minimum_deposit(), 1u64);
assert_eq!(next_free_ref_index(), 0u32);
assert_eq!(staking::SessionsPerEra::get(), 1u64);
assert_eq!(staking::TotalStake::get(), 210u64);
assert_eq!(staking::sessions_per_era(), 1u64);
assert_eq!(staking::total_stake(), 210u64);
});
}

Expand Down Expand Up @@ -521,19 +517,19 @@ mod tests {
PublicPass::test(&Alice).vote(0, true);
democracy::internal::end_block(system::block_number());
staking::internal::check_new_era();
assert_eq!(staking::BondingDuration::get(), 4u64);
assert_eq!(staking::bonding_duration(), 4u64);

with_env(|e| e.block_number = 2);
PublicPass::test(&Alice).vote(1, true);
democracy::internal::end_block(system::block_number());
staking::internal::check_new_era();
assert_eq!(staking::BondingDuration::get(), 3u64);
assert_eq!(staking::bonding_duration(), 3u64);

with_env(|e| e.block_number = 3);
PublicPass::test(&Alice).vote(2, true);
democracy::internal::end_block(system::block_number());
staking::internal::check_new_era();
assert_eq!(staking::BondingDuration::get(), 2u64);
assert_eq!(staking::bonding_duration(), 2u64);
});
}

Expand Down Expand Up @@ -634,7 +630,7 @@ mod tests {
fn passing_low_turnout_voting_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(staking::era_length(), 1u64);
assert_eq!(staking::TotalStake::get(), 210u64);
assert_eq!(staking::total_stake(), 210u64);

with_env(|e| e.block_number = 1);
let r = inject_referendum(1, sessions_per_era_propsal(2), VoteThreshold::SuperMajorityApprove);
Expand Down
89 changes: 31 additions & 58 deletions demo/runtime/src/runtime/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,31 @@

use rstd::prelude::*;
use codec::KeyedVec;
use runtime_support::{storage, StorageVec};
use runtime_support::{storage, StorageValue, StorageMap};
use demo_primitives::{AccountId, SessionKey, BlockNumber};
use runtime::{system, staking, consensus};
use runtime::system::PrivPass;
use runtime::staking::PublicPass;

pub const SESSION_LENGTH: &[u8] = b"ses:len";
pub const CURRENT_INDEX: &[u8] = b"ses:ind";
pub const LAST_LENGTH_CHANGE: &[u8] = b"ses:llc";
pub const NEXT_KEY_FOR: &[u8] = b"ses:nxt:";
pub const NEXT_SESSION_LENGTH: &[u8] = b"ses:nln";
pub const VALIDATOR_AT: &[u8] = b"ses:val:";
pub const VALIDATOR_COUNT: &[u8] = b"ses:val:len";

struct ValidatorStorageVec {}
impl StorageVec for ValidatorStorageVec {
type Item = AccountId;
const PREFIX: &'static[u8] = VALIDATOR_AT;
}

/// Get the current set of validators.
pub fn validators() -> Vec<AccountId> {
ValidatorStorageVec::items()
}

/// The number of blocks in each session.
pub fn length() -> BlockNumber {
storage::get_or(SESSION_LENGTH, 0)
storage_items!{
// The current set of validators.
pub Validators get(validators): b"ses:val" => required Vec<AccountId>;
// Current length of the session.
pub SessionLength get(length): b"ses:len" => required BlockNumber;
// Current index of the session.
pub CurrentIndex get(current_index): b"ses:ind" => required BlockNumber;

// Block at which the session length last changed.
LastLengthChange: b"ses:llc" => default BlockNumber;
// The next key for a given validator.
NextKeyFor: b"ses:nxt:" => map [ AccountId => SessionKey ];
// The next session length.
NextSessionLength: b"ses:nln" => BlockNumber;
}

/// The number of validators currently.
pub fn validator_count() -> u32 {
ValidatorStorageVec::count() as u32
}

/// The current era index.
pub fn current_index() -> BlockNumber {
storage::get_or(CURRENT_INDEX, 0)
}

/// The block number at which the era length last changed.
pub fn last_length_change() -> BlockNumber {
storage::get_or(LAST_LENGTH_CHANGE, 0)
Validators::get().len() as u32 // TODO: can probably optimised
}

impl_dispatch! {
Expand All @@ -74,7 +56,7 @@ impl<'a> public::Dispatch for PublicPass<'a> {
/// session.
fn set_key(self, key: SessionKey) {
// set new value for next session
storage::put(&self.to_keyed_vec(NEXT_KEY_FOR), &key);
NextKeyFor::insert(*self, key)
}
}

Expand All @@ -87,7 +69,7 @@ impl_dispatch! {
impl privileged::Dispatch for PrivPass {
/// Set a new era length. Won't kick in until the next era change (at current length).
fn set_length(self, new: BlockNumber) {
storage::put(NEXT_SESSION_LENGTH, &new);
NextSessionLength::put(new);
}

/// Forces a new session.
Expand All @@ -103,10 +85,10 @@ pub mod internal {

/// Set the current set of validators.
///
/// Called by staking::next_era() only. `next_session` should be called after this in order to
/// Called by `staking::next_era()` only. `next_session` should be called after this in order to
/// update the session keys to the next validator set.
pub fn set_validators(new: &[AccountId]) {
ValidatorStorageVec::set_items(new);
Validators::put(&new.to_vec()); // TODO: optimise.
consensus::internal::set_authorities(new);
}

Expand All @@ -115,27 +97,25 @@ pub mod internal {
// 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() - last_length_change()) % length() == 0 {
if (system::block_number() - LastLengthChange::get()) % length() == 0 {
rotate_session();
}
}

/// Move onto next session: register the new authority set.
pub fn rotate_session() {
// Increment current session index.
storage::put(CURRENT_INDEX, &(current_index() + 1));
CurrentIndex::put(CurrentIndex::get() + 1);

// Enact era length change.
if let Some(next_len) = storage::get::<u64>(NEXT_SESSION_LENGTH) {
storage::put(SESSION_LENGTH, &next_len);
storage::put(LAST_LENGTH_CHANGE, &system::block_number());
storage::kill(NEXT_SESSION_LENGTH);
if let Some(next_len) = NextSessionLength::take() {
SessionLength::put(next_len);
LastLengthChange::put(system::block_number());
}

// Update any changes in session keys.
validators().iter().enumerate().for_each(|(i, v)| {
let k = v.to_keyed_vec(NEXT_KEY_FOR);
if let Some(n) = storage::take(&k) {
if let Some(n) = NextKeyFor::take(v) {
consensus::internal::set_authority(i as u32, &n);
}
});
Expand All @@ -147,20 +127,15 @@ pub mod testing {
use super::*;
use runtime_io::{twox_128, TestExternalities};
use codec::{Joiner, KeyedVec};
use keyring::Keyring;
use keyring::Keyring::*;
use runtime::system;

pub fn externalities(session_length: u64) -> TestExternalities {
let one = Keyring::One.to_raw_public();
let two = Keyring::Two.to_raw_public();
let three = [3u8; 32];

let extras: TestExternalities = map![
twox_128(SESSION_LENGTH).to_vec() => vec![].and(&session_length),
twox_128(VALIDATOR_COUNT).to_vec() => vec![].and(&3u32),
twox_128(&0u32.to_keyed_vec(VALIDATOR_AT)).to_vec() => one.to_vec(),
twox_128(&1u32.to_keyed_vec(VALIDATOR_AT)).to_vec() => two.to_vec(),
twox_128(&2u32.to_keyed_vec(VALIDATOR_AT)).to_vec() => three.to_vec()
twox_128(SessionLength::key()).to_vec() => vec![].and(&session_length),
twox_128(Validators::key()).to_vec() => vec![].and(&vec![One.into(), Two.into(), three])
];
system::testing::externalities().into_iter().chain(extras.into_iter()).collect()
}
Expand All @@ -181,11 +156,9 @@ mod tests {

fn simple_setup() -> TestExternalities {
map![
twox_128(SESSION_LENGTH).to_vec() => vec![].and(&2u64),
twox_128(SessionLength::key()).to_vec() => vec![].and(&2u64),
// the validators (10, 20, ...)
twox_128(b"ses:val:len").to_vec() => vec![].and(&2u32),
twox_128(&0u32.to_keyed_vec(ValidatorStorageVec::PREFIX)).to_vec() => vec![10; 32],
twox_128(&1u32.to_keyed_vec(ValidatorStorageVec::PREFIX)).to_vec() => vec![20; 32],
twox_128(Validators::key()).to_vec() => vec![].and(&vec![[10u8; 32], [20; 32]]),
// initial session keys (11, 21, ...)
b":auth:len".to_vec() => vec![].and(&2u32),
0u32.to_keyed_vec(b":auth:") => vec![11; 32],
Expand Down
8 changes: 3 additions & 5 deletions demo/runtime/src/runtime/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,8 @@ mod tests {
#[test]
fn staking_should_work() {
let mut t: TestExternalities = map![
twox_128(session::SESSION_LENGTH).to_vec() => vec![].and(&1u64),
twox_128(session::VALIDATOR_COUNT).to_vec() => vec![].and(&2u32),
twox_128(&0u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => vec![10; 32],
twox_128(&1u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => vec![20; 32],
twox_128(session::SessionLength::key()).to_vec() => vec![].and(&1u64),
twox_128(session::Validators::key()).to_vec() => vec![].and(&vec![[10u8; 32], [20; 32]]),
twox_128(&SessionsPerEra::key()).to_vec() => vec![].and(&2u64),
twox_128(&ValidatorCount::key()).to_vec() => vec![].and(&2u32),
twox_128(&BondingDuration::key()).to_vec() => vec![].and(&3u64),
Expand Down Expand Up @@ -649,7 +647,7 @@ mod tests {
#[test]
fn staking_eras_work() {
let mut t: TestExternalities = map![
twox_128(session::SESSION_LENGTH).to_vec() => vec![].and(&1u64),
twox_128(session::SessionLength::key()).to_vec() => vec![].and(&1u64),
twox_128(&SessionsPerEra::key()).to_vec() => vec![].and(&2u64)
];
with_externalities(&mut t, || {
Expand Down