-
Notifications
You must be signed in to change notification settings - Fork 117
Genesis initial balances #1231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Genesis initial balances #1231
Changes from all commits
fd2a936
f9fec10
956fe35
39ee2e2
441f4bd
7b9c2f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use node_runtime::{AccountId, Balance}; | ||
| use serde::Deserialize; | ||
| use std::{fs, path::Path}; | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct SerializedInitialBalances { | ||
| balances: Vec<(AccountId, Balance)>, | ||
| } | ||
|
|
||
| fn parse_json(data_file: &Path) -> SerializedInitialBalances { | ||
| let data = fs::read_to_string(data_file).expect("Failed reading file"); | ||
| serde_json::from_str(&data).expect("failed parsing balances data") | ||
| } | ||
|
|
||
| /// Deserializes initial balances from json file | ||
| pub fn from_json(data_file: &Path) -> Vec<(AccountId, Balance)> { | ||
| parse_json(data_file).balances | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,8 @@ use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; | |
| use structopt::StructOpt; | ||
|
|
||
| use joystream_node::chain_spec::{ | ||
| self, chain_spec_properties, content_config, forum_config, initial_members, proposals_config, | ||
| AccountId, | ||
| self, chain_spec_properties, content_config, forum_config, initial_balances, initial_members, | ||
| proposals_config, AccountId, | ||
| }; | ||
|
|
||
| use sc_chain_spec::ChainType; | ||
|
|
@@ -88,6 +88,9 @@ enum ChainSpecBuilder { | |
| /// The path to an initial content directory data file | ||
| #[structopt(long, short)] | ||
| initial_content_path: Option<PathBuf>, | ||
| /// The path to an initial balances file | ||
| #[structopt(long, short)] | ||
| initial_balances_path: Option<PathBuf>, | ||
| /// Deployment type: dev, local, staging, live | ||
| #[structopt(long, short, default_value = "live")] | ||
| deployment: String, | ||
|
|
@@ -120,6 +123,9 @@ enum ChainSpecBuilder { | |
| /// The path to an initial content directory data file | ||
| #[structopt(long, short)] | ||
| initial_content_path: Option<PathBuf>, | ||
| /// The path to an initial balances file | ||
| #[structopt(long, short)] | ||
| initial_balances_path: Option<PathBuf>, | ||
| /// Deployment type: dev, local, staging, live | ||
| #[structopt(long, short, default_value = "live")] | ||
| deployment: String, | ||
|
|
@@ -179,6 +185,21 @@ impl ChainSpecBuilder { | |
| } | ||
| } | ||
|
|
||
| /// Returns the path to load initial platform content from | ||
| fn initial_balances_path(&self) -> &Option<PathBuf> { | ||
| match self { | ||
| ChainSpecBuilder::New { | ||
| initial_balances_path, | ||
| .. | ||
| } => initial_balances_path, | ||
| ChainSpecBuilder::Generate { | ||
| initial_balances_path, | ||
| .. | ||
| } => initial_balances_path, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the chain deployment | ||
| fn chain_deployment(&self) -> ChainDeployment { | ||
| match self { | ||
| ChainSpecBuilder::New { deployment, .. } => deployment | ||
|
|
@@ -191,6 +212,9 @@ impl ChainSpecBuilder { | |
| } | ||
| } | ||
|
|
||
| // TODO: This method should be refactored after Alexandria to reduce number of arguments | ||
| // as more args will likely be needed | ||
| #[allow(clippy::too_many_arguments)] | ||
| fn genesis_constructor( | ||
| deployment: &ChainDeployment, | ||
| authority_seeds: &[String], | ||
|
|
@@ -199,24 +223,23 @@ fn genesis_constructor( | |
| initial_members_path: &Option<PathBuf>, | ||
| initial_forum_path: &Option<PathBuf>, | ||
| initial_content_path: &Option<PathBuf>, | ||
| initial_balances_path: &Option<PathBuf>, | ||
| ) -> chain_spec::GenesisConfig { | ||
| let authorities = authority_seeds | ||
| .iter() | ||
| .map(AsRef::as_ref) | ||
| .map(chain_spec::get_authority_keys_from_seed) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let members = if let Some(path) = initial_members_path { | ||
| initial_members::from_json(path.as_path()) | ||
| } else { | ||
| initial_members::none() | ||
| }; | ||
| let members = initial_members_path | ||
| .as_ref() | ||
| .map(|path| initial_members::from_json(path.as_path())) | ||
| .unwrap_or_else(initial_members::none); | ||
|
|
||
| let forum_cfg = if let Some(path) = initial_forum_path { | ||
| forum_config::from_json(sudo_account.clone(), path.as_path()) | ||
| } else { | ||
| forum_config::empty(sudo_account.clone()) | ||
| }; | ||
| let forum_cfg = initial_forum_path | ||
| .as_ref() | ||
| .map(|path| forum_config::from_json(sudo_account.clone(), path.as_path())) | ||
| .unwrap_or_else(|| forum_config::empty(sudo_account.clone())); | ||
|
|
||
| let ( | ||
| versioned_store_cfg, | ||
|
|
@@ -241,6 +264,11 @@ fn genesis_constructor( | |
| ) | ||
| }; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about functional style or you prefer the imperative style more?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied suggestion to other code in same function as well. |
||
| let initial_account_balances = initial_balances_path | ||
| .as_ref() | ||
| .map(|path| initial_balances::from_json(path.as_path())) | ||
| .unwrap_or_else(Vec::new); | ||
|
|
||
| let proposals_cfg = match deployment { | ||
| ChainDeployment::live => proposals_config::production(), | ||
| ChainDeployment::staging => proposals_config::staging(), | ||
|
|
@@ -258,9 +286,13 @@ fn genesis_constructor( | |
| versioned_store_permissions_cfg, | ||
| data_directory_config, | ||
| content_working_group_config, | ||
| initial_account_balances, | ||
| ) | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a small comment on why we disable the lint here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| // TODO: This method should be refactored after Alexandria to reduce number of arguments | ||
| // as more args will likely be needed | ||
| #[allow(clippy::too_many_arguments)] | ||
| fn generate_chain_spec( | ||
| deployment: ChainDeployment, | ||
| authority_seeds: Vec<String>, | ||
|
|
@@ -269,6 +301,7 @@ fn generate_chain_spec( | |
| initial_members_path: Option<PathBuf>, | ||
| initial_forum_path: Option<PathBuf>, | ||
| initial_content_path: Option<PathBuf>, | ||
| initial_balances_path: Option<PathBuf>, | ||
| ) -> Result<String, String> { | ||
| let parse_account = |address: &String| { | ||
| AccountId::from_string(address) | ||
|
|
@@ -302,6 +335,7 @@ fn generate_chain_spec( | |
| &initial_members_path, | ||
| &initial_forum_path, | ||
| &initial_content_path, | ||
| &initial_balances_path, | ||
| ) | ||
| }, | ||
| vec![], | ||
|
|
@@ -376,6 +410,7 @@ fn main() -> Result<(), String> { | |
| let initial_members_path = builder.initial_members_path().clone(); | ||
| let initial_forum_path = builder.initial_forum_path().clone(); | ||
| let initial_content_path = builder.initial_content_path().clone(); | ||
| let initial_balances_path = builder.initial_balances_path().clone(); | ||
| let deployment = builder.chain_deployment(); | ||
|
|
||
| let (authority_seeds, endowed_accounts, sudo_account) = match builder { | ||
|
|
@@ -427,6 +462,7 @@ fn main() -> Result<(), String> { | |
| initial_members_path, | ||
| initial_forum_path, | ||
| initial_content_path, | ||
| initial_balances_path, | ||
| )?; | ||
|
|
||
| fs::write(chain_spec_path, json).map_err(|err| err.to_string()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed you disabled this lint at least twice. Do you mind to convert it to module-scope lint with a comment?