Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Add Session & Staking pallets
Browse files Browse the repository at this point in the history
  • Loading branch information
danforbes committed May 9, 2022
1 parent b6e58ad commit d7a0900
Show file tree
Hide file tree
Showing 6 changed files with 631 additions and 56 deletions.
150 changes: 114 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "gssmr-test-node"

[dependencies]
clap = { version = "3.1.6", features = ["derive"] }
rand = "0.8"

sc-cli = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.20", features = ["wasmtime"] }
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.20" }
Expand Down
53 changes: 46 additions & 7 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gssmr_test_runtime::{
AccountId, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig, Signature, SudoConfig,
SystemConfig, WASM_BINARY,
AccountId, BabeConfig, Balance, BalancesConfig, DOLLARS, GenesisConfig, GrandpaConfig, MaxNominations, SessionConfig,
SessionKeys, Signature, StakingConfig, StakerStatus, SudoConfig, SystemConfig, WASM_BINARY,
};
use sc_service::ChainType;
use sp_consensus_babe::AuthorityId as BabeId;
Expand Down Expand Up @@ -32,8 +32,8 @@ where
}

/// Generate an Aura authority key.
pub fn authority_keys_from_seed(s: &str) -> (BabeId, GrandpaId) {
(get_from_seed::<BabeId>(s), get_from_seed::<GrandpaId>(s))
pub fn authority_keys_from_seed(s: &str) -> (AccountId, BabeId, GrandpaId) {
(get_account_id_from_seed::<sr25519::Public>(s), get_from_seed::<BabeId>(s), get_from_seed::<GrandpaId>(s))
}

pub fn development_config() -> Result<ChainSpec, String> {
Expand Down Expand Up @@ -129,11 +129,30 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(BabeId, GrandpaId)>,
initial_authorities: Vec<(AccountId, BabeId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
) -> GenesisConfig {
let mut rng = rand::thread_rng();
const ENDOWMENT: Balance = 10_000_000 * DOLLARS;
const STASH: Balance = ENDOWMENT / 1000;
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), STASH, StakerStatus::Validator))
.chain(vec![].iter().map(|x: &AccountId| {
use rand::{seq::SliceRandom, Rng};
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
let count = rng.gen::<usize>() % limit;
let nominations = initial_authorities
.as_slice()
.choose_multiple(&mut rng, count)
.into_iter()
.map(|choice| choice.0.clone())
.collect::<Vec<_>>();
(x.clone(), x.clone(), STASH, StakerStatus::Nominator(nominations))
}))
.collect::<Vec<_>>();
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
Expand All @@ -144,11 +163,31 @@ fn testnet_genesis(
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},
babe: BabeConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone(), 1)).collect(),
authorities: vec![],
epoch_config: Some(gssmr_test_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
authorities: vec![],
},
session: SessionConfig {
keys: initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
SessionKeys { babe: x.1.clone(), grandpa: x.2.clone() },
)
})
.collect::<Vec<_>>(),
},
staking: StakingConfig {
validator_count: initial_authorities.len() as u32,
minimum_validator_count: initial_authorities.len() as u32,
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
slash_reward_fraction: sp_runtime::Perbill::from_percent(10),
stakers,
..Default::default()
},
sudo: SudoConfig {
// Assign network admin rights.
Expand Down
Loading

0 comments on commit d7a0900

Please sign in to comment.