Genesis map.

- fix warning
- remove more bare string ids
This commit is contained in:
Gav
2018-03-07 12:16:12 +01:00
parent 19424ce2ce
commit 3ad3288508
10 changed files with 116 additions and 96 deletions
+14 -1
View File
@@ -77,7 +77,20 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
session_length: 720, // that's 1 hour per session.
sessions_per_era: 24, // 24 hours per era.
bonding_duration: 90, // 90 days per bond.
approval_ratio: 667, // 66.7% approvals required for legislation.
launch_period: 120 * 24 * 14, // 2 weeks per public referendum
voting_period: 120 * 24 * 28, // 4 weeks to discuss & vote on an active referendum
minimum_deposit: 1000, // 1000 as the minimum deposit for a referendum
candidacy_bond: 1000, // 1000 to become a council candidate
voter_bond: 100, // 100 down to vote for a candidate
present_slash_per_voter: 1, // slash by 1 per voter for an invalid presentation.
carry_count: 24, // carry over the 24 runners-up to the next council election
presentation_duration: 120 * 24, // one day for presenting winners.
council_election_voting_period: 7 * 120 * 24, // one week period between possible council elections.
council_term_duration: 180 * 120 * 24, // 180 day term duration for the council.
desired_seats: 0, // start with no council: we'll raise this once the stake has been dispersed a bit.
inactive_grace_period: 1, // one addition vote should go by before an inactive voter can be reaped.
cooloff_period: 90 * 120 * 24, // 90 day cooling off period if council member vetoes a proposal.
council_proposal_voting_period: 7 * 120 * 24, // 7 day voting period for council members.
};
let prepare_genesis = || {
storage = genesis_config.genesis_map();
+33 -65
View File
@@ -39,9 +39,9 @@ mod tests {
use super::Executor;
use substrate_executor::WasmExecutor;
use codec::{KeyedVec, Slicable, Joiner};
use keyring::Keyring;
use keyring::Keyring::{self, One, Two};
use runtime_support::Hashable;
use demo_runtime::runtime::staking::balance;
use demo_runtime::runtime::staking::{self, balance, BALANCE_OF};
use state_machine::{CodeExecutor, TestExternalities};
use primitives::twox_128;
use demo_primitives::{Hash, Header, BlockNumber, Block, Digest, Transaction,
@@ -60,9 +60,9 @@ mod tests {
fn tx() -> UncheckedTransaction {
let transaction = Transaction {
signed: Keyring::One.to_raw_public(),
signed: One.into(),
nonce: 0,
function: Function::StakingTransfer(Keyring::Two.to_raw_public(), 69),
function: Function::StakingTransfer(Two.into(), 69),
};
let signature = Keyring::from_raw_public(transaction.signed).unwrap()
.sign(&transaction.encode());
@@ -72,9 +72,8 @@ mod tests {
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&One.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
];
let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
@@ -83,9 +82,8 @@ mod tests {
#[test]
fn panic_execution_with_native_equivalent_code_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&One.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
];
let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
@@ -94,62 +92,36 @@ mod tests {
#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let one = Keyring::One.to_raw_public();
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_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
];
let r = Executor::new().call(&mut t, COMPACT_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_ok());
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&one), 42);
assert_eq!(balance(&two), 69);
assert_eq!(balance(&One), 42);
assert_eq!(balance(&Two), 69);
});
}
#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let one = Keyring::One.to_raw_public();
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_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
];
let r = Executor::new().call(&mut t, BLOATY_CODE, "execute_transaction", &vec![].and(&Header::from_block_number(1u64)).and(&tx()));
assert!(r.is_ok());
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&one), 42);
assert_eq!(balance(&two), 69);
assert_eq!(balance(&One), 42);
assert_eq!(balance(&Two), 69);
});
}
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]
]
staking::testing::externalities(2, 2, 0)
}
fn construct_block(number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
@@ -180,11 +152,11 @@ mod tests {
construct_block(
1,
[69u8; 32].into(),
hex!("2481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29db").into(),
hex!("6f0202ee141932f5cf9efad76d9200ec0ae98e2782335c5c7940be6a66bb9418").into(),
vec![Transaction {
signed: Keyring::One.to_raw_public(),
signed: One.into(),
nonce: 0,
function: Function::StakingTransfer(Keyring::Two.to_raw_public(), 69),
function: Function::StakingTransfer(Two.into(), 69),
}]
)
}
@@ -193,17 +165,17 @@ mod tests {
construct_block(
2,
block1().1,
hex!("1feb4d3a2e587079e6ce1685fa79994efd995e33cb289d39cded67aac1bb46a9").into(),
hex!("04a2ca0ff5cdbe6a0ceb75009b4e707c8d052f28c95e9a7751ea814b13b95c92").into(),
vec![
Transaction {
signed: Keyring::Two.to_raw_public(),
signed: Two.into(),
nonce: 0,
function: Function::StakingTransfer(Keyring::One.to_raw_public(), 5),
function: Function::StakingTransfer(One.into(), 5),
},
Transaction {
signed: Keyring::One.to_raw_public(),
signed: One.into(),
nonce: 1,
function: Function::StakingTransfer(Keyring::Two.to_raw_public(), 15),
function: Function::StakingTransfer(Two.into(), 15),
}
]
)
@@ -216,15 +188,15 @@ mod tests {
Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Keyring::One.to_raw_public()), 42);
assert_eq!(balance(&Keyring::Two.to_raw_public()), 69);
assert_eq!(balance(&One), 42);
assert_eq!(balance(&Two), 69);
});
Executor::new().call(&mut t, COMPACT_CODE, "execute_block", &block2().0).unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Keyring::One.to_raw_public()), 32);
assert_eq!(balance(&Keyring::Two.to_raw_public()), 79);
assert_eq!(balance(&One), 32);
assert_eq!(balance(&Two), 79);
});
}
@@ -235,23 +207,22 @@ mod tests {
WasmExecutor.call(&mut t, COMPACT_CODE, "execute_block", &block1().0).unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Keyring::One.to_raw_public()), 42);
assert_eq!(balance(&Keyring::Two.to_raw_public()), 69);
assert_eq!(balance(&One), 42);
assert_eq!(balance(&Two), 69);
});
WasmExecutor.call(&mut t, COMPACT_CODE, "execute_block", &block2().0).unwrap();
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&Keyring::One.to_raw_public()), 32);
assert_eq!(balance(&Keyring::Two.to_raw_public()), 79);
assert_eq!(balance(&One), 32);
assert_eq!(balance(&Two), 79);
});
}
#[test]
fn panic_execution_gives_error() {
let one = Keyring::One.to_raw_public();
let mut t: TestExternalities = map![
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
twox_128(&One.to_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![68u8, 0, 0, 0, 0, 0, 0, 0]
];
let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
@@ -261,11 +232,8 @@ mod tests {
#[test]
fn successful_execution_gives_ok() {
let one = Keyring::One.to_raw_public();
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_raw_public().to_keyed_vec(BALANCE_OF)).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
];
let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm");
@@ -273,8 +241,8 @@ mod tests {
assert!(r.is_ok());
runtime_io::with_externalities(&mut t, || {
assert_eq!(balance(&one), 42);
assert_eq!(balance(&two), 69);
assert_eq!(balance(&One), 42);
assert_eq!(balance(&Two), 69);
});
}
}
+1 -3
View File
@@ -12,12 +12,10 @@ substrate-runtime-std = { path = "../../substrate/runtime-std" }
substrate-runtime-io = { path = "../../substrate/runtime-io" }
substrate-runtime-support = { path = "../../substrate/runtime-support" }
substrate-primitives = { path = "../../substrate/primitives" }
substrate-keyring = { path = "../../substrate/keyring" }
demo-primitives = { path = "../primitives" }
integer-sqrt = "0.1.0"
[dev-dependencies]
substrate-keyring = { path = "../../substrate/keyring" }
[features]
default = ["std"]
std = [
+60 -20
View File
@@ -23,6 +23,7 @@ use runtime_support::Hashable;
use primitives::Block;
use demo_primitives::{BlockNumber, AccountId};
use runtime::staking::Balance;
use runtime::{staking, session, consensus, system, democracy, council, council_vote};
/// Configuration of a general Substrate Demo genesis block.
pub struct GenesisConfig {
@@ -33,7 +34,20 @@ pub struct GenesisConfig {
pub session_length: BlockNumber,
pub sessions_per_era: BlockNumber,
pub bonding_duration: BlockNumber,
pub approval_ratio: u32,
pub launch_period: BlockNumber,
pub voting_period: BlockNumber,
pub minimum_deposit: Balance,
pub candidacy_bond: Balance,
pub voter_bond: Balance,
pub present_slash_per_voter: Balance,
pub carry_count: u32,
pub presentation_duration: BlockNumber,
pub council_election_voting_period: BlockNumber,
pub council_term_duration: BlockNumber,
pub desired_seats: u32,
pub inactive_grace_period: BlockNumber,
pub cooloff_period: BlockNumber,
pub council_proposal_voting_period: BlockNumber,
}
impl GenesisConfig {
@@ -42,43 +56,69 @@ impl GenesisConfig {
validators: authorities_validators.clone(),
authorities: authorities_validators.clone(),
balances: authorities_validators.iter().map(|v| (v.clone(), balance)).collect(),
block_time: 5, // 5 second block time.
session_length: 720, // that's 1 hour per session.
block_time: 30, // 30 second block time.
session_length: 120, // that's 1 hour per session.
sessions_per_era: 24, // 24 hours per era.
bonding_duration: 90, // 90 days per bond.
approval_ratio: 667, // 66.7% approvals required for legislation.
launch_period: 120 * 24 * 14, // 2 weeks per public referendum
voting_period: 120 * 24 * 28, // 4 weeks to discuss & vote on an active referendum
minimum_deposit: 1000, // 1000 as the minimum deposit for a referendum
candidacy_bond: 1000, // 1000 to become a council candidate
voter_bond: 100, // 100 down to vote for a candidate
present_slash_per_voter: 1, // slash by 1 per voter for an invalid presentation.
carry_count: 24, // carry over the 24 runners-up to the next council election
presentation_duration: 120 * 24, // one day for presenting winners.
council_election_voting_period: 7 * 120 * 24, // one week period between possible council elections.
council_term_duration: 180 * 120 * 24, // 180 day term duration for the council.
desired_seats: 0, // start with no council: we'll raise this once the stake has been dispersed a bit.
inactive_grace_period: 1, // one addition vote should go by before an inactive voter can be reaped.
cooloff_period: 90 * 120 * 24, // 90 day cooling off period if council member vetoes a proposal.
council_proposal_voting_period: 7 * 120 * 24, // 7 day voting period for council members.
}
}
pub fn genesis_map(&self) -> HashMap<Vec<u8>, Vec<u8>> {
let wasm_runtime = include_bytes!("../wasm/genesis.wasm").to_vec();
vec![
(&b"gov:apr"[..], vec![].and(&self.approval_ratio)),
(&b"ses:len"[..], vec![].and(&self.session_length)),
(&b"ses:val:len"[..], vec![].and(&(self.validators.len() as u32))),
(&b"sta:wil:len"[..], vec![].and(&0u32)),
(&b"sta:spe"[..], vec![].and(&self.sessions_per_era)),
(&b"sta:vac"[..], vec![].and(&(self.validators.len() as u32))),
(&b"sta:era"[..], vec![].and(&0u64)),
(&session::SESSION_LENGTH[..], vec![].and(&self.session_length)),
(&session::VALIDATOR_COUNT[..], vec![].and(&(self.validators.len() as u32))),
(&staking::INTENTION_COUNT[..], vec![].and(&0u32)),
(&staking::SESSIONS_PER_ERA[..], vec![].and(&self.sessions_per_era)),
(&staking::CURRENT_ERA[..], vec![].and(&0u64)),
(&democracy::LAUNCH_PERIOD[..], vec![].and(&self.launch_period)),
(&democracy::VOTING_PERIOD[..], vec![].and(&self.voting_period)),
(&democracy::MINIMUM_DEPOSIT[..], vec![].and(&self.minimum_deposit)),
(&council::CANDIDACY_BOND[..], vec![].and(&self.candidacy_bond)),
(&council::VOTING_BOND[..], vec![].and(&self.voter_bond)),
(&council::PRESENT_SLASH_PER_VOTER[..], vec![].and(&self.present_slash_per_voter)),
(&council::CARRY_COUNT[..], vec![].and(&self.carry_count)),
(&council::PRESENTATION_DURATION[..], vec![].and(&self.presentation_duration)),
(&council::VOTING_PERIOD[..], vec![].and(&self.council_election_voting_period)),
(&council::TERM_DURATION[..], vec![].and(&self.council_term_duration)),
(&council::DESIRED_SEATS[..], vec![].and(&self.desired_seats)),
(&council::INACTIVE_GRACE_PERIOD[..], vec![].and(&self.inactive_grace_period)),
(&council_vote::COOLOFF_PERIOD[..], vec![].and(&self.cooloff_period)),
(&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(b"ses:val:"), vec![].and(account)))
).chain(self.authorities.iter()
.enumerate()
.map(|(i, account)| ((i as u32).to_keyed_vec(b":auth:"), vec![].and(account)))
.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(b"sta:bal:"), vec![].and(&balance)))
.map(|&(account, balance)| (account.to_keyed_vec(staking::BALANCE_OF), vec![].and(&balance)))
)
.map(|(k, v)| (twox_128(&k[..])[..].to_vec(), v.to_vec()))
.chain(vec![
(b":code"[..].into(), wasm_runtime),
(b":auth:len"[..].into(), vec![].and(&(self.authorities.len() as u32))),
(system::CODE[..].into(), wasm_runtime),
(consensus::AUTHORITY_COUNT[..].into(), vec![].and(&(self.authorities.len() as u32))),
].into_iter())
.chain(self.authorities.iter()
.enumerate()
.map(|(i, account)| ((i as u32).to_keyed_vec(b":auth:"), vec![].and(account)))
.map(|(i, account)| ((i as u32).to_keyed_vec(consensus::AUTHORITY_AT), vec![].and(account)))
)
.collect()
}
@@ -87,6 +127,6 @@ impl GenesisConfig {
pub fn additional_storage_with_genesis(genesis_block: &Block) -> HashMap<Vec<u8>, Vec<u8>> {
use codec::Slicable;
map![
twox_128(&0u64.to_keyed_vec(b"sys:old:")).to_vec() => genesis_block.header.blake2_256().encode()
twox_128(&0u64.to_keyed_vec(system::BLOCK_HASH_AT)).to_vec() => genesis_block.header.blake2_256().encode()
]
}
+2 -2
View File
@@ -18,10 +18,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use] extern crate substrate_runtime_std as rstd;
#[allow(unused_imports)] #[macro_use] extern crate substrate_runtime_std as rstd;
#[macro_use] extern crate substrate_runtime_io as runtime_io;
extern crate substrate_runtime_support as runtime_support;
#[cfg(all(feature = "std", test))] extern crate substrate_keyring as keyring;
#[cfg(any(feature = "std", test))] extern crate substrate_keyring as keyring;
#[cfg(feature = "std")] extern crate rustc_hex;
@@ -21,6 +21,7 @@ use runtime_support::storage::unhashed::StorageVec;
use demo_primitives::SessionKey;
pub const AUTHORITY_AT: &'static[u8] = b":auth:";
pub const AUTHORITY_COUNT: &'static[u8] = b":auth:len";
struct AuthorityStorageVec {}
impl StorageVec for AuthorityStorageVec {
@@ -560,11 +560,11 @@ pub mod testing {
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),
twox_128(CARRY_COUNT).to_vec() => vec![].and(&2u64),
twox_128(CARRY_COUNT).to_vec() => vec![].and(&2u32),
twox_128(PRESENTATION_DURATION).to_vec() => vec![].and(&2u64),
twox_128(VOTING_PERIOD).to_vec() => vec![].and(&4u64),
twox_128(TERM_DURATION).to_vec() => vec![].and(&5u64),
twox_128(DESIRED_SEATS).to_vec() => vec![].and(&2u64),
twox_128(DESIRED_SEATS).to_vec() => vec![].and(&2u32),
twox_128(INACTIVE_GRACE_PERIOD).to_vec() => vec![].and(&1u32)
];
democracy::testing::externalities()
@@ -133,7 +133,7 @@ fn rotate_session() {
});
}
#[cfg(test)]
#[cfg(any(feature = "std", test))]
pub mod testing {
use super::*;
use runtime_io::{twox_128, TestExternalities};
@@ -518,7 +518,7 @@ fn new_era() {
);
}
#[cfg(test)]
#[cfg(any(feature = "std", test))]
pub mod testing {
use super::*;
use runtime_io::{twox_128, TestExternalities};
+1 -1
View File
@@ -223,7 +223,7 @@ fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
}
}
#[cfg(test)]
#[cfg(any(feature = "std", test))]
pub mod testing {
use super::*;
use runtime_io::{twox_128, TestExternalities};