Allow specifying alternative url for deposit_contract (#2295)
## Issue Addressed None ## Proposed Changes Adds support for downloading the deposit contract from a different location by setting the environement variables `LIGHTHOUSE_DEPOSIT_CONTRACT_SPEC_URL` and `LIGHTHOUSE_DEPOSIT_CONTRACT_TESTNET_URL`. It also adds support to fetch the content from a local file:// URL. This allows pre fetching to build in an environment without network access. ## Additional Info Being able to build without network access is required to package the application for https://nixos.org/. But I imagine it might be useful for other distributions too.
This commit is contained in:
parent
2992ca66cd
commit
c646d2f7a3
@ -3,6 +3,7 @@
|
|||||||
//!
|
//!
|
||||||
//! These files are required for some `include_bytes` calls used in this crate.
|
//! These files are required for some `include_bytes` calls used in this crate.
|
||||||
|
|
||||||
|
use reqwest::Url;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
use std::env;
|
use std::env;
|
||||||
@ -26,10 +27,41 @@ const TESTNET_BYTECODE_CHECKSUM: &str =
|
|||||||
"2b054e7d134e2d66566ba074c8a18a3a67841d67c8ef6175fc95f1639ee73a89";
|
"2b054e7d134e2d66566ba074c8a18a3a67841d67c8ef6175fc95f1639ee73a89";
|
||||||
|
|
||||||
fn spec_url() -> String {
|
fn spec_url() -> String {
|
||||||
format!("https://raw.githubusercontent.com/ethereum/eth2.0-specs/{}/deposit_contract/contracts/validator_registration.json", TAG)
|
env::var("LIGHTHOUSE_DEPOSIT_CONTRACT_SPEC_URL")
|
||||||
|
.unwrap_or(format!("https://raw.githubusercontent.com/ethereum/eth2.0-specs/{}/deposit_contract/contracts/validator_registration.json", TAG))
|
||||||
}
|
}
|
||||||
fn testnet_url() -> String {
|
fn testnet_url() -> String {
|
||||||
format!("https://raw.githubusercontent.com/sigp/unsafe-eth2-deposit-contract/{}/unsafe_validator_registration.json", UNSAFE_TAG)
|
env::var("LIGHTHOUSE_DEPOSIT_CONTRACT_TESTNET_URL")
|
||||||
|
.unwrap_or(format!("https://raw.githubusercontent.com/sigp/unsafe-eth2-deposit-contract/{}/unsafe_validator_registration.json", UNSAFE_TAG))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_contract_file_from_url(url: Url) -> Result<Value, String> {
|
||||||
|
if url.scheme() == "file" {
|
||||||
|
let path = url
|
||||||
|
.to_file_path()
|
||||||
|
.map_err(|e| format!("Unable to get file path from url: {:?}", e))?;
|
||||||
|
|
||||||
|
let file = File::open(path).map_err(|e| format!("Unable to open json file: {:?}", e))?;
|
||||||
|
|
||||||
|
let contract: Value = serde_json::from_reader(file)
|
||||||
|
.map_err(|e| format!("Unable to read from jeson file: {:?}", e))?;
|
||||||
|
Ok(contract)
|
||||||
|
} else {
|
||||||
|
match reqwest::blocking::get(url) {
|
||||||
|
Ok(response) => {
|
||||||
|
let contract: Value = response
|
||||||
|
.json()
|
||||||
|
.map_err(|e| format!("Respsonse is not a valid json {:?}", e))?;
|
||||||
|
Ok(contract)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(format!(
|
||||||
|
"No abi file found. Failed to download from github: {:?}",
|
||||||
|
e
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -69,21 +101,18 @@ pub fn download_deposit_contract(
|
|||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let abi_file = abi_dir().join(format!("{}_{}", TAG, abi_file));
|
let abi_file = abi_dir().join(format!("{}_{}", TAG, abi_file));
|
||||||
let bytecode_file = abi_dir().join(format!("{}_{}", TAG, bytecode_file));
|
let bytecode_file = abi_dir().join(format!("{}_{}", TAG, bytecode_file));
|
||||||
|
let url = reqwest::Url::parse(url).map_err(|e| format!("Unable to parse url: {}", e))?;
|
||||||
|
|
||||||
if abi_file.exists() {
|
if abi_file.exists() {
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
} else {
|
} else {
|
||||||
match reqwest::blocking::get(url) {
|
let contract = read_contract_file_from_url(url)?;
|
||||||
Ok(response) => {
|
|
||||||
let mut abi_file = File::create(abi_file)
|
let mut abi_file = File::create(abi_file)
|
||||||
.map_err(|e| format!("Failed to create local abi file: {:?}", e))?;
|
.map_err(|e| format!("Failed to create local abi file: {:?}", e))?;
|
||||||
let mut bytecode_file = File::create(bytecode_file)
|
let mut bytecode_file = File::create(bytecode_file)
|
||||||
.map_err(|e| format!("Failed to create local bytecode file: {:?}", e))?;
|
.map_err(|e| format!("Failed to create local bytecode file: {:?}", e))?;
|
||||||
|
|
||||||
let contract: Value = response
|
|
||||||
.json()
|
|
||||||
.map_err(|e| format!("Respsonse is not a valid json {:?}", e))?;
|
|
||||||
|
|
||||||
let abi = contract
|
let abi = contract
|
||||||
.get("abi")
|
.get("abi")
|
||||||
.ok_or("Response does not contain key: abi")?
|
.ok_or("Response does not contain key: abi")?
|
||||||
@ -102,17 +131,9 @@ pub fn download_deposit_contract(
|
|||||||
|
|
||||||
verify_checksum(bytecode.as_bytes(), bytecode_checksum);
|
verify_checksum(bytecode.as_bytes(), bytecode_checksum);
|
||||||
|
|
||||||
bytecode_file.write(bytecode.as_bytes()).map_err(|e| {
|
bytecode_file
|
||||||
format!("Failed to write http response to bytecode file: {:?}", e)
|
.write(bytecode.as_bytes())
|
||||||
})?;
|
.map_err(|e| format!("Failed to write http response to bytecode file: {:?}", e))?;
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(format!(
|
|
||||||
"No abi file found. Failed to download from github: {:?}",
|
|
||||||
e
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user