2020-12-16 03:44:05 +00:00
|
|
|
//! Extracts zipped genesis states on first run.
|
2020-10-25 22:15:46 +00:00
|
|
|
use eth2_config::{
|
2020-11-16 05:11:35 +00:00
|
|
|
altona, mainnet, medalla, pyrmont, spadina, toledo, Eth2NetArchiveAndDirectory,
|
|
|
|
GENESIS_FILE_NAME,
|
2020-10-25 22:15:46 +00:00
|
|
|
};
|
2020-03-03 23:02:44 +00:00
|
|
|
use std::fs::File;
|
2020-09-11 01:43:13 +00:00
|
|
|
use std::io;
|
|
|
|
use zip::ZipArchive;
|
2020-03-03 23:02:44 +00:00
|
|
|
|
2020-09-26 01:58:31 +00:00
|
|
|
const ETH2_NET_DIRS: &[Eth2NetArchiveAndDirectory<'static>] = &[
|
|
|
|
altona::ETH2_NET_DIR,
|
|
|
|
medalla::ETH2_NET_DIR,
|
|
|
|
spadina::ETH2_NET_DIR,
|
2020-11-16 05:11:35 +00:00
|
|
|
mainnet::ETH2_NET_DIR,
|
|
|
|
pyrmont::ETH2_NET_DIR,
|
2020-11-10 22:42:59 +00:00
|
|
|
toledo::ETH2_NET_DIR,
|
2020-09-26 01:58:31 +00:00
|
|
|
];
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-12-08 05:41:10 +00:00
|
|
|
for network in ETH2_NET_DIRS {
|
|
|
|
match uncompress_state(network) {
|
2020-09-26 01:58:31 +00:00
|
|
|
Ok(()) => (),
|
2020-10-25 22:15:46 +00:00
|
|
|
Err(e) => panic!(
|
|
|
|
"Failed to uncompress {} genesis state zip file: {}",
|
2020-12-08 05:41:10 +00:00
|
|
|
network.name, e
|
2020-10-25 22:15:46 +00:00
|
|
|
),
|
2020-03-19 00:22:15 +00:00
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:41:10 +00:00
|
|
|
/// Uncompress the network configs archive into a network configs folder.
|
|
|
|
fn uncompress_state(network: &Eth2NetArchiveAndDirectory<'static>) -> Result<(), String> {
|
2020-12-16 03:44:05 +00:00
|
|
|
let genesis_ssz_path = network.dir().join(GENESIS_FILE_NAME);
|
|
|
|
|
|
|
|
// Take care to not overwrite the genesis.ssz if it already exists, as that causes
|
|
|
|
// spurious rebuilds.
|
|
|
|
if genesis_ssz_path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:41:10 +00:00
|
|
|
if network.genesis_is_known {
|
2020-12-16 03:44:05 +00:00
|
|
|
// Extract genesis state from genesis.ssz.zip
|
2020-12-08 05:41:10 +00:00
|
|
|
let archive_path = network.genesis_state_archive();
|
2020-11-16 05:11:35 +00:00
|
|
|
let archive_file = File::open(&archive_path)
|
|
|
|
.map_err(|e| format!("Failed to open archive file {:?}: {:?}", archive_path, e))?;
|
2020-09-26 01:58:31 +00:00
|
|
|
|
2020-11-16 05:11:35 +00:00
|
|
|
let mut archive =
|
|
|
|
ZipArchive::new(archive_file).map_err(|e| format!("Error with zip file: {}", e))?;
|
2020-09-26 01:58:31 +00:00
|
|
|
|
2020-10-25 22:15:46 +00:00
|
|
|
let mut file = archive.by_name(GENESIS_FILE_NAME).map_err(|e| {
|
|
|
|
format!(
|
|
|
|
"Error retrieving file {} inside zip: {}",
|
|
|
|
GENESIS_FILE_NAME, e
|
|
|
|
)
|
|
|
|
})?;
|
2020-12-16 03:44:05 +00:00
|
|
|
let mut outfile = File::create(&genesis_ssz_path)
|
|
|
|
.map_err(|e| format!("Error while creating file {:?}: {}", genesis_ssz_path, e))?;
|
2020-09-26 01:58:31 +00:00
|
|
|
io::copy(&mut file, &mut outfile)
|
2020-12-16 03:44:05 +00:00
|
|
|
.map_err(|e| format!("Error writing file {:?}: {}", genesis_ssz_path, e))?;
|
2020-10-25 22:15:46 +00:00
|
|
|
} else {
|
|
|
|
// Create empty genesis.ssz if genesis is unknown
|
2020-12-16 03:44:05 +00:00
|
|
|
File::create(genesis_ssz_path)
|
|
|
|
.map_err(|e| format!("Failed to create {}: {}", GENESIS_FILE_NAME, e))?;
|
2020-09-11 01:43:13 +00:00
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|