From f7af45f95bb3e26fdb4622153b6b5bde68014b58 Mon Sep 17 00:00:00 2001 From: Gav <gavin@parity.io> Date: Sun, 4 Mar 2018 19:36:26 +0100 Subject: [PATCH] remove string constants <-- @rphmeier --- .../demo/runtime/src/runtime/consensus.rs | 4 +- substrate/demo/runtime/src/runtime/council.rs | 105 ++++++++---------- .../demo/runtime/src/runtime/democracy.rs | 95 +++++++++------- .../demo/runtime/src/runtime/governance.rs | 41 ++++--- substrate/demo/runtime/src/runtime/session.rs | 38 ++++++- substrate/demo/runtime/src/runtime/staking.rs | 69 ++++++++---- substrate/demo/runtime/src/runtime/system.rs | 45 ++++---- .../demo/runtime/src/runtime/timestamp.rs | 2 +- 8 files changed, 225 insertions(+), 174 deletions(-) diff --git a/substrate/demo/runtime/src/runtime/consensus.rs b/substrate/demo/runtime/src/runtime/consensus.rs index ff313daa036..eca7bd46bb7 100644 --- a/substrate/demo/runtime/src/runtime/consensus.rs +++ b/substrate/demo/runtime/src/runtime/consensus.rs @@ -20,10 +20,12 @@ use rstd::prelude::*; use runtime_support::storage::unhashed::StorageVec; use demo_primitives::SessionKey; +pub const AUTHORITY_AT: &'static[u8] = b":auth:"; + struct AuthorityStorageVec {} impl StorageVec for AuthorityStorageVec { type Item = SessionKey; - const PREFIX: &'static[u8] = b":auth:"; + const PREFIX: &'static[u8] = AUTHORITY_AT; } /// Get the current set of authorities. These are the session keys. diff --git a/substrate/demo/runtime/src/runtime/council.rs b/substrate/demo/runtime/src/runtime/council.rs index 07ccb674118..455d919db40 100644 --- a/substrate/demo/runtime/src/runtime/council.rs +++ b/substrate/demo/runtime/src/runtime/council.rs @@ -77,35 +77,35 @@ use runtime::staking::Balance; // after each vote as all but K entries are cleared. newly registering candidates must use cleared // entries before they increase the capacity. -type VoteIndex = u32; +pub type VoteIndex = u32; // parameters -const CANDIDACY_BOND: &[u8] = b"cou:cbo"; -const VOTING_BOND: &[u8] = b"cou:vbo"; -const PRESENT_SLASH_PER_VOTER: &[u8] = b"cou:pss"; -const CARRY_COUNT: &[u8] = b"cou:cco"; -const PRESENTATION_DURATION: &[u8] = b"cou:pdu"; -const INACTIVE_GRACE_PERIOD: &[u8] = b"cou:vgp"; -const VOTING_PERIOD: &[u8] = b"cou:per"; -const TERM_DURATION: &[u8] = b"cou:trm"; -const DESIRED_SEATS: &[u8] = b"cou:sts"; +pub const CANDIDACY_BOND: &[u8] = b"cou:cbo"; +pub const VOTING_BOND: &[u8] = b"cou:vbo"; +pub const PRESENT_SLASH_PER_VOTER: &[u8] = b"cou:pss"; +pub const CARRY_COUNT: &[u8] = b"cou:cco"; +pub const PRESENTATION_DURATION: &[u8] = b"cou:pdu"; +pub const INACTIVE_GRACE_PERIOD: &[u8] = b"cou:vgp"; +pub const VOTING_PERIOD: &[u8] = b"cou:per"; +pub const TERM_DURATION: &[u8] = b"cou:trm"; +pub const DESIRED_SEATS: &[u8] = b"cou:sts"; // permanent state (always relevant, changes only at the finalisation of voting) -const ACTIVE_COUNCIL: &[u8] = b"cou:act"; -const VOTE_COUNT: &[u8] = b"cou:vco"; +pub const ACTIVE_COUNCIL: &[u8] = b"cou:act"; +pub const VOTE_COUNT: &[u8] = b"cou:vco"; // persistent state (always relevant, changes constantly) -const APPROVALS_OF: &[u8] = b"cou:apr:"; // Vec<bool> -const REGISTER_INFO_OF: &[u8] = b"cou:reg:"; // Candidate -> (VoteIndex, u32) -const LAST_ACTIVE_OF: &[u8] = b"cou:lac:"; // Voter -> VoteIndex -const VOTERS: &[u8] = b"cou:vrs"; // Vec<AccountId> -const CANDIDATES: &[u8] = b"cou:can"; // Vec<AccountId>, has holes -const CANDIDATE_COUNT: &[u8] = b"cou:cnc"; // u32 +pub const APPROVALS_OF: &[u8] = b"cou:apr:"; // Vec<bool> +pub const REGISTER_INFO_OF: &[u8] = b"cou:reg:"; // Candidate -> (VoteIndex, u32) +pub const LAST_ACTIVE_OF: &[u8] = b"cou:lac:"; // Voter -> VoteIndex +pub const VOTERS: &[u8] = b"cou:vrs"; // Vec<AccountId> +pub const CANDIDATES: &[u8] = b"cou:can"; // Vec<AccountId>, has holes +pub const CANDIDATE_COUNT: &[u8] = b"cou:cnc"; // u32 // temporary state (only relevant during finalisation/presentation) -const NEXT_FINALISE: &[u8] = b"cou:nxt"; -const SNAPSHOTED_STAKES: &[u8] = b"cou:sss"; // Vec<Balance> -const LEADERBOARD: &[u8] = b"cou:win"; // Vec<(Balance, AccountId)> ORDERED low -> high +pub const NEXT_FINALISE: &[u8] = b"cou:nxt"; +pub const SNAPSHOTED_STAKES: &[u8] = b"cou:sss"; // Vec<Balance> +pub const LEADERBOARD: &[u8] = b"cou:win"; // Vec<(Balance, AccountId)> ORDERED low -> high /// How much should be locked up in order to submit one's candidacy. pub fn candidacy_bond() -> Balance { @@ -552,46 +552,14 @@ fn finalise_tally() { } #[cfg(test)] -mod tests { +pub mod testing { use super::*; - use runtime_io::{with_externalities, twox_128, TestExternalities}; - use codec::{KeyedVec, Joiner}; - use keyring::Keyring; - use environment::with_env; - use demo_primitives::{AccountId, Proposal}; - use runtime::{staking, session, democracy}; - - fn new_test_ext() -> TestExternalities { - let alice = Keyring::Alice.to_raw_public(); - let bob = Keyring::Bob.to_raw_public(); - let charlie = Keyring::Charlie.to_raw_public(); - let dave = Keyring::Dave.to_raw_public(); - let eve = Keyring::Eve.to_raw_public(); - let ferdie = Keyring::Ferdie.to_raw_public(); - let one = Keyring::One.to_raw_public(); - - map![ - twox_128(b"ses:len").to_vec() => vec![].and(&1u64), - twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => alice.to_vec(), - twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => bob.to_vec(), - twox_128(&2u32.to_keyed_vec(b"ses:val:")).to_vec() => charlie.to_vec(), - twox_128(b"sta:wil:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"sta:wil:")).to_vec() => alice.to_vec(), - twox_128(&1u32.to_keyed_vec(b"sta:wil:")).to_vec() => bob.to_vec(), - twox_128(&2u32.to_keyed_vec(b"sta:wil:")).to_vec() => charlie.to_vec(), - twox_128(&alice.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&10u64), - twox_128(&bob.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&20u64), - twox_128(&charlie.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&30u64), - twox_128(&dave.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&40u64), - twox_128(&eve.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&50u64), - twox_128(&ferdie.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&60u64), - twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&1u64), - twox_128(b"sta:tot").to_vec() => vec![].and(&210u64), - twox_128(b"sta:spe").to_vec() => vec![].and(&1u64), - twox_128(b"sta:vac").to_vec() => vec![].and(&3u64), - twox_128(b"sta:era").to_vec() => vec![].and(&1u64), + use runtime_io::{twox_128, TestExternalities}; + use codec::Joiner; + use runtime::democracy; + pub fn externalities() -> TestExternalities { + let extras: TestExternalities = map![ twox_128(CANDIDACY_BOND).to_vec() => vec![].and(&9u64), twox_128(VOTING_BOND).to_vec() => vec![].and(&3u64), twox_128(PRESENT_SLASH_PER_VOTER).to_vec() => vec![].and(&1u64), @@ -601,7 +569,24 @@ mod tests { twox_128(TERM_DURATION).to_vec() => vec![].and(&5u64), twox_128(DESIRED_SEATS).to_vec() => vec![].and(&2u64), twox_128(INACTIVE_GRACE_PERIOD).to_vec() => vec![].and(&1u32) - ] + ]; + democracy::testing::externalities() + .into_iter().chain(extras.into_iter()).collect() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use runtime_io::{with_externalities, twox_128, TestExternalities}; + use codec::{KeyedVec, Joiner}; + use keyring::Keyring; + use environment::with_env; + use demo_primitives::{AccountId, Proposal}; + use runtime::{staking, session, democracy}; + + fn new_test_ext() -> TestExternalities { + testing::externalities() } #[test] diff --git a/substrate/demo/runtime/src/runtime/democracy.rs b/substrate/demo/runtime/src/runtime/democracy.rs index af265e79790..36965153498 100644 --- a/substrate/demo/runtime/src/runtime/democracy.rs +++ b/substrate/demo/runtime/src/runtime/democracy.rs @@ -70,19 +70,24 @@ impl VoteThreshold { } // public proposals -const PUBLIC_PROP_COUNT: &[u8] = b"dem:ppc"; // PropIndex -const PUBLIC_PROPS: &[u8] = b"dem:pub"; // Vec<(PropIndex, Proposal)> -const DEPOSIT_OF: &[u8] = b"dem:dep:"; // PropIndex -> (Balance, Vec<AccountId>) -const LAUNCH_PERIOD: &[u8] = b"dem:lau"; // BlockNumber -const MINIMUM_DEPOSIT: &[u8] = b"dem:min"; // Balance +pub const PUBLIC_PROP_COUNT: &[u8] = b"dem:ppc"; // PropIndex +pub const PUBLIC_PROPS: &[u8] = b"dem:pub"; // Vec<(PropIndex, Proposal)> +pub const DEPOSIT_OF: &[u8] = b"dem:dep:"; // PropIndex -> (Balance, Vec<AccountId>) +pub const LAUNCH_PERIOD: &[u8] = b"dem:lau"; // BlockNumber +pub const MINIMUM_DEPOSIT: &[u8] = b"dem:min"; // Balance + +// council proposals +pub const COUNCIL_PROPOSAL: &[u8] = b"dem:cou:pro"; // (BlockNumber, Proposal) +pub const COUNCIL_VOTE_OF: &[u8] = b"dem:cou:vot:"; // AccountId -> CouncilVote +pub const COUNCIL_VOTERS: &[u8] = b"dem:cou:vts"; // Vec<AccountId> // referenda -const VOTING_PERIOD: &[u8] = b"dem:per"; // BlockNumber -const REFERENDUM_COUNT: &[u8] = b"dem:rco"; // ReferendumIndex -const NEXT_TALLY: &[u8] = b"dem:nxt"; // ReferendumIndex -const REFERENDUM_INFO_OF: &[u8] = b"dem:pro:"; // ReferendumIndex -> (BlockNumber, Proposal, VoteThreshold) -const VOTERS_FOR: &[u8] = b"dem:vtr:"; // ReferendumIndex -> Vec<AccountId> -const VOTE_OF: &[u8] = b"dem:vot:"; // (ReferendumIndex, AccountId) -> bool +pub const VOTING_PERIOD: &[u8] = b"dem:per"; // BlockNumber +pub const REFERENDUM_COUNT: &[u8] = b"dem:rco"; // ReferendumIndex +pub const NEXT_TALLY: &[u8] = b"dem:nxt"; // ReferendumIndex +pub const REFERENDUM_INFO_OF: &[u8] = b"dem:pro:"; // ReferendumIndex -> (BlockNumber, Proposal, VoteThreshold) +pub const VOTERS_FOR: &[u8] = b"dem:vtr:"; // ReferendumIndex -> Vec<AccountId> +pub const VOTE_OF: &[u8] = b"dem:vot:"; // (ReferendumIndex, AccountId) -> bool /// The minimum amount to be used as a deposit for a public referendum proposal. pub fn minimum_deposit() -> Balance { @@ -289,16 +294,14 @@ fn inject_referendum( } #[cfg(test)] -mod tests { +pub mod testing { use super::*; - use runtime_io::{with_externalities, twox_128, TestExternalities}; - use codec::{KeyedVec, Joiner}; + use runtime_io::{twox_128, TestExternalities}; + use codec::Joiner; use keyring::Keyring; - use environment::with_env; - use demo_primitives::{AccountId, Proposal}; - use runtime::{staking, session, democracy}; + use runtime::{session, staking}; - fn new_test_ext() -> TestExternalities { + pub fn externalities() -> TestExternalities { let alice = Keyring::Alice.to_raw_public(); let bob = Keyring::Bob.to_raw_public(); let charlie = Keyring::Charlie.to_raw_public(); @@ -308,31 +311,47 @@ mod tests { let one = Keyring::One.to_raw_public(); map![ - twox_128(b"ses:len").to_vec() => vec![].and(&1u64), - twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => alice.to_vec(), - twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => bob.to_vec(), - twox_128(&2u32.to_keyed_vec(b"ses:val:")).to_vec() => charlie.to_vec(), - twox_128(b"sta:wil:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"sta:wil:")).to_vec() => alice.to_vec(), - twox_128(&1u32.to_keyed_vec(b"sta:wil:")).to_vec() => bob.to_vec(), - twox_128(&2u32.to_keyed_vec(b"sta:wil:")).to_vec() => charlie.to_vec(), - twox_128(&alice.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&10u64), - twox_128(&bob.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&20u64), - twox_128(&charlie.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&30u64), - twox_128(&dave.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&40u64), - twox_128(&eve.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&50u64), - twox_128(&ferdie.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&60u64), - twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].and(&1u64), - twox_128(b"sta:tot").to_vec() => vec![].and(&210u64), - twox_128(b"sta:spe").to_vec() => vec![].and(&1u64), - twox_128(b"sta:vac").to_vec() => vec![].and(&3u64), - twox_128(b"sta:era").to_vec() => vec![].and(&1u64), + 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_vec(), + twox_128(&1u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => bob.to_vec(), + twox_128(&2u32.to_keyed_vec(session::VALIDATOR_AT)).to_vec() => charlie.to_vec(), + twox_128(staking::INTENTION_COUNT).to_vec() => vec![].and(&3u32), + twox_128(&0u32.to_keyed_vec(staking::INTENTION_AT)).to_vec() => alice.to_vec(), + twox_128(&1u32.to_keyed_vec(staking::INTENTION_AT)).to_vec() => bob.to_vec(), + twox_128(&2u32.to_keyed_vec(staking::INTENTION_AT)).to_vec() => charlie.to_vec(), + twox_128(&alice.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&10u64), + twox_128(&bob.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&20u64), + twox_128(&charlie.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&30u64), + twox_128(&dave.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&40u64), + twox_128(&eve.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&50u64), + twox_128(&ferdie.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&60u64), + twox_128(&one.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![].and(&1u64), + twox_128(staking::TOTAL_STAKE).to_vec() => vec![].and(&210u64), + twox_128(staking::SESSIONS_PER_ERA).to_vec() => vec![].and(&1u64), + twox_128(staking::VALIDATOR_COUNT).to_vec() => vec![].and(&3u64), + twox_128(staking::CURRENT_ERA).to_vec() => vec![].and(&1u64), + twox_128(LAUNCH_PERIOD).to_vec() => vec![].and(&1u64), twox_128(VOTING_PERIOD).to_vec() => vec![].and(&1u64), twox_128(MINIMUM_DEPOSIT).to_vec() => vec![].and(&1u64) ] } +} + +#[cfg(test)] +mod tests { + use super::*; + use runtime_io::{with_externalities, twox_128, TestExternalities}; + use codec::{KeyedVec, Joiner}; + use keyring::Keyring; + use environment::with_env; + use demo_primitives::{AccountId, Proposal}; + use runtime::{staking, session, democracy}; + + fn new_test_ext() -> TestExternalities { + testing::externalities() + } #[test] fn params_should_work() { diff --git a/substrate/demo/runtime/src/runtime/governance.rs b/substrate/demo/runtime/src/runtime/governance.rs index 0db372c4814..4689941de4c 100644 --- a/substrate/demo/runtime/src/runtime/governance.rs +++ b/substrate/demo/runtime/src/runtime/governance.rs @@ -32,9 +32,9 @@ use demo_primitives::{Proposal, AccountId, Hash, BlockNumber}; use runtime::{staking, system, session}; use dispatch::enact_proposal; -const APPROVALS_REQUIRED: &[u8] = b"gov:apr"; -const CURRENT_PROPOSAL: &[u8] = b"gov:pro"; -const APPROVAL_OF: &[u8] = b"gov:app:"; +pub const APPROVALS_REQUIRED: &[u8] = b"gov:apr"; +pub const CURRENT_PROPOSAL: &[u8] = b"gov:pro"; +pub const APPROVAL_OF: &[u8] = b"gov:app:"; /// The proportion of validators required for a propsal to be approved measured as the number out /// of 1000. @@ -113,6 +113,21 @@ pub mod internal { } } +#[cfg(test)] +pub mod testing { + use super::*; + use runtime_io::{twox_128, TestExternalities}; + use codec::Joiner; + + pub fn externalities(session_length: u64, sessions_per_era: u64, current_era: u64) -> TestExternalities { + let extras: TestExternalities = map![ + twox_128(APPROVALS_REQUIRED).to_vec() => vec![].and(&667u32) + ]; + staking::testing::externalities(session_length, sessions_per_era, current_era) + .into_iter().chain(extras.into_iter()).collect() + } +} + #[cfg(test)] mod tests { use super::*; @@ -124,25 +139,7 @@ mod tests { use runtime::{staking, session}; fn new_test_ext() -> TestExternalities { - let one = Keyring::One.to_raw_public(); - let two = Keyring::Two.to_raw_public(); - let three = [3u8; 32]; - - map![ - twox_128(APPROVALS_REQUIRED).to_vec() => vec![].and(&667u32), - twox_128(b"ses:len").to_vec() => vec![].and(&1u64), - twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => one.to_vec(), - twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => two.to_vec(), - twox_128(&2u32.to_keyed_vec(b"ses:val:")).to_vec() => three.to_vec(), - twox_128(b"sta:wil:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"sta:wil:")).to_vec() => one.to_vec(), - twox_128(&1u32.to_keyed_vec(b"sta:wil:")).to_vec() => two.to_vec(), - twox_128(&2u32.to_keyed_vec(b"sta:wil:")).to_vec() => three.to_vec(), - twox_128(b"sta:spe").to_vec() => vec![].and(&1u64), - twox_128(b"sta:vac").to_vec() => vec![].and(&3u64), - twox_128(b"sta:era").to_vec() => vec![].and(&1u64) - ] + testing::externalities(1, 1, 1) } #[test] diff --git a/substrate/demo/runtime/src/runtime/session.rs b/substrate/demo/runtime/src/runtime/session.rs index b80b5f4e8cc..193274b5682 100644 --- a/substrate/demo/runtime/src/runtime/session.rs +++ b/substrate/demo/runtime/src/runtime/session.rs @@ -23,16 +23,18 @@ use runtime_support::{storage, StorageVec}; use demo_primitives::{AccountId, SessionKey, BlockNumber}; use runtime::{system, staking, consensus}; -const SESSION_LENGTH: &[u8] = b"ses:len"; -const CURRENT_INDEX: &[u8] = b"ses:ind"; -const LAST_LENGTH_CHANGE: &[u8] = b"ses:llc"; -const NEXT_KEY_FOR: &[u8] = b"ses:nxt:"; -const NEXT_SESSION_LENGTH: &[u8] = b"ses:nln"; +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] = b"ses:val:"; + const PREFIX: &'static[u8] = VALIDATOR_AT; } /// Get the current set of validators. @@ -131,6 +133,30 @@ fn rotate_session() { }); } +#[cfg(test)] +pub mod testing { + use super::*; + use runtime_io::{twox_128, TestExternalities}; + use codec::{Joiner, KeyedVec}; + 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() + ]; + system::testing::externalities().into_iter().chain(extras.into_iter()).collect() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/substrate/demo/runtime/src/runtime/staking.rs b/substrate/demo/runtime/src/runtime/staking.rs index 9acdbb1205f..6d357a52dac 100644 --- a/substrate/demo/runtime/src/runtime/staking.rs +++ b/substrate/demo/runtime/src/runtime/staking.rs @@ -31,25 +31,27 @@ pub type Balance = u64; /// The amount of bonding period left in an account. Measured in eras. pub type Bondage = u64; -struct IntentionStorageVec {} +pub const BONDING_DURATION: &[u8] = b"sta:loc"; +pub const VALIDATOR_COUNT: &[u8] = b"sta:vac"; +pub const SESSIONS_PER_ERA: &[u8] = b"sta:spe"; +pub const NEXT_SESSIONS_PER_ERA: &[u8] = b"sta:nse"; +pub const CURRENT_ERA: &[u8] = b"sta:era"; +pub const LAST_ERA_LENGTH_CHANGE: &[u8] = b"sta:lec"; +pub const TOTAL_STAKE: &[u8] = b"sta:tot"; +pub const INTENTION_AT: &[u8] = b"sta:wil:"; +pub const INTENTION_COUNT: &[u8] = b"sta:wil:len"; + +pub const BALANCE_OF: &[u8] = b"sta:bal:"; +pub const BONDAGE_OF: &[u8] = b"sta:bon:"; +pub const CODE_OF: &[u8] = b"sta:cod:"; +pub const STORAGE_OF: &[u8] = b"sta:sto:"; + +pub struct IntentionStorageVec {} impl StorageVec for IntentionStorageVec { type Item = AccountId; - const PREFIX: &'static[u8] = b"sta:wil:"; + const PREFIX: &'static[u8] = INTENTION_AT; } -const BONDING_DURATION: &[u8] = b"sta:loc"; -const VALIDATOR_COUNT: &[u8] = b"sta:vac"; -const SESSIONS_PER_ERA: &[u8] = b"sta:spe"; -const NEXT_SESSIONS_PER_ERA: &[u8] = b"sta:nse"; -const CURRENT_ERA: &[u8] = b"sta:era"; -const LAST_ERA_LENGTH_CHANGE: &[u8] = b"sta:lec"; -const TOTAL_STAKE: &[u8] = b"sta:tot"; - -const BALANCE_OF: &[u8] = b"sta:bal:"; -const BONDAGE_OF: &[u8] = b"sta:bon:"; -const CODE_OF: &[u8] = b"sta:cod:"; -const STORAGE_OF: &[u8] = b"sta:sto:"; - /// The length of the bonding duration in eras. pub fn bonding_duration() -> BlockNumber { storage::get_or_default(BONDING_DURATION) @@ -434,6 +436,33 @@ fn new_era() { ); } +#[cfg(test)] +pub mod testing { + use super::*; + use runtime_io::{twox_128, TestExternalities}; + use codec::{Joiner, KeyedVec}; + use keyring::Keyring; + use runtime::session; + + pub fn externalities(session_length: u64, sessions_per_era: u64, current_era: 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(INTENTION_COUNT).to_vec() => vec![].and(&3u32), + twox_128(&0u32.to_keyed_vec(INTENTION_AT)).to_vec() => one.to_vec(), + twox_128(&1u32.to_keyed_vec(INTENTION_AT)).to_vec() => two.to_vec(), + twox_128(&2u32.to_keyed_vec(INTENTION_AT)).to_vec() => three.to_vec(), + twox_128(SESSIONS_PER_ERA).to_vec() => vec![].and(&sessions_per_era), + twox_128(VALIDATOR_COUNT).to_vec() => vec![].and(&3u64), + twox_128(CURRENT_ERA).to_vec() => vec![].and(¤t_era), + twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] + ]; + session::testing::externalities(session_length).into_iter().chain(extras.into_iter()).collect() + } +} + #[cfg(test)] mod tests { use super::*; @@ -456,10 +485,10 @@ mod tests { let four = [4u8; 32]; let mut t: TestExternalities = map![ - twox_128(b"ses:len").to_vec() => vec![].and(&1u64), - twox_128(b"ses:val:len").to_vec() => vec![].and(&2u32), - twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![10; 32], - twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![20; 32], + 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(SESSIONS_PER_ERA).to_vec() => vec![].and(&2u64), twox_128(VALIDATOR_COUNT).to_vec() => vec![].and(&2u32), twox_128(BONDING_DURATION).to_vec() => vec![].and(&3u64), @@ -526,7 +555,7 @@ mod tests { #[test] fn staking_eras_work() { let mut t: TestExternalities = map![ - twox_128(b"ses:len").to_vec() => vec![].and(&1u64), + twox_128(session::SESSION_LENGTH).to_vec() => vec![].and(&1u64), twox_128(SESSIONS_PER_ERA).to_vec() => vec![].and(&2u64) ]; with_externalities(&mut t, || { diff --git a/substrate/demo/runtime/src/runtime/system.rs b/substrate/demo/runtime/src/runtime/system.rs index b9b546f2f86..009dac82390 100644 --- a/substrate/demo/runtime/src/runtime/system.rs +++ b/substrate/demo/runtime/src/runtime/system.rs @@ -27,9 +27,9 @@ use demo_primitives::{AccountId, Hash, TxOrder, BlockNumber, Block, Header, UncheckedTransaction, Function, Log}; use runtime::{staking, session}; -const NONCE_OF: &[u8] = b"sys:non:"; -const BLOCK_HASH_AT: &[u8] = b"sys:old:"; -const CODE: &[u8] = b"sys:cod"; +pub const NONCE_OF: &[u8] = b"sys:non:"; +pub const BLOCK_HASH_AT: &[u8] = b"sys:old:"; +pub const CODE: &[u8] = b"sys:cod"; /// The current block number being processed. Set by `execute_block`. pub fn block_number() -> BlockNumber { @@ -229,6 +229,19 @@ fn info_expect_equal_hash(given: &Hash, expected: &Hash) { } } +#[cfg(test)] +pub mod testing { + use super::*; + use runtime_io::{twox_128, TestExternalities}; + use codec::Joiner; + + pub fn externalities() -> TestExternalities { + map![ + twox_128(&0u64.to_keyed_vec(BLOCK_HASH_AT)).to_vec() => [69u8; 32].encode() + ] + } +} + #[cfg(test)] mod tests { use super::*; @@ -240,7 +253,7 @@ mod tests { use environment::with_env; use primitives::hexdisplay::HexDisplay; use demo_primitives::{Header, Digest, UncheckedTransaction, Transaction, Function}; - use runtime::staking; + use runtime::{governance, staking}; #[test] fn staking_balance_transfer_dispatch_works() { @@ -248,7 +261,7 @@ mod tests { let two = Keyring::Two.to_raw_public(); let mut t: TestExternalities = map![ - twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] + twox_128(&one.to_keyed_vec(staking::BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] ]; let tx = UncheckedTransaction { @@ -268,27 +281,7 @@ mod tests { } fn new_test_ext() -> TestExternalities { - let one = Keyring::One.to_raw_public(); - let two = Keyring::Two.to_raw_public(); - let three = [3u8; 32]; - - map![ - twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => [69u8; 32].encode(), - twox_128(b"gov:apr").to_vec() => vec![].and(&667u32), - twox_128(b"ses:len").to_vec() => vec![].and(&2u64), - twox_128(b"ses:val:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => one.to_vec(), - twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => two.to_vec(), - twox_128(&2u32.to_keyed_vec(b"ses:val:")).to_vec() => three.to_vec(), - twox_128(b"sta:wil:len").to_vec() => vec![].and(&3u32), - twox_128(&0u32.to_keyed_vec(b"sta:wil:")).to_vec() => one.to_vec(), - twox_128(&1u32.to_keyed_vec(b"sta:wil:")).to_vec() => two.to_vec(), - twox_128(&2u32.to_keyed_vec(b"sta:wil:")).to_vec() => three.to_vec(), - twox_128(b"sta:spe").to_vec() => vec![].and(&2u64), - twox_128(b"sta:vac").to_vec() => vec![].and(&3u64), - twox_128(b"sta:era").to_vec() => vec![].and(&0u64), - twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] - ] + governance::testing::externalities(2, 2, 0) } #[test] diff --git a/substrate/demo/runtime/src/runtime/timestamp.rs b/substrate/demo/runtime/src/runtime/timestamp.rs index 89b49b53617..52b6cbe2d7d 100644 --- a/substrate/demo/runtime/src/runtime/timestamp.rs +++ b/substrate/demo/runtime/src/runtime/timestamp.rs @@ -20,7 +20,7 @@ use runtime_support::storage; pub type Timestamp = u64; -const CURRENT_TIMESTAMP: &[u8] = b"tim:val"; +pub const CURRENT_TIMESTAMP: &[u8] = b"tim:val"; /// Get the current time. pub fn get() -> Timestamp { -- GitLab