Skip to content

Commit

Permalink
Remove serde_utils duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 18, 2020
1 parent 803ce9b commit 27f6bdc
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 385 deletions.
2 changes: 1 addition & 1 deletion common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GenesisData {
#[serde(with = "serde_utils::quoted_u64")]
pub genesis_time: u64,
pub genesis_validators_root: Hash256,
#[serde(with = "serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub genesis_fork_version: [u8; 4],
}

Expand Down
40 changes: 40 additions & 0 deletions consensus/serde_utils/src/bytes_4_hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};

const BYTES_LEN: usize = 4;

pub fn serialize<S>(bytes: &[u8; BYTES_LEN], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut hex_string: String = "0x".to_string();
hex_string.push_str(&hex::encode(&bytes));

serializer.serialize_str(&hex_string)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; BYTES_LEN], D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
let mut array = [0 as u8; BYTES_LEN];

let start = s
.as_str()
.get(2..)
.ok_or_else(|| D::Error::custom("string length too small"))?;
let decoded: Vec<u8> = hex::decode(&start).map_err(D::Error::custom)?;

if decoded.len() != BYTES_LEN {
return Err(D::Error::custom("Fork length too long"));
}

for (i, item) in array.iter_mut().enumerate() {
if i > decoded.len() {
break;
}
*item = decoded[i];
}
Ok(array)
}
3 changes: 3 additions & 0 deletions consensus/serde_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod bytes_4_hex;
pub mod hex;
pub mod quoted_u64;
pub mod quoted_u64_vec;
pub mod u32_hex;
pub mod u8_hex;
28 changes: 28 additions & 0 deletions consensus/serde_utils/src/u32_hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S>(num: &u32, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut hex: String = "0x".to_string();
let bytes = num.to_le_bytes();
hex.push_str(&hex::encode(&bytes));

serializer.serialize_str(&hex)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
let start = s
.as_str()
.get(2..)
.ok_or_else(|| D::Error::custom("string length too small"))?;

u32::from_str_radix(&start, 16)
.map_err(D::Error::custom)
.map(u32::from_be)
}
25 changes: 25 additions & 0 deletions consensus/serde_utils/src/u8_hex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S>(byte: &u8, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut hex: String = "0x".to_string();
hex.push_str(&hex::encode(&[*byte]));

serializer.serialize_str(&hex)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u8, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;

let start = match s.as_str().get(2..) {
Some(start) => start,
None => return Err(D::Error::custom("string length too small")),
};
u8::from_str_radix(&start, 16).map_err(D::Error::custom)
}
2 changes: 1 addition & 1 deletion consensus/types/src/beacon_block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tree_hash_derive::TreeHash;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
pub struct BeaconBlockHeader {
pub slot: Slot,
#[serde(with = "crate::serde_utils::quoted_u64")]
#[serde(with = "serde_utils::quoted_u64")]
pub proposer_index: u64,
pub parent_root: Hash256,
pub state_root: Hash256,
Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ where
T: EthSpec,
{
// Versioning
#[serde(with = "crate::serde_utils::quoted_u64")]
#[serde(with = "serde_utils::quoted_u64")]
pub genesis_time: u64,
pub genesis_validators_root: Hash256,
pub slot: Slot,
Expand All @@ -174,7 +174,7 @@ where
// Ethereum 1.0 chain data
pub eth1_data: Eth1Data,
pub eth1_data_votes: VariableList<Eth1Data, T::SlotsPerEth1VotingPeriod>,
#[serde(with = "crate::serde_utils::quoted_u64")]
#[serde(with = "serde_utils::quoted_u64")]
pub eth1_deposit_index: u64,

// Registry
Expand Down
24 changes: 12 additions & 12 deletions consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub struct ChainSpec {
/*
* Initial Values
*/
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub genesis_fork_version: [u8; 4],
#[serde(with = "crate::serde_utils::u8_hex")]
#[serde(with = "serde_utils::u8_hex")]
pub bls_withdrawal_prefix_byte: u8,

/*
Expand Down Expand Up @@ -465,9 +465,9 @@ pub struct YamlConfig {
hysteresis_downward_multiplier: u64,
hysteresis_upward_multiplier: u64,
genesis_slot: u64,
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
genesis_fork_version: [u8; 4],
#[serde(with = "crate::serde_utils::u8_hex")]
#[serde(with = "serde_utils::u8_hex")]
bls_withdrawal_prefix: u8,
seconds_per_slot: u64,
min_attestation_inclusion_delay: u64,
Expand All @@ -483,21 +483,21 @@ pub struct YamlConfig {
min_slashing_penalty_quotient: u64,
safe_slots_to_update_justified: u64,

#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_beacon_proposer: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_beacon_attester: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_randao: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_deposit: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_voluntary_exit: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_selection_proof: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
domain_aggregate_and_proof: u32,
#[serde(with = "crate::serde_utils::u32_hex")]
#[serde(with = "serde_utils::u32_hex")]
// EthSpec
justification_bits_length: u32,
max_validators_per_committee: u32,
Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/enr_fork_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use tree_hash_derive::TreeHash;
Debug, Clone, PartialEq, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
)]
pub struct EnrForkId {
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub fork_digest: [u8; 4],
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub next_fork_version: [u8; 4],
pub next_fork_epoch: Epoch,
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use tree_hash_derive::TreeHash;
TestRandom,
)]
pub struct Fork {
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub previous_version: [u8; 4],
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub current_version: [u8; 4],
pub epoch: Epoch,
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/fork_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tree_hash_derive::TreeHash;
Debug, Clone, PartialEq, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
)]
pub struct ForkData {
#[serde(with = "crate::serde_utils::fork_bytes_4")]
#[serde(with = "serde_utils::bytes_4_hex")]
pub current_version: [u8; 4],
pub genesis_validators_root: Hash256,
}
Expand Down
Loading

0 comments on commit 27f6bdc

Please sign in to comment.