Skip to content

Commit

Permalink
Merge pull request #1 from nimadebi/v6-0d-wizard-genesis
Browse files Browse the repository at this point in the history
V6 0d wizard genesis
  • Loading branch information
0o-de-lally committed Mar 2, 2023
2 parents 121b95a + 0a24808 commit 78cb948
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 12 deletions.
14 changes: 7 additions & 7 deletions config/management/genesis/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use diem_global_constants::{GENESIS_WAYPOINT, OPERATOR_ACCOUNT, OWNER_ACCOUNT, W
use diem_management::{
config::ConfigPath,
error::Error,
secure_backend::{MGMTSecureBackend, SharedBackend},
secure_backend::{MGMTSecureBackend, SharedBackend, ValidatorBackend},
};
use std::{convert::TryFrom, path::PathBuf, str::FromStr};
use diem_secure_storage::{
Expand All @@ -16,12 +16,12 @@ use diem_secure_storage::{
use diem_types::{waypoint::Waypoint, account_address::AccountAddress};
use structopt::StructOpt;

diem_management::secure_backend!(
ValidatorBackend,
validator_backend,
"validator configuration",
"path-to-key"
);
// diem_management::secure_backend!(
// ValidatorBackend,
// validator_backend,
// "validator configuration",
// "path-to-key"
// );

#[derive(Debug, StructOpt)]
pub struct Key {
Expand Down
2 changes: 1 addition & 1 deletion config/management/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod waypoint; //////// 0L /////////
pub mod layout;
mod move_modules;
pub mod validator_builder;
mod validator_config;
pub mod validator_config;
pub mod validator_operator; //////// 0L ///////// make public
pub mod verify; //////// 0L ///////// make public

Expand Down
27 changes: 26 additions & 1 deletion config/management/genesis/src/validator_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use diem_global_constants::{OWNER_ACCOUNT, OWNER_KEY};
use diem_management::{constants, error::Error, secure_backend::SharedBackend};
use diem_management::{constants, error::Error, secure_backend::{SharedBackend, ValidatorBackend}};
use diem_types::{
network_address::NetworkAddress,
transaction::{authenticator::AuthenticationKey, Transaction}
};
use structopt::StructOpt;
use diem_management::config::ConfigPath;
use diem_types::chain_id::{ChainId};

#[derive(Debug, StructOpt)]
pub struct ValidatorConfig {
Expand All @@ -26,6 +28,29 @@ pub struct ValidatorConfig {
}

impl ValidatorConfig {
pub fn new(
owner_name: String,
validator_address: NetworkAddress,
fullnode_address: NetworkAddress,
shared_backend: &SharedBackend,
validator_backend: &ValidatorBackend,
disable_address_validation: bool,
chain_id: ChainId,
) -> Self {
Self {
owner_name,
validator_config: diem_management::validator_config::ValidatorConfig{
config: ConfigPath { config: None},
validator_backend: validator_backend.to_owned(),
chain_id: Some(chain_id)
},
validator_address,
fullnode_address,
shared_backend: shared_backend.to_owned(),
disable_address_validation
}
}

pub fn execute(self) -> Result<Transaction, Error> {
let config = self
.validator_config
Expand Down
4 changes: 4 additions & 0 deletions ol/genesis-tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ fn main() -> Result<()> {
#[options(help = "github token as string for github")]
genesis_gh_token: Option<String>,

#[options(help = "epoch to restore to")]
genesis_restore_epoch: Option<u64>,

#[options(help = "path to snapshot dir to read")]
snapshot_path: Option<PathBuf>,

Expand Down Expand Up @@ -71,6 +74,7 @@ fn main() -> Result<()> {
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.epoch = opts.genesis_restore_epoch.unwrap();
w.start_wizard()?;
return Ok(()); // exit
}
Expand Down
79 changes: 76 additions & 3 deletions ol/genesis-tools/src/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use dialoguer::{Confirm, Input};

use diem_genesis_tool::{
validator_operator::ValidatorOperator,
key::{OperatorKey, Key, OwnerKey}
key::{OperatorKey, Key, OwnerKey},
validator_config::ValidatorConfig,
};

use indicatif::{ProgressIterator, ProgressBar};
Expand All @@ -16,6 +17,13 @@ use dirs;
use ol_types::OLProgress;
use diem_github_client;
use std::path::PathBuf;
use std::str::FromStr;
use diem_types::chain_id::ChainId;
use diem_types::network_address::{NetworkAddress, Protocol};
use ol::mgmt::restore::Backup;
use ol::application::APPLICATION;
use crate::fork_genesis::make_recovery_genesis_from_db_backup;



#[test]
Expand All @@ -31,6 +39,7 @@ pub struct GenesisWizard {
github_username: String,
github_token: String,
data_path: PathBuf,
pub epoch: u64
}

impl Default for GenesisWizard {
Expand All @@ -44,7 +53,8 @@ impl Default for GenesisWizard {
repo_name: "dev-genesis".to_string(),
github_username: "".to_string(),
github_token: "".to_string(),
data_path
data_path,
epoch: 0 // What should this default value be?
}
}
}
Expand Down Expand Up @@ -88,10 +98,16 @@ pub fn start_wizard(&mut self) -> anyhow::Result<()>{
// register the configs on the new forked repo, and make the pull request
self.register_configs(&app_config)?;

self.make_pull_request()?;


// Download the snapshot from the epoch archive. Ask user which epoch to use.
// ol/cli/src/mgmt/restore.rs
// TODO: PANICS, check comments in function.
// self.restore_snapshot(self.epoch)?;

// run genesis
self.fork_genesis()?;

// create the files

Expand Down Expand Up @@ -238,8 +254,18 @@ fn git_setup(&mut self) -> anyhow::Result<()> {


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


let val_config = ValidatorConfig::new(
app_cfg.format_owner_namespace().clone(),
NetworkAddress::from_str(&*format!("{}{}", Protocol::Ip4(app_cfg.profile.ip), Protocol::Tcp(6180))).unwrap(),
NetworkAddress::from_str(&*format!("{}{}", Protocol::Ip4(app_cfg.profile.vfn_ip.unwrap()), Protocol::Tcp(6179))).unwrap(),
&sh,
&val,
false,
ChainId::new(app_cfg.chain_info.chain_id.id()),
);
val_config.execute()?;
pb.inc(1);


// # OPER does this
Expand All @@ -258,6 +284,53 @@ fn git_setup(&mut self) -> anyhow::Result<()> {
Ok(())
}

fn restore_snapshot(&self, epoch: u64) -> anyhow::Result<()> {
let pb = ProgressBar::new(1)
.with_style(OLProgress::bar());

// We need to initialize the abscissa application state for this to work.. Else it panics
// TODO: fix panic of Backup::new().

println!("Downloading snapshot for epoch {}", epoch);
// All we are doing is download the snapshot from github.
let mut backup = Backup::new(Option::from(epoch));
println!("Created backup object");
backup.fetch_backup(false)?;
println!("Downloaded snapshot for epoch {}", epoch);

pb.inc(1);
pb.finish_and_clear();
Ok(())
}

fn make_pull_request(&self) -> anyhow::Result<()> {
let gh_token_path = self.data_path.join("github_token.txt");
let api_token = std::fs::read_to_string(&gh_token_path)?;

let pb = ProgressBar::new(1)
.with_style(OLProgress::bar());
let gh_client = diem_github_client::Client::new(
self.repo_owner.clone(),
self.repo_name.clone(),
"master".to_string(),
api_token.clone(),
);
// repository_owner, genesis_repo_name, username
// This will also fail if there already is a pull request!
match gh_client.make_genesis_pull_request(&*self.repo_owner, &*self.repo_name, &*self.github_username) {
Ok(_) => println!("created pull request to genesis repo"),
Err(e) => println!("failed to create pull request to genesis repo: {:?}", e),
};
pb.inc(1);
pb.finish_and_clear();
Ok(())
}

fn fork_genesis(&self) -> anyhow::Result<()> {
// TODO
Ok(())
}

}


Expand Down

0 comments on commit 78cb948

Please sign in to comment.