Skip to content

Commit

Permalink
Implement serde quoted integers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner authored and michaelsproul committed Aug 31, 2020
1 parent f68d9d4 commit 7784a78
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 82 deletions.
1 change: 1 addition & 0 deletions consensus/types/src/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tree_hash_derive::TreeHash;
#[serde(bound = "T: EthSpec")]
pub struct BeaconBlock<T: EthSpec> {
pub slot: Slot,
#[serde(with = "crate::serde_utils::quoted")]
pub proposer_index: u64,
pub parent_root: Hash256,
pub state_root: Hash256,
Expand Down
6 changes: 1 addition & 5 deletions consensus/types/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::test_utils::TestRandom;
use crate::utils::{graffiti_from_hex_str, graffiti_to_hex_str, Graffiti};
use crate::*;

use serde_derive::{Deserialize, Serialize};
Expand All @@ -17,10 +16,7 @@ use tree_hash_derive::TreeHash;
pub struct BeaconBlockBody<T: EthSpec> {
pub randao_reveal: Signature,
pub eth1_data: Eth1Data,
#[serde(
serialize_with = "graffiti_to_hex_str",
deserialize_with = "graffiti_from_hex_str"
)]
#[serde(with = "crate::serde_utils::graffiti")]
pub graffiti: Graffiti,
pub proposer_slashings: VariableList<ProposerSlashing, T::MaxProposerSlashings>,
pub attester_slashings: VariableList<AttesterSlashing<T>, T::MaxAttesterSlashings>,
Expand Down
7 changes: 7 additions & 0 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@ impl<T: EthSpec> BeaconState<T> {
self.exit_cache = ExitCache::default();
}

/// Returns `true` if the committee cache for `relative_epoch` is built and ready to use.
pub fn committee_cache_is_initialized(&self, relative_epoch: RelativeEpoch) -> bool {
let i = Self::committee_cache_index(relative_epoch);

self.committee_caches[i].is_initialized_at(relative_epoch.into_epoch(self.current_epoch()))
}

/// Build an epoch cache, unless it is has already been built.
pub fn build_committee_cache(
&mut self,
Expand Down
58 changes: 12 additions & 46 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use serde_derive::{Deserialize, Serialize};
use std::fs::File;
use std::path::Path;
use tree_hash::TreeHash;
use utils::{
fork_from_hex_str, fork_to_hex_str, u32_from_hex_str, u32_to_hex_str, u8_from_hex_str,
u8_to_hex_str,
};

/// Each of the BLS signature domains.
///
Expand Down Expand Up @@ -64,12 +60,9 @@ pub struct ChainSpec {
/*
* Initial Values
*/
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub genesis_fork_version: [u8; 4],
#[serde(deserialize_with = "u8_from_hex_str", serialize_with = "u8_to_hex_str")]
#[serde(with = "crate::serde_utils::u8_hex")]
pub bls_withdrawal_prefix_byte: u8,

/*
Expand Down Expand Up @@ -468,12 +461,9 @@ pub struct YamlConfig {
hysteresis_downward_multiplier: u64,
hysteresis_upward_multiplier: u64,
genesis_slot: u64,
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
genesis_fork_version: [u8; 4],
#[serde(deserialize_with = "u8_from_hex_str", serialize_with = "u8_to_hex_str")]
#[serde(with = "crate::serde_utils::u8_hex")]
bls_withdrawal_prefix: u8,
seconds_per_slot: u64,
min_attestation_inclusion_delay: u64,
Expand All @@ -489,45 +479,21 @@ pub struct YamlConfig {
min_slashing_penalty_quotient: u64,
safe_slots_to_update_justified: u64,

#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_beacon_proposer: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_beacon_attester: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_randao: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_deposit: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_voluntary_exit: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_selection_proof: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
domain_aggregate_and_proof: u32,
#[serde(
deserialize_with = "u32_from_hex_str",
serialize_with = "u32_to_hex_str"
)]
#[serde(with = "crate::serde_utils::u32_hex")]
// EthSpec
justification_bits_length: u32,
max_validators_per_committee: u32,
Expand Down
11 changes: 2 additions & 9 deletions consensus/types/src/enr_fork_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::test_utils::TestRandom;
use crate::utils::{fork_from_hex_str, fork_to_hex_str};
use crate::Epoch;

use serde_derive::{Deserialize, Serialize};
Expand All @@ -16,15 +15,9 @@ use tree_hash_derive::TreeHash;
Debug, Clone, PartialEq, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
)]
pub struct EnrForkId {
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub fork_digest: [u8; 4],
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub next_fork_version: [u8; 4],
pub next_fork_epoch: Epoch,
}
Expand Down
11 changes: 2 additions & 9 deletions consensus/types/src/fork.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::test_utils::TestRandom;
use crate::utils::{fork_from_hex_str, fork_to_hex_str};
use crate::Epoch;

use serde_derive::{Deserialize, Serialize};
Expand All @@ -25,15 +24,9 @@ use tree_hash_derive::TreeHash;
TestRandom,
)]
pub struct Fork {
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub previous_version: [u8; 4],
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub current_version: [u8; 4],
pub epoch: Epoch,
}
Expand Down
6 changes: 1 addition & 5 deletions consensus/types/src/fork_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::test_utils::TestRandom;
use crate::utils::{fork_from_hex_str, fork_to_hex_str};
use crate::{Hash256, SignedRoot};

use serde_derive::{Deserialize, Serialize};
Expand All @@ -15,10 +14,7 @@ use tree_hash_derive::TreeHash;
Debug, Clone, PartialEq, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
)]
pub struct ForkData {
#[serde(
serialize_with = "fork_to_hex_str",
deserialize_with = "fork_from_hex_str"
)]
#[serde(with = "crate::serde_utils::fork_bytes_4")]
pub current_version: [u8; 4],
pub genesis_validators_root: Hash256,
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ pub use bls::{
AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey, Signature, SignatureBytes,
};
pub use ssz_types::{typenum, typenum::Unsigned, BitList, BitVector, FixedVector, VariableList};
pub use utils::{Graffiti, GRAFFITI_BYTES_LEN};
pub use utils::serde_utils::{self, Graffiti, GRAFFITI_BYTES_LEN};
6 changes: 2 additions & 4 deletions consensus/types/src/slot_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::test_utils::TestRandom;
use crate::SignedRoot;

use rand::RngCore;
use serde_derive::{Deserialize, Serialize};
use ssz::{ssz_encode, Decode, DecodeError, Encode};
use std::cmp::{Ord, Ordering};
use std::fmt;
Expand All @@ -23,12 +22,11 @@ use std::iter::Iterator;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};

#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Eq, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(transparent)]
#[derive(Eq, Clone, Copy, Default)]
pub struct Slot(u64);

#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Eq, Clone, Copy, Default, Serialize, Deserialize)]
#[derive(Eq, Clone, Copy, Default)]
pub struct Epoch(u64);

impl_common!(Slot);
Expand Down
38 changes: 38 additions & 0 deletions consensus/types/src/slot_epoch_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,42 @@ macro_rules! impl_hash {
};
}

macro_rules! impl_serde {
($type: ident) => {
impl serde::Serialize for $type {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("{}", self.0))
}
}

impl<'de> serde::Deserialize<'de> for $type {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer
.deserialize_any(crate::serde_utils::quoted::QuotedIntVisitor)
.map($type::from)
}
}
};
}

macro_rules! impl_from_str {
($type: ident) => {
impl std::str::FromStr for $type {
type Err = std::num::ParseIntError;

fn from_str(s: &str) -> Result<$type, Self::Err> {
u64::from_str(s).map($type)
}
}
};
}

macro_rules! impl_common {
($type: ident) => {
impl_from_into_u64!($type);
Expand All @@ -288,6 +324,8 @@ macro_rules! impl_common {
impl_debug!($type);
impl_ssz!($type);
impl_hash!($type);
impl_serde!($type);
impl_from_str!($type);
};
}

Expand Down
4 changes: 1 addition & 3 deletions consensus/types/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
mod serde_utils;

pub use serde_utils::*;
pub mod serde_utils;
Loading

0 comments on commit 7784a78

Please sign in to comment.