Skip to content

Commit

Permalink
Merge pull request #293 from ralexstokes/toml-serde
Browse files Browse the repository at this point in the history
Toml serde
  • Loading branch information
ralexstokes authored Oct 18, 2023
2 parents d82e213 + 0f6c7be commit c84f44e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
1 change: 1 addition & 0 deletions ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ unicode-normalization = { version = "0.1.22", optional = true }
[dev-dependencies]
serde_with = "1.13.0"
snap = "1"
toml = "0.8.2"

[[bin]]
name = "ec"
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/altair/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ pub fn compute_fork_digest(
genesis_validators_root: Root,
) -> Result<ForkDigest> {
let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?;
let digest = &fork_data_root.as_ref()[..4];
let digest = &fork_data_root[..4];
Ok(digest.try_into().expect("should not fail"))
}
pub fn compute_domain(
Expand All @@ -1243,7 +1243,7 @@ pub fn compute_domain(
let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?;
let mut domain = Domain::default();
domain[..4].copy_from_slice(&domain_type.as_bytes());
domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]);
domain[4..].copy_from_slice(&fork_data_root[..28]);
Ok(domain)
}
pub fn compute_fork_data_root(
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/bellatrix/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ pub fn compute_fork_digest(
genesis_validators_root: Root,
) -> Result<ForkDigest> {
let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?;
let digest = &fork_data_root.as_ref()[..4];
let digest = &fork_data_root[..4];
Ok(digest.try_into().expect("should not fail"))
}
pub fn compute_domain(
Expand All @@ -2192,7 +2192,7 @@ pub fn compute_domain(
let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?;
let mut domain = Domain::default();
domain[..4].copy_from_slice(&domain_type.as_bytes());
domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]);
domain[4..].copy_from_slice(&fork_data_root[..28]);
Ok(domain)
}
pub fn compute_fork_data_root(
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/capella/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ pub fn compute_fork_digest(
genesis_validators_root: Root,
) -> Result<ForkDigest> {
let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?;
let digest = &fork_data_root.as_ref()[..4];
let digest = &fork_data_root[..4];
Ok(digest.try_into().expect("should not fail"))
}
pub fn compute_domain(
Expand All @@ -2455,7 +2455,7 @@ pub fn compute_domain(
let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?;
let mut domain = Domain::default();
domain[..4].copy_from_slice(&domain_type.as_bytes());
domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]);
domain[4..].copy_from_slice(&fork_data_root[..28]);
Ok(domain)
}
pub fn compute_fork_data_root(
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/deneb/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,7 @@ pub fn compute_fork_digest(
genesis_validators_root: Root,
) -> Result<ForkDigest> {
let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?;
let digest = &fork_data_root.as_ref()[..4];
let digest = &fork_data_root[..4];
Ok(digest.try_into().expect("should not fail"))
}
pub fn compute_domain(
Expand All @@ -2526,7 +2526,7 @@ pub fn compute_domain(
let fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)?;
let mut domain = Domain::default();
domain[..4].copy_from_slice(&domain_type.as_bytes());
domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]);
domain[4..].copy_from_slice(&fork_data_root[..28]);
Ok(domain)
}
pub fn compute_fork_data_root(
Expand Down
42 changes: 39 additions & 3 deletions ethereum-consensus/src/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::Error;
/// `Network` describes one of the established networks this repository supports
/// or otherwise a `Custom` variant that wraps a path to a local configuration file
/// for the custom network (useful for devnets).
#[derive(Default, Debug, Clone, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "lowercase", into = "String", from = "String")]
pub enum Network {
#[default]
Mainnet,
Expand All @@ -23,7 +23,25 @@ impl std::fmt::Display for Network {
Self::Sepolia => write!(f, "sepolia"),
Self::Goerli => write!(f, "goerli"),
Self::Holesky => write!(f, "holesky"),
Self::Custom(config_file) => write!(f, "custom network with config at `{config_file}`"),
Self::Custom(config_file) => write!(f, "{config_file}"),
}
}
}

impl From<Network> for String {
fn from(value: Network) -> Self {
format!("{value}")
}
}

impl From<String> for Network {
fn from(value: String) -> Self {
match value.as_str() {
"mainnet" => Self::Mainnet,
"sepolia" => Self::Sepolia,
"goerli" => Self::Goerli,
"holesky" => Self::Holesky,
_ => Self::Custom(value),
}
}
}
Expand All @@ -47,3 +65,21 @@ impl TryFrom<&Network> for Context {
pub fn typical_genesis_time(context: &Context) -> u64 {
context.min_genesis_time + context.genesis_delay
}

#[cfg(test)]
mod tests {
use super::*;

#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct File {
network: Network,
}

#[test]
fn test_serde() {
let file = File { network: Network::Custom("/path/to/foo.yaml".to_string()) };
let str = toml::to_string(&file).unwrap();
let recovered_file: File = toml::from_str(&str).unwrap();
assert_eq!(file, recovered_file);
}
}
4 changes: 2 additions & 2 deletions ethereum-consensus/src/phase0/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub fn compute_fork_digest(
genesis_validators_root: Root,
) -> Result<ForkDigest> {
let fork_data_root = compute_fork_data_root(current_version, genesis_validators_root)?;
let digest = &fork_data_root.as_ref()[..4];
let digest = &fork_data_root[..4];
Ok(digest.try_into().expect("should not fail"))
}

Expand All @@ -382,7 +382,7 @@ pub fn compute_domain(

let mut domain = Domain::default();
domain[..4].copy_from_slice(&domain_type.as_bytes());
domain[4..].copy_from_slice(&fork_data_root.as_ref()[..28]);
domain[4..].copy_from_slice(&fork_data_root[..28]);
Ok(domain)
}

Expand Down

0 comments on commit c84f44e

Please sign in to comment.