Skip to content

Commit

Permalink
[tools] cli init includes OL pledges (0LNetworkCommunity#307)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <[email protected]>
  • Loading branch information
codilion and 0o-de-lally committed Aug 17, 2024
1 parent 5e5c07f commit ad50111
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 126 deletions.
52 changes: 35 additions & 17 deletions tools/config/src/config_cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
legacy_config,
config_wizard,
make_yaml_public_fullnode::{download_genesis, get_genesis_waypoint, init_fullnode_yaml},
validator_config::{validator_dialogue, vfn_dialogue},
};
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use libra_types::{
core_types::app_cfg::{self, AppCfg},
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct ConfigCli {

#[derive(clap::Subcommand)]
enum ConfigSub {
/// Generates a libra-cli-config.yaml for cli tools like txs, tower, etc. Note: the file can also be used for Carpe, though that app uses a different default directory than these cli tools.
/// Generates a libra-cli-config.yaml for cli tools like `txs`, `query`, etc. Note: the file can also be used for Carpe, though that app uses a different default directory than these cli tools.
Init {
/// force an account address instead of reading from mnemonic, requires --force_authkey
#[clap(long)]
Expand All @@ -49,13 +49,10 @@ enum ConfigSub {
#[clap(long)]
playlist_url: Option<Url>,
},
// TODO: add WhoAmI to show libra-cli-config.yaml profile info.
/// Utils for libra-cli-config.yaml file
#[clap(arg_required_else_help(true))]
Fix {
/// optional, reset the address from mnemonic. Will also lookup on the chain for the actual address if you forgot it, or rotated your authkey.
#[clap(short, long)]
address: bool,
#[clap(short('a'), long)]
reset_address: bool,

#[clap(short, long)]
remove_profile: Option<String>,
Expand All @@ -67,7 +64,7 @@ enum ConfigSub {
/// Show the addresses and configs on this device
View {},

// COMMIT NOTE: we havent'used vendor tooling configs for anything.
// COMMIT NOTE: we haven't used vendor tooling configs for anything.
/// Generate validators' config file
ValidatorInit {
// just make the VFN file
Expand All @@ -88,16 +85,25 @@ impl ConfigCli {
pub async fn run(&self) -> Result<()> {
match &self.subcommand {
Some(ConfigSub::Fix {
address,
reset_address,
remove_profile,
force_url,
}) => {
// Load configuration file
let mut cfg = AppCfg::load(self.path.clone())?;
let mut cfg = AppCfg::load(self.path.clone())
.map_err(|e| anyhow!("no config file found for libra tools, {}", e))?;
if !cfg.user_profiles.is_empty() {
println!("your profiles:");
for p in &cfg.user_profiles {
println!("- address: {}, nickname: {}", p.account, p.nickname);
}
} else {
println!("no profiles found");
}

// Handle address fix option
if *address {
let mut account_keys = legacy_config::prompt_for_account()?;
let profile = if *reset_address {
let mut account_keys = config_wizard::prompt_for_account()?;

let client = Client::new(cfg.pick_url(self.chain_name)?);

Expand All @@ -120,6 +126,9 @@ impl ConfigCli {
let profile =
app_cfg::Profile::new(account_keys.auth_key, account_keys.account);

// Add profile to configuration
cfg.maybe_add_profile(profile)?;

// Prompt to set as default profile
if dialoguer::Confirm::new()
.with_prompt("set as default profile?")
Expand All @@ -129,9 +138,18 @@ impl ConfigCli {
.set_default(account_keys.account.to_hex_literal());
}

// Add profile to configuration
cfg.maybe_add_profile(profile)?;
}
cfg.get_profile_mut(Some(account_keys.account.to_hex_literal()))
} else {
// get default profile
println!("will try to fix your default profile");
cfg.get_profile_mut(None)
}?;

println!("using profile: {}", &profile.nickname);

// user can take pledge here on fix or on init
profile.maybe_offer_basic_pledge();
profile.maybe_offer_validator_pledge();

// Remove profile if specified
if let Some(p) = remove_profile {
Expand Down Expand Up @@ -160,7 +178,7 @@ impl ConfigCli {
test_private_key,
playlist_url,
}) => {
legacy_config::wizard(
config_wizard::wizard(
force_authkey.to_owned(),
force_address.to_owned(),
self.path.to_owned(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use diem_types::chain_id::NamedChain;
use libra_types::{
core_types::{app_cfg::AppCfg, network_playlist::NetworkPlaylist},
exports::{AccountAddress, AuthenticationKey, Client},
ol_progress,
type_extensions::client_ext::ClientExt,
};
use libra_wallet::account_keys::{get_ol_legacy_address, AccountKeys};
Expand Down Expand Up @@ -33,6 +34,8 @@ pub async fn wizard(
(account_keys.auth_key, account_keys.account)
};

let spin = ol_progress::OLProgress::spin_steady(250, "fetching metadata".to_string());

// if the user specified both a chain name and playlist, then the playlist will override the default settings for the named chain.
let mut np = match network_playlist {
Some(a) => a,
Expand Down Expand Up @@ -60,14 +63,21 @@ pub async fn wizard(
};
}

let cfg = AppCfg::init_app_configs(authkey, address, config_dir, chain_name, Some(np))?;
let mut cfg = AppCfg::init_app_configs(authkey, address, config_dir, chain_name, Some(np))?;

spin.finish();
// offer both pledges on init
let profile = cfg.get_profile_mut(None)?;
profile.maybe_offer_basic_pledge();

let p = cfg.save_file().context(format!(
"could not initialize configs at {}",
cfg.workspace.node_home.to_str().unwrap()
))?;

println!("Success, config saved to {}", p.to_str().unwrap());
ol_progress::OLProgress::make_fun();
println!("config saved to {}", p.display());
ol_progress::OLProgress::complete("SUCCESS: libra tool configured");

Ok(cfg)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod config_cli;
pub mod legacy_config;
pub mod config_wizard;
pub mod make_profile; // TODO: deprecated?
pub mod make_yaml_public_fullnode;
pub mod make_yaml_validator;
Expand Down
10 changes: 5 additions & 5 deletions tools/config/src/make_yaml_public_fullnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use std::{
path::{Path, PathBuf},
};

const FN_FILENAME: &str = "fullnode.yaml";
const VFN_FILENAME: &str = "vfn.yaml";
const DEFAULT_WAYPOINT_VERSION: &str = "6.9.0";
pub const FN_FILENAME: &str = "fullnode.yaml";
pub const VFN_FILENAME: &str = "vfn.yaml";
pub const GENESIS_FILES_VERSION: &str = "7.0.0";
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct GithubContent {
Expand Down Expand Up @@ -201,7 +201,7 @@ pub async fn download_genesis(home_dir: Option<PathBuf>) -> anyhow::Result<()> {
let latest_path = format!(
"{}/v{}/genesis.blob",
"https://raw.githubusercontent.com/0LNetworkCommunity/epoch-archive-mainnet/main/upgrades",
latest_version.unwrap_or(DEFAULT_WAYPOINT_VERSION)
latest_version.unwrap_or(GENESIS_FILES_VERSION)
);

// Fetch the latest waypoint
Expand Down Expand Up @@ -244,7 +244,7 @@ pub async fn get_genesis_waypoint(home_dir: Option<PathBuf>) -> anyhow::Result<W
let latest_path = format!(
"{}/v{}/waypoint.txt",
"https://raw.githubusercontent.com/0LNetworkCommunity/epoch-archive-mainnet/main/upgrades",
latest_version.unwrap_or(DEFAULT_WAYPOINT_VERSION)
latest_version.unwrap_or(GENESIS_FILES_VERSION)
);

// Fetch the latest waypoint
Expand Down
18 changes: 14 additions & 4 deletions tools/config/src/validator_config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::{make_yaml_public_fullnode::make_private_vfn_yaml, make_yaml_validator};
use crate::{
make_yaml_public_fullnode::{make_private_vfn_yaml, VFN_FILENAME},
make_yaml_validator,
};
use anyhow::{anyhow, bail, Context};
use dialoguer::{Confirm, Input};
use diem_crypto::x25519;
use diem_genesis::{config::HostAndPort, keys::PublicIdentity};
use diem_types::{chain_id::NamedChain, network_address::DnsName};
use libra_types::{
core_types::{app_cfg::AppCfg, network_playlist::NetworkPlaylist},
ol_progress::OLProgress,
ol_progress::{self, OLProgress},
};
use libra_wallet::{utils::read_public_identity_file, validator_files::SetValidatorConfiguration};
use std::{
Expand Down Expand Up @@ -38,14 +41,19 @@ pub async fn initialize_validator(
// TODO: nice to have
// also for convenience create a local user libra-cli-config.yaml file so the
// validator can make transactions against the localhost
let cfg = AppCfg::init_app_configs(
let mut cfg = AppCfg::init_app_configs(
keys.child_0_owner.auth_key,
keys.child_0_owner.account,
home_path,
chain_name,
Some(NetworkPlaylist::localhost(chain_name)),
)?;

// offer the validator pledge on startup
let profile = cfg.get_profile_mut(None)?;
profile.maybe_offer_basic_pledge();
profile.maybe_offer_validator_pledge();

cfg.save_file().context(format!(
"could not initialize configs at {}",
cfg.workspace.node_home.to_str().unwrap()
Expand Down Expand Up @@ -169,7 +177,9 @@ pub async fn vfn_dialogue(
pk, dns,
)?;

println!("SUCCESS: on your VFN you should have vfn.yaml, validator-full-node.yaml files before starting node.");
ol_progress::OLProgress::complete(&format!("SUCCESS: config saved to {}", VFN_FILENAME));

println!("NOTE: on your VFN host you must place this vfn.yaml file in config directory before starting node.");

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion tools/genesis/src/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl GenesisWizard {
)?;

for _ in (0..10)
.progress_with_style(OLProgress::fun())
.progress_with_style(OLProgress::fun_style())
.with_message("Initializing 0L")
{
thread::sleep(Duration::from_millis(100));
Expand Down
Loading

0 comments on commit ad50111

Please sign in to comment.