Browse Source

chainspec-builder: take initial-forum-path arg

Mokhtar Naamani 4 years ago
parent
commit
3abd0e05dd

File diff suppressed because it is too large
+ 0 - 0
node/res/forum_nicaea_encoded.json


+ 9 - 2
node/src/chain_spec.rs

@@ -38,7 +38,7 @@ use node_runtime::{
 };
 
 // Exported to be used by chain-spec-builder
-pub use node_runtime::{membership, AccountId, GenesisConfig, Moment};
+pub use node_runtime::{membership, AccountId, ForumConfig, GenesisConfig, Moment};
 
 type AccountPublic = <Signature as Verify>::Signer;
 
@@ -129,6 +129,9 @@ impl Alternative {
                         ],
                         crate::proposals_config::development(),
                         crate::initial_members::none(),
+                        crate::forum_config::empty(get_account_id_from_seed::<sr25519::Public>(
+                            "Alice",
+                        )),
                     )
                 },
                 Vec::new(),
@@ -164,6 +167,9 @@ impl Alternative {
                         ],
                         crate::proposals_config::development(),
                         crate::initial_members::none(),
+                        crate::forum_config::empty(get_account_id_from_seed::<sr25519::Public>(
+                            "Alice",
+                        )),
                     )
                 },
                 Vec::new(),
@@ -206,6 +212,7 @@ pub fn testnet_genesis(
     endowed_accounts: Vec<AccountId>,
     cpcp: node_runtime::ProposalsConfigParameters,
     members: Vec<membership::genesis::Member<u64, AccountId, Moment>>,
+    forum_config: ForumConfig,
 ) -> GenesisConfig {
     const CENTS: Balance = 1;
     const DOLLARS: Balance = 100 * CENTS;
@@ -282,7 +289,7 @@ pub fn testnet_genesis(
             default_paid_membership_fee: 100u128,
             members,
         }),
-        forum: Some(crate::forum_config::create(root_key)),
+        forum: Some(forum_config),
         data_object_type_registry: Some(DataObjectTypeRegistryConfig {
             first_data_object_type_id: 1,
         }),

+ 19 - 5
node/src/forum_config.rs

@@ -5,6 +5,7 @@ use node_runtime::{
     AccountId, BlockNumber, ForumConfig, Moment, PostId, ThreadId,
 };
 use serde::Deserialize;
+use std::{fs, path::Path};
 
 fn new_validation(min: u16, max_min_diff: u16) -> InputValidationLengthConstraint {
     InputValidationLengthConstraint { min, max_min_diff }
@@ -39,13 +40,26 @@ fn decode_thread(encoded: String) -> Thread<BlockNumber, Moment, AccountId, Thre
     Decode::decode(&mut encoded.as_slice()).unwrap()
 }
 
-fn parse_forum_json() -> serde_json::Result<ForumData> {
-    let data = include_str!("../res/forum_nicaea_encoded.json");
-    serde_json::from_str(data)
+fn parse_forum_json(data_file: &Path) -> ForumData {
+    let data = fs::read_to_string(data_file).expect("Failed reading file");
+    serde_json::from_str(&data).expect("failed parsing members data")
 }
 
-pub fn create(forum_sudo: AccountId) -> ForumConfig {
-    let forum_data = parse_forum_json().expect("failed loading forum data");
+pub fn from_json(forum_sudo: AccountId, data_file: &Path) -> ForumConfig {
+    let forum_data = parse_forum_json(data_file);
+    create(forum_sudo, forum_data)
+}
+
+pub fn empty(forum_sudo: AccountId) -> ForumConfig {
+    let forum_data = ForumData {
+        categories: vec![],
+        threads: vec![],
+        posts: vec![],
+    };
+    create(forum_sudo, forum_data)
+}
+
+fn create(forum_sudo: AccountId, forum_data: ForumData) -> ForumConfig {
     let first_id = 1;
 
     let next_category_id: CategoryId = forum_data.categories.last().map_or(first_id, |category| {

+ 52 - 16
utils/chain-spec-builder/src/main.rs

@@ -24,8 +24,8 @@ use rand::{distributions::Alphanumeric, rngs::OsRng, Rng};
 use structopt::StructOpt;
 
 use joystream_node::{
-    chain_spec::{self, chain_spec_properties, membership, AccountId, Moment},
-    initial_members, proposals_config,
+    chain_spec::{self, chain_spec_properties, AccountId},
+    forum_config, initial_members, proposals_config,
 };
 use sc_chain_spec::ChainType;
 use sc_keystore::Store as Keystore;
@@ -61,6 +61,9 @@ enum ChainSpecBuilder {
         /// The path to an initial members data
         #[structopt(long, short)]
         initial_members_path: Option<PathBuf>,
+        /// The path to an initial forum data
+        #[structopt(long, short)]
+        initial_forum_path: Option<PathBuf>,
     },
     /// Create a new chain spec with the given number of authorities and endowed
     /// accounts. Random keys will be generated as required.
@@ -84,6 +87,9 @@ enum ChainSpecBuilder {
         /// The path to an initial members data
         #[structopt(long, short)]
         initial_members_path: Option<PathBuf>,
+        /// The path to an initial forum data
+        #[structopt(long, short)]
+        initial_forum_path: Option<PathBuf>,
     },
 }
 
@@ -113,13 +119,26 @@ impl ChainSpecBuilder {
             } => initial_members_path,
         }
     }
+
+    /// Returns the path where the chain spec should be saved.
+    fn initial_forum_path(&self) -> &Option<PathBuf> {
+        match self {
+            ChainSpecBuilder::New {
+                initial_forum_path, ..
+            } => initial_forum_path,
+            ChainSpecBuilder::Generate {
+                initial_forum_path, ..
+            } => initial_forum_path,
+        }
+    }
 }
 
 fn genesis_constructor(
     authority_seeds: &[String],
     endowed_accounts: &[AccountId],
     sudo_account: &AccountId,
-    genesis_members: &Vec<membership::genesis::Member<u64, AccountId, Moment>>,
+    initial_members_path: &Option<PathBuf>,
+    initial_forum_path: &Option<PathBuf>,
 ) -> chain_spec::GenesisConfig {
     let authorities = authority_seeds
         .iter()
@@ -127,12 +146,25 @@ fn genesis_constructor(
         .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 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())
+    };
+
     chain_spec::testnet_genesis(
         authorities,
         sudo_account.clone(),
         endowed_accounts.to_vec(),
         proposals_config::default(),
-        genesis_members.clone(),
+        members,
+        forum_cfg,
     )
 }
 
@@ -140,7 +172,8 @@ fn generate_chain_spec(
     authority_seeds: Vec<String>,
     endowed_accounts: Vec<String>,
     sudo_account: String,
-    genesis_members: Vec<membership::genesis::Member<u64, AccountId, Moment>>,
+    initial_members_path: Option<PathBuf>,
+    initial_forum_path: Option<PathBuf>,
 ) -> Result<String, String> {
     let parse_account = |address: &String| {
         AccountId::from_string(address)
@@ -170,7 +203,8 @@ fn generate_chain_spec(
                 &authority_seeds,
                 &endowed_accounts,
                 &sudo_account,
-                &genesis_members,
+                &initial_members_path,
+                &initial_forum_path,
             )
         },
         vec![],
@@ -242,13 +276,8 @@ fn main() -> Result<(), String> {
 
     let builder = ChainSpecBuilder::from_args();
     let chain_spec_path = builder.chain_spec_path().to_path_buf();
-    let initial_members_path = builder.initial_members_path();
-
-    let members = if let Some(path) = initial_members_path {
-        initial_members::from_json(path.as_path())
-    } else {
-        initial_members::none()
-    };
+    let initial_members_path = builder.initial_members_path().clone();
+    let initial_forum_path = builder.initial_forum_path().clone();
 
     let (authority_seeds, endowed_accounts, sudo_account) = match builder {
         ChainSpecBuilder::Generate {
@@ -277,8 +306,9 @@ fn main() -> Result<(), String> {
                 })
                 .collect();
 
-            let sudo_account =
-                chain_spec::get_account_id_from_seed::<sr25519::Public>(&sudo_seed).to_ss58check();
+            let sudo_account_id =
+                chain_spec::get_account_id_from_seed::<sr25519::Public>(&sudo_seed);
+            let sudo_account = sudo_account_id.clone().to_ss58check();
 
             (authority_seeds, endowed_accounts, sudo_account)
         }
@@ -290,7 +320,13 @@ fn main() -> Result<(), String> {
         } => (authority_seeds, endowed_accounts, sudo_account),
     };
 
-    let json = generate_chain_spec(authority_seeds, endowed_accounts, sudo_account, members)?;
+    let json = generate_chain_spec(
+        authority_seeds,
+        endowed_accounts,
+        sudo_account,
+        initial_members_path,
+        initial_forum_path,
+    )?;
 
     fs::write(chain_spec_path, json).map_err(|err| err.to_string())
 }

Some files were not shown because too many files changed in this diff