Skip to content

Commit

Permalink
Resolve conflict merging from main
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-open-source committed Feb 16, 2022
2 parents f16e792 + 820ec66 commit 5cb8864
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 55 deletions.
77 changes: 41 additions & 36 deletions ol/cli/src/commands/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ use diem_types::waypoint::Waypoint;
use diem_wallet::WalletLibrary;
use fs_extra::file::{copy, CopyOptions};
use ol_keys::{scheme::KeyScheme, wallet};
use ol_types::{config::fix_missing_fields, fixtures, rpc_playlist};
use ol_types::{config::{fix_missing_fields, parse_toml}, fixtures, rpc_playlist};
use std::process::exit;
use std::{fs, path::PathBuf};
use url::Url;
/// `init` subcommand
#[derive(Command, Debug, Default, Options)]
pub struct InitCmd {
/// Create the 0L.toml file for 0L apps
/// Create the 0L.toml file for 0L apps
#[options(help = "Create the 0L.toml file for 0L apps")]
app: bool,

/// For "app" option an upstream peer to use in 0L.toml
#[options(help = "An upstream peer to use in 0L.toml")]
rpc_peer: Option<Url>,


/// For "app" option home path for app config
#[options(help = "home path for app config")]
app_cfg_path: Option<PathBuf>,
Expand All @@ -50,9 +51,7 @@ pub struct InitCmd {
fullnode: bool,

/// Set the upstream peers playlist from an http served playlist file
#[options(
help = "Use a playlist.json file hosted online to set the upstream_peers field in 0L.toml"
)]
#[options(help = "Use a playlist.json file hosted online to set the upstream_peers field in 0L.toml")]
rpc_playlist: Option<String>, // Using string so that the user can use a default upstream

/// Search and get seed peers from chain
Expand Down Expand Up @@ -158,39 +157,45 @@ impl Runnable for InitCmd {
}
}
}

if let Some(url) = self.rpc_playlist.as_ref() {
// try to parse it, otherwise get_known_fullnodes will use a default playlist
let playlist_url: Option<Url> = url.parse().ok();

match rpc_playlist::get_known_fullnodes(playlist_url) {
Ok(f) => {
println!("peers found:");
f.get_urls()
.into_iter()
.for_each(|u| println!("{}", u.as_str()));

match f.update_config_file(self.app_cfg_path.clone()) {
Ok(_) => println!("Upstream RPC peers updated in 0L.toml"),
Err(e) => {
println!(
"could not update rpc peers in config file, exiting. Message: {:?}",
e
);
exit(1);
}
}
return;
}
Err(e) => {
println!(
"could not read playlists from {:?}, exiting. Message: {:?}",
url, e
);
exit(1);
}
};


// try to parse it, otherwise get_known_fullnodes will use a default playlist
let playlist_url: Option<Url> = url.parse().ok();

match rpc_playlist::get_known_fullnodes(playlist_url){
Ok(f) => {

let mut new_cfg = match parse_toml(self.app_cfg_path.clone()) { // if None path just use default
Ok(c) => c,
Err(e) => {
println!("could not parse app config toml file, exiting. Message: {:?}", e );
exit(1);
},
};
new_cfg.profile.upstream_nodes = f.get_urls();

println!("peers found:");
new_cfg.profile.upstream_nodes.iter()
.for_each(|u| {
println!("{}", u.as_str())
});

// println!("peers found: {:?}", new_cfg.profile.upstream_nodes);

new_cfg.save_file();
println!("Upstream RPC peers updated in 0L.toml");
return
},
Err(e) => {
println!("could not read playlists from {:?}, exiting. Message: {:?}", url, e);
exit(1);
},
};
}



// fetch a list of seed peers from the current on chain discovery
// doesn't need mnemonic
Expand Down
2 changes: 1 addition & 1 deletion ol/types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn parse_toml(path: Option<PathBuf>) -> Result<AppCfg, Error> {
/// Get a AppCfg object from toml file
pub fn fix_missing_fields(path: PathBuf) -> Result<(), Error> {
let cfg: AppCfg = parse_toml(Some(path))?;
cfg.save_file()?;
cfg.save_file();
Ok(())
}

Expand Down
19 changes: 1 addition & 18 deletions ol/types/src/rpc_playlist.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//! seed peers for connecting to various networks.
use std::path::PathBuf;

use anyhow::Error;
use serde::{Deserialize};
use url::Url;
use rand::{seq::SliceRandom, thread_rng};

use crate::config;

#[derive(Deserialize)]
/// A list of host information for upstream fullnodes serving RPC servers
Expand Down Expand Up @@ -39,24 +34,12 @@ impl FullnodePlaylist {
Ok(play)
}

/// extract the urls from the playlist struct
/// extract the urls from the playlist struct
pub fn get_urls(&self) -> Vec<Url>{
self.nodes.iter()
.filter_map(|a| {
Some(a.url.to_owned())
})
.collect()
}

/// update the app configs 0L.toml file
pub fn update_config_file(&self, path: Option<PathBuf>) -> Result<(), Error> {
let mut new_cfg = config::parse_toml(path)?;
let mut peers = self.get_urls();
let mut rng = thread_rng();
peers.shuffle(&mut rng);

new_cfg.profile.upstream_nodes = peers;

new_cfg.save_file()
}
}

0 comments on commit 5cb8864

Please sign in to comment.