Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions node/src/chain_spec/initial_balances.rs
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
}
9 changes: 9 additions & 0 deletions node/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub use node_runtime::{AccountId, GenesisConfig};

pub mod content_config;
pub mod forum_config;
pub mod initial_balances;
pub mod initial_members;
pub mod proposals_config;

Expand Down Expand Up @@ -138,6 +139,7 @@ impl Alternative {
content_config::empty_versioned_store_permissions_config(),
content_config::empty_data_directory_config(),
content_config::empty_content_working_group_config(),
vec![],
)
},
Vec::new(),
Expand Down Expand Up @@ -178,6 +180,7 @@ impl Alternative {
content_config::empty_versioned_store_permissions_config(),
content_config::empty_data_directory_config(),
content_config::empty_content_working_group_config(),
vec![],
)
},
Vec::new(),
Expand Down Expand Up @@ -222,6 +225,7 @@ pub fn testnet_genesis(
versioned_store_permissions_config: VersionedStorePermissionsConfig,
data_directory_config: DataDirectoryConfig,
content_working_group_config: ContentWorkingGroupConfig,
initial_balances: Vec<(AccountId, Balance)>,
) -> GenesisConfig {
const STASH: Balance = 5_000;
const ENDOWMENT: Balance = 100_000_000;
Expand All @@ -239,6 +243,11 @@ pub fn testnet_genesis(
.cloned()
.map(|k| (k, ENDOWMENT))
.chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
.chain(
initial_balances
.iter()
.map(|(account, balance)| (account.clone(), *balance)),
)
.collect(),
}),
pallet_staking: Some(StakingConfig {
Expand Down
39 changes: 37 additions & 2 deletions utils/chain-spec-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -191,6 +212,7 @@ impl ChainSpecBuilder {
}
}

Copy link
Copy Markdown
Contributor

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?

#[allow(clippy::too_many_arguments)]
fn genesis_constructor(
deployment: &ChainDeployment,
authority_seeds: &[String],
Expand All @@ -199,6 +221,7 @@ 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()
Expand Down Expand Up @@ -241,6 +264,12 @@ fn genesis_constructor(
)
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?
let initial_account_balances = initial_balances_path.map(|path| {initial_balances::from_json(path.as_path())}).unwrap_or_else(vec![]);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 = if let Some(path) = initial_balances_path {
initial_balances::from_json(path.as_path())
} else {
vec![]
};

let proposals_cfg = match deployment {
ChainDeployment::live => proposals_config::production(),
ChainDeployment::staging => proposals_config::staging(),
Expand All @@ -258,9 +287,11 @@ fn genesis_constructor(
versioned_store_permissions_cfg,
data_directory_config,
content_working_group_config,
initial_account_balances,
)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a small comment on why we disable the lint here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#[allow(clippy::too_many_arguments)]
fn generate_chain_spec(
deployment: ChainDeployment,
authority_seeds: Vec<String>,
Expand All @@ -269,6 +300,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)
Expand Down Expand Up @@ -302,6 +334,7 @@ fn generate_chain_spec(
&initial_members_path,
&initial_forum_path,
&initial_content_path,
&initial_balances_path,
)
},
vec![],
Expand Down Expand Up @@ -376,6 +409,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 {
Expand Down Expand Up @@ -427,6 +461,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())
Expand Down