Skip to content

Commit

Permalink
feat: support multiple network MAINNET/TESTNET etc
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML committed Jan 12, 2024
1 parent 09bb5ed commit ea72788
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 86 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions ethportal-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ authors = ["https://github.com/ethereum/trin/graphs/contributors"]
[dependencies]
anyhow = "1.0.68"
base64 = "0.13.0"
bimap = "0.6.3"
bytes = "1.3.0"
clap = { version = "4.2.1", features = ["derive"] }
discv5 = { version = "0.4.0", features = ["serde"] }
Expand All @@ -27,6 +28,7 @@ jsonrpsee = {version="0.20.0", features = ["async-client", "client", "macros", "
keccak-hash = "0.10.0"
lazy_static = "1.4.0"
nanotemplate = "0.3.0"
once_cell = "1.17"
quickcheck = "1.0.3"
rand = "0.8.5"
reth-rpc-types = { tag = "v0.1.0-alpha.10", git = "https://github.com/paradigmxyz/reth.git"}
Expand Down
30 changes: 25 additions & 5 deletions ethportal-api/src/types/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::{
Args, Parser, Subcommand,
};
use ethereum_types::H256;
use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr};
use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc};
use url::Url;

use crate::types::bootnodes::Bootnodes;
Expand All @@ -19,11 +19,14 @@ pub const BEACON_NETWORK: &str = "beacon";
pub const HISTORY_NETWORK: &str = "history";
pub const STATE_NETWORK: &str = "state";
const DEFAULT_SUBNETWORKS: &str = "history";
const DEFAULT_NETWORK: &str = "mainnet";
pub const DEFAULT_STORAGE_CAPACITY_MB: &str = "100";
pub const DEFAULT_WEB3_TRANSPORT: &str = "ipc";

use crate::dashboard::grafana::{GrafanaAPI, DASHBOARD_TEMPLATES};

use super::portal_wire::{NetworkSpec, MAINNET, TESTNET};

#[derive(Debug, PartialEq, Clone)]
pub enum Web3TransportType {
HTTP,
Expand Down Expand Up @@ -129,12 +132,20 @@ pub struct TrinConfig {
pub trusted_block_root: Option<String>,

#[arg(
long = "networks",
long = "portal-subnetworks",
help = "Comma-separated list of which portal subnetworks to activate",
default_value = DEFAULT_SUBNETWORKS,
use_value_delimiter = true
)]
pub networks: Vec<String>,
pub portal_subnetworks: Vec<String>,

#[arg(
long = "network",
help = "Choose mainnet or testnet",
default_value = DEFAULT_NETWORK,
value_parser = network_parser
)]
pub network: Arc<NetworkSpec>,

/// Storage capacity specified in megabytes.
#[arg(
Expand Down Expand Up @@ -200,7 +211,7 @@ impl Default for TrinConfig {
no_upnp: false,
private_key: None,
trusted_block_root: None,
networks: DEFAULT_SUBNETWORKS
portal_subnetworks: DEFAULT_SUBNETWORKS
.split(',')
.map(|n| n.to_string())
.collect(),
Expand All @@ -214,6 +225,7 @@ impl Default for TrinConfig {
ws: false,
ws_port: DEFAULT_WEB3_WS_PORT,
command: None,
network: MAINNET.clone(),
}
}
}
Expand Down Expand Up @@ -273,6 +285,14 @@ pub fn check_private_key_length(private_key: &str) -> Result<H256, String> {
))
}

fn network_parser(network_string: &str) -> Result<Arc<NetworkSpec>, String> {
match network_string {
"mainnet" => Ok(MAINNET.clone()),
"testnet" => Ok(TESTNET.clone()),
_ => Err(format!("Not a valid network: {network_string}")),
}
}

fn check_trusted_block_root(trusted_root: &str) -> Result<String, String> {
if !trusted_root.starts_with("0x") {
return Err("Trusted block root must be prefixed with 0x".to_owned());
Expand All @@ -298,7 +318,7 @@ impl fmt::Display for TrinConfig {
write!(
f,
"TrinConfig {{ networks: {:?}, capacity_mb: {}, ephemeral: {}, json_rpc_url: {}, metrics_enabled: {} }}",
self.networks, self.mb, self.ephemeral, json_rpc_url, self.enable_metrics_with_url.is_some()
self.portal_subnetworks, self.mb, self.ephemeral, json_rpc_url, self.enable_metrics_with_url.is_some()
)
}
}
Expand Down
74 changes: 34 additions & 40 deletions ethportal-api/src/types/portal_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use std::{
convert::{TryFrom, TryInto},
fmt,
ops::Deref,
str::FromStr,
sync::Arc,
};

use bimap::BiHashMap;
use ethereum_types::U256;
use once_cell::sync::Lazy;
use rlp::Encodable;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -165,7 +167,7 @@ pub enum ProtocolIdError {
}

/// Protocol identifiers
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ProtocolId {
State,
History,
Expand All @@ -175,22 +177,32 @@ pub enum ProtocolId {
Utp,
}

/// Encode hex string to protocol id
impl FromStr for ProtocolId {
type Err = ProtocolIdError;

fn from_str(input: &str) -> Result<ProtocolId, Self::Err> {
match input {
"0x500A" => Ok(ProtocolId::State),
"0x500B" => Ok(ProtocolId::History),
"0x500C" => Ok(ProtocolId::TransactionGossip),
"0x500D" => Ok(ProtocolId::CanonicalIndices),
"0x501A" => Ok(ProtocolId::Beacon),
"0x757470" => Ok(ProtocolId::Utp),
_ => Err(ProtocolIdError::Invalid),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NetworkSpec {
pub portal_networks: BiHashMap<ProtocolId, String>,
}

pub static MAINNET: Lazy<Arc<NetworkSpec>> = Lazy::new(|| {
let mut portal_networks = BiHashMap::new();
portal_networks.insert(ProtocolId::State, "0x500A".to_string());
portal_networks.insert(ProtocolId::History, "0x500B".to_string());
portal_networks.insert(ProtocolId::TransactionGossip, "0x500C".to_string());
portal_networks.insert(ProtocolId::CanonicalIndices, "0x500D".to_string());
portal_networks.insert(ProtocolId::Beacon, "0x501A".to_string());
portal_networks.insert(ProtocolId::Utp, "0x757470".to_string());
NetworkSpec { portal_networks }.into()
});

pub static TESTNET: Lazy<Arc<NetworkSpec>> = Lazy::new(|| {
let mut portal_networks = BiHashMap::new();
portal_networks.insert(ProtocolId::State, "0x501A".to_string());
portal_networks.insert(ProtocolId::History, "0x501B".to_string());
portal_networks.insert(ProtocolId::TransactionGossip, "0x501C".to_string());
portal_networks.insert(ProtocolId::CanonicalIndices, "0x501D".to_string());
portal_networks.insert(ProtocolId::Beacon, "0x502A".to_string());
portal_networks.insert(ProtocolId::Utp, "0x757470".to_string());
NetworkSpec { portal_networks }.into()
});

impl fmt::Display for ProtocolId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -206,23 +218,6 @@ impl fmt::Display for ProtocolId {
}
}

/// Decode ProtocolId to raw bytes
impl TryFrom<ProtocolId> for Vec<u8> {
type Error = ProtocolIdError;

fn try_from(protocol_id: ProtocolId) -> Result<Self, Self::Error> {
let bytes = match protocol_id {
ProtocolId::State => hex_decode("0x500A"),
ProtocolId::History => hex_decode("0x500B"),
ProtocolId::TransactionGossip => hex_decode("0x500C"),
ProtocolId::CanonicalIndices => hex_decode("0x500D"),
ProtocolId::Beacon => hex_decode("0x501A"),
ProtocolId::Utp => hex_decode("0x757470"),
};
bytes.map_err(ProtocolIdError::Decode)
}
}

/// A Portal protocol message.
#[derive(Debug, PartialEq, Clone, Encode, Decode)]
#[ssz(enum_behaviour = "union")]
Expand Down Expand Up @@ -578,21 +573,20 @@ impl From<Accept> for Value {
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
use crate::utils::bytes::hex_encode_upper;
use std::str::FromStr;
use test_log::test;

#[test]
#[should_panic]
fn protocol_id_invalid() {
let hex = "0x500F";
ProtocolId::from_str(hex).unwrap();
assert!(!MAINNET.portal_networks.contains_right(hex));
}

#[test]
fn protocol_id_encoding() {
let hex = "0x500A";
let protocol_id = ProtocolId::from_str(hex).unwrap();
let expected_hex = hex_encode_upper(Vec::try_from(protocol_id).unwrap());
let protocol_id = MAINNET.portal_networks.get_by_right(hex).unwrap();
let expected_hex = MAINNET.portal_networks.get_by_left(protocol_id).unwrap();
assert_eq!(hex, expected_hex);
}

Expand Down
4 changes: 2 additions & 2 deletions ethportal-peertest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn generate_trin_config(id: u16, bootnode_enr: Option<&Enr>) -> TrinConfig {
let web3_ipc_path_str = web3_ipc_path.as_path().display().to_string();
let trin_config_args = vec![
"trin",
"--networks",
"--portal-subnetworks",
"history,beacon",
"--external-address",
external_addr.as_str(),
Expand All @@ -102,7 +102,7 @@ fn generate_trin_config(id: u16, bootnode_enr: Option<&Enr>) -> TrinConfig {
let web3_ipc_path_str = web3_ipc_path.as_path().display().to_string();
let trin_config_args = vec![
"trin",
"--networks",
"--portal-subnetworks",
"history,beacon",
"--external-address",
external_addr.as_str(),
Expand Down
2 changes: 1 addition & 1 deletion ethportal-peertest/src/scenarios/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn test_gossip_with_trace(peertest: &Peertest, target: &Client) {
let trin_config = TrinConfig::new_from(
[
"trin",
"--networks",
"--portal-subnetworks",
"history,state",
"--external-address",
external_addr.as_str(),
Expand Down
2 changes: 1 addition & 1 deletion portal-bridge/src/client_handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn trin_handle(
.args(["--ephemeral"])
.args(["--mb", "0"])
.args(["--web3-transport", "http"])
.args(["--networks", &networks])
.args(["--portal-subnetworks", &networks])
.args(["--unsafe-private-key", &private_key])
.args([
"--web3-http-address",
Expand Down
Loading

0 comments on commit ea72788

Please sign in to comment.