diff --git a/beacon_node/http_api/Cargo.toml b/beacon_node/http_api/Cargo.toml index 638fe0f2192..8d831d15e7b 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -43,9 +43,9 @@ store = { workspace = true } bytes = { workspace = true } beacon_processor = { workspace = true } rand = { workspace = true } +serde_json = { workspace = true } [dev-dependencies] -serde_json = { workspace = true } proto_array = { workspace = true } genesis = { workspace = true } logging = { workspace = true } diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index fe05f55a01a..b1ea935d4b4 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -46,11 +46,14 @@ use bytes::Bytes; use directory::DEFAULT_ROOT_DIR; use eth2::types::{ self as api_types, BroadcastValidation, EndpointVersion, ForkChoice, ForkChoiceNode, - LightClientUpdatesQuery, PublishBlockRequest, ValidatorBalancesRequestBody, ValidatorId, - ValidatorStatus, ValidatorsRequestBody, + LightClientUpdatesQuery, PeerDirection, PublishBlockRequest, ValidatorBalancesRequestBody, + ValidatorId, ValidatorStatus, ValidatorsRequestBody, }; use eth2::{CONSENSUS_VERSION_HEADER, CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER}; -use lighthouse_network::{types::SyncState, EnrExt, NetworkGlobals, PeerId, PubsubMessage}; +use lighthouse_network::{ + types::SyncState, ConnectionDirection, EnrExt, NetworkGlobals, PeerConnectionStatus, PeerId, + PubsubMessage, +}; use lighthouse_version::version_with_platform; use logging::SSELoggingComponents; use network::{NetworkMessage, NetworkSenders, ValidatorSubscriptionMessage}; @@ -2870,9 +2873,15 @@ pub fn serve( let meta_data = network_globals.local_metadata.read(); Ok(api_types::GenericResponse::from(api_types::IdentityData { peer_id: network_globals.local_peer_id().to_base58(), - enr, - p2p_addresses, - discovery_addresses, + enr: enr.to_string(), + p2p_addresses: p2p_addresses + .into_iter() + .map(|addr| addr.to_string()) + .collect(), + discovery_addresses: discovery_addresses + .into_iter() + .map(|addr| addr.to_string()) + .collect(), metadata: api_types::MetaData { seq_number: *meta_data.seq_number(), attnets: format!( @@ -3059,10 +3068,26 @@ pub fn serve( peer_id: peer_id.to_string(), enr: peer_info.enr().map(|enr| enr.to_base64()), last_seen_p2p_address: address, - direction: api_types::PeerDirection::from_connection_direction(dir), - state: api_types::PeerState::from_peer_connection_status( - peer_info.connection_status(), - ), + direction: match dir { + ConnectionDirection::Incoming => PeerDirection::Inbound, + ConnectionDirection::Outgoing => PeerDirection::Outbound, + }, + state: match peer_info.connection_status() { + PeerConnectionStatus::Connected { .. } => { + api_types::PeerState::Connected + } + PeerConnectionStatus::Dialing { .. } => { + api_types::PeerState::Connecting + } + PeerConnectionStatus::Disconnecting { .. } => { + api_types::PeerState::Disconnecting + } + PeerConnectionStatus::Disconnected { .. } + | PeerConnectionStatus::Banned { .. } + | PeerConnectionStatus::Unknown => { + api_types::PeerState::Disconnected + } + }, })); } } @@ -3104,11 +3129,26 @@ pub fn serve( // the eth2 API spec implies only peers we have been connected to at some point should be included. if let Some(dir) = peer_info.connection_direction() { - let direction = - api_types::PeerDirection::from_connection_direction(dir); - let state = api_types::PeerState::from_peer_connection_status( - peer_info.connection_status(), - ); + let direction = match dir { + ConnectionDirection::Incoming => PeerDirection::Inbound, + ConnectionDirection::Outgoing => PeerDirection::Outbound, + }; + let state = match peer_info.connection_status() { + PeerConnectionStatus::Connected { .. } => { + api_types::PeerState::Connected + } + PeerConnectionStatus::Dialing { .. } => { + api_types::PeerState::Connecting + } + PeerConnectionStatus::Disconnecting { .. } => { + api_types::PeerState::Disconnecting + } + PeerConnectionStatus::Disconnected { .. } + | PeerConnectionStatus::Banned { .. } + | PeerConnectionStatus::Unknown => { + api_types::PeerState::Disconnected + } + }; let state_matches = query.state.as_ref().map_or(true, |states| { states.iter().any(|state_param| *state_param == state) @@ -3159,16 +3199,13 @@ pub fn serve( .peers .read() .peers() - .for_each(|(_, peer_info)| { - let state = api_types::PeerState::from_peer_connection_status( - peer_info.connection_status(), - ); - match state { - api_types::PeerState::Connected => connected += 1, - api_types::PeerState::Connecting => connecting += 1, - api_types::PeerState::Disconnected => disconnected += 1, - api_types::PeerState::Disconnecting => disconnecting += 1, - } + .for_each(|(_, peer_info)| match peer_info.connection_status() { + PeerConnectionStatus::Connected { .. } => connected += 1, + PeerConnectionStatus::Dialing { .. } => connecting += 1, + PeerConnectionStatus::Disconnecting { .. } => disconnecting += 1, + PeerConnectionStatus::Disconnected { .. } + | PeerConnectionStatus::Banned { .. } + | PeerConnectionStatus::Unknown => disconnected += 1, }); Ok(api_types::GenericResponse::from(api_types::PeerCount { @@ -4166,15 +4203,18 @@ pub fn serve( |task_spawner: TaskSpawner, network_globals: Arc>| { task_spawner.blocking_json_task(Priority::P1, move || { - Ok(network_globals - .peers - .read() - .peers() - .map(|(peer_id, peer_info)| eth2::lighthouse::Peer { + let mut peers = vec![]; + for (peer_id, peer_info) in network_globals.peers.read().peers() { + peers.push(eth2::lighthouse::Peer { peer_id: peer_id.to_string(), - peer_info: peer_info.clone(), - }) - .collect::>()) + peer_info: serde_json::to_value(peer_info).map_err(|e| { + warp_utils::reject::custom_not_found(format!( + "unable to serialize peer_info: {e:?}", + )) + })?, + }); + } + Ok(peers) }) }, ); @@ -4190,15 +4230,18 @@ pub fn serve( |task_spawner: TaskSpawner, network_globals: Arc>| { task_spawner.blocking_json_task(Priority::P1, move || { - Ok(network_globals - .peers - .read() - .connected_peers() - .map(|(peer_id, peer_info)| eth2::lighthouse::Peer { + let mut peers = vec![]; + for (peer_id, peer_info) in network_globals.peers.read().connected_peers() { + peers.push(eth2::lighthouse::Peer { peer_id: peer_id.to_string(), - peer_info: peer_info.clone(), - }) - .collect::>()) + peer_info: serde_json::to_value(peer_info).map_err(|e| { + warp_utils::reject::custom_not_found(format!( + "unable to serialize peer_info: {e:?}", + )) + })?, + }); + } + Ok(peers) }) }, ); diff --git a/common/eth2/Cargo.toml b/common/eth2/Cargo.toml index d23a4068f1b..3b82509804c 100644 --- a/common/eth2/Cargo.toml +++ b/common/eth2/Cargo.toml @@ -12,7 +12,6 @@ serde_json = { workspace = true } ssz_types = { workspace = true } types = { workspace = true } reqwest = { workspace = true } -lighthouse_network = { workspace = true } proto_array = { workspace = true } ethereum_serde_utils = { workspace = true } eth2_keystore = { workspace = true } diff --git a/common/eth2/src/lib.rs b/common/eth2/src/lib.rs index 522c6414eae..fd41f6ed068 100644 --- a/common/eth2/src/lib.rs +++ b/common/eth2/src/lib.rs @@ -19,7 +19,6 @@ use self::types::{Error as ResponseError, *}; use derivative::Derivative; use futures::Stream; use futures_util::StreamExt; -use lighthouse_network::PeerId; use pretty_reqwest_error::PrettyReqwestError; pub use reqwest; use reqwest::{ @@ -48,6 +47,8 @@ pub const CONSENSUS_BLOCK_VALUE_HEADER: &str = "Eth-Consensus-Block-Value"; pub const CONTENT_TYPE_HEADER: &str = "Content-Type"; pub const SSZ_CONTENT_TYPE_HEADER: &str = "application/octet-stream"; +type PeerId = String; + #[derive(Debug)] pub enum Error { /// The `reqwest` client raised an error. diff --git a/common/eth2/src/lighthouse.rs b/common/eth2/src/lighthouse.rs index 66dd5d779bd..5c4eb3b0e65 100644 --- a/common/eth2/src/lighthouse.rs +++ b/common/eth2/src/lighthouse.rs @@ -8,9 +8,7 @@ mod standard_block_rewards; mod sync_committee_rewards; use crate::{ - types::{ - DepositTreeSnapshot, Epoch, EthSpec, FinalizedExecutionBlock, GenericResponse, ValidatorId, - }, + types::{DepositTreeSnapshot, Epoch, FinalizedExecutionBlock, GenericResponse, ValidatorId}, BeaconNodeHttpClient, DepositData, Error, Eth1Data, Hash256, Slot, }; use proto_array::core::ProtoArray; @@ -27,7 +25,6 @@ pub use block_packing_efficiency::{ BlockPackingEfficiency, BlockPackingEfficiencyQuery, ProposerInfo, UniqueAttestation, }; pub use block_rewards::{AttestationRewards, BlockReward, BlockRewardMeta, BlockRewardsQuery}; -pub use lighthouse_network::{types::SyncState, PeerInfo}; pub use standard_block_rewards::StandardBlockReward; pub use sync_committee_rewards::SyncCommitteeReward; @@ -39,12 +36,11 @@ four_byte_option_impl!(four_byte_option_hash256, Hash256); /// Information returned by `peers` and `connected_peers`. // TODO: this should be deserializable.. #[derive(Debug, Clone, Serialize)] -#[serde(bound = "E: EthSpec")] -pub struct Peer { +pub struct Peer { /// The Peer's ID pub peer_id: String, /// The PeerInfo associated with the peer. - pub peer_info: PeerInfo, + pub peer_info: serde_json::Value, } /// The results of validators voting during an epoch. @@ -379,7 +375,9 @@ impl BeaconNodeHttpClient { } /// `GET lighthouse/syncing` - pub async fn get_lighthouse_syncing(&self) -> Result, Error> { + pub async fn get_lighthouse_syncing( + &self, + ) -> Result, Error> { let mut path = self.server.full.clone(); path.path_segments_mut() diff --git a/common/eth2/src/types.rs b/common/eth2/src/types.rs index c187399ebd7..e8519d87dcb 100644 --- a/common/eth2/src/types.rs +++ b/common/eth2/src/types.rs @@ -5,7 +5,6 @@ use crate::{ Error as ServerError, CONSENSUS_BLOCK_VALUE_HEADER, CONSENSUS_VERSION_HEADER, EXECUTION_PAYLOAD_BLINDED_HEADER, EXECUTION_PAYLOAD_VALUE_HEADER, }; -use lighthouse_network::{ConnectionDirection, Enr, Multiaddr, PeerConnectionStatus}; use mediatype::{names, MediaType, MediaTypeList}; use reqwest::header::HeaderMap; use serde::{Deserialize, Deserializer, Serialize}; @@ -578,9 +577,9 @@ pub struct ChainHeadData { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct IdentityData { pub peer_id: String, - pub enr: Enr, - pub p2p_addresses: Vec, - pub discovery_addresses: Vec, + pub enr: String, + pub p2p_addresses: Vec, + pub discovery_addresses: Vec, pub metadata: MetaData, } @@ -853,19 +852,6 @@ pub enum PeerState { Disconnecting, } -impl PeerState { - pub fn from_peer_connection_status(status: &PeerConnectionStatus) -> Self { - match status { - PeerConnectionStatus::Connected { .. } => PeerState::Connected, - PeerConnectionStatus::Dialing { .. } => PeerState::Connecting, - PeerConnectionStatus::Disconnecting { .. } => PeerState::Disconnecting, - PeerConnectionStatus::Disconnected { .. } - | PeerConnectionStatus::Banned { .. } - | PeerConnectionStatus::Unknown => PeerState::Disconnected, - } - } -} - impl FromStr for PeerState { type Err = String; @@ -898,15 +884,6 @@ pub enum PeerDirection { Outbound, } -impl PeerDirection { - pub fn from_connection_direction(direction: &ConnectionDirection) -> Self { - match direction { - ConnectionDirection::Incoming => PeerDirection::Inbound, - ConnectionDirection::Outgoing => PeerDirection::Outbound, - } - } -} - impl FromStr for PeerDirection { type Err = String;