Skip to content

Commit 7bfcc03

Browse files
authored
Reduce eth2 dependency space (#8524)
Remove certain dependencies from `eth2`, and feature-gate others which are only used by certain endpoints. | Removed | Optional | Dev only | | -------- | -------- | -------- | | `either` `enr` `libp2p-identity` `multiaddr` | `protoarray` `eth2_keystore` `eip_3076` `zeroize` `reqwest-eventsource` `futures` `futures-util` | `rand` `test_random_derive` | This is done by adding an `events` feature which enables the events endpoint and its associated dependencies. The `lighthouse` feature also enables its associated dependencies making them optional. The networking-adjacent dependencies were removed by just having certain fields use a `String` instead of an explicit network type. This means the user should handle conversion at the call site instead. This is a bit spicy, but I believe `PeerId`, `Enr` and `Multiaddr` are easily converted to and from `String`s so I think it's fine and reduces our dependency space by a lot. The alternative is to feature gate these types behind a `network` feature instead. Co-Authored-By: Mac L <[email protected]>
1 parent 2afa878 commit 7bfcc03

File tree

14 files changed

+61
-46
lines changed

14 files changed

+61
-46
lines changed

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon_node/beacon_chain/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ alloy-primitives = { workspace = true }
1919
bitvec = { workspace = true }
2020
bls = { workspace = true }
2121
educe = { workspace = true }
22-
eth2 = { workspace = true }
22+
eth2 = { workspace = true, features = ["lighthouse"] }
2323
eth2_network_config = { workspace = true }
2424
ethereum_hashing = { workspace = true }
2525
ethereum_serde_utils = { workspace = true }

beacon_node/execution_layer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ alloy-rpc-types-eth = { workspace = true }
1212
arc-swap = "1.6.0"
1313
builder_client = { path = "../builder_client" }
1414
bytes = { workspace = true }
15-
eth2 = { workspace = true }
15+
eth2 = { workspace = true, features = ["events", "lighthouse"] }
1616
ethereum_serde_utils = { workspace = true }
1717
ethereum_ssz = { workspace = true }
1818
fixed_bytes = { workspace = true }

beacon_node/http_api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bs58 = "0.4.0"
1212
bytes = { workspace = true }
1313
directory = { workspace = true }
1414
either = { workspace = true }
15-
eth2 = { workspace = true }
15+
eth2 = { workspace = true, features = ["lighthouse"] }
1616
ethereum_serde_utils = { workspace = true }
1717
ethereum_ssz = { workspace = true }
1818
execution_layer = { workspace = true }

beacon_node/http_api/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,9 +2118,12 @@ pub fn serve<T: BeaconChainTypes>(
21182118
let discovery_addresses = enr.multiaddr_p2p_udp();
21192119
Ok(api_types::GenericResponse::from(api_types::IdentityData {
21202120
peer_id: network_globals.local_peer_id().to_base58(),
2121-
enr,
2122-
p2p_addresses,
2123-
discovery_addresses,
2121+
enr: enr.to_base64(),
2122+
p2p_addresses: p2p_addresses.iter().map(|a| a.to_string()).collect(),
2123+
discovery_addresses: discovery_addresses
2124+
.iter()
2125+
.map(|a| a.to_string())
2126+
.collect(),
21242127
metadata: utils::from_meta_data::<T::EthSpec>(
21252128
&network_globals.local_metadata,
21262129
&chain.spec,

beacon_node/http_api/tests/tests.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,9 +2853,19 @@ impl ApiTester {
28532853

28542854
let expected = IdentityData {
28552855
peer_id: self.local_enr.peer_id().to_string(),
2856-
enr: self.local_enr.clone(),
2857-
p2p_addresses: self.local_enr.multiaddr_p2p_tcp(),
2858-
discovery_addresses: self.local_enr.multiaddr_p2p_udp(),
2856+
enr: self.local_enr.to_base64(),
2857+
p2p_addresses: self
2858+
.local_enr
2859+
.multiaddr_p2p_tcp()
2860+
.iter()
2861+
.map(|a| a.to_string())
2862+
.collect(),
2863+
discovery_addresses: self
2864+
.local_enr
2865+
.multiaddr_p2p_udp()
2866+
.iter()
2867+
.map(|a| a.to_string())
2868+
.collect(),
28592869
metadata: MetaData::V2(MetaDataV2 {
28602870
seq_number: 0,
28612871
attnets: "0x0000000000000000".to_string(),
@@ -2884,7 +2894,7 @@ impl ApiTester {
28842894
pub async fn test_get_node_peers_by_id(self) -> Self {
28852895
let result = self
28862896
.client
2887-
.get_node_peers_by_id(self.external_peer_id)
2897+
.get_node_peers_by_id(&self.external_peer_id.to_string())
28882898
.await
28892899
.unwrap()
28902900
.data;

beacon_node/lighthouse_network/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ directory = { workspace = true }
1717
dirs = { workspace = true }
1818
discv5 = { workspace = true }
1919
either = { workspace = true }
20-
eth2 = { workspace = true }
20+
eth2 = { workspace = true, features = ["lighthouse"] }
2121
ethereum_ssz = { workspace = true }
2222
ethereum_ssz_derive = { workspace = true }
2323
fnv = { workspace = true }

common/eth2/Cargo.toml

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,33 @@ authors = ["Paul Hauner <[email protected]>"]
55
edition = { workspace = true }
66

77
[features]
8-
default = ["lighthouse"]
9-
lighthouse = []
8+
default = []
9+
lighthouse = ["proto_array", "eth2_keystore", "eip_3076", "zeroize"]
10+
events = ["reqwest-eventsource", "futures", "futures-util"]
1011

1112
[dependencies]
1213
context_deserialize = { workspace = true }
1314
educe = { workspace = true }
14-
eip_3076 = { workspace = true }
15-
either = { workspace = true }
16-
enr = { version = "0.13.0", features = ["ed25519"] }
17-
eth2_keystore = { workspace = true }
15+
eip_3076 = { workspace = true, optional = true }
16+
eth2_keystore = { workspace = true, optional = true }
1817
ethereum_serde_utils = { workspace = true }
1918
ethereum_ssz = { workspace = true }
2019
ethereum_ssz_derive = { workspace = true }
21-
futures = { workspace = true }
22-
futures-util = "0.3.8"
23-
libp2p-identity = { version = "0.2", features = ["peerid"] }
20+
futures = { workspace = true, optional = true }
21+
futures-util = { version = "0.3.8", optional = true }
2422
mediatype = "0.19.13"
25-
multiaddr = "0.18.2"
2623
pretty_reqwest_error = { workspace = true }
27-
proto_array = { workspace = true }
28-
rand = { workspace = true }
24+
proto_array = { workspace = true, optional = true }
2925
reqwest = { workspace = true }
30-
reqwest-eventsource = "0.6.0"
26+
reqwest-eventsource = { version = "0.6.0", optional = true }
3127
sensitive_url = { workspace = true }
3228
serde = { workspace = true }
3329
serde_json = { workspace = true }
3430
ssz_types = { workspace = true }
35-
test_random_derive = { path = "../../common/test_random_derive" }
3631
types = { workspace = true }
37-
zeroize = { workspace = true }
32+
zeroize = { workspace = true, optional = true }
3833

3934
[dev-dependencies]
35+
rand = { workspace = true }
36+
test_random_derive = { path = "../../common/test_random_derive" }
4037
tokio = { workspace = true }

common/eth2/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{fmt, path::PathBuf};
1414
pub enum Error {
1515
/// The `reqwest` client raised an error.
1616
HttpClient(PrettyReqwestError),
17+
#[cfg(feature = "events")]
1718
/// The `reqwest_eventsource` client raised an error.
1819
SseClient(Box<reqwest_eventsource::Error>),
1920
/// The server returned an error message where the body was able to be parsed.
@@ -91,6 +92,7 @@ impl Error {
9192
pub fn status(&self) -> Option<StatusCode> {
9293
match self {
9394
Error::HttpClient(error) => error.inner().status(),
95+
#[cfg(feature = "events")]
9496
Error::SseClient(error) => {
9597
if let reqwest_eventsource::Error::InvalidStatusCode(status, _) = error.as_ref() {
9698
Some(*status)

common/eth2/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ pub use beacon_response::{
2222
};
2323

2424
pub use self::error::{Error, ok_or_error, success_or_error};
25+
pub use reqwest;
26+
pub use reqwest::{StatusCode, Url};
27+
pub use sensitive_url::SensitiveUrl;
28+
2529
use self::mixin::{RequestAccept, ResponseOptional};
2630
use self::types::*;
2731
use educe::Educe;
32+
#[cfg(feature = "events")]
2833
use futures::Stream;
34+
#[cfg(feature = "events")]
2935
use futures_util::StreamExt;
30-
use libp2p_identity::PeerId;
31-
pub use reqwest;
3236
use reqwest::{
3337
Body, IntoUrl, RequestBuilder, Response,
3438
header::{HeaderMap, HeaderValue},
3539
};
36-
pub use reqwest::{StatusCode, Url};
40+
#[cfg(feature = "events")]
3741
use reqwest_eventsource::{Event, EventSource};
38-
pub use sensitive_url::SensitiveUrl;
3942
use serde::{Serialize, de::DeserializeOwned};
4043
use ssz::Encode;
4144
use std::fmt;
@@ -1978,15 +1981,15 @@ impl BeaconNodeHttpClient {
19781981
/// `GET node/peers/{peer_id}`
19791982
pub async fn get_node_peers_by_id(
19801983
&self,
1981-
peer_id: PeerId,
1984+
peer_id: &str,
19821985
) -> Result<GenericResponse<PeerData>, Error> {
19831986
let mut path = self.eth_path(V1)?;
19841987

19851988
path.path_segments_mut()
19861989
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
19871990
.push("node")
19881991
.push("peers")
1989-
.push(&peer_id.to_string());
1992+
.push(peer_id);
19901993

19911994
self.get(path).await
19921995
}
@@ -2761,6 +2764,7 @@ impl BeaconNodeHttpClient {
27612764
}
27622765

27632766
/// `GET events?topics`
2767+
#[cfg(feature = "events")]
27642768
pub async fn get_events<E: EthSpec>(
27652769
&self,
27662770
topic: &[EventTopic],

0 commit comments

Comments
 (0)