Skip to content

Commit

Permalink
[aptos-cli] Save genesis to genesis.blob
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed May 5, 2022
1 parent 9f93f94 commit ac2c10e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
26 changes: 10 additions & 16 deletions crates/aptos/src/genesis/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
common::types::{CliError, CliTypedResult},
common::{
types::{CliError, CliTypedResult},
utils::write_to_file,
},
genesis::config::Layout,
CliCommand,
};
Expand All @@ -11,11 +14,7 @@ use aptos_github_client::Client as GithubClient;
use async_trait::async_trait;
use clap::Parser;
use serde::{de::DeserializeOwned, Serialize};
use std::{
io::{Read, Write},
path::PathBuf,
str::FromStr,
};
use std::{io::Read, path::PathBuf, str::FromStr};

pub const LAYOUT_NAME: &str = "layout";

Expand Down Expand Up @@ -160,16 +159,11 @@ impl GitClient {
match self {
GitClient::Local(local_repository_path) => {
let path = local_repository_path.join(format!("{}.yml", name));
let mut file = if path.exists() {
std::fs::File::open(path.as_path())
.map_err(|e| CliError::IO(path.display().to_string(), e))?
} else {
std::fs::File::create(path.as_path())
.map_err(|e| CliError::IO(path.display().to_string(), e))?
};

file.write_all(to_yaml(input)?.as_bytes())
.map_err(|e| CliError::IO(path.display().to_string(), e))?;
write_to_file(
path.as_path(),
&path.display().to_string(),
to_yaml(input)?.as_bytes(),
)?;
}
GitClient::Github(client) => {
client.put(&format!("{}.yml", name), &to_base64_encoded_yaml(input)?)?;
Expand Down
19 changes: 17 additions & 2 deletions crates/aptos/src/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ pub mod git;
pub mod keys;

use crate::{
common::types::CliTypedResult,
common::{
types::{CliError, CliTypedResult, PromptOptions},
utils::{check_if_file_exists, write_to_file},
},
genesis::{
config::{Layout, ValidatorConfiguration},
git::{GitOptions, LAYOUT_NAME},
Expand All @@ -17,6 +20,7 @@ use aptos_crypto::ed25519::Ed25519PublicKey;
use aptos_types::chain_id::ChainId;
use async_trait::async_trait;
use clap::Parser;
use std::path::PathBuf;
use vm_genesis::Validator;

const MIN_PRICE_PER_GAS_UNIT: u64 = 1;
Expand Down Expand Up @@ -45,8 +49,12 @@ impl GenesisTool {
/// Generate genesis from a git repository
#[derive(Parser)]
pub struct GenerateGenesis {
#[clap(flatten)]
prompt_options: PromptOptions,
#[clap(flatten)]
github_options: GitOptions,
#[clap(long, parse(from_os_str), default_value = ".")]
output_dir: PathBuf,
}

#[async_trait]
Expand All @@ -57,15 +65,22 @@ impl CliCommand<()> for GenerateGenesis {

async fn execute(self) -> CliTypedResult<()> {
let genesis_info = fetch_genesis_info(self.github_options)?;
let genesis_file = self.output_dir.join("genesis.blob");
check_if_file_exists(genesis_file.as_path(), self.prompt_options)?;

vm_genesis::encode_genesis_transaction(
let txn = vm_genesis::encode_genesis_transaction(
genesis_info.root_key.clone(),
&genesis_info.validators,
&genesis_info.modules,
genesis_info.chain_id,
MIN_PRICE_PER_GAS_UNIT,
);

write_to_file(
genesis_file.as_path(),
"genesis.blob",
&bcs::to_bytes(&txn).map_err(|e| CliError::BCS("genesis.blob", e))?,
)?;
Ok(())
}
}
Expand Down

0 comments on commit ac2c10e

Please sign in to comment.