From dd2996daf04d5d386fb7f2f8a7b69991c8d8085a Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Tue, 14 May 2024 17:44:48 -0400 Subject: [PATCH] Use correct set of endowed accounts in genesis (#834) * Split endowment functions into dev and external account functions * Use correct endowed accounts in chainspec genesis config * Add CI check for building the testnet chainspec * Push external and dev accounts directly to `balances` key * Format the iterator chain a bit better * Also check the other chainspecs Looks like I broke them * Temporarily stop piping stuff into `/dev/null` * Ensure that genesis balances are unique --- .circleci/then.yml | 15 ++++++ Cargo.lock | 1 + node/cli/Cargo.toml | 1 + node/cli/src/chain_spec/dev.rs | 15 +++++- node/cli/src/chain_spec/integration_tests.rs | 15 +++++- node/cli/src/chain_spec/testnet.rs | 21 +++++--- node/cli/src/endowed_accounts.rs | 57 ++++++++++---------- 7 files changed, 86 insertions(+), 39 deletions(-) diff --git a/.circleci/then.yml b/.circleci/then.yml index f26ce9a4d..42facbd9c 100644 --- a/.circleci/then.yml +++ b/.circleci/then.yml @@ -141,6 +141,18 @@ jobs: steps: - install-dependencies-and-checkout - run: cargo doc --no-deps + check-chainspecs: + machine: + image: ubuntu-2204:2022.10.2 + resource_class: xlarge + steps: + - install-dependencies-and-checkout + - run: + command: | + cargo run -p entropy -- build-spec --raw --chain dev > chainspec-dev-raw.json + cargo run -p entropy -- build-spec --raw --chain integration-tests > chainspec-integration-raw.json + cargo run -p entropy -- build-spec --raw --chain testnet > chainspec-testnet-raw.json + parameters: crates: @@ -172,6 +184,9 @@ workflows: - << pipeline.parameters.pallets >> - << pipeline.parameters.runtime >> - pipeline.parameters.crates + chainspecs: + jobs: + - check-chainspecs documentation: jobs: - check-doc-build diff --git a/Cargo.lock b/Cargo.lock index adfdaa252..90f3a8719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2369,6 +2369,7 @@ dependencies = [ "frame-system-rpc-runtime-api", "futures", "hex-literal", + "itertools 0.12.1", "jsonrpsee 0.20.3", "lazy_static", "log", diff --git a/node/cli/Cargo.toml b/node/cli/Cargo.toml index abb1980a6..0f84b4dbc 100644 --- a/node/cli/Cargo.toml +++ b/node/cli/Cargo.toml @@ -23,6 +23,7 @@ clap ={ version="4.0.9", features=["derive"], optional=true } codec ={ package="parity-scale-codec", version="3.0.0" } futures ="0.3.30" hex-literal ="0.4.1" +itertools ="0.12.1" jsonrpsee ={ version="0.20.3", features=["server"] } lazy_static ={ version="1.4.0", features=["spin_no_std"] } log ="0.4.21" diff --git a/node/cli/src/chain_spec/dev.rs b/node/cli/src/chain_spec/dev.rs index b054dc4de..581ceab5c 100644 --- a/node/cli/src/chain_spec/dev.rs +++ b/node/cli/src/chain_spec/dev.rs @@ -29,6 +29,7 @@ use entropy_shared::{ INITIAL_MAX_INSTRUCTIONS_PER_PROGRAM, }; use grandpa_primitives::AuthorityId as GrandpaId; +use itertools::Itertools; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use sc_service::ChainType; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; @@ -92,7 +93,11 @@ pub fn development_genesis_config( root_key: AccountId, threshold_server_endpoints: Vec<&str>, ) -> serde_json::Value { - let (mut endowed_accounts, funded_accounts) = endowed_accounts_dev(false); + // Note that any endowed_accounts added here will be included in the `elections` and + // `technical_committee` genesis configs. If you don't want that, don't push those accounts to + // this list. + let mut endowed_accounts = vec![]; + // endow all authorities and nominators. initial_authorities.iter().map(|x| &x.0).chain(initial_nominators.iter()).for_each(|x| { if !endowed_accounts.contains(x) { @@ -125,7 +130,13 @@ pub fn development_genesis_config( serde_json::json!({ "balances": BalancesConfig { - balances: funded_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(), + balances: endowed_accounts + .iter() + .chain(endowed_accounts_dev().iter()) + .cloned() + .map(|x| (x, ENDOWMENT)) + .unique() + .collect(), }, "indices": IndicesConfig { indices: vec![] }, "session": SessionConfig { diff --git a/node/cli/src/chain_spec/integration_tests.rs b/node/cli/src/chain_spec/integration_tests.rs index d31123be5..556f0fad6 100644 --- a/node/cli/src/chain_spec/integration_tests.rs +++ b/node/cli/src/chain_spec/integration_tests.rs @@ -29,6 +29,7 @@ use entropy_shared::{ INITIAL_MAX_INSTRUCTIONS_PER_PROGRAM, }; use grandpa_primitives::AuthorityId as GrandpaId; +use itertools::Itertools; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use sc_service::ChainType; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; @@ -71,7 +72,11 @@ pub fn integration_tests_genesis_config( initial_nominators: Vec, root_key: AccountId, ) -> serde_json::Value { - let (mut endowed_accounts, funded_accounts) = endowed_accounts_dev(false); + // Note that any endowed_accounts added here will be included in the `elections` and + // `technical_committee` genesis configs. If you don't want that, don't push those accounts to + // this list. + let mut endowed_accounts = vec![]; + // endow all authorities and nominators. initial_authorities.iter().map(|x| &x.0).chain(initial_nominators.iter()).for_each(|x| { if !endowed_accounts.contains(x) { @@ -104,7 +109,13 @@ pub fn integration_tests_genesis_config( serde_json::json!( { "balances": BalancesConfig { - balances: funded_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(), + balances: endowed_accounts + .iter() + .chain(endowed_accounts_dev().iter()) + .cloned() + .map(|x| (x, ENDOWMENT)) + .unique() + .collect(), }, "indices": IndicesConfig { indices: vec![] }, "session": SessionConfig { diff --git a/node/cli/src/chain_spec/testnet.rs b/node/cli/src/chain_spec/testnet.rs index 093f3af4a..e6e2fb1aa 100644 --- a/node/cli/src/chain_spec/testnet.rs +++ b/node/cli/src/chain_spec/testnet.rs @@ -14,7 +14,7 @@ // along with this program. If not, see . use crate::chain_spec::{get_account_id_from_seed, ChainSpec}; -use crate::endowed_accounts::endowed_accounts_dev; +use crate::endowed_accounts::endowed_testnet_accounts; use entropy_runtime::{ constants::currency::*, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig, @@ -29,6 +29,7 @@ use entropy_shared::{ }; use grandpa_primitives::AuthorityId as GrandpaId; use hex_literal::hex; +use itertools::Itertools; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use sc_service::ChainType; use sc_telemetry::TelemetryEndpoints; @@ -298,7 +299,10 @@ pub fn testnet_genesis_config( "Each validator node needs to have an accompanying threshold server." ); - let (mut endowed_accounts, mut funded_accounts) = endowed_accounts_dev(true); + // Note that any endowed_accounts added here will be included in the `elections` and + // `technical_committee` genesis configs. If you don't want that, don't push those accounts to + // this list. + let mut endowed_accounts = vec![]; // Ensure that the `testnet-local` config doesn't have a duplicate balance since `Alice` is // both a validator and root. @@ -306,10 +310,6 @@ pub fn testnet_genesis_config( endowed_accounts.push(root_key.clone()); } - if !funded_accounts.contains(&root_key) { - funded_accounts.push(root_key.clone()); - } - // We endow the: // - Initial TSS server accounts // - Initial the validator stash accounts @@ -359,9 +359,14 @@ pub fn testnet_genesis_config( const SIGNING_GROUPS: usize = 2; serde_json::json!( { - "balances": BalancesConfig { - balances: funded_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(), + balances: endowed_accounts + .iter() + .chain(endowed_testnet_accounts().iter()) + .cloned() + .map(|x| (x, ENDOWMENT)) + .unique() + .collect(), }, "indices": IndicesConfig { indices: vec![] }, "session": SessionConfig { diff --git a/node/cli/src/endowed_accounts.rs b/node/cli/src/endowed_accounts.rs index e0d17618c..ebd58528f 100644 --- a/node/cli/src/endowed_accounts.rs +++ b/node/cli/src/endowed_accounts.rs @@ -27,7 +27,9 @@ pub struct AddressStruct { name: String, } -pub fn endowed_accounts_dev(is_prod: bool) -> (Vec, Vec) { +/// These are accounts which are populated from an external source, with the intention of them +/// being funded an ready to use in a `testnet` configuration. +pub fn endowed_testnet_accounts() -> Vec { // handle user submitted file for tokens let mut externally_endowed_accounts: Vec = Vec::new(); let project_root = get_project_root(); @@ -40,37 +42,38 @@ pub fn endowed_accounts_dev(is_prod: bool) -> (Vec, Vec) { serde_json::from_str(&data).expect("JSON parse error"); externally_endowed_accounts.append(&mut incoming_accounts) }; - let mut inital_accounts = vec![]; - if !is_prod { - inital_accounts = vec![ - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Bob"), - get_account_id_from_seed::("Charlie"), - get_account_id_from_seed::("Dave"), - get_account_id_from_seed::("Eve"), - get_account_id_from_seed::("Ferdie"), - get_account_id_from_seed::("One"), - get_account_id_from_seed::("Two"), - get_account_id_from_seed::("Alice//stash"), - get_account_id_from_seed::("Bob//stash"), - get_account_id_from_seed::("Charlie//stash"), - get_account_id_from_seed::("Dave//stash"), - get_account_id_from_seed::("Eve//stash"), - get_account_id_from_seed::("Ferdie//stash"), - get_account_id_from_seed::("One//stash"), - get_account_id_from_seed::("Two//stash"), - crate::chain_spec::tss_account_id::ALICE.clone(), - crate::chain_spec::tss_account_id::BOB.clone(), - crate::chain_spec::tss_account_id::CHARLIE.clone(), - ]; - } - let mut funded_accounts = inital_accounts.clone(); + let mut funded_accounts = vec![]; for address in externally_endowed_accounts { funded_accounts.push(AccountId::from_string(&address.address).unwrap_or_else(|_| { panic!("failed to convert a testnet_address address: {:?}", address) })) } - (inital_accounts, funded_accounts) + funded_accounts +} + +/// Development accounts which correspond to our usual cast of characters (e.g `//Alice`, `//Bob`). +pub fn endowed_accounts_dev() -> Vec { + vec![ + get_account_id_from_seed::("Alice"), + get_account_id_from_seed::("Bob"), + get_account_id_from_seed::("Charlie"), + get_account_id_from_seed::("Dave"), + get_account_id_from_seed::("Eve"), + get_account_id_from_seed::("Ferdie"), + get_account_id_from_seed::("One"), + get_account_id_from_seed::("Two"), + get_account_id_from_seed::("Alice//stash"), + get_account_id_from_seed::("Bob//stash"), + get_account_id_from_seed::("Charlie//stash"), + get_account_id_from_seed::("Dave//stash"), + get_account_id_from_seed::("Eve//stash"), + get_account_id_from_seed::("Ferdie//stash"), + get_account_id_from_seed::("One//stash"), + get_account_id_from_seed::("Two//stash"), + crate::chain_spec::tss_account_id::ALICE.clone(), + crate::chain_spec::tss_account_id::BOB.clone(), + crate::chain_spec::tss_account_id::CHARLIE.clone(), + ] }