2020-03-04 03:28:02 +00:00
|
|
|
use clap::ArgMatches;
|
2021-07-09 06:15:32 +00:00
|
|
|
use clap_utils::{parse_optional, parse_required, parse_ssz_optional};
|
2020-12-08 05:41:10 +00:00
|
|
|
use eth2_network_config::Eth2NetworkConfig;
|
2021-10-02 19:57:23 +00:00
|
|
|
use genesis::interop_genesis_state;
|
2021-10-08 00:51:35 +00:00
|
|
|
use ssz::Decode;
|
2021-10-02 19:57:23 +00:00
|
|
|
use ssz::Encode;
|
2021-10-08 00:51:35 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2020-03-04 03:28:02 +00:00
|
|
|
use std::path::PathBuf;
|
2021-10-02 19:57:23 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2021-10-08 00:51:35 +00:00
|
|
|
use types::{
|
|
|
|
test_utils::generate_deterministic_keypairs, Address, Config, EthSpec, ExecutionPayloadHeader,
|
2022-10-26 19:15:26 +00:00
|
|
|
ExecutionPayloadHeaderMerge,
|
2021-10-08 00:51:35 +00:00
|
|
|
};
|
2020-03-04 03:28:02 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
pub fn run<T: EthSpec>(testnet_dir_path: PathBuf, matches: &ArgMatches) -> Result<(), String> {
|
2020-04-19 02:20:43 +00:00
|
|
|
let deposit_contract_address: Address = parse_required(matches, "deposit-contract-address")?;
|
2020-04-20 01:58:39 +00:00
|
|
|
let deposit_contract_deploy_block = parse_required(matches, "deposit-contract-deploy-block")?;
|
2020-03-04 03:28:02 +00:00
|
|
|
|
2020-04-23 09:01:16 +00:00
|
|
|
let overwrite_files = matches.is_present("force");
|
|
|
|
|
2020-07-23 14:18:00 +00:00
|
|
|
if testnet_dir_path.exists() && !overwrite_files {
|
|
|
|
return Err(format!(
|
|
|
|
"{:?} already exists, will not overwrite. Use --force to overwrite",
|
|
|
|
testnet_dir_path
|
|
|
|
));
|
2020-03-04 03:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut spec = T::default_spec();
|
2020-04-20 01:58:39 +00:00
|
|
|
|
|
|
|
// Update the spec value if the flag was defined. Otherwise, leave it as the default.
|
|
|
|
macro_rules! maybe_update {
|
|
|
|
($flag: tt, $var: ident) => {
|
|
|
|
if let Some(val) = parse_optional(matches, $flag)? {
|
|
|
|
spec.$var = val
|
|
|
|
}
|
|
|
|
};
|
2020-03-04 03:28:02 +00:00
|
|
|
}
|
2020-04-20 01:58:39 +00:00
|
|
|
|
2020-11-23 23:54:03 +00:00
|
|
|
spec.deposit_contract_address = deposit_contract_address;
|
|
|
|
|
2020-04-20 01:58:39 +00:00
|
|
|
maybe_update!("min-genesis-time", min_genesis_time);
|
|
|
|
maybe_update!("min-deposit-amount", min_deposit_amount);
|
|
|
|
maybe_update!(
|
|
|
|
"min-genesis-active-validator-count",
|
|
|
|
min_genesis_active_validator_count
|
|
|
|
);
|
|
|
|
maybe_update!("max-effective-balance", max_effective_balance);
|
|
|
|
maybe_update!("effective-balance-increment", effective_balance_increment);
|
|
|
|
maybe_update!("ejection-balance", ejection_balance);
|
2020-05-03 22:03:31 +00:00
|
|
|
maybe_update!("eth1-follow-distance", eth1_follow_distance);
|
2020-06-11 00:07:10 +00:00
|
|
|
maybe_update!("genesis-delay", genesis_delay);
|
2021-03-30 05:17:58 +00:00
|
|
|
maybe_update!("eth1-id", deposit_chain_id);
|
|
|
|
maybe_update!("eth1-id", deposit_network_id);
|
2021-07-31 03:50:52 +00:00
|
|
|
maybe_update!("seconds-per-slot", seconds_per_slot);
|
2021-03-30 05:17:58 +00:00
|
|
|
maybe_update!("seconds-per-eth1-block", seconds_per_eth1_block);
|
2020-04-20 01:58:39 +00:00
|
|
|
|
|
|
|
if let Some(v) = parse_ssz_optional(matches, "genesis-fork-version")? {
|
2020-03-04 03:28:02 +00:00
|
|
|
spec.genesis_fork_version = v;
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
if let Some(fork_epoch) = parse_optional(matches, "altair-fork-epoch")? {
|
|
|
|
spec.altair_fork_epoch = Some(fork_epoch);
|
|
|
|
}
|
|
|
|
|
2021-10-02 19:57:23 +00:00
|
|
|
if let Some(fork_epoch) = parse_optional(matches, "merge-fork-epoch")? {
|
2022-01-19 00:24:19 +00:00
|
|
|
spec.bellatrix_fork_epoch = Some(fork_epoch);
|
2021-10-02 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let genesis_state_bytes = if matches.is_present("interop-genesis-state") {
|
2021-10-08 00:51:35 +00:00
|
|
|
let execution_payload_header: Option<ExecutionPayloadHeader<T>> =
|
|
|
|
parse_optional(matches, "execution-payload-header")?
|
|
|
|
.map(|filename: String| {
|
|
|
|
let mut bytes = vec![];
|
|
|
|
let mut file = File::open(filename.as_str())
|
|
|
|
.map_err(|e| format!("Unable to open {}: {}", filename, e))?;
|
|
|
|
file.read_to_end(&mut bytes)
|
|
|
|
.map_err(|e| format!("Unable to read {}: {}", filename, e))?;
|
2022-10-26 19:15:26 +00:00
|
|
|
//FIXME(sean)
|
|
|
|
ExecutionPayloadHeaderMerge::<T>::from_ssz_bytes(bytes.as_slice())
|
|
|
|
.map(ExecutionPayloadHeader::Merge)
|
2021-10-08 00:51:35 +00:00
|
|
|
.map_err(|e| format!("SSZ decode failed: {:?}", e))
|
|
|
|
})
|
|
|
|
.transpose()?;
|
|
|
|
|
|
|
|
let (eth1_block_hash, genesis_time) = if let Some(payload) =
|
|
|
|
execution_payload_header.as_ref()
|
|
|
|
{
|
|
|
|
let eth1_block_hash =
|
2022-10-26 19:15:26 +00:00
|
|
|
parse_optional(matches, "eth1-block-hash")?.unwrap_or(payload.block_hash());
|
2021-10-08 00:51:35 +00:00
|
|
|
let genesis_time =
|
2022-10-26 19:15:26 +00:00
|
|
|
parse_optional(matches, "genesis-time")?.unwrap_or(payload.timestamp());
|
2021-10-08 00:51:35 +00:00
|
|
|
(eth1_block_hash, genesis_time)
|
2021-10-02 19:57:23 +00:00
|
|
|
} else {
|
2021-10-08 00:51:35 +00:00
|
|
|
let eth1_block_hash = parse_required(matches, "eth1-block-hash").map_err(|_| {
|
|
|
|
"One of `--execution-payload-header` or `--eth1-block-hash` must be set".to_string()
|
|
|
|
})?;
|
|
|
|
let genesis_time = parse_optional(matches, "genesis-time")?.unwrap_or(
|
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.map_err(|e| format!("Unable to get time: {:?}", e))?
|
|
|
|
.as_secs(),
|
|
|
|
);
|
|
|
|
(eth1_block_hash, genesis_time)
|
2021-10-02 19:57:23 +00:00
|
|
|
};
|
|
|
|
|
2021-10-08 00:51:35 +00:00
|
|
|
let validator_count = parse_required(matches, "validator-count")?;
|
|
|
|
|
2021-10-02 19:57:23 +00:00
|
|
|
let keypairs = generate_deterministic_keypairs(validator_count);
|
2021-10-08 00:51:35 +00:00
|
|
|
|
|
|
|
let genesis_state = interop_genesis_state::<T>(
|
|
|
|
&keypairs,
|
|
|
|
genesis_time,
|
2022-02-28 22:07:48 +00:00
|
|
|
eth1_block_hash.into_root(),
|
2021-10-08 00:51:35 +00:00
|
|
|
execution_payload_header,
|
|
|
|
&spec,
|
|
|
|
)?;
|
2021-10-02 19:57:23 +00:00
|
|
|
|
|
|
|
Some(genesis_state.as_ssz_bytes())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-12-08 05:41:10 +00:00
|
|
|
let testnet = Eth2NetworkConfig {
|
2020-03-04 03:28:02 +00:00
|
|
|
deposit_contract_deploy_block,
|
|
|
|
boot_enr: Some(vec![]),
|
2021-10-02 19:57:23 +00:00
|
|
|
genesis_state_bytes,
|
2021-07-09 06:15:32 +00:00
|
|
|
config: Config::from_chain_spec::<T>(&spec),
|
2020-03-04 03:28:02 +00:00
|
|
|
};
|
|
|
|
|
2020-04-23 09:01:16 +00:00
|
|
|
testnet.write_to_file(testnet_dir_path, overwrite_files)
|
2020-03-04 03:28:02 +00:00
|
|
|
}
|