Skip to content

Commit

Permalink
VC: accept unknown fields in chain spec
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Mar 22, 2021
1 parent 9a71a7e commit c4e7f5d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
36 changes: 33 additions & 3 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::*;
use int_to_bytes::int_to_bytes4;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use tree_hash::TreeHash;
Expand Down Expand Up @@ -449,10 +450,8 @@ mod tests {
}

/// YAML config file as defined by the spec.
///
/// Spec v0.12.3
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "UPPERCASE", deny_unknown_fields)]
#[serde(rename_all = "UPPERCASE")]
pub struct YamlConfig {
pub config_name: String,
// ChainSpec
Expand Down Expand Up @@ -576,6 +575,10 @@ pub struct YamlConfig {
#[serde(with = "serde_utils::quoted_u64")]
deposit_network_id: u64,
deposit_contract_address: Address,

// Extra fields (could be from a future hard-fork that we don't yet know).
#[serde(flatten)]
pub extra_fields: HashMap<String, String>,
}

impl Default for YamlConfig {
Expand Down Expand Up @@ -671,6 +674,8 @@ impl YamlConfig {
deposit_chain_id: spec.deposit_chain_id,
deposit_network_id: spec.deposit_network_id,
deposit_contract_address: spec.deposit_contract_address,

extra_fields: HashMap::new(),
}
}

Expand Down Expand Up @@ -849,6 +854,31 @@ mod yaml_tests {
assert_eq!(from, yamlconfig);
}

#[test]
fn extra_fields_round_trip() {
let tmp_file = NamedTempFile::new().expect("failed to create temp file");
let writer = OpenOptions::new()
.read(false)
.write(true)
.open(tmp_file.as_ref())
.expect("error opening file");
let mainnet_spec = ChainSpec::mainnet();
let mut yamlconfig = YamlConfig::from_spec::<MainnetEthSpec>(&mainnet_spec);
let (k1, v1) = ("SAMPLE_HARDFORK_KEY1", "123456789");
let (k2, v2) = ("SAMPLE_HARDFORK_KEY2", "987654321");
yamlconfig.extra_fields.insert(k1.into(), v1.into());
yamlconfig.extra_fields.insert(k2.into(), v2.into());
serde_yaml::to_writer(writer, &yamlconfig).expect("failed to write or serialize");

let reader = OpenOptions::new()
.read(true)
.write(false)
.open(tmp_file.as_ref())
.expect("error while opening the file");
let from: YamlConfig = serde_yaml::from_reader(reader).expect("error while deserializing");
assert_eq!(from, yamlconfig);
}

#[test]
fn apply_to_spec() {
let mut spec = ChainSpec::minimal();
Expand Down
8 changes: 8 additions & 0 deletions validator_client/src/beacon_node_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ impl<E: EthSpec> CandidateBeaconNode<E> {
CandidateError::Incompatible
})?;

if !yaml_config.extra_fields.is_empty() {
debug!(
log,
"Beacon spec includes unknown fields";
"fields" => ?yaml_config.extra_fields
);
}

if *spec == beacon_node_spec {
Ok(())
} else {
Expand Down

0 comments on commit c4e7f5d

Please sign in to comment.