Skip to content

Commit

Permalink
[Cli] Retrieve a waypoint value from a given url
Browse files Browse the repository at this point in the history
Summary:
Added a waypoint-url cmd line argument to download the waypoint value from.
These are still optional but we might consider making it a non-optional value with a default pointing to https://developers.libra.org/testnet_waypoint.txt.

Testing:
```
./scripts/cli/start_cli_testnet.sh -- --waypoint-url https://developers.libra.org/testnet_waypoint.txt
./scripts/cli/start_cli_testnet.sh
./scripts/cli/start_cli_testnet.sh -- --waypoint 0:997acd1b112a19eb1d2d3dff78677a0009343727926071c3858aeff2ea3499bf
```

Issues: ref #2425

Closes: #2446
Approved by: dmitri-perelman
  • Loading branch information
Dmitri Perelman authored and bors-libra committed Feb 6, 2020
1 parent d2fff56 commit 126c729
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reqwest = { version = "0.10.1", features = ["blocking"], default-features = fals
serde = { version = "1.0.96", features = ["derive"] }
serde_json = "1.0.40"
structopt = "0.3.2"
ureq = { version = "0.11.3"}

admission-control-proto = { path = "../../admission_control/admission-control-proto", version = "0.1.0" }
libra-config = { path = "../../config", version = "0.1.0" }
Expand Down
49 changes: 45 additions & 4 deletions client/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ use cli::{
use libra_logger::set_default_global_logger;
use libra_types::waypoint::Waypoint;
use rustyline::{config::CompletionType, error::ReadlineError, Config, Editor};
use std::num::NonZeroU16;
use std::time::{Duration, UNIX_EPOCH};
use std::{
num::NonZeroU16,
str::FromStr,
time::{Duration, UNIX_EPOCH},
};
use structopt::StructOpt;
use ureq;

#[derive(Debug, StructOpt)]
#[structopt(
Expand Down Expand Up @@ -51,8 +55,18 @@ struct Args {
#[structopt(short = "r", long = "sync")]
pub sync: bool,
/// If set, a client uses the waypoint parameter for its initial LedgerInfo verification.
#[structopt(name = "waypoint", short, long)]
#[structopt(
name = "waypoint",
long,
help = "Explicitly specify the waypoint to use"
)]
pub waypoint: Option<Waypoint>,
#[structopt(
name = "waypoint_url",
long,
help = "URL for a file with the waypoint to use"
)]
pub waypoint_url: Option<String>,
/// Verbose output.
#[structopt(short = "v", long = "verbose")]
pub verbose: bool,
Expand All @@ -70,14 +84,25 @@ fn main() {
.clone()
.unwrap_or_else(|| "".to_string());
let mnemonic_file = args.mnemonic_file.clone();

// If waypoint is given explicitly, use its value,
// otherwise if waypoint_url is given, try to retrieve the waypoint from the URL,
// otherwise waypoint is None.
let waypoint = args.waypoint.or_else(|| {
args.waypoint_url.as_ref().map(|url_str| {
retrieve_waypoint(url_str.as_str()).unwrap_or_else(|e| {
panic!("Failure to retrieve a waypoint from {}: {}", url_str, e)
})
})
});
let mut client_proxy = ClientProxy::new(
&args.host,
args.port.get(),
&faucet_account_file,
args.sync,
args.faucet_server.clone(),
mnemonic_file,
args.waypoint,
waypoint,
)
.expect("Failed to construct client.");

Expand Down Expand Up @@ -181,6 +206,22 @@ fn print_help(client_info: &str, commands: &[std::sync::Arc<dyn Command>]) {
println!("\n");
}

/// Retrieve a waypoint given the URL.
fn retrieve_waypoint(url_str: &str) -> anyhow::Result<Waypoint> {
let response = ureq::get(url_str).timeout_connect(10_000).call();
match response.status() {
200 => response
.into_string()
.map_err(|_| anyhow::format_err!("Failed to parse waypoint from URL {}", url_str))
.and_then(|r| Waypoint::from_str(r.trim())),
_ => Err(anyhow::format_err!(
"URL {} returned {}",
url_str,
response.status_line()
)),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
16 changes: 14 additions & 2 deletions types/src/waypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,21 @@ impl Waypoint {

/// Errors in case the given ledger info does not match the waypoint.
pub fn verify(&self, ledger_info: &LedgerInfo) -> Result<()> {
ensure!(ledger_info.version() == self.version(), "Version mismatch");
ensure!(
ledger_info.version() == self.version(),
"Waypoint version mismatch: waypoint version = {}, given version = {}",
self.version(),
ledger_info.version()
);
let converter = Ledger2WaypointConverter::new(ledger_info)?;
ensure!(converter.hash() == self.value(), "HashValue mismatch");
ensure!(
converter.hash() == self.value(),
format!(
"Waypoint value mismatch: waypoint value = {}, given value = {}",
self.value().to_hex(),
converter.hash().to_hex()
)
);
Ok(())
}
}
Expand Down

0 comments on commit 126c729

Please sign in to comment.