2020-05-06 05:24:25 +00:00
|
|
|
//! Downloads a testnet configuration from Github.
|
|
|
|
|
2020-03-03 23:02:44 +00:00
|
|
|
use reqwest;
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-06-29 06:51:26 +00:00
|
|
|
const TESTNET_ID: &str = "altona-v3";
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-03-19 00:22:15 +00:00
|
|
|
if !base_dir().exists() {
|
2020-06-25 14:04:08 +00:00
|
|
|
std::fs::create_dir_all(base_dir())
|
|
|
|
.unwrap_or_else(|_| panic!("Unable to create {:?}", base_dir()));
|
2020-03-19 00:22:15 +00:00
|
|
|
|
|
|
|
match get_all_files() {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(e) => {
|
2020-06-25 14:04:08 +00:00
|
|
|
std::fs::remove_dir_all(base_dir()).unwrap_or_else(|_| panic!(
|
2020-03-19 00:22:15 +00:00
|
|
|
"{}. Failed to remove {:?}, please remove the directory manually because it may contains incomplete testnet data.",
|
|
|
|
e,
|
|
|
|
base_dir(),
|
|
|
|
));
|
|
|
|
panic!(e);
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_all_files() -> Result<(), String> {
|
2020-03-19 00:22:15 +00:00
|
|
|
get_file("boot_enr.yaml")?;
|
|
|
|
get_file("config.yaml")?;
|
|
|
|
get_file("deploy_block.txt")?;
|
|
|
|
get_file("deposit_contract.txt")?;
|
2020-06-28 00:46:49 +00:00
|
|
|
get_file("genesis.ssz")?;
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_file(filename: &str) -> Result<(), String> {
|
|
|
|
let url = format!(
|
2020-06-29 06:51:26 +00:00
|
|
|
"https://raw.githubusercontent.com/sigp/witti/a94e00c1a03df851f960fcf44a79f2a6b1d29af1/altona/lighthouse/{}",
|
2020-05-06 05:24:25 +00:00
|
|
|
filename
|
2020-03-03 23:02:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let path = base_dir().join(filename);
|
|
|
|
let mut file =
|
|
|
|
File::create(path).map_err(|e| format!("Failed to create {}: {:?}", filename, e))?;
|
|
|
|
|
2020-05-17 11:16:48 +00:00
|
|
|
let request = reqwest::blocking::Client::builder()
|
|
|
|
.build()
|
|
|
|
.map_err(|_| "Could not build request client".to_string())?
|
|
|
|
.get(&url)
|
|
|
|
.timeout(std::time::Duration::from_secs(120));
|
|
|
|
|
|
|
|
let contents = request
|
|
|
|
.send()
|
2020-05-06 05:24:25 +00:00
|
|
|
.map_err(|e| format!("Failed to download {}: {}", filename, e))?
|
|
|
|
.error_for_status()
|
2020-05-17 11:16:48 +00:00
|
|
|
.map_err(|e| format!("Error downloading {}: {}", filename, e))?
|
|
|
|
.bytes()
|
2020-03-03 23:02:44 +00:00
|
|
|
.map_err(|e| format!("Failed to read {} response bytes: {}", filename, e))?;
|
|
|
|
|
|
|
|
file.write(&contents)
|
|
|
|
.map_err(|e| format!("Failed to write to {}: {:?}", filename, e))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn base_dir() -> PathBuf {
|
|
|
|
env::var("CARGO_MANIFEST_DIR")
|
|
|
|
.expect("should know manifest dir")
|
|
|
|
.parse::<PathBuf>()
|
|
|
|
.expect("should parse manifest dir as path")
|
|
|
|
.join(TESTNET_ID)
|
|
|
|
}
|