Skip to content

Commit

Permalink
consolidate serde impls
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Oct 18, 2023
1 parent bcc00be commit 6c73998
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 240 deletions.
2 changes: 1 addition & 1 deletion beacon-api-client/src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) use ethereum_consensus::serde::{as_hex, as_string, collection_over_string};
pub(crate) use ethereum_consensus::serde::{as_hex, as_str, seq_of_str};

pub(crate) mod as_u16 {
use http::StatusCode;
Expand Down
76 changes: 37 additions & 39 deletions beacon-api-client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct CoordinateWithMetadata {

#[derive(Serialize, Deserialize)]
pub struct DepositContract {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub chain_id: ChainId,
pub address: ExecutionAddress,
}
Expand All @@ -37,16 +37,16 @@ pub struct DepositContract {
pub struct DepositSnapshot {
pub finalized: Vec<Hash32>,
pub deposit_root: Hash32,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub deposit_count: u64,
pub execution_block_hash: Hash32,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub execution_block_height: u64,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GenesisDetails {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub genesis_time: u64,
pub genesis_validators_root: Root,
#[serde(with = "crate::serde::as_hex")]
Expand Down Expand Up @@ -212,19 +212,19 @@ impl From<BlsPublicKey> for PublicKeyOrIndex {

#[derive(Debug, Serialize, Deserialize)]
pub struct ValidatorSummary {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub balance: Gwei,
pub status: ValidatorStatus,
pub validator: Validator,
}

#[derive(Serialize, Deserialize)]
pub struct BalanceSummary {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub balance: Gwei,
}

Expand All @@ -236,22 +236,20 @@ pub struct CommitteeFilter {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Committee(
#[serde(with = "crate::serde::collection_over_string")] pub Vec<ValidatorIndex>,
);
pub struct Committee(#[serde(with = "crate::serde::seq_of_str")] pub Vec<ValidatorIndex>);

#[derive(Serialize, Deserialize, Debug)]
pub struct CommitteeSummary {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub index: CommitteeIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub validators: Committee,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct SyncCommitteeSummary {
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub validators: Vec<ValidatorIndex>,
pub validator_aggregates: Vec<Committee>,
}
Expand Down Expand Up @@ -355,21 +353,21 @@ pub struct PeerDescription {

#[derive(Serialize, Deserialize, Debug)]
pub struct PeerSummary {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub disconnected: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub connecting: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub connected: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub disconnecting: usize,
}

#[derive(Serialize, Deserialize)]
pub struct SyncStatus {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub head_slot: Slot,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub sync_distance: usize,
pub is_syncing: bool,
}
Expand All @@ -386,73 +384,73 @@ pub enum HealthStatus {
pub struct AttestationDuty {
#[serde(rename = "pubkey")]
pub public_key: BlsPublicKey,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub committee_index: CommitteeIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub committee_length: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub committees_at_slot: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_committee_index: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ProposerDuty {
#[serde(rename = "pubkey")]
pub public_key: BlsPublicKey,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
}

#[derive(Serialize, Deserialize)]
pub struct SyncCommitteeDuty {
#[serde(rename = "pubkey")]
pub public_key: BlsPublicKey,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub validator_sync_committee_indices: Vec<usize>,
}

#[derive(Serialize, Deserialize)]
pub struct CommitteeDescriptor {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub committee_index: CommitteeIndex,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub committees_at_slot: usize,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub is_aggregator: bool,
}

#[derive(Serialize, Deserialize)]
pub struct SyncCommitteeDescriptor {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub sync_committee_indices: Vec<usize>,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub until_epoch: Epoch,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BeaconProposerRegistration {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
pub fee_recipient: ExecutionAddress,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ValidatorLiveness {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
index: ValidatorIndex,
is_live: bool,
}
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/altair/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub struct BeaconBlock<
const MAX_VOLUNTARY_EXITS: usize,
const SYNC_COMMITTEE_SIZE: usize,
> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub proposer_index: ValidatorIndex,
pub parent_root: Root,
pub state_root: Root,
Expand Down
16 changes: 8 additions & 8 deletions ethereum-consensus/src/altair/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub struct BeaconState<
const MAX_VALIDATORS_PER_COMMITTEE: usize,
const SYNC_COMMITTEE_SIZE: usize,
> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub genesis_time: u64,
pub genesis_validators_root: Root,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub fork: Fork,
pub latest_block_header: BeaconBlockHeader,
Expand All @@ -32,23 +32,23 @@ pub struct BeaconState<
pub historical_roots: List<Root, HISTORICAL_ROOTS_LIMIT>,
pub eth1_data: Eth1Data,
pub eth1_data_votes: List<Eth1Data, ETH1_DATA_VOTES_BOUND>,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub eth1_deposit_index: u64,
pub validators: List<Validator, VALIDATOR_REGISTRY_LIMIT>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub balances: List<Gwei, VALIDATOR_REGISTRY_LIMIT>,
pub randao_mixes: Vector<Bytes32, EPOCHS_PER_HISTORICAL_VECTOR>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub slashings: Vector<Gwei, EPOCHS_PER_SLASHINGS_VECTOR>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub previous_epoch_participation: List<ParticipationFlags, VALIDATOR_REGISTRY_LIMIT>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub current_epoch_participation: List<ParticipationFlags, VALIDATOR_REGISTRY_LIMIT>,
pub justification_bits: Bitvector<JUSTIFICATION_BITS_LENGTH>,
pub previous_justified_checkpoint: Checkpoint,
pub current_justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub inactivity_scores: List<u64, VALIDATOR_REGISTRY_LIMIT>,
pub current_sync_committee: SyncCommittee<SYNC_COMMITTEE_SIZE>,
pub next_sync_committee: SyncCommittee<SYNC_COMMITTEE_SIZE>,
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/altair/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

#[derive(serde::Serialize, serde::Deserialize)]
pub struct MetaData {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub seq_number: u64,
pub attnets: Bitvector<ATTESTATION_SUBNET_COUNT>,
pub syncnets: Bitvector<SYNC_COMMITTEE_SUBNET_COUNT>,
Expand Down
10 changes: 5 additions & 5 deletions ethereum-consensus/src/altair/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ use crate::{

#[derive(Debug, Default, Clone, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct SyncCommitteeMessage {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub beacon_block_root: Root,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub validator_index: ValidatorIndex,
pub signature: BlsSignature,
}

#[derive(Debug, Default, Clone, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct SyncCommitteeContribution<const SYNC_SUBCOMMITTEE_SIZE: usize> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub beacon_block_root: Root,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub subcommittee_index: u64,
pub aggregation_bits: Bitvector<SYNC_SUBCOMMITTEE_SIZE>,
pub signature: BlsSignature,
}

#[derive(Debug, Default, Clone, SimpleSerialize, serde::Serialize, serde::Deserialize)]
pub struct ContributionAndProof<const SYNC_SUBCOMMITTEE_SIZE: usize> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub aggregator_index: ValidatorIndex,
pub contribution: SyncCommitteeContribution<SYNC_SUBCOMMITTEE_SIZE>,
pub selection_proof: BlsSignature,
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/bellatrix/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ pub struct BeaconBlock<
const MAX_BYTES_PER_TRANSACTION: usize,
const MAX_TRANSACTIONS_PER_PAYLOAD: usize,
> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub proposer_index: ValidatorIndex,
pub parent_root: Root,
pub state_root: Root,
Expand Down
16 changes: 8 additions & 8 deletions ethereum-consensus/src/bellatrix/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub struct BeaconState<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub genesis_time: u64,
pub genesis_validators_root: Root,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
pub fork: Fork,
pub latest_block_header: BeaconBlockHeader,
Expand All @@ -34,23 +34,23 @@ pub struct BeaconState<
pub historical_roots: List<Root, HISTORICAL_ROOTS_LIMIT>,
pub eth1_data: Eth1Data,
pub eth1_data_votes: List<Eth1Data, ETH1_DATA_VOTES_BOUND>,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub eth1_deposit_index: u64,
pub validators: List<Validator, VALIDATOR_REGISTRY_LIMIT>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub balances: List<Gwei, VALIDATOR_REGISTRY_LIMIT>,
pub randao_mixes: Vector<Bytes32, EPOCHS_PER_HISTORICAL_VECTOR>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub slashings: Vector<Gwei, EPOCHS_PER_SLASHINGS_VECTOR>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub previous_epoch_participation: List<ParticipationFlags, VALIDATOR_REGISTRY_LIMIT>,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub current_epoch_participation: List<ParticipationFlags, VALIDATOR_REGISTRY_LIMIT>,
pub justification_bits: Bitvector<JUSTIFICATION_BITS_LENGTH>,
pub previous_justified_checkpoint: Checkpoint,
pub current_justified_checkpoint: Checkpoint,
pub finalized_checkpoint: Checkpoint,
#[serde(with = "crate::serde::collection_over_string")]
#[serde(with = "crate::serde::seq_of_str")]
pub inactivity_scores: List<u64, VALIDATOR_REGISTRY_LIMIT>,
pub current_sync_committee: SyncCommittee<SYNC_COMMITTEE_SIZE>,
pub next_sync_committee: SyncCommittee<SYNC_COMMITTEE_SIZE>,
Expand Down
4 changes: 2 additions & 2 deletions ethereum-consensus/src/bellatrix/blinded_beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ pub struct BlindedBeaconBlock<
const BYTES_PER_LOGS_BLOOM: usize,
const MAX_EXTRA_DATA_BYTES: usize,
> {
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub slot: Slot,
#[serde(with = "crate::serde::as_string")]
#[serde(with = "crate::serde::as_str")]
pub proposer_index: ValidatorIndex,
pub parent_root: Root,
pub state_root: Root,
Expand Down
Loading

0 comments on commit 6c73998

Please sign in to comment.