Skip to content

Commit

Permalink
Relocate PortalnetConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Sep 28, 2023
1 parent 16f2284 commit 6d2394e
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 65 deletions.
56 changes: 56 additions & 0 deletions portalnet/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::net::SocketAddr;

use ethereum_types::H256;

use crate::metrics::overlay::OverlayMetrics;
use crate::metrics::registry::METRICS_REGISTRY;
use ethportal_api::types::bootnodes::Bootnodes;
use ethportal_api::types::cli::TrinConfig;
use ethportal_api::types::distance::Distance;

/// Capacity of the cache for observed `NodeAddress` values.
/// Provides capacity for 32 full k-buckets. This capacity will be shared among all active portal
/// subnetworks.
const NODE_ADDR_CACHE_CAPACITY: usize = discv5::kbucket::MAX_NODES_PER_BUCKET * 32;

#[derive(Clone)]
pub struct PortalnetConfig {
pub external_addr: Option<SocketAddr>,
pub private_key: H256,
pub listen_port: u16,
pub bootnodes: Bootnodes,
pub data_radius: Distance,
pub internal_ip: bool,
pub no_stun: bool,
pub node_addr_cache_capacity: usize,
pub metrics: OverlayMetrics,
}

impl PortalnetConfig {
pub fn new(trin_config: &TrinConfig, private_key: H256) -> Self {
Self {
external_addr: trin_config.external_addr,
private_key,
listen_port: trin_config.discovery_port,
no_stun: trin_config.no_stun,
bootnodes: trin_config.bootnodes.clone(),
..Default::default()
}
}
}

impl Default for PortalnetConfig {
fn default() -> Self {
Self {
external_addr: None,
private_key: H256::random(),
listen_port: 4242,
bootnodes: Bootnodes::default(),
data_radius: Distance::MAX,
internal_ip: false,
no_stun: false,
node_addr_cache_capacity: NODE_ADDR_CACHE_CAPACITY,
metrics: METRICS_REGISTRY.overlay.clone(),
}
}
}
3 changes: 2 additions & 1 deletion portalnet/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use tokio::sync::mpsc;
use tracing::{debug, info, warn};
use utp_rs::{cid::ConnectionPeer, udp::AsyncUdpSocket};

use super::types::messages::{PortalnetConfig, ProtocolId};
use super::config::PortalnetConfig;
use super::types::messages::ProtocolId;
use crate::socket;
use ethportal_api::types::enr::Enr;
use ethportal_api::utils::bytes::hex_encode;
Expand Down
2 changes: 1 addition & 1 deletion portalnet/src/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2739,10 +2739,10 @@ mod tests {

use crate::metrics::registry::METRICS_REGISTRY;
use crate::{
config::PortalnetConfig,
discovery::{Discovery, NodeAddress},
overlay::OverlayConfig,
storage::{DistanceFunction, MemoryContentStore},
types::messages::PortalnetConfig,
};
use ethportal_api::types::content_key::overlay::IdentityContentKey;
use ethportal_api::types::distance::XorMetric;
Expand Down
55 changes: 1 addition & 54 deletions portalnet/src/types/messages.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::{
convert::{TryFrom, TryInto},
fmt,
net::SocketAddr,
ops::Deref,
str::FromStr,
};

use ethereum_types::{H256, U256};
use ethereum_types::U256;
use rlp::Encodable;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
Expand All @@ -16,11 +15,7 @@ use ssz_types::{typenum, BitList};
use thiserror::Error;
use validator::ValidationError;

use crate::metrics::overlay::OverlayMetrics;
use crate::metrics::registry::METRICS_REGISTRY;
use ethportal_api::types::bootnodes::Bootnodes;
use ethportal_api::types::bytes::ByteList;
use ethportal_api::types::cli::TrinConfig;
use ethportal_api::types::distance::Distance;
use ethportal_api::types::enr::{Enr, SszEnr};
use ethportal_api::utils::bytes::{hex_decode, hex_encode, ByteUtilsError};
Expand Down Expand Up @@ -156,54 +151,6 @@ pub enum DiscoveryRequestError {
InvalidMessage,
}

/// Capacity of the cache for observed `NodeAddress` values.
/// Provides capacity for 32 full k-buckets. This capacity will be shared among all active portal
/// subnetworks.
const NODE_ADDR_CACHE_CAPACITY: usize = discv5::kbucket::MAX_NODES_PER_BUCKET * 32;

// TODO move to separate module
#[derive(Clone)]
pub struct PortalnetConfig {
pub external_addr: Option<SocketAddr>,
pub private_key: H256,
pub listen_port: u16,
pub bootnodes: Bootnodes,
pub data_radius: Distance,
pub internal_ip: bool,
pub no_stun: bool,
pub node_addr_cache_capacity: usize,
pub metrics: OverlayMetrics,
}

impl PortalnetConfig {
pub fn new(trin_config: &TrinConfig, private_key: H256) -> Self {
Self {
external_addr: trin_config.external_addr,
private_key,
listen_port: trin_config.discovery_port,
no_stun: trin_config.no_stun,
bootnodes: trin_config.bootnodes.clone(),
..Default::default()
}
}
}

impl Default for PortalnetConfig {
fn default() -> Self {
Self {
external_addr: None,
private_key: H256::random(),
listen_port: 4242,
bootnodes: Bootnodes::default(),
data_radius: Distance::MAX,
internal_ip: false,
no_stun: false,
node_addr_cache_capacity: NODE_ADDR_CACHE_CAPACITY,
metrics: METRICS_REGISTRY.overlay.clone(),
}
}
}

#[derive(Error, Debug)]
pub enum ProtocolIdError {
#[error("Unable to decode protocol id to bytes")]
Expand Down
3 changes: 2 additions & 1 deletion portalnet/tests/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ use ethportal_api::types::distance::XorMetric;
use ethportal_api::types::enr::{Enr, SszEnr};
use ethportal_api::utils::bytes::hex_encode_upper;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, Discv5UdpSocket},
metrics::registry::METRICS_REGISTRY,
overlay::{OverlayConfig, OverlayProtocol},
storage::{ContentStore, DistanceFunction, MemoryContentStore},
types::messages::{Content, Message, PortalnetConfig, ProtocolId},
types::messages::{Content, Message, ProtocolId},
};
use trin_validation::validator::MockValidator;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use ethportal_api::types::cli::Web3TransportType;
use ethportal_api::types::cli::{TrinConfig, BEACON_NETWORK, HISTORY_NETWORK, STATE_NETWORK};
use portalnet::metrics::registry::METRICS_REGISTRY;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, Discv5UdpSocket},
events::PortalnetEvents,
storage::PortalStorageConfig,
types::messages::PortalnetConfig,
utils::db::{configure_node_data_dir, configure_trin_data_dir},
};
use trin_beacon::initialize_beacon_network;
Expand Down
2 changes: 1 addition & 1 deletion trin-beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::{events::BeaconEvents, jsonrpc::BeaconRequestHandler};
use ethportal_api::types::enr::Enr;
use ethportal_api::types::jsonrpc::request::BeaconJsonRpcRequest;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
storage::PortalStorageConfig,
types::messages::PortalnetConfig,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
3 changes: 2 additions & 1 deletion trin-beacon/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use ethportal_api::types::distance::XorMetric;
use ethportal_api::types::enr::Enr;
use ethportal_api::BeaconContentKey;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
overlay::{OverlayConfig, OverlayProtocol},
storage::{PortalStorage, PortalStorageConfig},
types::messages::{PortalnetConfig, ProtocolId},
types::messages::ProtocolId,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
2 changes: 1 addition & 1 deletion trin-history/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use crate::{events::HistoryEvents, jsonrpc::HistoryRequestHandler};
use ethportal_api::types::enr::Enr;
use ethportal_api::types::jsonrpc::request::HistoryJsonRpcRequest;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
storage::PortalStorageConfig,
types::messages::PortalnetConfig,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
3 changes: 2 additions & 1 deletion trin-history/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use ethportal_api::types::distance::XorMetric;
use ethportal_api::types::enr::Enr;
use ethportal_api::HistoryContentKey;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
overlay::{OverlayConfig, OverlayProtocol},
storage::{PortalStorage, PortalStorageConfig},
types::messages::{PortalnetConfig, ProtocolId},
types::messages::ProtocolId,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
2 changes: 1 addition & 1 deletion trin-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::{events::StateEvents, jsonrpc::StateRequestHandler};
use ethportal_api::types::enr::Enr;
use ethportal_api::types::jsonrpc::request::StateJsonRpcRequest;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
storage::PortalStorageConfig,
types::messages::PortalnetConfig,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
3 changes: 2 additions & 1 deletion trin-state/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use ethportal_api::types::distance::XorMetric;
use ethportal_api::types::enr::Enr;
use ethportal_api::StateContentKey;
use portalnet::{
config::PortalnetConfig,
discovery::{Discovery, UtpEnr},
overlay::{OverlayConfig, OverlayProtocol},
storage::{PortalStorage, PortalStorageConfig},
types::messages::{PortalnetConfig, ProtocolId},
types::messages::ProtocolId,
};
use trin_validation::oracle::HeaderOracle;

Expand Down
3 changes: 2 additions & 1 deletion utp-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use ethportal_api::utils::bytes::{hex_encode, hex_encode_upper};
use jsonrpsee::core::{async_trait, RpcResult};
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::server::{Server, ServerHandle};
use portalnet::config::PortalnetConfig;
use portalnet::discovery::{Discovery, UtpEnr};
use portalnet::types::messages::{PortalnetConfig, ProtocolId};
use portalnet::types::messages::ProtocolId;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
Expand Down

0 comments on commit 6d2394e

Please sign in to comment.