Skip to content

Commit

Permalink
Anchor pre-PR: Decouple eth2 (#6770)
Browse files Browse the repository at this point in the history
* decouple `eth2` from `store` and `lighthouse_network`

Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>

* remove unused dependency

---------

Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com>
Co-authored-by: Jimmy Chen <jimmy@sigmaprime.io>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent 3208430 commit 9a4768a
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 86 deletions.
7 changes: 4 additions & 3 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
7 changes: 4 additions & 3 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 @@ -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 }

Expand All @@ -37,4 +38,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
Loading

0 comments on commit 9a4768a

Please sign in to comment.