Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Mar 1, 2023
1 parent 034ed5c commit be42125
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 93 deletions.
24 changes: 11 additions & 13 deletions config/management/genesis/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ pub struct Key {
}

impl Key {
pub fn new(validator_backend: ValidatorBackend, shared_backend: SharedBackend) -> Self {
pub fn new(validator_backend: &ValidatorBackend, shared_backend: &SharedBackend) -> Self {
Self {
config: ConfigPath { config: None },
shared_backend,
validator_backend,
shared_backend: shared_backend.to_owned(),
validator_backend: validator_backend.to_owned(),
path_to_key: None,
}
}
pub fn shared_backend(namespace: String, github_org: String, repo_name: String, data_path: PathBuf) -> anyhow::Result<SharedBackend> {

// BLACK MAGIC with MACROS.
// BLACK MAGIC with MACROS
// ... AND STRING FORMATTING
// I curse your first born.

let storage_cfg = format!(
Expand All @@ -63,14 +64,11 @@ impl Key {

pub fn validator_backend(namespace: String, data_path: PathBuf) -> anyhow::Result<ValidatorBackend> {

// BLACK MAGIC with MACROS.
// I curse your first born.

let storage_cfg = format!(
"backend=disk;path=${data_path}/key_store.json;namespace=${namespace}",
namespace = namespace,
data_path = data_path.to_str().unwrap(),
);
let storage_cfg = format!(
"backend=disk;path={data_path}key_store.json;namespace={namespace}",
namespace = namespace,
data_path = data_path.to_str().unwrap(),
);

Ok(ValidatorBackend::from_str(storage_cfg.as_str())?)

Expand Down Expand Up @@ -191,7 +189,7 @@ impl OperatorKey {
#[derive(Debug, StructOpt)]
pub struct OwnerKey {
#[structopt(flatten)]
key: Key,
pub key: Key, //////// 0L ////////
}

impl OwnerKey {
Expand Down
4 changes: 2 additions & 2 deletions config/management/genesis/src/validator_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub struct ValidatorOperator {
impl ValidatorOperator {
//////// 0L ////////
/// need to instantiate besides from CLI
pub fn new(operator_name: String, shared_backend: SharedBackend) -> Self {
pub fn new(operator_name: String, shared_backend: &SharedBackend) -> Self {
Self {
config: ConfigPath { config: None },
operator_name,
shared_backend,
shared_backend: shared_backend.to_owned(),
}
}
pub fn execute(self) -> Result<String, Error> {
Expand Down
1 change: 0 additions & 1 deletion config/src/config/secure_backend_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use crate::config::Error;
use diem_global_constants::NODE_HOME;
use diem_secure_storage::{
GitHubStorage, InMemoryStorage, Namespaced, OnDiskStorage, Storage, VaultStorage,
};
Expand Down
40 changes: 28 additions & 12 deletions ol/genesis-tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use ol_genesis_tools::{
};
use indicatif::ProgressIterator;

#[tokio::main]
async fn main() -> Result<()> {
// #[tokio::main]
fn main() -> Result<()> {
#[derive(Debug, Options)]
struct Args {
#[options(help = "use wizard")]
wizard: bool,

#[options(help = "org of remote github repo for genesis coordination")]
#[options(short="o", help = "org of remote github repo for genesis coordination")]
genesis_repo_owner: Option<String>,

#[options(help = "name of remote github repo for genesis coordination")]
#[options(short="n", help = "name of remote github repo for genesis coordination")]
genesis_repo_name: Option<String>,

#[options(help = "github token as string for github")]
Expand Down Expand Up @@ -64,13 +64,18 @@ async fn main() -> Result<()> {

let opts = Args::parse_args_default_or_exit();

if opts.wizard && opts.genesis_repo_owner.is_some() && opts.genesis_repo_name.is_some() {
if opts.wizard &&
opts.genesis_repo_owner.is_some() &&
opts.genesis_repo_name.is_some()
{
let mut w = wizard::GenesisWizard::default();
w.repo_name = opts.genesis_repo_name.as_ref().unwrap().clone();
w.repo_owner = opts.genesis_repo_owner.as_ref().unwrap().clone();
w.start_wizard()?
}

let rt = tokio::runtime::Runtime::new().unwrap();

if opts.fork {
// create a genesis.blob
// there are two paths here
Expand Down Expand Up @@ -120,17 +125,27 @@ async fn main() -> Result<()> {
// create testnet genesis

};

make_recovery_genesis_from_db_backup(

rt.block_on({
make_recovery_genesis_from_db_backup(
output_path.clone(),
snapshot_path,
!opts.debug,
opts.legacy,
&genesis_vals
// opts.genesis_vals
)
.await
.expect("ERROR: could not create genesis from snapshot");
})?;
// make_recovery_genesis_from_db_backup(
// output_path.clone(),
// snapshot_path,
// !opts.debug,
// opts.legacy,
// &genesis_vals
// // opts.genesis_vals
// )
// // .await
// .expect("ERROR: could not create genesis from snapshot");
carpe_diem();
Ok(())
}
Expand Down Expand Up @@ -183,9 +198,10 @@ async fn main() -> Result<()> {
if !snapshot_path.exists() {
panic!("ERROR: --snapshot-path file does not exist");
}
let recovery_struct = db_backup_into_recovery_struct(&snapshot_path)
.await
.expect("could not export DB into JSON recovery file");

let recovery_struct = rt.block_on({
db_backup_into_recovery_struct(&snapshot_path)
})?;

save_recovery_file(&recovery_struct, &json_destination_path).unwrap_or_else(|_| panic!("ERROR: recovery data extracted, but failed to save file {:?}",
&json_destination_path));
Expand Down
82 changes: 30 additions & 52 deletions ol/genesis-tools/src/wizard.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
//! A simple workflow tool to organize all genesis
//! instead of using many CLI tools.

use std::convert::TryInto;
use std::str::FromStr;


use anyhow::bail;
use dialoguer::{Confirm, Input};
use diem_config::config::SecureBackend;

use diem_genesis_tool::{
validator_operator::ValidatorOperator,
key::{OperatorKey, Key, OwnerKey}
};
use futures::future::Shared;
use indicatif::ProgressIterator;

use indicatif::{ProgressIterator, ProgressBar};
use ol::config::AppCfg;
use std::{path::Path, thread, time::Duration};
use dirs;
use ol_types::OLProgress;
use diem_github_client;
use std::path::PathBuf;
use diem_management::{secure_backend, secure_backend::{SharedBackend, ValidatorBackend, MGMTSecureBackend}};


#[test]
fn test_wizard() {
Expand All @@ -39,6 +39,7 @@ impl Default for GenesisWizard {
/// testnet values for genesis wizard
fn default() -> Self {
let data_path = dirs::home_dir().expect("no home dir found").join(".0L/");
dbg!(&data_path);
Self {
namespace: "alice".to_string(),
repo_owner: "0l-testnet".to_string(),
Expand All @@ -51,7 +52,7 @@ impl Default for GenesisWizard {
}
impl GenesisWizard {
/// start wizard for end-to-end genesis
pub fn start_wizard(&self) -> anyhow::Result<()>{
pub fn start_wizard(&mut self) -> anyhow::Result<()>{

Confirm::new()
.with_prompt("Let's do this?")
Expand Down Expand Up @@ -84,14 +85,11 @@ pub fn start_wizard(&self) -> anyhow::Result<()>{

let app_config = ol_types::config::parse_toml(self.data_path.join("0L.toml"))?;


// register the configs on the new forked repo, and make the pull request
self.register_configs(&app_config)?;


for _ in (0..10).progress_with_style(OLProgress::bar()) {
thread::sleep(Duration::from_millis(100));
}

for _ in (0..10).progress_with_style(OLProgress::fun())
.with_message("Initializing 0L") {
thread::sleep(Duration::from_millis(100));
Expand All @@ -116,7 +114,7 @@ pub fn start_wizard(&self) -> anyhow::Result<()>{
Ok(())
}

fn git_setup(&self) -> anyhow::Result<()> {
fn git_setup(&mut self) -> anyhow::Result<()> {
let gh_token_path = self.data_path.join("github_token.txt");
if !Path::exists(&gh_token_path) {
println!("no github token found");
Expand All @@ -139,10 +137,11 @@ fn git_setup(&self) -> anyhow::Result<()> {
api_token.clone(),
);

let github_username = gh_client.get_authenticated_user()?;
// Use the github token to find out who is the user behind it.
self.github_username = gh_client.get_authenticated_user()?;

if !Confirm::new()
.with_prompt(format!("Is this your github user? {} ", &github_username))
.with_prompt(format!("Is this your github user? {} ", &self.github_username))
.interact()? {
println!("Please update your github token");
return Ok(());
Expand All @@ -167,45 +166,20 @@ fn git_setup(&self) -> anyhow::Result<()> {
} else {
println!("found a genesis repo on your account, we'll use that for registration");
}
// Remeber to clear out the /owner key from the key_store.json for safety.
Ok(())

}

// fn shared_backend(&self, namespace: &str) -> anyhow::Result<SharedBackend> {

// // BLACK MAGIC with MACROS.
// // I curse your first born.

// let storage_cfg = format!(
// "backend=github;repository_owner=${GITHUB_USER};repository=${REPO_NAME};token=${DATA_PATH}/github_token.txt;namespace=${ACC}",
// ACC=namespace,
// GITHUB_USER=self.github_username,
// REPO_NAME=self.repo_name,
// DATA_PATH=self.data_path.to_str().unwrap(),
// );

// Ok(SharedBackend::from_str(storage_cfg.as_str())?)

// }

// fn local_val_backend(&self, namespace: &str) -> anyhow::Result<ValidatorBackend> {

// // BLACK MAGIC with MACROS.
// // I curse your first born.

// let storage_cfg = format!(
// "backend=disk;path=${DATA_PATH}/key_store.json;namespace=${ACC}",
// ACC=namespace,
// DATA_PATH=self.data_path.to_str().unwrap(),
// );

// Ok(ValidatorBackend::from_str(storage_cfg.as_str())?)
fn register_configs(&self, app_cfg: &AppCfg) -> anyhow::Result<()>{
let pb = ProgressBar::new(4)
.with_style(OLProgress::bar());


// }

fn register_configs(&self, app_cfg: &AppCfg) -> anyhow::Result<()>{
// These are abstractions for github and the local key storage.
let val = Key::validator_backend(
app_cfg.format_owner_namespace().clone(),
app_cfg.format_oper_namespace().clone(),
self.data_path.clone()
)?;

Expand All @@ -215,28 +189,31 @@ fn git_setup(&self) -> anyhow::Result<()> {
self.repo_name.clone(),
self.data_path.clone()
)?;

let default_key_struct = Key::new(val, sh);

let op = OperatorKey {
key: default_key_struct
key: Key::new(&val, &sh)
};

op.execute()?;

pb.inc(1);

let own = OwnerKey {
key: default_key_struct
key: Key::new(&val, &sh)
};

own.execute()?;
pb.inc(1);

let set_oper = ValidatorOperator::new(
app_cfg.format_owner_namespace().clone(),
sh
&sh
);

set_oper.execute()?;
pb.inc(1);

pb.finish_and_clear();

//TODO(nima) send the validator config. similar to above

Expand Down Expand Up @@ -280,7 +257,8 @@ fn git_setup(&self) -> anyhow::Result<()> {


fn initialize_host() -> anyhow::Result<()> {
let w = onboard::wizard::Wizard::default();
let mut w = onboard::wizard::Wizard::default();
w.genesis_ceremony = true;
w.run()
}

Expand Down
9 changes: 0 additions & 9 deletions ol/onboard/src/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ impl Wizard {

let base_waypoint = app_config.chain_info.base_waypoint.clone();

// OLProgress::complete("App configs written [0L.toml]");

// if let Some(url) = &self.template_url {
// let mut url = url.to_owned();
// url.set_port(Some(3030)).unwrap(); //web port
// save_template(&url.join("account.json").unwrap(), home_path);
// // get autopay
// status_ok!("\nAccount Template saved", "\n...........................\n");
// }

// Initialize Validator Keys
// this also sets a genesis waypoint if one was provide, e.g. from an upstream peer.
Expand Down
4 changes: 0 additions & 4 deletions ol/types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ impl AppCfg {
if let Some(u) = upstream_peer {
default_config.profile.upstream_nodes = vec![u.to_owned()]
};
// Add link to previous tower
// if !*IS_TEST {
// default_config.profile.tower_link = add_tower(&default_config);
// }

if let Some(id) = network_id {
default_config.chain_info.chain_id = id.to_owned();
Expand Down

0 comments on commit be42125

Please sign in to comment.