From c48ba5eedaad052c5db2c05a546b5be70a1068ed Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 27 Aug 2020 16:53:10 +1000 Subject: [PATCH] Progress with quoted lists --- common/eth2/src/types.rs | 19 ++++++++ consensus/types/src/utils/serde_utils.rs | 59 ++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/common/eth2/src/types.rs b/common/eth2/src/types.rs index 43bd0419c9c..586647ad433 100644 --- a/common/eth2/src/types.rs +++ b/common/eth2/src/types.rs @@ -3,6 +3,7 @@ mod validator_status; use serde::{Deserialize, Serialize}; use std::fmt; use std::str::FromStr; +use types::serde_utils; pub use types::{Checkpoint, Fork, Hash256, PublicKeyBytes, Slot}; pub use validator_status::{ValidatorData, ValidatorStatus}; @@ -163,3 +164,21 @@ impl fmt::Display for ValidatorId { } } } + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct CommitteeData { + #[serde(with = "serde_utils::quoted")] + pub index: u64, + pub slot: Slot, + #[serde(with = "serde_utils::quoted_u64_vec")] + pub validators: Vec, +} + +/* +QuotedItem(T); + +impl fmt::Display for QuotedVec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + } +} +*/ diff --git a/consensus/types/src/utils/serde_utils.rs b/consensus/types/src/utils/serde_utils.rs index ef8c59b91fe..24d45a42b9e 100644 --- a/consensus/types/src/utils/serde_utils.rs +++ b/consensus/types/src/utils/serde_utils.rs @@ -278,10 +278,7 @@ pub mod quoted { type Value = u64; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - write!( - formatter, - "a string containing digits or an int fitting into u64" - ) + write!(formatter, "a quoted or unquoted integer") } fn visit_str(self, s: &str) -> Result @@ -311,3 +308,57 @@ pub mod quoted { deserializer.deserialize_any(QuotedIntVisitor) } } + +pub mod quoted_u64_vec { + use super::*; + use serde::ser::SerializeSeq; + use serde_derive::{Deserialize, Serialize}; + + #[derive(Serialize, Deserialize)] + #[serde(transparent)] + pub struct QuotedIntWrapper { + #[serde(with = "super::quoted")] + int: u64, + } + + pub struct QuotedIntVecVisitor; + impl<'a> serde::de::Visitor<'a> for QuotedIntVecVisitor { + type Value = Vec; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(formatter, "a list of quoted or unquoted integers") + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: serde::de::SeqAccess<'a>, + { + let mut vec = vec![]; + + while let Some(val) = seq.next_element()? { + let val: QuotedIntWrapper = val; + vec.push(val.int); + } + + Ok(vec) + } + } + + pub fn serialize(value: &Vec, serializer: S) -> Result + where + S: Serializer, + { + let mut seq = serializer.serialize_seq(Some(value.len()))?; + for &int in value { + seq.serialize_element(&QuotedIntWrapper { int })?; + } + seq.end() + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(QuotedIntVecVisitor) + } +}