From 6d2394ecf05bbe4c09f857cd445ff79fdbf20e94 Mon Sep 17 00:00:00 2001 From: njgheorghita Date: Thu, 28 Sep 2023 15:03:03 -0400 Subject: [PATCH] Relocate PortalnetConfig --- portalnet/src/config.rs | 56 ++++++++++++++++++++++++++++++++ portalnet/src/discovery.rs | 3 +- portalnet/src/overlay_service.rs | 2 +- portalnet/src/types/messages.rs | 55 +------------------------------ portalnet/tests/overlay.rs | 3 +- src/lib.rs | 2 +- trin-beacon/src/lib.rs | 2 +- trin-beacon/src/network.rs | 3 +- trin-history/src/lib.rs | 2 +- trin-history/src/network.rs | 3 +- trin-state/src/lib.rs | 2 +- trin-state/src/network.rs | 3 +- utp-testing/src/lib.rs | 3 +- 13 files changed, 74 insertions(+), 65 deletions(-) diff --git a/portalnet/src/config.rs b/portalnet/src/config.rs index e69de29bb..a238207ac 100644 --- a/portalnet/src/config.rs +++ b/portalnet/src/config.rs @@ -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, + 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(), + } + } +} diff --git a/portalnet/src/discovery.rs b/portalnet/src/discovery.rs index 1e1bd3d60..a36e6cb95 100644 --- a/portalnet/src/discovery.rs +++ b/portalnet/src/discovery.rs @@ -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; diff --git a/portalnet/src/overlay_service.rs b/portalnet/src/overlay_service.rs index ae0a8f182..786e05819 100644 --- a/portalnet/src/overlay_service.rs +++ b/portalnet/src/overlay_service.rs @@ -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; diff --git a/portalnet/src/types/messages.rs b/portalnet/src/types/messages.rs index 275f28ae3..4e71ecf1a 100644 --- a/portalnet/src/types/messages.rs +++ b/portalnet/src/types/messages.rs @@ -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}; @@ -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}; @@ -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, - 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")] diff --git a/portalnet/tests/overlay.rs b/portalnet/tests/overlay.rs index ad19ea2be..e0a46e320 100644 --- a/portalnet/tests/overlay.rs +++ b/portalnet/tests/overlay.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 4946e3c42..aa0ef6ea5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/trin-beacon/src/lib.rs b/trin-beacon/src/lib.rs index fa8a3fcda..6db752010 100644 --- a/trin-beacon/src/lib.rs +++ b/trin-beacon/src/lib.rs @@ -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; diff --git a/trin-beacon/src/network.rs b/trin-beacon/src/network.rs index f3be73e7f..fc2be91e6 100644 --- a/trin-beacon/src/network.rs +++ b/trin-beacon/src/network.rs @@ -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; diff --git a/trin-history/src/lib.rs b/trin-history/src/lib.rs index f1bc5fee1..408d56e0a 100644 --- a/trin-history/src/lib.rs +++ b/trin-history/src/lib.rs @@ -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; diff --git a/trin-history/src/network.rs b/trin-history/src/network.rs index 9aaa0313d..9be8bea7d 100644 --- a/trin-history/src/network.rs +++ b/trin-history/src/network.rs @@ -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; diff --git a/trin-state/src/lib.rs b/trin-state/src/lib.rs index 1d99dd539..ca7d8fea5 100644 --- a/trin-state/src/lib.rs +++ b/trin-state/src/lib.rs @@ -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; diff --git a/trin-state/src/network.rs b/trin-state/src/network.rs index 476af7cf7..0131631f9 100644 --- a/trin-state/src/network.rs +++ b/trin-state/src/network.rs @@ -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; diff --git a/utp-testing/src/lib.rs b/utp-testing/src/lib.rs index 72fa488bf..18a757e11 100644 --- a/utp-testing/src/lib.rs +++ b/utp-testing/src/lib.rs @@ -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;