Skip to content

Commit

Permalink
Deserialize u64::max from config as None
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Aug 24, 2021
1 parent aa9170a commit 136c637
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::*;
use int_to_bytes::int_to_bytes4;
use serde::Deserializer;
use serde_derive::{Deserialize, Serialize};
use serde_utils::quoted_u64::MaybeQuoted;
use std::fs::File;
Expand Down Expand Up @@ -467,7 +468,7 @@ impl ChainSpec {
domain_sync_committee_selection_proof: 8,
domain_contribution_and_proof: 9,
altair_fork_version: [0x01, 0x00, 0x00, 0x00],
altair_fork_epoch: Some(Epoch::new(u64::MAX)),
altair_fork_epoch: None,

/*
* Network specific
Expand Down Expand Up @@ -506,7 +507,7 @@ impl ChainSpec {
// Altair
epochs_per_sync_committee_period: Epoch::new(8),
altair_fork_version: [0x01, 0x00, 0x00, 0x01],
altair_fork_epoch: Some(Epoch::new(u64::MAX)),
altair_fork_epoch: None,
// Other
network_id: 2, // lighthouse testnet network id
deposit_chain_id: 5,
Expand Down Expand Up @@ -544,6 +545,7 @@ pub struct Config {

#[serde(with = "serde_utils::bytes_4_hex")]
altair_fork_version: [u8; 4],
#[serde(deserialize_with = "deserialize_fork_epoch")]
altair_fork_epoch: Option<MaybeQuoted<Epoch>>,

#[serde(with = "serde_utils::quoted_u64")]
Expand Down Expand Up @@ -582,6 +584,20 @@ impl Default for Config {
}
}

/// Util function to deserialize a u64::max() fork epoch as `None`.
fn deserialize_fork_epoch<'de, D>(deserializer: D) -> Result<Option<MaybeQuoted<Epoch>>, D::Error>
where
D: Deserializer<'de>,
{
let decoded: Option<MaybeQuoted<Epoch>> = serde::de::Deserialize::deserialize(deserializer)?;
if let Some(fork_epoch) = decoded {
if fork_epoch.value != Epoch::max_value() {
return Ok(Some(fork_epoch));
}
}
Ok(None)
}

impl Config {
/// Maps `self` to an identifier for an `EthSpec` instance.
///
Expand All @@ -606,7 +622,7 @@ impl Config {
altair_fork_version: spec.altair_fork_version,
altair_fork_epoch: spec
.altair_fork_epoch
.map(|slot| MaybeQuoted { value: slot }),
.map(|epoch| MaybeQuoted { value: epoch }),

seconds_per_slot: spec.seconds_per_slot,
seconds_per_eth1_block: spec.seconds_per_eth1_block,
Expand Down

0 comments on commit 136c637

Please sign in to comment.