Skip to content

Commit

Permalink
Merge pull request #3 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 10, 2023
2 parents 2363875 + 40e9732 commit cdfdfc5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 46 deletions.
34 changes: 17 additions & 17 deletions ol/cli/src/mgmt/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,25 +294,25 @@ impl Backup {
Ok(())
}

/// helper to get path to manifest file
pub fn manifest_path(&self) -> Result<PathBuf, Error> {
let glob_format = &format!("{}/**/epoch_ending.manifest", &self.restore_path.to_str().expect("no restore path provided"));
let manifest_path = match glob(glob_format)
.expect("Failed to read glob pattern")
.next()
{
Some(Ok(p)) => p,
_ => bail!("no path found for {:?}", glob_format),
};
/// helper to get path to manifest file
pub fn manifest_path(&self) -> Result<PathBuf, Error> {
let glob_format = &format!("{}/**/state.manifest", &self.restore_path.to_str().expect("no restore path provided"));
let manifest_path = match glob(glob_format)
.expect("Failed to read glob pattern")
.next()
{
Some(Ok(p)) => p,
_ => bail!("no path found for {:?}", glob_format),
};

if !manifest_path.exists() {
let msg = format!("manifest path does not exist at: {:?}", &manifest_path);
println!("{}", &msg);
bail!(msg);
} else {
Ok(manifest_path)
if !manifest_path.exists() {
let msg = format!("manifest path does not exist at: {:?}", &manifest_path);
println!("{}", &msg);
bail!(msg);
} else {
Ok(manifest_path)
}
}
}
}

fn get_highest_epoch_archive() -> Result<(u64, String), Error> {
Expand Down
6 changes: 2 additions & 4 deletions ol/genesis-tools/src/fork_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn make_recovery_genesis_from_vec_legacy_recovery(
// Otherwise, we might need to just collect the validator accounts
// for debugging or other test purposes.
// let expected_len_all_users = recovery.len() as u64;

let recovery_changeset = encode_recovery_genesis_changeset(
&all_validator_configs.vals,
&all_validator_configs.opers,
Expand All @@ -59,9 +58,7 @@ pub fn make_recovery_genesis_from_vec_legacy_recovery(
append_user_accounts,
recovery, // TODO: turn this into an option type
)?;

let gen_tx = Transaction::GenesisTransaction(WriteSetPayload::Direct(recovery_changeset));

save_genesis(&gen_tx, genesis_blob_path)?;
Ok(gen_tx)
}
Expand All @@ -86,7 +83,8 @@ pub fn make_recovery_genesis_from_vec_legacy_recovery(

/// save the genesis blob
pub fn save_genesis(gen_tx: &Transaction, output_path: PathBuf) -> Result<(), Error> {
let mut file = File::create(output_path)?;
let file_path = output_path.join("genesis").with_extension("blob");
let mut file = File::create(file_path)?;
let bytes = bcs::to_bytes(&gen_tx)?;
file.write_all(&bytes)?;
Ok(())
Expand Down
50 changes: 25 additions & 25 deletions ol/genesis-tools/src/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Default for GenesisWizard {
github_username: "".to_string(),
github_token: "".to_string(),
data_path,
epoch: None, // What should this default value be?
epoch: None,
}
}
}
Expand Down Expand Up @@ -122,12 +122,14 @@ impl GenesisWizard {
let snapshot_path = if Confirm::new()
.with_prompt("Do we need to download a new legacy snapshot?")
.interact()? {
self.download_snapshot(&app_config)?
} else {
// TODO(Nima): Instead of using a test, let's ask the user for the patht to a snapshot

ol_types::fixtures::get_test_snapshot()
};
self.download_snapshot(&app_config)?
} else {
let input = Input::<String>::new()
.with_prompt("Enter the (absolute) path to the snapshot state.manifest file")
.interact_text()?;
PathBuf::from(input)
};
println!("snapshot path: {:?}", snapshot_path);

// do the whole genesis workflow and create the files
run::default_run(
Expand All @@ -139,22 +141,18 @@ impl GenesisWizard {
false,
)?;


// reset the safety rules
reset_safety_data(&self.data_path.join("key_store.json"), &app_config.format_oper_namespace());
reset_safety_data(&self.data_path, &app_config.format_oper_namespace());

// check db
self.maybe_backup_db();

// remove "owner" key from key_store.json
self.maybe_remove_money_keys(&app_config);


// verify genesis
self.check_keys_and_genesis(&app_config)?;



for _ in (0..10)
.progress_with_style(OLProgress::fun())
.with_message("Initializing 0L")
Expand All @@ -171,15 +169,16 @@ impl GenesisWizard {
fn git_token_check(&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");
match Input::<String>::new()
.with_prompt("No github token found, enter one now, or save to github_token.txt:")
.with_prompt("No github token found, enter one now, or save to github_token.txt")
.interact_text()
{
Ok(s) => {
// creates the folders if necessary (this check is called before host init)
std::fs::create_dir_all(&self.data_path)?;
std::fs::write(&gh_token_path, s)?;
}
_ => println!("somehow couldn't read what you typed "),
_ => println!("somehow couldn't read what you typed"),
}
}

Expand Down Expand Up @@ -250,7 +249,7 @@ impl GenesisWizard {
.interact().unwrap()
{
let storage_helper =
StorageHelper::get_with_path(self.data_path.join("key_store.json"));
StorageHelper::get_with_path(self.data_path.clone());

let mut owner_storage = storage_helper.storage(app_cfg.format_oper_namespace().clone());
owner_storage.set(OWNER_KEY, "").unwrap();
Expand Down Expand Up @@ -318,11 +317,10 @@ impl GenesisWizard {

pb.inc(1);
pb.set_message("registering the OPERATOR account.");
// The oper key is saved locally as key + -oper. This little hack works..
let set_oper =
ValidatorOperator::new(app_cfg.format_owner_namespace().clone(), &owner_shared);

ValidatorOperator::new(app_cfg.format_oper_namespace(), &owner_shared);
set_oper.execute()?;

// # OWNER does this
// # Links to an operator on github, creates the OWNER_ACCOUNT locally
// assign:
Expand Down Expand Up @@ -373,12 +371,11 @@ impl GenesisWizard {

fn check_keys_and_genesis(&self, app_cfg: &AppCfg) -> anyhow::Result<String> {
let val = Key::validator_backend(
app_cfg.format_oper_namespace().clone(),
app_cfg.format_owner_namespace().clone(),
self.data_path.clone(),
)?;

let v = Verify::new(&val,self.data_path.join("genesis.blob"));

Ok(v.execute()?)
}

Expand All @@ -403,13 +400,16 @@ impl GenesisWizard {

pb.enable_steady_tick(Duration::from_millis(100));


//TODO(Nima): Check if we already have the snapshot downloaded.

// All we are doing is download the snapshot from github.
let backup = Backup::new(self.epoch, app_cfg);
backup.fetch_backup(false)?;

if backup.manifest_path().is_err() {
backup.fetch_backup(true)?;
} else {
println!("Already have snapshot for epoch {}", self.epoch.unwrap());
}

// I changed the manifest file name to state.manifest instead of epoch_ending.manifest
let snapshot_manifest_file = backup.manifest_path()?;

let snapshot_dir = snapshot_manifest_file.parent().unwrap().to_path_buf();
Expand Down

0 comments on commit cdfdfc5

Please sign in to comment.