Skip to content

Commit

Permalink
Delete duplicated serde code (#6027)
Browse files Browse the repository at this point in the history
* Delete duplicated serde code
  • Loading branch information
michaelsproul authored Jul 1, 2024
1 parent 70bcba1 commit 3f2af5c
Showing 1 changed file with 2 additions and 39 deletions.
41 changes: 2 additions & 39 deletions consensus/types/src/indexed_attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ use tree_hash_derive::TreeHash;
pub struct IndexedAttestation<E: EthSpec> {
/// Lists validator registry indices, not committee indices.
#[superstruct(only(Base), partial_getter(rename = "attesting_indices_base"))]
#[serde(with = "quoted_variable_list_u64")]
#[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")]
pub attesting_indices: VariableList<u64, E::MaxValidatorsPerCommittee>,
#[superstruct(only(Electra), partial_getter(rename = "attesting_indices_electra"))]
#[serde(with = "quoted_variable_list_u64")]
#[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")]
pub attesting_indices: VariableList<u64, E::MaxValidatorsPerSlot>,
pub data: AttestationData,
pub signature: AggregateSignature,
Expand Down Expand Up @@ -203,43 +203,6 @@ impl<E: EthSpec> Hash for IndexedAttestation<E> {
}
}

/// Serialize a variable list of `u64` such that each int is quoted. Deserialize a variable
/// list supporting both quoted and un-quoted ints.
///
/// E.g.,`["0", "1", "2"]`
mod quoted_variable_list_u64 {
use super::*;
use crate::Unsigned;
use serde::ser::SerializeSeq;
use serde::{Deserializer, Serializer};
use serde_utils::quoted_u64_vec::{QuotedIntVecVisitor, QuotedIntWrapper};

pub fn serialize<S, T>(value: &VariableList<u64, T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Unsigned,
{
let mut seq = serializer.serialize_seq(Some(value.len()))?;
for &int in value.iter() {
seq.serialize_element(&QuotedIntWrapper { int })?;
}
seq.end()
}

pub fn deserialize<'de, D, T>(deserializer: D) -> Result<VariableList<u64, T>, D::Error>
where
D: Deserializer<'de>,
T: Unsigned,
{
deserializer
.deserialize_any(QuotedIntVecVisitor)
.and_then(|vec| {
VariableList::new(vec)
.map_err(|e| serde::de::Error::custom(format!("invalid length: {:?}", e)))
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 3f2af5c

Please sign in to comment.