use pallet_staking::Forcing; use sp_staking::StakerStatus; use casper_runtime_constants::currency::{STRH, STNK, CSPR}; use primitives::{AccountId, AccountPublic}; use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId; use babe_primitives::AuthorityId as BabeId; use grandpa_primitives::AuthorityId as GrandpaId; use hex_literal::hex; use sp_core::crypto::UncheckedFrom; use sp_core::{sr25519, H256, Pair, Public}; use sp_runtime::{traits::IdentifyAccount, Perbill}; #[cfg(not(feature = "std"))] use sp_std::alloc::format; use sp_std::prelude::*; use sp_std::vec::Vec; use crate::{Balance, NetworkId}; use ghost_weaver::sr25519::AuthorityId as WeaverId; use ghost_exodus::sr25519::AuthorityId as ExodusId; use ghost_helpers::networks::{NetworkClaim, NetworkClaimBuilder, NetworkInitiation}; #[derive(Clone)] pub struct AuthorityInitiation { stash: AccountId, account: AccountId, babe_id: BabeId, grandpa_id: GrandpaId, authority_discovery_id: AuthorityDiscoveryId, weaver_id: WeaverId, exodus_id: ExodusId, } impl AuthorityInitiation { pub fn casper_session_keys(self) -> crate::opaque::SessionKeys { crate::opaque::SessionKeys { babe: self.babe_id, grandpa: self.grandpa_id, authority_discovery: self.authority_discovery_id, weaver: self.weaver_id, exodus: self.exodus_id, } } } #[derive(Default)] struct AuthorityInitiationBuilder { stash: Option, account: Option, babe_id: Option, grandpa_id: Option, authority_discovery_id: Option, weaver_id: Option, exodus_id: Option, } impl AuthorityInitiationBuilder { pub fn with_stash(mut self, stash: AccountId) -> Self { self.stash = Some(stash); self } pub fn with_account(mut self, account: AccountId) -> Self { self.account = Some(account); self } pub fn with_babe_id(mut self, babe_id: BabeId) -> Self { self.babe_id = Some(babe_id); self } pub fn with_grandpa_id(mut self, grandpa_id: GrandpaId) -> Self { self.grandpa_id = Some(grandpa_id); self } pub fn with_authority_id(mut self, authority_id: AuthorityDiscoveryId,) -> Self { self.authority_discovery_id = Some(authority_id); self } pub fn with_weaver_id(mut self, weaver_id: WeaverId) -> Self { self.weaver_id = Some(weaver_id); self } pub fn with_exodus_id(mut self, exodus_id: ExodusId) -> Self { self.exodus_id = Some(exodus_id); self } pub fn build(self) -> AuthorityInitiation { AuthorityInitiation { stash: self.stash.expect("No stash key provided during authority initiation."), account: self.account.expect("No account key provided during authority initiation."), babe_id: self.babe_id.expect("No babe key provided during authority initiation."), grandpa_id: self.grandpa_id.expect("No gran key provided during authority initiation."), authority_discovery_id: self.authority_discovery_id.expect("No audi key provided during authority initiation."), weaver_id: self.weaver_id.expect("No weav key provided during authority initiation."), exodus_id: self.exodus_id.expect("No exds key provided during authority initiation."), } } } pub struct AccountInitiation { account: AccountId, balance: u128, } #[derive(Default)] struct AccountInitiationBuilder { account: Option, balance: u128, } impl AccountInitiationBuilder { pub fn with_account(mut self, account: AccountId) -> Self { self.account = Some(account); self } pub fn with_balance(mut self, balance: u128) -> Self { self.balance = balance; self } pub fn build(self) -> AccountInitiation { AccountInitiation { balance: self.balance, account: self.account .expect("No account key provided during account initiation."), } } } fn get_from_seed(seed: &str) -> ::Public { TPublic::Pair::from_string(&format!("//{}", seed), None) .expect("static values are valid; qed") .public() } fn get_account_id_from_seed(seed: &str) -> AccountId where AccountPublic: From<::Public>, { AccountPublic::from(get_from_seed::(seed)).into_account() } fn get_authority_keys_from_seed( seed: &str, ) -> AuthorityInitiation { let stash = get_account_id_from_seed::(&format!("{}//stash", seed)); let account = get_account_id_from_seed::(seed); let babe_id = get_from_seed::(seed); let weaver_id = get_from_seed::(seed); let exodus_id = get_from_seed::(seed); let grandpa_id = get_from_seed::(seed); let authority_id = get_from_seed::(seed); AuthorityInitiationBuilder::default() .with_stash(stash) .with_account(account) .with_babe_id(babe_id) .with_weaver_id(weaver_id) .with_exodus_id(exodus_id) .with_grandpa_id(grandpa_id) .with_authority_id(authority_id) .build() } fn casper_staging_initial_authorities() -> Vec { vec![ // sfErNwRgZ6ypB7wY8M2smXMZjxqUkc2TgUcNvC1JNQJFXS8bw AuthorityInitiationBuilder::default() .with_stash(AccountId::new(hex!("507045c82be367f95408466cd054ca39bfa52697a3ef22809af14cf9de304f02"))) .with_account(AccountId::new(hex!("328d3b7c3046ef7700937d99fb2e98ce2591682c2b5dcf3f562e4da157650237"))) .with_babe_id(BabeId::unchecked_from(hex!("daaaaab6a6e574099e24ae9bb75b543610edef9d374fa85a378edb573b47615f"))) .with_grandpa_id(GrandpaId::unchecked_from(hex!("55446f9a7aa99ced06b317c80ce90d56b84e56526775683af2525969e8da0b64"))) .with_authority_id(AuthorityDiscoveryId::unchecked_from(hex!("12c14850562021eb99f58f90ab624fb6cfaf3ac9228a92f8b60115fe6a6af15a"))) .with_weaver_id(WeaverId::unchecked_from(hex!("681cbbab1f95d0ee4183a8c60081380b59920e1a9189d872c8fc8b38dcdd020b"))) .with_exodus_id(ExodusId::unchecked_from(hex!("7277c176a3eb110ea2e259d09994c1bce303ca028b8daccc5b10d8d8f1baea40"))) .build(), // sfHcJxw5cgkvukZZyxcNUMCdbm9e7773orByLrGgAREka81TK AuthorityInitiationBuilder::default() .with_stash(AccountId::new(hex!("8a0d0b66e827bf20e79f9a499317e73925ce4f422371067edfab690e43857f13"))) .with_account(AccountId::new(hex!("ac871e8bab00dd56ba3a1c0bd289357203dcaf10010b0b04ad7472870cd22a3c"))) .with_babe_id(BabeId::unchecked_from(hex!("e0f0a776ecc9fa5e1f22e2fa001fe3fba5aea52b9444bc894b45589d42132475"))) .with_grandpa_id(GrandpaId::unchecked_from(hex!("b63c5a0cf342b9b04931bc8ed74d7d0165ab99ab5f8a4514797d4b299a4501fe"))) .with_authority_id(AuthorityDiscoveryId::unchecked_from(hex!("2cf69452e9f2a8457119139408884941ed50f590c0fc0f2b044c4d82c69e4245"))) .with_weaver_id(WeaverId::unchecked_from(hex!("22bc96d10ca7c04ba72a649db6b4c8f499744bb718d67b6fb7694f4bc2b46010"))) .with_exodus_id(ExodusId::unchecked_from(hex!("c20f8dbc1f86ae1520e6f9dc65239ec7e1e1e80eea7b5c1de2035a684e25ea48"))) .build(), // sfFD7KSRi2aSREJWc9X75sVTN4a5pbPM5VSSJefmPvMuiVbPr AuthorityInitiationBuilder::default() .with_stash(AccountId::new(hex!("6c0283f4c688f0e75ad546c790bbd5961c1a6931543aa589f368f8272c44b758"))) .with_account(AccountId::new(hex!("425ccd7bda4f5c76788ba23bc0381d7a2e496179c93301208c57501c80a4232a"))) .with_babe_id(BabeId::unchecked_from(hex!("74fa7381a7a74b316afb6793a00387eed9d95d46a69866cbb316b5d9c918af0e"))) .with_grandpa_id(GrandpaId::unchecked_from(hex!("236d2fa03f4ed8cb65de7e514d7540159b328f1c170dd402b094ad7fbf547218"))) .with_authority_id(AuthorityDiscoveryId::unchecked_from(hex!("00946618c353e4c6546b87f9ca1089b846b0ea4658ee8e6d9d1200c24cb5ee27"))) .with_weaver_id(WeaverId::unchecked_from(hex!("10ab42a4a2e41d7921ded8138f406b15a61cf98147c43f9cdd5dd0b4e854ca0f"))) .with_exodus_id(ExodusId::unchecked_from(hex!("76aba3841978e8a4c5f22172c0cd0c34bedee4ebfcc8d93e1e44201ee013081b"))) .build(), // sfH29zyFYtoFj6fSXskAEXG5XyyMCa5YycahJkYMwSUz8CcEU AuthorityInitiationBuilder::default() .with_stash(AccountId::new(hex!("b24feb55b2cac4b365a9245c2a97525b01bd1a594d2d42b91f6bc38c9c2e6517"))) .with_account(AccountId::new(hex!("927a98dcf8f721103005f168476c24b91d7d10d580f457006a908e10e62c7729"))) .with_babe_id(BabeId::unchecked_from(hex!("3c944c704cae203619b9e7a5a4b6742736da6a8e76c762291bebdc7652cfec2f"))) .with_grandpa_id(GrandpaId::unchecked_from(hex!("0aa3a88f6b777c95c3dfe7e997b76798413f16aa325f34824cae0c9102b281d5"))) .with_authority_id(AuthorityDiscoveryId::unchecked_from(hex!("a8e828d10cf7b74481b6e746e5532d4740ea8014a0d3d856540a59847f8a6b76"))) .with_weaver_id(WeaverId::unchecked_from(hex!("78efbd73e499bea76bf9bab1cb136fbe51aee389b1a8d151496e30bd55888429"))) .with_exodus_id(ExodusId::unchecked_from(hex!("b469187ddf8f7458d67262328e236a252bd2db9e2f87e8313e7e3b322aecdc3e"))) .build(), ] } fn casper_testnet_networks() -> Vec> { vec![] } pub fn testnet_config_genesis( initial_authorities: Vec, initial_accounts: Vec, initial_networks: Vec>, initial_claims: Vec>, ) -> serde_json::Value { const ENDOWMENT: u128 = 31 * CSPR; const STASH_BOND: u128 = 68 * CSPR; const STASH_FEES: u128 = 1 * CSPR; let sudo = initial_authorities.first().map(|x| x.account.clone()).unwrap(); serde_json::json!({ "balances": { "balances": initial_accounts .iter() .map(|x| (x.account.clone(), x.balance)) .chain(initial_authorities.iter().map(|x| (x.account.clone(), ENDOWMENT))) .chain(initial_authorities.iter().map(|x| (x.stash.clone(), STASH_BOND + STASH_FEES))) .collect::>(), }, "session": { "keys": initial_authorities .iter() .map(|x| { (x.stash.clone(), x.stash.clone(), x.clone().casper_session_keys()) }) .collect::>(), }, "staking": { "minNominatorBond": 42 * CSPR, "minValidatorBond": 6900 * STRH, "maxNominatorCount": 3000u32, "maxValidatorCount": 500u32, "minimumValidatorCount": if cfg!(feature = "runtime-benchmarks") { 1u32 } else { 4u32 }, "validatorCount": 37u32, "forceEra": Forcing::NotForcing, "slashRewardFraction": Perbill::from_percent(80), "stakers": initial_authorities .iter() .map(|x| ( x.stash.clone(), x.stash.clone(), STASH_BOND, StakerStatus::::Validator, )) .collect::>(), }, "babe": { "epochConfig": Some(crate::BABE_GENESIS_EPOCH_CONFIG) }, "ghostNominationPools": { "minJoinBond": 10 * STNK, "minCreateBond": 50 * CSPR, "maxPools": 256u32, "maxMembersPerPool": 1024u32, "maxMembers": 20000u32, "globalMaxCommission": Perbill::from_percent(15), }, "ghostSudo": { "key": sudo }, "ghostNetworks": { "networks": initial_networks }, "ghostGovernance": { "networkClaims": initial_claims }, "ghostWeaver": { "authorities": [], "loomStates": [] }, "ghostExodus": { "authorities": [] } }) } pub fn casper_development_config_genesis() -> serde_json::Value { let initial_authorities = vec![get_authority_keys_from_seed("Alice")]; testnet_config_genesis( initial_authorities, Default::default(), Default::default(), Default::default(), ) } pub fn casper_local_config_genesis() -> serde_json::Value { let initial_authorities = vec![ get_authority_keys_from_seed("Alice"), get_authority_keys_from_seed("Bob"), get_authority_keys_from_seed("Charlie"), get_authority_keys_from_seed("Dave"), ]; let initial_accounts = vec![ AccountInitiationBuilder::default() .with_account(get_account_id_from_seed::("Eve")) .with_balance(420 * CSPR) .build(), AccountInitiationBuilder::default() .with_account(get_account_id_from_seed::("Ferdie")) .with_balance(1337 * CSPR) .build(), ]; testnet_config_genesis( initial_authorities, initial_accounts, casper_testnet_networks(), Default::default(), ) } pub fn casper_staging_config_genesis() -> serde_json::Value { let initial_preclaims = vec![ NetworkClaimBuilder::default() .with_amount(1922500000000000000000u128.into()) .with_network_id(11155111) .with_merkle_root(H256::from_slice(&hex!("f8d8c981f214958cc2738bf068807c0f504f0e9a1d61ba5c8cd01bc7f9cc6d26"))) .with_proof_size(8) .build(), NetworkClaimBuilder::default() .with_amount(18550200000000000000000u128.into()) .with_network_id(7001) .with_merkle_root(H256::from_slice(&hex!("793b300540c6530c24be7cc40b1ba91f0a586da8e180b98e91b055a9a12f45e2"))) .with_proof_size(11) .build(), ]; testnet_config_genesis( casper_staging_initial_authorities(), Default::default(), casper_testnet_networks(), initial_preclaims, ) } /// Provides the JSON representation of predefined genesis config for given `id`. pub fn get_preset(id: &sp_genesis_builder::PresetId) -> Option> { let patch = match id.try_into() { Ok("casper_development") => casper_development_config_genesis(), Ok("casper_local_testnest") => casper_local_config_genesis(), Ok("casper_staging_testnet") => casper_staging_config_genesis(), _ => return None, }; Some( serde_json::to_string(&patch) .expect("serialization to json is expected to work; qed") .into_bytes(), ) } /// Returns a list of identifiers for available builtin `RuntimeGenesisConfig` presets. pub fn preset_names() -> Vec { Vec::from([ sp_genesis_builder::PresetId::from("casper_staging_testnet"), sp_genesis_builder::PresetId::from("casper_local_testnet"), sp_genesis_builder::PresetId::from("casper_development"), ]) }