Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
add support for API "envelopes"
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed May 9, 2022
1 parent af4fe0c commit 4cd4836
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 257 deletions.
18 changes: 18 additions & 0 deletions examples/sketch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use beacon_api_client::{ApiResponse, ConsensusVersion, VersionedApiResponse};
use ethereum_consensus::bellatrix::mainnet::BlindedBeaconBlock;
use serde_json;

fn main() {
let block: ApiResponse<BlindedBeaconBlock> = ApiResponse {
data: BlindedBeaconBlock::default(),
};
let block_repr = serde_json::to_string(&block).unwrap();
println!("{block_repr}");

let block_with_version: VersionedApiResponse<BlindedBeaconBlock> = VersionedApiResponse {
version: ConsensusVersion::Bellatrix,
data: BlindedBeaconBlock::default(),
};
let block_with_version_repr = serde_json::to_string(&block_with_version).unwrap();
println!("{block_with_version_repr}");
}
269 changes: 12 additions & 257 deletions src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,274 +1,29 @@
use crate::types::{
AttestationDuty, BalanceSummary, BeaconHeaderSummary, BeaconProposerRegistration, BlockId,
CommitteeDescriptor, CommitteeFilter, CommitteeSummary, EventTopic, FinalityCheckpoints,
GenesisDetails, HealthStatus, NetworkIdentity, PeerDescription, PeerDescriptor, PeerSummary,
ProposerDuty, PubkeyOrIndex, StateId, SyncCommitteeDescriptor, SyncCommitteeDuty,
SyncCommitteeSummary, SyncStatus, ValidatorDescriptor, ValidatorSummary,
};
use ethereum_consensus::altair::mainnet::{
SignedContributionAndProof, SyncCommitteeContribution, SyncCommitteeMessage,
};
use ethereum_consensus::bellatrix::mainnet::{BlindedBeaconBlock, SignedBlindedBeaconBlock};
use ethereum_consensus::networking::{Enr, MetaData, Multiaddr, PeerId};
use ethereum_consensus::networking::Multiaddr;
use ethereum_consensus::phase0::mainnet::{
Attestation, AttestationData, AttesterSlashing, BeaconBlock, BeaconState, Checkpoint, Fork,
ProposerSlashing, SignedAggregateAndProof, SignedBeaconBlock, SignedBeaconBlockHeader,
SignedVoluntaryExit, Validator,
Attestation, AttestationData, AttesterSlashing, BeaconBlock, BeaconState, Fork,
ProposerSlashing, SignedAggregateAndProof, SignedBeaconBlock, SignedVoluntaryExit,
};
use ethereum_consensus::primitives::{
BlsPublicKey, Bytes32, ChainId, CommitteeIndex, Coordinate, Epoch, ExecutionAddress, Gwei,
RandaoReveal, Root, Slot, ValidatorIndex, Version,
Bytes32, ChainId, CommitteeIndex, Coordinate, Epoch, ExecutionAddress, RandaoReveal, Root,
Slot, ValidatorIndex,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub enum Error {}

pub struct Client {}

#[derive(Serialize, Deserialize)]
pub struct GenesisDetails {
#[serde(with = "crate::serde::as_string")]
pub genesis_time: u64,
pub genesis_validators_root: Root,
#[serde(with = "crate::serde::as_hex")]
pub genesis_fork_version: Version,
}

#[derive(Serialize, Deserialize)]
pub enum StateId {
Head,
Genesis,
Finalized,
Justified,
Slot(Slot),
Root(Root),
}

#[derive(Serialize, Deserialize)]
pub enum BlockId {
Head,
Genesis,
Finalized,
Slot(Slot),
Root(Root),
}

#[derive(Serialize, Deserialize)]
pub enum ExecutionStatus {
Default,
Optimistic,
}

#[derive(Serialize, Deserialize)]
pub struct FinalityCheckpoints {
pub previous_justified: Checkpoint,
pub current_justified: Checkpoint,
pub finalized: Checkpoint,
}

#[derive(Serialize, Deserialize)]
pub enum ValidatorStatus {
PendingInitialized,
PendingQueued,
ActiveOngoing,
ActiveExiting,
ActiveSlashed,
ExitedUnslashed,
ExitedSlashed,
WithdrawalPossible,
WithdrawalDone,
// TODO what are these?
Active,
Pending,
Exited,
Withdrawal,
}

pub enum PubkeyOrIndex {
Pubkey(BlsPublicKey),
Index(ValidatorIndex),
}

pub struct ValidatorDescriptor {
pub pubkey_or_index: PubkeyOrIndex,
pub status: ValidatorStatus,
}

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

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

pub struct CommitteeFilter {
pub epoch: Option<Epoch>,
pub index: Option<CommitteeIndex>,
pub slot: Option<Slot>,
}

#[derive(Serialize, Deserialize)]
pub struct CommitteeSummary {
#[serde(with = "crate::serde::as_string")]
pub index: CommitteeIndex,
#[serde(with = "crate::serde::as_string")]
pub slot: Slot,
#[serde(with = "crate::serde::collection_over_string")]
pub validators: Vec<ValidatorIndex>,
}

#[derive(Serialize, Deserialize)]
pub struct SyncCommitteeSummary {
#[serde(with = "crate::serde::collection_over_string")]
pub validators: Vec<ValidatorIndex>,
// TODO fix serde here
pub validator_aggregates: Vec<Vec<ValidatorIndex>>,
}

#[derive(Serialize, Deserialize)]
pub struct BeaconHeaderSummary {
pub root: Root,
pub canonical: bool,
pub signed_header: SignedBeaconBlockHeader,
}

pub enum EventTopic {
Head,
Block,
Attestation,
VoluntaryExit,
FinalizedCheckpoint,
ChainReorg,
ContributionAndProof,
}

#[derive(Serialize, Deserialize)]
pub struct NetworkIdentity {
pub peer_id: PeerId,
pub enr: Enr,
pub p2p_addresses: Vec<Multiaddr>,
pub discovery_addresses: Vec<Multiaddr>,
pub metadata: MetaData,
}

#[derive(Serialize, Deserialize)]
pub enum PeerState {
Disconnected,
Connecting,
Connected,
Disconnecting,
}

#[derive(Serialize, Deserialize)]
pub enum ConnectionOrientation {
Inbound,
Outbound,
}

#[derive(Serialize, Deserialize)]
pub struct PeerDescriptor {
pub state: PeerState,
pub direction: ConnectionOrientation,
}

#[derive(Serialize, Deserialize)]
pub struct PeerDescription {
pub peer_id: PeerId,
pub enr: Enr,
pub last_seen_p2p_address: Multiaddr,
pub state: PeerState,
pub direction: ConnectionOrientation,
}

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

#[derive(Serialize, Deserialize)]
pub struct SyncStatus {
#[serde(with = "crate::serde::as_string")]
pub head_slot: Slot,
#[serde(with = "crate::serde::as_string")]
pub sync_distance: usize,
pub is_syncing: bool,
}

#[derive(Serialize, Deserialize)]
pub enum HealthStatus {
Ready,
Syncing,
NotInitialized,
}

#[derive(Serialize, Deserialize)]
pub struct AttestationDuty {
pub pubkey: BlsPublicKey,
#[serde(with = "crate::serde::as_string")]
pub validator_index: ValidatorIndex,
#[serde(with = "crate::serde::as_string")]
pub committee_index: CommitteeIndex,
#[serde(with = "crate::serde::as_string")]
pub committee_length: usize,
#[serde(with = "crate::serde::as_string")]
pub committees_at_slot: usize,
#[serde(with = "crate::serde::as_string")]
pub validator_committee_index: usize,
#[serde(with = "crate::serde::as_string")]
pub slot: Slot,
}

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

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

pub struct CommitteeDescriptor {
pub validator_index: ValidatorIndex,
pub committee_index: CommitteeIndex,
pub committees_at_slot: usize,
pub slot: Slot,
pub is_aggregator: bool,
}

pub struct SyncCommitteeDescriptor {
pub validator_index: ValidatorIndex,
pub sync_committee_indices: Vec<usize>,
pub until_epoch: Epoch,
}

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

impl Client {
/* beacon namespace */
pub async fn get_genesis_details(&self) -> Result<GenesisDetails, Error> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod api_client;
mod serde;
mod types;

pub use api_client::*;
pub use types::*;
Loading

0 comments on commit 4cd4836

Please sign in to comment.