Created
September 19, 2019 01:00
-
-
Save paulhauner/d12168f9f03639af40565cadd0ee78f2 to your computer and use it in GitHub Desktop.
Eth1 build script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use reqwest::Response; | |
use std::env; | |
use std::fs::File; | |
use std::path::PathBuf; | |
const GITHUB_RAW: &str = "https://raw.githubusercontent.com"; | |
const SPEC_REPO: &str = "ethereum/eth2.0-specs"; | |
const SPEC_TAG: &str = "v0.8.3"; | |
const ABI_FILE: &str = "validator_registration.json"; | |
fn main() { | |
match init_deposit_contract_abi() { | |
Ok(()) => (), | |
Err(e) => panic!(e), | |
} | |
} | |
/// Attempts to download the deposit contract ABI from github if a local copy is not already | |
/// present. | |
fn init_deposit_contract_abi() -> Result<(), String> { | |
let local_file = abi_dir().join(format!("{}_{}", SPEC_TAG, ABI_FILE)); | |
if local_file.exists() { | |
// Nothing to do. | |
} else { | |
match download_abi() { | |
Ok(mut response) => { | |
let mut file = File::create(local_file) | |
.map_err(|e| format!("Failed to create local abi file: {:?}", e))?; | |
// TODO: This is broken. The file that is downloaded contains a JSON | |
// object with the `abi` and `bytecode` keys. The program expects only | |
// the `abi` value. We should use serde_json to parse the response and | |
// write only the `abi` list to file. | |
response | |
.copy_to(&mut file) | |
.map_err(|e| format!("Failed to write http response to file: {:?}", e))?; | |
} | |
Err(e) => { | |
return Err(format!( | |
"No abi file found. Failed to download from github: {:?}", | |
e | |
)) | |
} | |
} | |
} | |
Ok(()) | |
} | |
/// Attempts to download the deposit contract file from the Ethereum github. | |
fn download_abi() -> Result<Response, String> { | |
reqwest::get(&format!( | |
"{}/{}/{}/deposit_contract/contracts/{}", | |
GITHUB_RAW, SPEC_REPO, SPEC_TAG, ABI_FILE | |
)) | |
.map_err(|e| format!("Failed to download deposit ABI from github: {:?}", e)) | |
} | |
/// Returns the directory that will be used to store the deposit contract ABI. | |
fn abi_dir() -> PathBuf { | |
let base = env::var("CARGO_MANIFEST_DIR") | |
.expect("should know manifest dir") | |
.parse::<PathBuf>() | |
.expect("should parse manifest dir as path") | |
.join("abi"); | |
std::fs::create_dir_all(base.clone()) | |
.expect("should be able to create abi directory in manifest"); | |
base | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment