1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::builder::GenesisBuilder;
use diem_management::{config::ConfigPath, error::Error, secure_backend::SharedBackend};
use diem_secure_storage::Storage;
use diem_types::{chain_id::ChainId, transaction::Transaction};
use std::{fs::File, io::Write, path::PathBuf};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct Genesis {
#[structopt(flatten)]
pub config: ConfigPath,
#[structopt(long, required_unless("config"))]
pub chain_id: Option<ChainId>,
#[structopt(flatten)]
pub backend: SharedBackend,
#[structopt(long)]
pub path: Option<PathBuf>,
}
impl Genesis {
fn config(&self) -> Result<diem_management::config::Config, Error> {
self.config
.load()?
.override_chain_id(self.chain_id)
.override_shared_backend(&self.backend.shared_backend)
}
pub fn execute(self) -> Result<Transaction, Error> {
let config = self.config()?;
let chain_id = config.chain_id;
let storage = Storage::from(&config.shared_backend);
let genesis = GenesisBuilder::new(storage)
.build(chain_id, None)
.map_err(|e| Error::UnexpectedError(e.to_string()))?;
if let Some(path) = self.path {
let mut file = File::create(path).map_err(|e| {
Error::UnexpectedError(format!("Unable to create genesis file: {}", e))
})?;
let bytes = bcs::to_bytes(&genesis).map_err(|e| {
Error::UnexpectedError(format!("Unable to serialize genesis: {}", e))
})?;
file.write_all(&bytes).map_err(|e| {
Error::UnexpectedError(format!("Unable to write genesis file: {}", e))
})?;
}
Ok(genesis)
}
}