Skip to main content

phos_data_network/config/
networks.rs

1use std::fmt::Display;
2#[cfg(not(target_arch = "wasm32"))]
3use std::path::PathBuf;
4use std::str::FromStr;
5use std::time::Duration;
6
7#[cfg(not(target_arch = "wasm32"))]
8use dirs::home_dir;
9use eyre::Result;
10use serde::{Deserialize, Serialize};
11use strum::EnumIter;
12use url::Url;
13
14use helios_common::fork_schedule::ForkSchedule;
15use phos_light_client::verifier::{options::Options, types::TrustThreshold};
16
17use crate::config::base::BaseConfig;
18use crate::config::types::ChainConfig;
19
20#[derive(
21    Debug, Clone, Copy, Serialize, Deserialize, EnumIter, Hash, Eq, PartialEq, PartialOrd, Ord,
22)]
23pub enum Network {
24    Mainnet,
25    Aeneid,
26}
27
28impl FromStr for Network {
29    type Err = eyre::Report;
30
31    fn from_str(s: &str) -> Result<Self> {
32        match s {
33            "mainnet" => Ok(Self::Mainnet),
34            "aeneid" => Ok(Self::Aeneid),
35            _ => Err(eyre::eyre!("network not recognized")),
36        }
37    }
38}
39
40impl Display for Network {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        let str = match self {
43            Self::Mainnet => "mainnet",
44            Self::Aeneid => "aeneid",
45        };
46
47        f.write_str(str)
48    }
49}
50
51impl Network {
52    pub fn to_base_config(&self) -> BaseConfig {
53        match self {
54            Self::Mainnet => mainnet(),
55            Self::Aeneid => aeneid(),
56        }
57    }
58
59    pub fn from_chain_id(id: u64) -> Result<Self> {
60        match id {
61            1514 => Ok(Network::Mainnet),
62            1315 => Ok(Network::Aeneid),
63            _ => Err(eyre::eyre!("chain id not known")),
64        }
65    }
66}
67
68pub fn mainnet() -> BaseConfig {
69    BaseConfig {
70        rpc_port: 8545,
71        consensus_rpc: Some(Url::parse("https://story-consensus-rpc.publicnode.com").unwrap()),
72        execution_rpc: Some(Url::parse("https://story-rpc.publicnode.com").unwrap()),
73        chain: ChainConfig { chain_id: 1514 },
74        verifier_options: Options {
75            trust_threshold: TrustThreshold::ONE_THIRD,
76            trusting_period: Duration::from_secs(806_400),
77            clock_drift: Duration::from_secs(5),
78        },
79        execution_forks: DataNetworkForkSchedule::mainnet(),
80        #[cfg(not(target_arch = "wasm32"))]
81        data_dir: Some(data_dir(Network::Mainnet)),
82        ..std::default::Default::default()
83    }
84}
85
86pub fn aeneid() -> BaseConfig {
87    BaseConfig {
88        rpc_port: 8545,
89        consensus_rpc: None,
90        chain: ChainConfig { chain_id: 1315 },
91        verifier_options: Options {
92            trust_threshold: TrustThreshold::ONE_THIRD,
93            trusting_period: Duration::from_secs(806_400),
94            clock_drift: Duration::from_secs(5),
95        },
96        execution_forks: DataNetworkForkSchedule::aeneid(),
97        #[cfg(not(target_arch = "wasm32"))]
98        data_dir: Some(data_dir(Network::Aeneid)),
99        ..std::default::Default::default()
100    }
101}
102
103#[cfg(not(target_arch = "wasm32"))]
104fn data_dir(network: Network) -> PathBuf {
105    match home_dir() {
106        Some(home) => home.join(format!(".helios/data/{network}")),
107        None => std::env::temp_dir().join(format!("helios/data/{network}")),
108    }
109}
110
111pub struct DataNetworkForkSchedule;
112
113impl DataNetworkForkSchedule {
114    fn mainnet() -> ForkSchedule {
115        ForkSchedule {
116            frontier_timestamp: 0,
117            homestead_timestamp: 0,
118            dao_timestamp: u64::MAX,
119            tangerine_timestamp: 0,
120            spurious_dragon_timestamp: 0,
121            byzantium_timestamp: 0,
122            constantinople_timestamp: 0,
123            petersburg_timestamp: 0,
124            istanbul_timestamp: 0,
125            muir_glacier_timestamp: u64::MAX,
126            berlin_timestamp: 0,
127            london_timestamp: 0,
128            arrow_glacier_timestamp: 0,
129            gray_glacier_timestamp: 0,
130            paris_timestamp: 0,
131            shanghai_timestamp: 0,
132            cancun_timestamp: 0,
133            prague_timestamp: 1_751_934_608,
134            osaka_timestamp: 1_768_435_200,
135
136            ..Default::default()
137        }
138    }
139
140    fn aeneid() -> ForkSchedule {
141        ForkSchedule {
142            frontier_timestamp: 0,
143            homestead_timestamp: 0,
144            dao_timestamp: u64::MAX,
145            tangerine_timestamp: 0,
146            spurious_dragon_timestamp: 0,
147            byzantium_timestamp: 0,
148            constantinople_timestamp: 0,
149            petersburg_timestamp: 0,
150            istanbul_timestamp: 0,
151            muir_glacier_timestamp: u64::MAX,
152            berlin_timestamp: 0,
153            london_timestamp: 0,
154            arrow_glacier_timestamp: 0,
155            gray_glacier_timestamp: 0,
156            paris_timestamp: 0,
157            shanghai_timestamp: 0,
158            cancun_timestamp: 0,
159            prague_timestamp: 1_748_305_808,
160            osaka_timestamp: 1_767_830_400,
161
162            ..Default::default()
163        }
164    }
165}