Skip to content

Commit

Permalink
Serialize altair fork epoch as u64::max
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Aug 27, 2021
1 parent 0ef0cfd commit 6e3b210
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
3 changes: 2 additions & 1 deletion beacon_node/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ fn spawn_service<T: BeaconChainTypes>(
"peer" => %source,
"fork_topic" => %topic,
);
service.libp2p.report_peer(&source, PeerAction::LowToleranceError, ReportSource::Gossipsub);
// Penalize with MidTolerance to account for slight clock skews beyond `MAXIMUM_GOSSIP_CLOCK_DISPARITY`
service.libp2p.report_peer(&source, PeerAction::MidToleranceError, ReportSource::Gossipsub);
service
.libp2p
.swarm
Expand Down
22 changes: 19 additions & 3 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::*;
use int_to_bytes::int_to_bytes4;
use serde::Deserializer;
use serde_derive::{Deserialize, Serialize};
use serde::{Deserializer, Serialize, Serializer};
use serde_derive::Deserialize;
use serde_utils::quoted_u64::MaybeQuoted;
use std::fs::File;
use std::path::Path;
Expand Down Expand Up @@ -545,8 +545,9 @@ pub struct Config {

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

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

/// Util function to serialize a `None` fork epoch value
/// as `Epoch::max_value()`.
fn serialize_fork_epoch<S>(val: &Option<MaybeQuoted<Epoch>>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match val {
None => MaybeQuoted {
value: Epoch::max_value(),
}
.serialize(s),
Some(epoch) => epoch.serialize(s),
}
}

/// 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
Expand Down
17 changes: 6 additions & 11 deletions consensus/types/src/fork_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ impl ForkContext {
)];

// Only add Altair to list of forks if it's enabled
// Note: `altair_fork_epoch = None | Some(Epoch::max_value())` implies altair hasn't been activated yet on the config.
if let Some(altair_epoch) = spec.altair_fork_epoch {
if altair_epoch != Epoch::max_value() {
fork_to_digest.push((
ForkName::Altair,
ChainSpec::compute_fork_digest(
spec.altair_fork_version,
genesis_validators_root,
),
));
}
// Note: `altair_fork_epoch == None` implies altair hasn't been activated yet on the config.
if spec.altair_fork_epoch.is_some() {
fork_to_digest.push((
ForkName::Altair,
ChainSpec::compute_fork_digest(spec.altair_fork_version, genesis_validators_root),
));
}

let fork_to_digest: HashMap<ForkName, [u8; 4]> = fork_to_digest.into_iter().collect();
Expand Down

0 comments on commit 6e3b210

Please sign in to comment.