From aa24667ceb0107c767359aacd4fd02b283317b4b Mon Sep 17 00:00:00 2001 From: Stepan Naumov Date: Thu, 6 Aug 2020 07:45:10 -0400 Subject: [PATCH] Genesis New Template cli command --- Cargo.lock | 11 ++++++++ forest/Cargo.toml | 3 +- forest/src/cli/genesis_cmd.rs | 50 +++++++++++++++++++++++++++++++++ forest/src/cli/mod.rs | 5 ++++ forest/src/subcommand.rs | 3 ++ types/Cargo.toml | 1 + types/src/genesis/mod.rs | 52 +++++++++++++++++++++++++++++++++++ types/src/lib.rs | 1 + 8 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 forest/src/cli/genesis_cmd.rs create mode 100644 types/src/genesis/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 881bdfbf11f7..6510efaa6e2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2173,6 +2173,7 @@ dependencies = [ "clock", "commcid", "filecoin-proofs-api", + "forest_address", "forest_bigint", "forest_cid", "forest_encoding", @@ -2361,6 +2362,7 @@ dependencies = [ "structopt", "surf", "utils", + "uuid", ] [[package]] @@ -7081,6 +7083,15 @@ dependencies = [ "toml", ] +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand 0.7.3", +] + [[package]] name = "vcpkg" version = "0.2.10" diff --git a/forest/Cargo.toml b/forest/Cargo.toml index 8b1700956bb4..b4c909a2f1f7 100644 --- a/forest/Cargo.toml +++ b/forest/Cargo.toml @@ -36,4 +36,5 @@ pbr = "1.0.3" pin-project-lite = "0.1" message_pool = { package = "message_pool", path = "../blockchain/message_pool" } wallet = {package = "key_management", path = "../key_management"} -jsonrpc-v2 = { version = "0.5.2", features = ["easy-errors", "macros"] } \ No newline at end of file +jsonrpc-v2 = { version = "0.5.2", features = ["easy-errors", "macros"] } +uuid = { version = "0.8.1", features = ["v4"] } \ No newline at end of file diff --git a/forest/src/cli/genesis_cmd.rs b/forest/src/cli/genesis_cmd.rs new file mode 100644 index 000000000000..342daa3c327f --- /dev/null +++ b/forest/src/cli/genesis_cmd.rs @@ -0,0 +1,50 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +use fil_types::genesis; +use std::fs::File; +use structopt::StructOpt; +use uuid::Uuid; + +#[derive(Debug, StructOpt)] +pub enum GenesisCommands { + /// Creates new genesis template + #[structopt(about = "Create new Genesis template")] + NewTemplate { + #[structopt(short, help = "Input a network name")] + network_name: Option, + #[structopt( + short, + default_value = "genesis.json", + help = "File path, i.e, './genesis.json'. This command WILL NOT create a directory if it does not exist." + )] + file_path: String, + }, +} + +impl GenesisCommands { + pub async fn run(&self) { + match self { + Self::NewTemplate { + network_name, + file_path, + } => { + let template = genesis::Template::new( + network_name + .as_ref() + .unwrap_or(&format!("localnet-{}", Uuid::new_v4().to_string())) + .to_string(), + ); + + match &File::create(file_path) { + Ok(file) => { + serde_json::to_writer_pretty(file, &template).unwrap(); + } + Err(err) => { + println!("Can not write to a file, error: {}", err); + } + } + } + } + } +} diff --git a/forest/src/cli/mod.rs b/forest/src/cli/mod.rs index f37ad621bdf0..ecedf3a0dd8d 100644 --- a/forest/src/cli/mod.rs +++ b/forest/src/cli/mod.rs @@ -5,11 +5,13 @@ mod chain_cmd; mod config; mod fetch_params_cmd; mod genesis; +mod genesis_cmd; pub(super) use self::chain_cmd::ChainCommands; pub use self::config::Config; pub(super) use self::fetch_params_cmd::FetchCommands; pub(super) use self::genesis::initialize_genesis; +pub(super) use self::genesis_cmd::GenesisCommands; use jsonrpc_v2::Error as JsonRpcError; use std::cell::RefCell; @@ -47,6 +49,9 @@ pub enum Subcommand { #[structopt(name = "chain", about = "Interact with Filecoin blockchain")] Chain(ChainCommands), + + #[structopt(name = "genesis", about = "Work with blockchain genesis")] + Genesis(GenesisCommands), } /// Daemon process command line options. diff --git a/forest/src/subcommand.rs b/forest/src/subcommand.rs index e894847ce581..a91e41b99d6a 100644 --- a/forest/src/subcommand.rs +++ b/forest/src/subcommand.rs @@ -12,5 +12,8 @@ pub(super) async fn process(command: Subcommand) { Subcommand::Chain(cmd) => { cmd.run().await; } + Subcommand::Genesis(cmd) => { + cmd.run().await; + } } } diff --git a/types/Cargo.toml b/types/Cargo.toml index 9da39435c74a..4737241c8a21 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" features = ["json"] [dependencies] +address = { package = "forest_address", path = "../vm/address" } serde = { version = "1.0", features = ["derive"] } commcid = { path = "../utils/commcid" } filecoin-proofs-api = "4.0.1" diff --git a/types/src/genesis/mod.rs b/types/src/genesis/mod.rs new file mode 100644 index 000000000000..8c79d777eb3a --- /dev/null +++ b/types/src/genesis/mod.rs @@ -0,0 +1,52 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +use super::SectorSize; +use address::Address; +use num_bigint::bigint_ser; +use serde::Serialize; +use vm::TokenAmount; + +#[derive(Serialize)] +enum ActorType { + // Account, +// MultiSig, +} + +#[derive(Serialize)] +pub struct Actor { + actor_type: ActorType, + #[serde(with = "bigint_ser")] + token_amount: TokenAmount, +} + +#[derive(Serialize)] +pub struct Miner { + owner: Address, + worker: Address, + peer_id: Vec, + + #[serde(with = "bigint_ser")] + market_balance: TokenAmount, + #[serde(with = "bigint_ser")] + power_balance: TokenAmount, + sector_size: SectorSize, +} + +#[derive(Serialize)] +pub struct Template { + accounts: Vec, + miners: Vec, + network_name: String, + // timestamp: SystemTime, +} + +impl Template { + pub fn new(network_name: String) -> Template { + Template { + accounts: Vec::new(), + miners: Vec::new(), + network_name, + } + } +} diff --git a/types/src/lib.rs b/types/src/lib.rs index 3c9ef91204cc..02ad2a4727aa 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -1,6 +1,7 @@ // Copyright 2020 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +pub mod genesis; mod piece; pub mod sector;