Skip to content

Commit

Permalink
New Genesis Template cli command (#612)
Browse files Browse the repository at this point in the history
* Genesis New Template cli command

* PascalCase

* addressing more comments

* lowercase on ActorType
  • Loading branch information
snaumov authored Aug 14, 2020
1 parent 8192c12 commit bbbb9e2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
jsonrpc-v2 = { version = "0.5.2", features = ["easy-errors", "macros"] }
uuid = { version = "0.8.1", features = ["v4"] }
50 changes: 50 additions & 0 deletions forest/src/cli/genesis_cmd.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[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);
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions forest/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions forest/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ pub(super) async fn process(command: Subcommand) {
Subcommand::Chain(cmd) => {
cmd.run().await;
}
Subcommand::Genesis(cmd) => {
cmd.run().await;
}
}
}
1 change: 1 addition & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 56 additions & 0 deletions types/src/genesis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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)]
#[serde(rename_all = "lowercase")]
pub enum ActorType {
Account,
MultiSig,
}

#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Actor {
pub actor_type: ActorType,
#[serde(with = "bigint_ser")]
pub token_amount: TokenAmount,
}

#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Miner {
pub owner: Address,
pub worker: Address,
pub peer_id: Vec<u8>,

#[serde(with = "bigint_ser")]
pub market_balance: TokenAmount,
#[serde(with = "bigint_ser")]
pub power_balance: TokenAmount,
pub sector_size: SectorSize,
}

#[derive(Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Template {
pub accounts: Vec<Actor>,
pub miners: Vec<Miner>,
pub network_name: String,
// timestamp: SystemTime,
}

impl Template {
pub fn new(network_name: String) -> Template {
Template {
accounts: Vec::new(),
miners: Vec::new(),
network_name,
}
}
}
1 change: 1 addition & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

pub mod genesis;
mod piece;
pub mod sector;

Expand Down

0 comments on commit bbbb9e2

Please sign in to comment.