Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete duplicated serde code #6027

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading