Skip to content

Commit

Permalink
Genesis New Template cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
snaumov committed Aug 9, 2020
1 parent d7dcaf6 commit 213f6c6
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions forest/src/cli/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use std::fs::File;
use std::include_bytes;
use std::io::BufReader;
use std::sync::Arc;
use structopt::StructOpt;
use fil_types::genesis;
// use serde_json::Result;

/// Uses an optional file path or the default genesis to parse the genesis and determine if
/// chain store has existing data for the given genesis.
Expand Down
40 changes: 40 additions & 0 deletions forest/src/cli/genesis_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fs::{File, create_dir_all};
use std::path::Path;
use std::error::Error as StdError;
use structopt::StructOpt;
use fil_types::genesis;

#[derive(Debug, StructOpt)]
pub enum GenesisCommands {
/// Creates new genesis template
#[structopt(about = "Create new Genesis template")]
GenesisNewTemplate {
#[structopt(short, help = "Input a network name")]
network_name: 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::GenesisNewTemplate { network_name, file_path } => {
genesis_new_template(network_name.to_string(), file_path.to_string());
},
}
}
}

fn genesis_new_template(network_name: String, file_path: String) {
let template = genesis::Template::new(network_name);

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);
}
}
}
7 changes: 7 additions & 0 deletions forest/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ 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_cmd::GenesisCommands;
pub(super) use self::genesis::initialize_genesis;

use jsonrpc_v2::Error as JsonRpcError;
Expand Down Expand Up @@ -47,6 +49,11 @@ 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
53 changes: 53 additions & 0 deletions types/src/genesis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use address::Address;
use super::SectorSize;
use vm::TokenAmount;
use serde::Serialize;
use num_bigint::bigint_ser;
use std::time::SystemTime;

#[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<u8>,

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

#[derive(Serialize)]
pub struct Template {
accounts: Vec<Actor>,
miners: Vec<Miner>,
network_name: String,

#[serde(skip_serializing)]
timestamp: SystemTime,
}

impl Template {
pub fn new(network_name: String) -> Template {
Template{
accounts: Vec::new(),
miners: Vec::new(),
network_name,
timestamp: SystemTime::now(),
}
}
}
1 change: 1 addition & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod piece;
pub mod sector;
pub mod genesis;

pub use self::piece::*;
pub use self::sector::*;
Expand Down

0 comments on commit 213f6c6

Please sign in to comment.