From f43fe4d12323c59229103015668d23332e5c710a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 29 Nov 2019 09:20:57 +1100 Subject: [PATCH] Remove ---/n from the start of testnet_dir files --- eth2/utils/eth2_testnet/src/lib.rs | 59 +++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/eth2/utils/eth2_testnet/src/lib.rs b/eth2/utils/eth2_testnet/src/lib.rs index 4bea7200aa..d014adb1c0 100644 --- a/eth2/utils/eth2_testnet/src/lib.rs +++ b/eth2/utils/eth2_testnet/src/lib.rs @@ -80,8 +80,21 @@ impl Eth2TestnetDir { ($file: ident, $variable: expr) => { File::create(base_dir.join($file)) .map_err(|e| format!("Unable to create {}: {:?}", $file, e)) - .and_then(|file| { - serde_yaml::to_writer(file, &$variable) + .and_then(|mut file| { + let yaml = serde_yaml::to_string(&$variable) + .map_err(|e| format!("Unable to YAML encode {}: {:?}", $file, e))?; + + // Remove the doc header from the YAML file. + // + // This allows us to play nice with other clients that are expecting + // plain-text, not YAML. + let no_doc_header = if yaml.starts_with("---\n") { + &yaml[4..] + } else { + &yaml + }; + + file.write_all(no_doc_header.as_bytes()) .map_err(|e| format!("Unable to write {}: {:?}", $file, e)) })?; }; @@ -183,12 +196,34 @@ impl Eth2TestnetDir { mod tests { use super::*; use tempdir::TempDir; - use types::MinimalEthSpec; + use types::{Eth1Data, Hash256, MinimalEthSpec, YamlConfig}; type E = MinimalEthSpec; #[test] fn round_trip() { + let spec = &E::default_spec(); + + let eth1_data = Eth1Data { + deposit_root: Hash256::zero(), + deposit_count: 0, + block_hash: Hash256::zero(), + }; + + // TODO: figure out how to generate ENR and add some here. + let boot_enr = None; + let genesis_state = Some(BeaconState::new(42, eth1_data, spec)); + let yaml_config = Some(YamlConfig::from_spec::(spec)); + + do_test::(boot_enr, genesis_state.clone(), yaml_config.clone()); + do_test::(None, None, None); + } + + fn do_test( + boot_enr: Option>, + genesis_state: Option>, + yaml_config: Option, + ) { let temp_dir = TempDir::new("eth2_testnet_test").expect("should create temp dir"); let base_dir = PathBuf::from(temp_dir.path().join("my_testnet")); let deposit_contract_address = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413".to_string(); @@ -197,12 +232,9 @@ mod tests { let testnet: Eth2TestnetDir = Eth2TestnetDir { deposit_contract_address: deposit_contract_address.clone(), deposit_contract_deploy_block: deposit_contract_deploy_block, - // TODO: add some Enr for testing. - boot_enr: None, - // TODO: add a genesis state for testing. - genesis_state: None, - // TODO: add a yaml config for testing. - yaml_config: None, + boot_enr, + genesis_state, + yaml_config, }; testnet @@ -211,15 +243,6 @@ mod tests { let decoded = Eth2TestnetDir::load(base_dir).expect("should load struct"); - assert_eq!( - decoded.deposit_contract_address, deposit_contract_address, - "deposit_contract_address" - ); - assert_eq!( - decoded.deposit_contract_deploy_block, deposit_contract_deploy_block, - "deposit_contract_deploy_block" - ); - assert_eq!(testnet, decoded, "should decode as encoded"); } }