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

decouple eth2 from store and lighthouse_network #6680

Open
wants to merge 11 commits into
base: unstable
Choose a base branch
from
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion beacon_node/execution_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion beacon_node/http_api/src/database.rs
Original file line number Diff line number Diff line change
@@ -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<T: BeaconChainTypes>(
chain: Arc<BeaconChain<T>>,
Expand Down
56 changes: 30 additions & 26 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3059,9 +3059,9 @@ pub fn serve<T: BeaconChainTypes>(
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(),
),
}));
}
Expand Down Expand Up @@ -3104,10 +3104,9 @@ pub fn serve<T: BeaconChainTypes>(

// 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| {
Expand Down Expand Up @@ -3160,9 +3159,8 @@ pub fn serve<T: BeaconChainTypes>(
.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,
Expand Down Expand Up @@ -4175,15 +4173,18 @@ pub fn serve<T: BeaconChainTypes>(
|task_spawner: TaskSpawner<T::EthSpec>,
network_globals: Arc<NetworkGlobals<T::EthSpec>>| {
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::<Vec<_>>())
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)
})
},
);
Expand All @@ -4199,15 +4200,18 @@ pub fn serve<T: BeaconChainTypes>(
|task_spawner: TaskSpawner<T::EthSpec>,
network_globals: Arc<NetworkGlobals<T::EthSpec>>| {
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::<Vec<_>>())
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)
})
},
);
Expand Down
13 changes: 10 additions & 3 deletions beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<AnchorInfo>(info.get("anchor").unwrap().clone()).unwrap(),
self.chain.store.get_anchor_info()
);
assert_eq!(
serde_json::from_value::<Split>(info.get("split").unwrap().clone()).unwrap(),
self.chain.store.get_split_info()
);
assert_eq!(
serde_json::from_value::<u64>(info.get("schema_version").unwrap().clone()).unwrap(),
store::metadata::CURRENT_SCHEMA_VERSION.as_u64()
);

Expand Down
1 change: 1 addition & 0 deletions beacon_node/lighthouse_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -506,6 +507,15 @@ pub enum ConnectionDirection {
Outgoing,
}

impl From<ConnectionDirection> 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 {
Expand Down Expand Up @@ -599,3 +609,14 @@ impl Serialize for PeerConnectionStatus {
}
}
}

impl From<PeerConnectionStatus> for PeerState {
fn from(status: PeerConnectionStatus) -> Self {
match status {
Connected { .. } => PeerState::Connected,
Dialing { .. } => PeerState::Connecting,
Disconnecting { .. } => PeerState::Disconnecting,
Disconnected { .. } | Banned { .. } | Unknown => PeerState::Disconnected,
}
}
}
3 changes: 1 addition & 2 deletions beacon_node/lighthouse_network/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod globals;
mod pubsub;
mod subnet;
mod sync_state;
mod topics;

use types::{BitVector, EthSpec};
Expand All @@ -11,10 +10,10 @@ pub type EnrSyncCommitteeBitfield<E> = BitVector<<E as EthSpec>::SyncCommitteeSu

pub type Enr = discv5::enr::Enr<discv5::enr::CombinedKey>;

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,
Expand Down
6 changes: 4 additions & 2 deletions common/eth2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -37,4 +39,4 @@ procfs = { version = "0.15.1", optional = true }

[features]
default = ["lighthouse"]
lighthouse = ["psutil", "procfs"]
lighthouse = ["dep:psutil", "dep:procfs"]
4 changes: 2 additions & 2 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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);
Expand Down
27 changes: 7 additions & 20 deletions common/eth2/src/lighthouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -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<E: EthSpec> {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Peer {
dknopik marked this conversation as resolved.
Show resolved Hide resolved
/// The Peer's ID
pub peer_id: String,
/// The PeerInfo associated with the peer.
pub peer_info: PeerInfo<E>,
pub peer_info: serde_json::Value,
}

/// The results of validators voting during an epoch.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -356,15 +352,6 @@ impl From<Eth1Block> 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<GenericResponse<Health>, Error> {
Expand Down Expand Up @@ -503,7 +490,7 @@ impl BeaconNodeHttpClient {
}

/// `GET lighthouse/database/info`
pub async fn get_lighthouse_database_info(&self) -> Result<DatabaseInfo, Error> {
pub async fn get_lighthouse_database_info(&self) -> Result<serde_json::Value, Error> {
dknopik marked this conversation as resolved.
Show resolved Hide resolved
let mut path = self.server.full.clone();

path.path_segments_mut()
Expand Down
Loading
Loading