diff --git a/Cargo.lock b/Cargo.lock index c62e9fbc878..42e9df4e796 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2563,14 +2563,16 @@ name = "eth2" version = "0.1.0" dependencies = [ "derivative", + "enr", "eth2_keystore", "ethereum_serde_utils", "ethereum_ssz", "ethereum_ssz_derive", "futures", "futures-util", - "lighthouse_network", + "libp2p-identity", "mediatype", + "multiaddr", "pretty_reqwest_error", "procfs", "proto_array", @@ -2582,7 +2584,6 @@ dependencies = [ "serde_json", "slashing_protection", "ssz_types", - "store", "tokio", "types", "zeroize", @@ -3023,7 +3024,6 @@ dependencies = [ "builder_client", "bytes", "eth2", - "eth2_network_config", "ethereum_serde_utils", "ethereum_ssz", "ethers-core", @@ -5314,6 +5314,7 @@ dependencies = [ "dirs", "discv5", "either", + "eth2", "ethereum_ssz", "ethereum_ssz_derive", "fnv", diff --git a/beacon_node/execution_layer/Cargo.toml b/beacon_node/execution_layer/Cargo.toml index 7eb7b4a15e1..236180c011b 100644 --- a/beacon_node/execution_layer/Cargo.toml +++ b/beacon_node/execution_layer/Cargo.toml @@ -12,7 +12,6 @@ arc-swap = "1.6.0" builder_client = { path = "../builder_client" } bytes = { workspace = true } eth2 = { workspace = true } -eth2_network_config = { workspace = true } ethereum_serde_utils = { workspace = true } ethereum_ssz = { workspace = true } ethers-core = { workspace = true } diff --git a/beacon_node/http_api/Cargo.toml b/beacon_node/http_api/Cargo.toml index 5d601008bc0..b4551f3915b 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -30,6 +30,7 @@ rand = { workspace = true } safe_arith = { workspace = true } sensitive_url = { workspace = true } serde = { workspace = true } +serde_json = { workspace = true } slog = { workspace = true } slot_clock = { workspace = true } state_processing = { workspace = true } @@ -48,7 +49,6 @@ warp_utils = { workspace = true } genesis = { workspace = true } logging = { workspace = true } proto_array = { workspace = true } -serde_json = { workspace = true } [[test]] name = "bn_http_api_tests" diff --git a/beacon_node/http_api/src/database.rs b/beacon_node/http_api/src/database.rs index aa8b0e8ffca..8a50ec45b08 100644 --- a/beacon_node/http_api/src/database.rs +++ b/beacon_node/http_api/src/database.rs @@ -1,7 +1,17 @@ use beacon_chain::store::metadata::CURRENT_SCHEMA_VERSION; use beacon_chain::{BeaconChain, BeaconChainTypes}; -use eth2::lighthouse::DatabaseInfo; +use serde::Serialize; use std::sync::Arc; +use store::{AnchorInfo, BlobInfo, Split, StoreConfig}; + +#[derive(Debug, Serialize)] +pub struct DatabaseInfo { + pub schema_version: u64, + pub config: StoreConfig, + pub split: Split, + pub anchor: AnchorInfo, + pub blob_info: BlobInfo, +} pub fn info( chain: Arc>, diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 23d177da785..2ff6cea641e 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -3059,9 +3059,9 @@ 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: api_types::PeerDirection::from((*dir).clone()), + state: api_types::PeerState::from( + peer_info.connection_status().clone(), ), })); } @@ -3104,10 +3104,9 @@ 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 = api_types::PeerDirection::from((*dir).clone()); + let state = api_types::PeerState::from( + peer_info.connection_status().clone(), ); let state_matches = query.state.as_ref().map_or(true, |states| { @@ -3160,9 +3159,8 @@ pub fn serve( .read() .peers() .for_each(|(_, peer_info)| { - let state = api_types::PeerState::from_peer_connection_status( - peer_info.connection_status(), - ); + let state = + api_types::PeerState::from(peer_info.connection_status().clone()); match state { api_types::PeerState::Connected => connected += 1, api_types::PeerState::Connecting => connecting += 1, @@ -4175,15 +4173,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) }) }, ); @@ -4199,15 +4200,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/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index 7007a14466c..1f97565e3a1 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -35,6 +35,7 @@ use state_processing::per_slot_processing; use state_processing::state_advance::partial_state_advance; use std::convert::TryInto; use std::sync::Arc; +use store::{AnchorInfo, Split}; use tokio::time::Duration; use tree_hash::TreeHash; use types::application_domain::ApplicationDomain; @@ -5646,10 +5647,16 @@ impl ApiTester { pub async fn test_get_lighthouse_database_info(self) -> Self { let info = self.client.get_lighthouse_database_info().await.unwrap(); - assert_eq!(info.anchor, self.chain.store.get_anchor_info()); - assert_eq!(info.split, self.chain.store.get_split_info()); assert_eq!( - info.schema_version, + serde_json::from_value::(info.get("anchor").unwrap().clone()).unwrap(), + self.chain.store.get_anchor_info() + ); + assert_eq!( + serde_json::from_value::(info.get("split").unwrap().clone()).unwrap(), + self.chain.store.get_split_info() + ); + assert_eq!( + serde_json::from_value::(info.get("schema_version").unwrap().clone()).unwrap(), store::metadata::CURRENT_SCHEMA_VERSION.as_u64() ); diff --git a/beacon_node/lighthouse_network/Cargo.toml b/beacon_node/lighthouse_network/Cargo.toml index 485f32b37a7..959398fb2da 100644 --- a/beacon_node/lighthouse_network/Cargo.toml +++ b/beacon_node/lighthouse_network/Cargo.toml @@ -13,6 +13,7 @@ directory = { workspace = true } dirs = { workspace = true } discv5 = { workspace = true } either = { workspace = true } +eth2 = { workspace = true } ethereum_ssz = { workspace = true } ethereum_ssz_derive = { workspace = true } fnv = { workspace = true } diff --git a/beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs b/beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs index ee8c27f474c..057e8356250 100644 --- a/beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs +++ b/beacon_node/lighthouse_network/src/peer_manager/peerdb/peer_info.rs @@ -4,6 +4,7 @@ use super::sync_status::SyncStatus; use crate::discovery::Eth2Enr; use crate::{rpc::MetaData, types::Subnet}; use discv5::Enr; +use eth2::types::{PeerDirection, PeerState}; use libp2p::core::multiaddr::{Multiaddr, Protocol}; use serde::{ ser::{SerializeStruct, Serializer}, @@ -506,6 +507,15 @@ pub enum ConnectionDirection { Outgoing, } +impl From for PeerDirection { + fn from(direction: ConnectionDirection) -> Self { + match direction { + ConnectionDirection::Incoming => PeerDirection::Inbound, + ConnectionDirection::Outgoing => PeerDirection::Outbound, + } + } +} + /// Connection Status of the peer. #[derive(Debug, Clone, Default)] pub enum PeerConnectionStatus { @@ -599,3 +609,14 @@ impl Serialize for PeerConnectionStatus { } } } + +impl From for PeerState { + fn from(status: PeerConnectionStatus) -> Self { + match status { + Connected { .. } => PeerState::Connected, + Dialing { .. } => PeerState::Connecting, + Disconnecting { .. } => PeerState::Disconnecting, + Disconnected { .. } | Banned { .. } | Unknown => PeerState::Disconnected, + } + } +} diff --git a/beacon_node/lighthouse_network/src/types/mod.rs b/beacon_node/lighthouse_network/src/types/mod.rs index 6f266fd2bad..1812c7b3e2d 100644 --- a/beacon_node/lighthouse_network/src/types/mod.rs +++ b/beacon_node/lighthouse_network/src/types/mod.rs @@ -1,7 +1,6 @@ mod globals; mod pubsub; mod subnet; -mod sync_state; mod topics; use types::{BitVector, EthSpec}; @@ -11,10 +10,10 @@ pub type EnrSyncCommitteeBitfield = BitVector<::SyncCommitteeSu pub type Enr = discv5::enr::Enr; +pub use eth2::lighthouse::sync_state::{BackFillState, SyncState}; pub use globals::NetworkGlobals; pub use pubsub::{PubsubMessage, SnappyTransform}; pub use subnet::{Subnet, SubnetDiscovery}; -pub use sync_state::{BackFillState, SyncState}; pub use topics::{ attestation_sync_committee_topics, core_topics_to_subscribe, fork_core_topics, subnet_from_topic_hash, GossipEncoding, GossipKind, GossipTopic, ALTAIR_CORE_TOPICS, diff --git a/common/eth2/Cargo.toml b/common/eth2/Cargo.toml index 9d6dea100d4..2d00f42c95a 100644 --- a/common/eth2/Cargo.toml +++ b/common/eth2/Cargo.toml @@ -7,14 +7,16 @@ edition = { workspace = true } [dependencies] derivative = { workspace = true } +enr = { version = "0.13.0", features = ["ed25519"] } eth2_keystore = { workspace = true } ethereum_serde_utils = { workspace = true } ethereum_ssz = { workspace = true } ethereum_ssz_derive = { workspace = true } futures = { workspace = true } futures-util = "0.3.8" -lighthouse_network = { workspace = true } +libp2p-identity = { version = "0.2", features = ["peerid"] } mediatype = "0.19.13" +multiaddr = "0.18.2" pretty_reqwest_error = { workspace = true } proto_array = { workspace = true } reqwest = { workspace = true } @@ -24,7 +26,6 @@ serde = { workspace = true } serde_json = { workspace = true } slashing_protection = { workspace = true } ssz_types = { workspace = true } -store = { workspace = true } types = { workspace = true } zeroize = { workspace = true } @@ -37,4 +38,4 @@ procfs = { version = "0.15.1", optional = true } [features] default = ["lighthouse"] -lighthouse = ["psutil", "procfs"] +lighthouse = ["dep:psutil", "dep:procfs"] diff --git a/common/eth2/src/lib.rs b/common/eth2/src/lib.rs index 12b1538984e..5aac11fe10a 100644 --- a/common/eth2/src/lib.rs +++ b/common/eth2/src/lib.rs @@ -16,10 +16,11 @@ pub mod types; use self::mixin::{RequestAccept, ResponseOptional}; use self::types::{Error as ResponseError, *}; +use ::types::fork_versioned_response::ExecutionOptimisticFinalizedForkVersionedResponse; use derivative::Derivative; use futures::Stream; use futures_util::StreamExt; -use lighthouse_network::PeerId; +use libp2p_identity::PeerId; use pretty_reqwest_error::PrettyReqwestError; pub use reqwest; use reqwest::{ @@ -35,7 +36,6 @@ use std::fmt; use std::future::Future; use std::path::PathBuf; use std::time::Duration; -use store::fork_versioned_response::ExecutionOptimisticFinalizedForkVersionedResponse; pub const V1: EndpointVersion = EndpointVersion(1); pub const V2: EndpointVersion = EndpointVersion(2); diff --git a/common/eth2/src/lighthouse.rs b/common/eth2/src/lighthouse.rs index 66dd5d779bd..4cba0192a48 100644 --- a/common/eth2/src/lighthouse.rs +++ b/common/eth2/src/lighthouse.rs @@ -6,18 +6,16 @@ mod block_packing_efficiency; mod block_rewards; mod standard_block_rewards; mod sync_committee_rewards; +pub mod sync_state; 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; use serde::{Deserialize, Serialize}; use ssz::four_byte_option_impl; use ssz_derive::{Decode, Encode}; -use store::{AnchorInfo, BlobInfo, Split, StoreConfig}; pub use attestation_performance::{ AttestationPerformance, AttestationPerformanceQuery, AttestationPerformanceStatistics, @@ -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; @@ -37,14 +34,12 @@ four_byte_option_impl!(four_byte_option_u64, u64); 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 { +#[derive(Debug, Clone, Serialize, Deserialize)] +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. @@ -88,6 +83,7 @@ pub struct ValidatorInclusionData { pub is_previous_epoch_head_attester: bool, } +use crate::lighthouse::sync_state::SyncState; #[cfg(target_os = "linux")] use { psutil::cpu::os::linux::CpuTimesExt, psutil::memory::os::linux::VirtualMemoryExt, @@ -356,15 +352,6 @@ impl From for FinalizedExecutionBlock { } } -#[derive(Debug, Serialize, Deserialize)] -pub struct DatabaseInfo { - pub schema_version: u64, - pub config: StoreConfig, - pub split: Split, - pub anchor: AnchorInfo, - pub blob_info: BlobInfo, -} - impl BeaconNodeHttpClient { /// `GET lighthouse/health` pub async fn get_lighthouse_health(&self) -> Result, Error> { @@ -503,7 +490,7 @@ impl BeaconNodeHttpClient { } /// `GET lighthouse/database/info` - pub async fn get_lighthouse_database_info(&self) -> Result { + pub async fn get_lighthouse_database_info(&self) -> Result { let mut path = self.server.full.clone(); path.path_segments_mut() diff --git a/beacon_node/lighthouse_network/src/types/sync_state.rs b/common/eth2/src/lighthouse/sync_state.rs similarity index 100% rename from beacon_node/lighthouse_network/src/types/sync_state.rs rename to common/eth2/src/lighthouse/sync_state.rs diff --git a/common/eth2/src/types.rs b/common/eth2/src/types.rs index a303953a863..b3134714a1f 100644 --- a/common/eth2/src/types.rs +++ b/common/eth2/src/types.rs @@ -5,8 +5,9 @@ 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 enr::{CombinedKey, Enr}; use mediatype::{names, MediaType, MediaTypeList}; +use multiaddr::Multiaddr; use reqwest::header::HeaderMap; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; @@ -578,7 +579,7 @@ pub struct ChainHeadData { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct IdentityData { pub peer_id: String, - pub enr: Enr, + pub enr: Enr, pub p2p_addresses: Vec, pub discovery_addresses: Vec, pub metadata: MetaData, @@ -853,19 +854,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 +886,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;