From df182aaccdbbbfef049db1a392ef88ade8ab8bde Mon Sep 17 00:00:00 2001 From: dereksione Date: Thu, 29 Jun 2023 06:17:50 -0400 Subject: [PATCH 1/6] feat(primitives): add new crate symphony-primitives --- Cargo.lock | 4 ++++ Cargo.toml | 4 +++- crates/symphony-primitives/Cargo.toml | 17 +++++++++++++++++ crates/symphony-primitives/src/lib.rs | 0 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 crates/symphony-primitives/Cargo.toml create mode 100644 crates/symphony-primitives/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 06036c4a16af..3edc9e09eb4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6838,6 +6838,10 @@ dependencies = [ "symbolic-common", ] +[[package]] +name = "symphony-primitives" +version = "0.1.0-alpha.1" + [[package]] name = "syn" version = "0.15.44" diff --git a/Cargo.toml b/Cargo.toml index 6b94e8db06f0..65f45d52d14e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ members = [ "crates/transaction-pool", "crates/trie", "testing/ef-tests", + "crates/symphony-primitives" ] default-members = ["bin/reth"] @@ -58,7 +59,7 @@ edition = "2021" rust-version = "1.70" # Remember to update .clippy.toml and README.md license = "MIT OR Apache-2.0" homepage = "https://paradigmxyz.github.io/reth" -repository = "https://github.com/paradigmxyz/reth" +repository = "https://github.com/astriaorg/reth" # Like release, but with full debug symbols. Useful for e.g. `perf`. [profile.debug-fast] @@ -92,6 +93,7 @@ reth-payload-builder = { path = "./crates/payload/builder" } reth-transaction-pool = { path = "./crates/transaction-pool" } reth-tasks = { path = "./crates/tasks" } reth-network-api = { path = "./crates/net/network-api" } +symphony-primitives = { path = "./crates/symphony-primitives"} ## eth ethers-core = { version = "2.0.7", default-features = false } diff --git a/crates/symphony-primitives/Cargo.toml b/crates/symphony-primitives/Cargo.toml new file mode 100644 index 000000000000..825d0aaac026 --- /dev/null +++ b/crates/symphony-primitives/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "symphony-primitives" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +description = "Symphony specific primitive data types." + +# [dependencies] + +# [dev-dependencies] + +# [features] + +# [[bench]] \ No newline at end of file diff --git a/crates/symphony-primitives/src/lib.rs b/crates/symphony-primitives/src/lib.rs new file mode 100644 index 000000000000..e69de29bb2d1 From 6955cd66adc16bdd25de688ad85409841d1b4009 Mon Sep 17 00:00:00 2001 From: dereksione Date: Thu, 29 Jun 2023 09:06:04 -0400 Subject: [PATCH 2/6] wip: adding new chains ds --- Cargo.lock | 60 ++++- Cargo.toml | 1 + bin/reth/Cargo.toml | 1 + crates/net/eth-wire/Cargo.toml | 3 + crates/primitives/Cargo.toml | 4 + crates/primitives/src/chain/mod.rs | 232 +++++++++----------- crates/symphony-primitives/Cargo.toml | 6 +- crates/symphony-primitives/src/chains.rs | 68 ++++++ crates/symphony-primitives/src/constants.rs | 8 + crates/symphony-primitives/src/lib.rs | 4 + 10 files changed, 252 insertions(+), 135 deletions(-) create mode 100644 crates/symphony-primitives/src/chains.rs create mode 100644 crates/symphony-primitives/src/constants.rs diff --git a/Cargo.lock b/Cargo.lock index 3edc9e09eb4d..43025d62a235 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4174,6 +4174,32 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "parse-display" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddcac6cdc2aaa03a89780c6681ffb46ac5b4b7dbade8d2a20a6a501f6fd363a8" +dependencies = [ + "once_cell", + "parse-display-derive", + "regex", +] + +[[package]] +name = "parse-display-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e503280f7d6dbf7566bc63d903c3b0f595fc11553a7e1f87f81adb8fcbca983" +dependencies = [ + "once_cell", + "proc-macro2 1.0.60", + "quote 1.0.28", + "regex", + "regex-syntax 0.6.29", + "structmeta", + "syn 2.0.18", +] + [[package]] name = "paste" version = "1.0.11" @@ -4935,6 +4961,7 @@ dependencies = [ "metrics-exporter-prometheus", "metrics-process", "metrics-util", + "parse-display", "pin-project", "pretty_assertions", "proptest", @@ -5267,6 +5294,7 @@ dependencies = [ "serde", "smol_str", "snap", + "symphony-primitives", "test-fuzz", "thiserror", "tokio", @@ -5469,7 +5497,6 @@ dependencies = [ "reth-interfaces", "reth-metrics", "reth-primitives", - "reth-revm-primitives", "reth-rlp", "reth-rpc-types", "revm-primitives", @@ -5498,6 +5525,7 @@ dependencies = [ "impl-serde", "modular-bitfield", "once_cell", + "parse-display", "plain_hasher", "pprof", "proptest", @@ -5514,6 +5542,7 @@ dependencies = [ "serde_with", "strum", "sucds", + "symphony-primitives", "test-fuzz", "thiserror", "tiny-keccak", @@ -6755,6 +6784,29 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "structmeta" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ad9e09554f0456d67a69c1584c9798ba733a5b50349a6c0d0948710523922d" +dependencies = [ + "proc-macro2 1.0.60", + "quote 1.0.28", + "structmeta-derive", + "syn 2.0.18", +] + +[[package]] +name = "structmeta-derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a60bcaff7397072dca0017d1db428e30d5002e00b6847703e2e42005c95fbe00" +dependencies = [ + "proc-macro2 1.0.60", + "quote 1.0.28", + "syn 2.0.18", +] + [[package]] name = "strum" version = "0.24.1" @@ -6841,6 +6893,12 @@ dependencies = [ [[package]] name = "symphony-primitives" version = "0.1.0-alpha.1" +dependencies = [ + "parse-display", + "serde", + "serde_json", + "thiserror", +] [[package]] name = "syn" diff --git a/Cargo.toml b/Cargo.toml index 65f45d52d14e..1be46fbaa22d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,7 @@ thiserror = "1.0.37" serde_json = "1.0.94" serde = { version = "1.0", default-features = false } rand = "0.8.5" +parse-display = { version = "0.8.1"} ## tokio diff --git a/bin/reth/Cargo.toml b/bin/reth/Cargo.toml index d9955fa8910d..5cfc3ee038cf 100644 --- a/bin/reth/Cargo.toml +++ b/bin/reth/Cargo.toml @@ -86,6 +86,7 @@ hex = "0.4" thiserror = { workspace = true } pretty_assertions = "1.3.0" humantime = "2.1.0" +parse-display.workspace = true [features] jemalloc = ["dep:jemallocator", "dep:jemalloc-ctl"] diff --git a/crates/net/eth-wire/Cargo.toml b/crates/net/eth-wire/Cargo.toml index de43afd7130b..ac360263640d 100644 --- a/crates/net/eth-wire/Cargo.toml +++ b/crates/net/eth-wire/Cargo.toml @@ -13,6 +13,9 @@ bytes.workspace = true thiserror = { workspace = true } serde = { workspace = true, optional = true } +#symphony +symphony-primitives = { workspace = true } + # reth reth-codecs = { path = "../../storage/codecs" } reth-primitives = { workspace = true } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index a34e21094d04..7476e8e9f198 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -9,6 +9,9 @@ repository.workspace = true description = "Commonly used types in reth." [dependencies] +#symphony +symphony-primitives = { workspace = true } + # reth reth-rlp = { workspace = true, features = ["std", "derive", "ethereum-types"] } reth-rlp-derive = { path = "../rlp/rlp-derive" } @@ -57,6 +60,7 @@ url = "2.3" impl-serde = "0.4.0" once_cell = "1.17.0" zstd = { version = "0.12", features = ["experimental"] } +parse-display = { workspace = true } # proof related triehash = "0.8" diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index 8a5a5c7d338b..3b7a6efc05ea 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -1,11 +1,11 @@ use crate::{ - net::{goerli_nodes, mainnet_nodes, sepolia_nodes}, NodeRecord, U256, }; use ethers_core::types::U64; use reth_codecs::add_arbitrary_tests; use reth_rlp::{Decodable, Encodable}; use serde::{Deserialize, Serialize}; +use symphony_primitives::{SymphonyChains}; use std::{fmt, str::FromStr}; // The chain spec module. @@ -20,11 +20,11 @@ mod info; pub use info::ChainInfo; /// Either a named or chain id or the actual id value -#[add_arbitrary_tests(rlp)] +// #[add_arbitrary_tests(rlp)] // FIXME: what do we need to do to add this back? #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Chain { /// Contains a known chain - Named(ethers_core::types::Chain), + Named(SymphonyChains), /// Contains the id of a chain Id(u64), } @@ -32,17 +32,17 @@ pub enum Chain { impl Chain { /// Returns the mainnet chain. pub const fn mainnet() -> Self { - Chain::Named(ethers_core::types::Chain::Mainnet) + Chain::Named(SymphonyChains::Mainnet) } - /// Returns the goerli chain. - pub const fn goerli() -> Self { - Chain::Named(ethers_core::types::Chain::Goerli) + /// Returns the devnet chain. + pub const fn devnet() -> Self { + Chain::Named(SymphonyChains::Devnet) } - /// Returns the sepolia chain. - pub const fn sepolia() -> Self { - Chain::Named(ethers_core::types::Chain::Sepolia) + /// Returns the testnet chain. + pub const fn testnet() -> Self { + Chain::Named(SymphonyChains::Testnet) } /// The id of the chain @@ -53,40 +53,31 @@ impl Chain { } } - /// Helper function for checking if a chainid corresponds to a legacy chainid - /// without eip1559 - pub fn is_legacy(&self) -> bool { - match self { - Chain::Named(c) => c.is_legacy(), - Chain::Id(_) => false, - } - } - - /// Returns the address of the public DNS node list for the given chain. - /// - /// See also - pub fn public_dns_network_protocol(self) -> Option { - use ethers_core::types::Chain::*; - const DNS_PREFIX: &str = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@"; - - let named: ethers_core::types::Chain = self.try_into().ok()?; - - if matches!(named, Mainnet | Goerli | Sepolia | Ropsten | Rinkeby) { - return Some(format!("{DNS_PREFIX}all.{}.ethdisco.net", named.as_ref().to_lowercase())) - } - None - } - - /// Returns bootnodes for the given chain. - pub fn bootnodes(self) -> Option> { - use ethers_core::types::Chain::*; - match self.try_into().ok()? { - Mainnet => Some(mainnet_nodes()), - Goerli => Some(goerli_nodes()), - Sepolia => Some(sepolia_nodes()), - _ => None, - } - } + // Returns the address of the public DNS node list for the given chain. + // + // See also + // pub fn public_dns_network_protocol(self) -> Option { + // use ethers_core::types::Chain::*; + // const DNS_PREFIX: &str = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@"; + + // let named: ethers_core::types::Chain = self.try_into().ok()?; + + // if matches!(named, Mainnet | Goerli | Sepolia | Ropsten | Rinkeby) { + // return Some(format!("{DNS_PREFIX}all.{}.ethdisco.net", named.as_ref().to_lowercase())) + // } + // None + // } + + // Returns bootnodes for the given chain. + // pub fn bootnodes(self) -> Option> { + // use ethers_core::types::Chain::*; + // match self.try_into().ok()? { + // Mainnet => Some(mainnet_nodes()), + // Goerli => Some(goerli_nodes()), + // Sepolia => Some(sepolia_nodes()), + // _ => None, + // } + // } } impl fmt::Display for Chain { @@ -104,15 +95,15 @@ impl fmt::Display for Chain { } } -impl From for Chain { - fn from(id: ethers_core::types::Chain) -> Self { +impl From for Chain { + fn from(id: SymphonyChains) -> Self { Chain::Named(id) } } impl From for Chain { fn from(id: u64) -> Self { - ethers_core::types::Chain::try_from(id).map(Chain::Named).unwrap_or_else(|_| Chain::Id(id)) + SymphonyChains::try_from(id).map(Chain::Named).unwrap_or_else(|_| Chain::Id(id)) } } @@ -143,22 +134,11 @@ impl From for U256 { } } -impl TryFrom for ethers_core::types::Chain { - type Error = >::Error; - - fn try_from(chain: Chain) -> Result { - match chain { - Chain::Named(chain) => Ok(chain), - Chain::Id(id) => id.try_into(), - } - } -} - impl FromStr for Chain { type Err = String; fn from_str(s: &str) -> Result { - if let Ok(chain) = ethers_core::types::Chain::from_str(s) { + if let Ok(chain) = SymphonyChains::try_from(s) { Ok(Chain::Named(chain)) } else { s.parse::() @@ -171,13 +151,14 @@ impl FromStr for Chain { impl Encodable for Chain { fn encode(&self, out: &mut dyn reth_rlp::BufMut) { match self { - Self::Named(chain) => u64::from(*chain).encode(out), - Self::Id(id) => id.encode(out), - } + Self::Named(chain) => u64::try_from(*chain).unwrap(), + Self::Id(id) => *id, + }.encode(out) } + fn length(&self) -> usize { match self { - Self::Named(chain) => u64::from(*chain).length(), + Self::Named(chain) => u64::try_from(*chain).unwrap().length(), Self::Id(id) => id.length(), } } @@ -191,49 +172,52 @@ impl Decodable for Chain { impl Default for Chain { fn default() -> Self { - ethers_core::types::Chain::Mainnet.into() + Chain::Named(SymphonyChains::Mainnet) } } -#[cfg(any(test, feature = "arbitrary"))] -impl<'a> arbitrary::Arbitrary<'a> for Chain { - fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - if u.ratio(1, 2)? { - let chain = u.int_in_range(0..=(ethers_core::types::Chain::COUNT - 1))?; - - return Ok(Chain::Named(ethers_core::types::Chain::iter().nth(chain).expect("in range"))) - } - - Ok(Self::Id(u64::arbitrary(u)?)) - } -} - -#[cfg(any(test, feature = "arbitrary"))] -use strum::{EnumCount, IntoEnumIterator}; - -#[cfg(any(test, feature = "arbitrary"))] -use proptest::{ - arbitrary::ParamsFor, - prelude::{any, Strategy}, - sample::Selector, - strategy::BoxedStrategy, -}; - -#[cfg(any(test, feature = "arbitrary"))] -impl proptest::arbitrary::Arbitrary for Chain { - type Parameters = ParamsFor; - fn arbitrary_with(_: Self::Parameters) -> Self::Strategy { - let named = any::() - .prop_map(move |sel| Chain::Named(sel.select(ethers_core::types::Chain::iter()))); - let id = any::().prop_map(Chain::from); - proptest::strategy::Union::new_weighted(vec![(50, named.boxed()), (50, id.boxed())]).boxed() - } - - type Strategy = BoxedStrategy; -} +// FIXME: what does this do? +// #[cfg(any(test, feature = "arbitrary"))] +// impl<'a> arbitrary::Arbitrary<'a> for Chain { +// fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { +// if u.ratio(1, 2)? { +// let chain = u.int_in_range(0..=(ethers_core::types::Chain::COUNT - 1))?; + +// return Ok(Chain::Named(ethers_core::types::Chain::iter().nth(chain).expect("in range"))) +// } + +// Ok(Self::Id(u64::arbitrary(u)?)) +// } +// } + +// #[cfg(any(test, feature = "arbitrary"))] +// use strum::{EnumCount, IntoEnumIterator}; + +// #[cfg(any(test, feature = "arbitrary"))] +// use proptest::{ +// arbitrary::ParamsFor, +// prelude::{any, Strategy}, +// sample::Selector, +// strategy::BoxedStrategy, +// }; + +// #[cfg(any(test, feature = "arbitrary"))] +// impl proptest::arbitrary::Arbitrary for Chain { +// type Parameters = ParamsFor; +// fn arbitrary_with(_: Self::Parameters) -> Self::Strategy { +// let named = any::() +// .prop_map(move |sel| Chain::Named(sel.select(ethers_core::types::Chain::iter()))); +// let id = any::().prop_map(Chain::from); +// proptest::strategy::Union::new_weighted(vec![(50, named.boxed()), (50, id.boxed())]).boxed() +// } + +// type Strategy = BoxedStrategy; +// } #[cfg(test)] mod tests { + use symphony_primitives::constants::DEVNET_ID; + use super::*; #[test] @@ -244,31 +228,13 @@ mod tests { #[test] fn test_named_id() { - let chain = Chain::Named(ethers_core::types::Chain::Goerli); - assert_eq!(chain.id(), 5); - } - - #[test] - fn test_legacy_named_chain() { - let chain = Chain::Named(ethers_core::types::Chain::Optimism); - assert!(chain.is_legacy()); - } - - #[test] - fn test_not_legacy_named_chain() { - let chain = Chain::Named(ethers_core::types::Chain::Mainnet); - assert!(!chain.is_legacy()); - } - - #[test] - fn test_not_legacy_id_chain() { - let chain = Chain::Id(1234); - assert!(!chain.is_legacy()); + let chain = Chain::Named(SymphonyChains::Devnet); + assert_eq!(chain.id(), DEVNET_ID); } #[test] fn test_display_named_chain() { - let chain = Chain::Named(ethers_core::types::Chain::Mainnet); + let chain = Chain::Named(SymphonyChains::Mainnet); assert_eq!(format!("{chain}"), "mainnet"); } @@ -289,9 +255,9 @@ mod tests { #[test] fn test_into_u256() { - let chain = Chain::Named(ethers_core::types::Chain::Goerli); + let chain = Chain::Named(SymphonyChains::Devnet); let n: U256 = chain.into(); - let expected = U256::from(5); + let expected = U256::from(DEVNET_ID); assert_eq!(n, expected); } @@ -299,9 +265,9 @@ mod tests { #[test] #[allow(non_snake_case)] fn test_into_U64() { - let chain = Chain::Named(ethers_core::types::Chain::Goerli); + let chain = Chain::Named(SymphonyChains::Devnet); let n: U64 = chain.into(); - let expected = U64::from(5); + let expected = U64::from(DEVNET_ID); assert_eq!(n, expected); } @@ -309,7 +275,7 @@ mod tests { #[test] fn test_from_str_named_chain() { let result = Chain::from_str("mainnet"); - let expected = Chain::Named(ethers_core::types::Chain::Mainnet); + let expected = Chain::Named(SymphonyChains::Mainnet); assert!(result.is_ok()); assert_eq!(result.unwrap(), expected); @@ -334,7 +300,7 @@ mod tests { #[test] fn test_default() { let default = Chain::default(); - let expected = Chain::Named(ethers_core::types::Chain::Mainnet); + let expected = Chain::Named(SymphonyChains::Mainnet); assert_eq!(default, expected); } @@ -346,10 +312,10 @@ mod tests { assert_eq!(chain.length(), 3); } - #[test] - fn test_dns_network() { - let s = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.mainnet.ethdisco.net"; - let chain: Chain = ethers_core::types::Chain::Mainnet.into(); - assert_eq!(s, chain.public_dns_network_protocol().unwrap().as_str()); - } + // #[test] + // fn test_dns_network() { + // let s = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.mainnet.ethdisco.net"; + // let chain: Chain = ethers_core::types::Chain::Mainnet.into(); + // assert_eq!(s, chain.public_dns_network_protocol().unwrap().as_str()); + // } } diff --git a/crates/symphony-primitives/Cargo.toml b/crates/symphony-primitives/Cargo.toml index 825d0aaac026..3d23f718a067 100644 --- a/crates/symphony-primitives/Cargo.toml +++ b/crates/symphony-primitives/Cargo.toml @@ -8,7 +8,11 @@ homepage.workspace = true repository.workspace = true description = "Symphony specific primitive data types." -# [dependencies] +[dependencies] +thiserror ={ workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +parse-display = { workspace = true } # [dev-dependencies] diff --git a/crates/symphony-primitives/src/chains.rs b/crates/symphony-primitives/src/chains.rs new file mode 100644 index 000000000000..375a7c396f8c --- /dev/null +++ b/crates/symphony-primitives/src/chains.rs @@ -0,0 +1,68 @@ + +use std::{fmt::{Display, Debug}}; + +use serde::{Serialize, Deserialize}; +use crate::constants::{DEVNET_ID, MAINNET_ID, TESTNET_ID}; + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[repr(u64)] +pub enum SymphonyChains { + Mainnet = MAINNET_ID, + Devnet = DEVNET_ID, + Testnet = TESTNET_ID +} + +#[derive(Debug)] +pub enum SymphonyChainError { + UnrecognizedChainId, + UnrecognizedStr +} + +impl TryFrom for SymphonyChains { + type Error = SymphonyChainError; + fn try_from(value: u64) -> Result { + match value { + MAINNET_ID => Ok(Self::Mainnet), + DEVNET_ID => Ok(Self::Devnet), + TESTNET_ID => Ok(Self::Testnet), + _ => Err(SymphonyChainError::UnrecognizedChainId) + } + } +} + +impl TryFrom for u64 { + type Error = SymphonyChainError; + fn try_from(value: SymphonyChains) -> Result { + match value { + SymphonyChains::Mainnet => Ok(MAINNET_ID), + SymphonyChains::Devnet => Ok(DEVNET_ID), + SymphonyChains::Testnet => Ok(TESTNET_ID), + _ => Err(SymphonyChainError::UnrecognizedChainId) + } + } +} + + +impl TryFrom<&str> for SymphonyChains { + type Error = SymphonyChainError; + fn try_from(value: &str) -> Result { + match value.to_lowercase().as_str() { + "mainnet" => Ok(Self::Mainnet), + "devnet" => Ok(Self::Devnet), + "testnet" => Ok(Self::Testnet), + _ => Err(SymphonyChainError::UnrecognizedStr) + } + } +} + +impl Display for SymphonyChains { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let chain_name = match self { + SymphonyChains::Mainnet => "mainnet", + SymphonyChains::Devnet => "devnet", + SymphonyChains::Testnet => "testnet" + }; + + write!(f, "symphony-{chain_name}") + } +} \ No newline at end of file diff --git a/crates/symphony-primitives/src/constants.rs b/crates/symphony-primitives/src/constants.rs new file mode 100644 index 000000000000..d6e92b7e824c --- /dev/null +++ b/crates/symphony-primitives/src/constants.rs @@ -0,0 +1,8 @@ + +pub const MAINNET_NAME: &str = "MAINNET"; +pub const DEVNET_NAME: &str = "DEVNET"; +pub const TESTNET_NAME: &str = "TESTNET"; + +pub const MAINNET_ID: u64 = 70047; +pub const DEVNET_ID: u64 = 70048; +pub const TESTNET_ID: u64 = 70049; \ No newline at end of file diff --git a/crates/symphony-primitives/src/lib.rs b/crates/symphony-primitives/src/lib.rs index e69de29bb2d1..97e96848011c 100644 --- a/crates/symphony-primitives/src/lib.rs +++ b/crates/symphony-primitives/src/lib.rs @@ -0,0 +1,4 @@ +pub mod constants; +pub mod chains; + +pub use chains::{SymphonyChains, SymphonyChainError}; \ No newline at end of file From 1d7577d5e7bcf5a8a196a4792c83f83ea862566c Mon Sep 17 00:00:00 2001 From: dereksione Date: Thu, 29 Jun 2023 09:41:07 -0400 Subject: [PATCH 3/6] feat(primitives): naive chain metadata replacements --- Cargo.lock | 1 + bin/reth/src/args/utils.rs | 14 ++++++------- bin/reth/src/dirs.rs | 8 ++++---- .../consensus/beacon/src/beacon_consensus.rs | 2 +- crates/consensus/common/src/calc.rs | 2 +- crates/net/eth-wire/src/types/status.rs | 9 +++++---- crates/net/network/src/config.rs | 6 +++--- crates/primitives/src/chain/mod.rs | 20 ++++++++++--------- crates/primitives/src/chain/spec.rs | 20 +++++++++---------- crates/primitives/src/lib.rs | 2 +- crates/revm/revm-primitives/src/env.rs | 2 +- 11 files changed, 45 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43025d62a235..75548273dda7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5497,6 +5497,7 @@ dependencies = [ "reth-interfaces", "reth-metrics", "reth-primitives", + "reth-revm-primitives", "reth-rlp", "reth-rpc-types", "revm-primitives", diff --git a/bin/reth/src/args/utils.rs b/bin/reth/src/args/utils.rs index 031b457d2ef4..52c65077f458 100644 --- a/bin/reth/src/args/utils.rs +++ b/bin/reth/src/args/utils.rs @@ -1,6 +1,6 @@ //! Clap parser utilities -use reth_primitives::{AllGenesisFormats, BlockHashOrNumber, ChainSpec, GOERLI, MAINNET, SEPOLIA}; +use reth_primitives::{AllGenesisFormats, BlockHashOrNumber, ChainSpec, TESTNET_SPEC, MAINNET_SPEC, DEVNET_SPEC}; use reth_revm::primitives::B256 as H256; use std::{ net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}, @@ -20,9 +20,9 @@ pub fn parse_duration_from_secs(arg: &str) -> eyre::Result eyre::Result, eyre::Error> { Ok(match s { - "mainnet" => MAINNET.clone(), - "goerli" => GOERLI.clone(), - "sepolia" => SEPOLIA.clone(), + "mainnet" => MAINNET_SPEC.clone(), + "testnet" => TESTNET_SPEC.clone(), + "devnet" => DEVNET_SPEC.clone(), _ => { let raw = std::fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?; serde_json::from_str(&raw)? @@ -34,9 +34,9 @@ pub fn chain_spec_value_parser(s: &str) -> eyre::Result, eyre::Er /// to a custom one. pub fn genesis_value_parser(s: &str) -> eyre::Result, eyre::Error> { Ok(match s { - "mainnet" => MAINNET.clone(), - "goerli" => GOERLI.clone(), - "sepolia" => SEPOLIA.clone(), + "mainnet" => MAINNET_SPEC.clone(), + "testnet" => TESTNET_SPEC.clone(), + "devnet" => DEVNET_SPEC.clone(), _ => { let raw = std::fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?; let genesis: AllGenesisFormats = serde_json::from_str(&raw)?; diff --git a/bin/reth/src/dirs.rs b/bin/reth/src/dirs.rs index 796377b6a164..67cc42eb6b0c 100644 --- a/bin/reth/src/dirs.rs +++ b/bin/reth/src/dirs.rs @@ -12,10 +12,10 @@ use std::{ pub fn config_path_prefix(chain: Chain) -> String { if chain == Chain::mainnet() { "mainnet".to_string() - } else if chain == Chain::goerli() { - "goerli".to_string() - } else if chain == Chain::sepolia() { - "sepolia".to_string() + } else if chain == Chain::devnet() { + "devnet".to_string() + } else if chain == Chain::testnet() { + "testnet".to_string() } else { chain.id().to_string() } diff --git a/crates/consensus/beacon/src/beacon_consensus.rs b/crates/consensus/beacon/src/beacon_consensus.rs index 449d46845c76..762fa65bde23 100644 --- a/crates/consensus/beacon/src/beacon_consensus.rs +++ b/crates/consensus/beacon/src/beacon_consensus.rs @@ -72,7 +72,7 @@ impl Consensus for BeaconConsensus { // Goerli exception: // * If the network is goerli pre-merge, ignore the extradata check, since we do not // support clique. - if self.chain_spec.chain != Chain::goerli() { + if self.chain_spec.chain != Chain::devnet() { validate_header_extradata(header)?; } } diff --git a/crates/consensus/common/src/calc.rs b/crates/consensus/common/src/calc.rs index 304a081e5ce2..f687adece23e 100644 --- a/crates/consensus/common/src/calc.rs +++ b/crates/consensus/common/src/calc.rs @@ -25,7 +25,7 @@ pub fn base_block_reward( block_difficulty: U256, total_difficulty: U256, ) -> Option { - if chain_spec.chain == Chain::goerli() || + if chain_spec.chain == Chain::devnet() || chain_spec.fork(Hardfork::Paris).active_at_ttd(total_difficulty, block_difficulty) { None diff --git a/crates/net/eth-wire/src/types/status.rs b/crates/net/eth-wire/src/types/status.rs index a29af1894147..ca6264c637b5 100644 --- a/crates/net/eth-wire/src/types/status.rs +++ b/crates/net/eth-wire/src/types/status.rs @@ -2,9 +2,10 @@ use crate::{EthVersion, StatusBuilder}; use reth_codecs::derive_arbitrary; use reth_primitives::{ - hex, Chain, ChainSpec, ForkId, Genesis, Hardfork, Head, H256, MAINNET, U256, + hex, Chain, ChainSpec, ForkId, Genesis, Hardfork, Head, H256, MAINNET_SPEC, U256, }; use reth_rlp::{RlpDecodable, RlpEncodable}; +use symphony_primitives::SymphonyChains; use std::fmt::{Debug, Display}; #[cfg(feature = "serde")] @@ -132,15 +133,15 @@ impl Debug for Status { // impl Default for Status { fn default() -> Self { - let mainnet_genesis = MAINNET.genesis_hash(); + let mainnet_genesis = MAINNET_SPEC.genesis_hash(); Status { version: EthVersion::Eth68 as u8, - chain: Chain::Named(ethers_core::types::Chain::Mainnet), + chain: Chain::Named(SymphonyChains::Mainnet), total_difficulty: U256::from(17_179_869_184u64), blockhash: mainnet_genesis, genesis: mainnet_genesis, forkid: Hardfork::Frontier - .fork_id(&MAINNET) + .fork_id(&MAINNET_SPEC) .expect("The Frontier hardfork should always exist"), } } diff --git a/crates/net/network/src/config.rs b/crates/net/network/src/config.rs index dbfc570873a5..08aebf243ed0 100644 --- a/crates/net/network/src/config.rs +++ b/crates/net/network/src/config.rs @@ -11,7 +11,7 @@ use reth_discv4::{Discv4Config, Discv4ConfigBuilder, DEFAULT_DISCOVERY_PORT}; use reth_dns_discovery::DnsDiscoveryConfig; use reth_ecies::util::pk2id; use reth_eth_wire::{HelloMessage, Status}; -use reth_primitives::{ChainSpec, ForkFilter, Head, NodeRecord, PeerId, MAINNET}; +use reth_primitives::{ChainSpec, ForkFilter, Head, NodeRecord, PeerId, MAINNET_SPEC}; use reth_provider::{BlockReader, HeaderProvider}; use reth_tasks::{TaskSpawner, TokioTaskExecutor}; use secp256k1::SECP256K1; @@ -160,7 +160,7 @@ impl NetworkConfigBuilder { listener_addr: None, peers_config: None, sessions_config: None, - chain_spec: MAINNET.clone(), + chain_spec: MAINNET_SPEC.clone(), network_mode: Default::default(), executor: None, hello_message: None, @@ -456,7 +456,7 @@ mod tests { #[test] fn test_network_fork_filter_default() { - let mut chain_spec = Arc::clone(&MAINNET); + let mut chain_spec = Arc::clone(&MAINNET_SPEC); // remove any `next` fields we would have by removing all hardforks Arc::make_mut(&mut chain_spec).hardforks = BTreeMap::new(); diff --git a/crates/primitives/src/chain/mod.rs b/crates/primitives/src/chain/mod.rs index 3b7a6efc05ea..30fff8c5b2a0 100644 --- a/crates/primitives/src/chain/mod.rs +++ b/crates/primitives/src/chain/mod.rs @@ -12,7 +12,7 @@ use std::{fmt, str::FromStr}; mod spec; pub use spec::{ AllGenesisFormats, ChainSpec, ChainSpecBuilder, DisplayHardforks, ForkCondition, - ForkTimestamps, GOERLI, MAINNET, SEPOLIA, + ForkTimestamps, TESTNET_SPEC, MAINNET_SPEC, DEVNET_SPEC, }; // The chain info module. @@ -53,10 +53,11 @@ impl Chain { } } - // Returns the address of the public DNS node list for the given chain. - // - // See also - // pub fn public_dns_network_protocol(self) -> Option { + /// Returns the address of the public DNS node list for the given chain. + /// + /// See also + pub fn public_dns_network_protocol(self) -> Option { + todo!() // use ethers_core::types::Chain::*; // const DNS_PREFIX: &str = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@"; @@ -66,10 +67,11 @@ impl Chain { // return Some(format!("{DNS_PREFIX}all.{}.ethdisco.net", named.as_ref().to_lowercase())) // } // None - // } + } - // Returns bootnodes for the given chain. - // pub fn bootnodes(self) -> Option> { + /// Returns bootnodes for the given chain. + pub fn bootnodes(self) -> Option> { + todo!() // use ethers_core::types::Chain::*; // match self.try_into().ok()? { // Mainnet => Some(mainnet_nodes()), @@ -77,7 +79,7 @@ impl Chain { // Sepolia => Some(sepolia_nodes()), // _ => None, // } - // } + } } impl fmt::Display for Chain { diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 4e888a5c10d1..92f8148bee58 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -16,7 +16,7 @@ use std::{ }; /// The Ethereum mainnet spec -pub static MAINNET: Lazy> = Lazy::new(|| { +pub static MAINNET_SPEC: Lazy> = Lazy::new(|| { ChainSpec { chain: Chain::mainnet(), genesis: serde_json::from_str(include_str!("../../res/genesis/mainnet.json")) @@ -58,10 +58,10 @@ pub static MAINNET: Lazy> = Lazy::new(|| { .into() }); -/// The Goerli spec -pub static GOERLI: Lazy> = Lazy::new(|| { +/// The devnet spec +pub static DEVNET_SPEC: Lazy> = Lazy::new(|| { ChainSpec { - chain: Chain::goerli(), + chain: Chain::devnet(), genesis: serde_json::from_str(include_str!("../../res/genesis/goerli.json")) .expect("Can't deserialize Goerli genesis json"), genesis_hash: Some(H256(hex!( @@ -92,10 +92,10 @@ pub static GOERLI: Lazy> = Lazy::new(|| { .into() }); -/// The Sepolia spec -pub static SEPOLIA: Lazy> = Lazy::new(|| { +/// The Testnet spec +pub static TESTNET_SPEC: Lazy> = Lazy::new(|| { ChainSpec { - chain: Chain::sepolia(), + chain: Chain::testnet(), genesis: serde_json::from_str(include_str!("../../res/genesis/sepolia.json")) .expect("Can't deserialize Sepolia genesis json"), genesis_hash: Some(H256(hex!( @@ -444,9 +444,9 @@ impl ChainSpecBuilder { /// Construct a new builder from the mainnet chain spec. pub fn mainnet() -> Self { Self { - chain: Some(MAINNET.chain), - genesis: Some(MAINNET.genesis.clone()), - hardforks: MAINNET.hardforks.clone(), + chain: Some(MAINNET_SPEC.chain), + genesis: Some(MAINNET_SPEC.genesis.clone()), + hardforks: MAINNET_SPEC.hardforks.clone(), } } diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 73845c2fd226..9d4f4a73ae1f 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -58,7 +58,7 @@ pub use block::{ pub use bloom::Bloom; pub use chain::{ AllGenesisFormats, Chain, ChainInfo, ChainSpec, ChainSpecBuilder, DisplayHardforks, - ForkCondition, ForkTimestamps, GOERLI, MAINNET, SEPOLIA, + ForkCondition, ForkTimestamps, TESTNET_SPEC, MAINNET_SPEC, DEVNET_SPEC, }; pub use compression::*; pub use constants::{ diff --git a/crates/revm/revm-primitives/src/env.rs b/crates/revm/revm-primitives/src/env.rs index 4a3ee3239282..0c53a276c43d 100644 --- a/crates/revm/revm-primitives/src/env.rs +++ b/crates/revm/revm-primitives/src/env.rs @@ -77,7 +77,7 @@ pub fn fill_block_env_with_coinbase( /// Return the coinbase address for the given header and chain spec. pub fn block_coinbase(chain_spec: &ChainSpec, header: &Header, after_merge: bool) -> Address { - if chain_spec.chain == Chain::goerli() && !after_merge { + if chain_spec.chain == Chain::devnet() && !after_merge { recover_header_signer(header).expect("failed to recover signer") } else { header.beneficiary From 694b7bb2c5938af8884d0a1d88626d9bf04a696c Mon Sep 17 00:00:00 2001 From: dereksione Date: Thu, 29 Jun 2023 21:23:16 -0400 Subject: [PATCH 4/6] fix: addressing comments --- crates/symphony-primitives/src/chains.rs | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/crates/symphony-primitives/src/chains.rs b/crates/symphony-primitives/src/chains.rs index 375a7c396f8c..e5f5abcf787e 100644 --- a/crates/symphony-primitives/src/chains.rs +++ b/crates/symphony-primitives/src/chains.rs @@ -2,7 +2,7 @@ use std::{fmt::{Display, Debug}}; use serde::{Serialize, Deserialize}; -use crate::constants::{DEVNET_ID, MAINNET_ID, TESTNET_ID}; +use crate::constants::{DEVNET_ID, MAINNET_ID, TESTNET_ID, MAINNET_NAME, DEVNET_NAME, TESTNET_NAME}; #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] #[repr(u64)] @@ -30,14 +30,12 @@ impl TryFrom for SymphonyChains { } } -impl TryFrom for u64 { - type Error = SymphonyChainError; - fn try_from(value: SymphonyChains) -> Result { +impl From for u64 { + fn from(value: SymphonyChains) -> Self { match value { - SymphonyChains::Mainnet => Ok(MAINNET_ID), - SymphonyChains::Devnet => Ok(DEVNET_ID), - SymphonyChains::Testnet => Ok(TESTNET_ID), - _ => Err(SymphonyChainError::UnrecognizedChainId) + SymphonyChains::Mainnet => MAINNET_ID, + SymphonyChains::Devnet => DEVNET_ID, + SymphonyChains::Testnet => TESTNET_ID, } } } @@ -46,10 +44,10 @@ impl TryFrom for u64 { impl TryFrom<&str> for SymphonyChains { type Error = SymphonyChainError; fn try_from(value: &str) -> Result { - match value.to_lowercase().as_str() { - "mainnet" => Ok(Self::Mainnet), - "devnet" => Ok(Self::Devnet), - "testnet" => Ok(Self::Testnet), + match value.to_uppercase().as_str() { + MAINNET_NAME => Ok(Self::Mainnet), + DEVNET_NAME => Ok(Self::Devnet), + TESTNET_NAME => Ok(Self::Testnet), _ => Err(SymphonyChainError::UnrecognizedStr) } } @@ -58,11 +56,13 @@ impl TryFrom<&str> for SymphonyChains { impl Display for SymphonyChains { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let chain_name = match self { - SymphonyChains::Mainnet => "mainnet", - SymphonyChains::Devnet => "devnet", - SymphonyChains::Testnet => "testnet" + SymphonyChains::Mainnet => MAINNET_NAME, + SymphonyChains::Devnet => DEVNET_NAME, + SymphonyChains::Testnet => TESTNET_NAME }; + let chain_name = chain_name.to_lowercase(); + write!(f, "symphony-{chain_name}") } } \ No newline at end of file From 2875309f376a7913d23b1b7a547f85618452164d Mon Sep 17 00:00:00 2001 From: dereksione Date: Thu, 29 Jun 2023 21:27:33 -0400 Subject: [PATCH 5/6] fix: rustfmt --- crates/symphony-primitives/src/chains.rs | 24 ++++++++++----------- crates/symphony-primitives/src/constants.rs | 3 +-- crates/symphony-primitives/src/lib.rs | 4 ++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/crates/symphony-primitives/src/chains.rs b/crates/symphony-primitives/src/chains.rs index e5f5abcf787e..3b3b7f68f769 100644 --- a/crates/symphony-primitives/src/chains.rs +++ b/crates/symphony-primitives/src/chains.rs @@ -1,21 +1,22 @@ +use std::fmt::{Debug, Display}; -use std::{fmt::{Display, Debug}}; - -use serde::{Serialize, Deserialize}; -use crate::constants::{DEVNET_ID, MAINNET_ID, TESTNET_ID, MAINNET_NAME, DEVNET_NAME, TESTNET_NAME}; +use crate::constants::{ + DEVNET_ID, DEVNET_NAME, MAINNET_ID, MAINNET_NAME, TESTNET_ID, TESTNET_NAME, +}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] #[repr(u64)] pub enum SymphonyChains { Mainnet = MAINNET_ID, Devnet = DEVNET_ID, - Testnet = TESTNET_ID + Testnet = TESTNET_ID, } #[derive(Debug)] pub enum SymphonyChainError { UnrecognizedChainId, - UnrecognizedStr + UnrecognizedStr, } impl TryFrom for SymphonyChains { @@ -25,7 +26,7 @@ impl TryFrom for SymphonyChains { MAINNET_ID => Ok(Self::Mainnet), DEVNET_ID => Ok(Self::Devnet), TESTNET_ID => Ok(Self::Testnet), - _ => Err(SymphonyChainError::UnrecognizedChainId) + _ => Err(SymphonyChainError::UnrecognizedChainId), } } } @@ -40,7 +41,6 @@ impl From for u64 { } } - impl TryFrom<&str> for SymphonyChains { type Error = SymphonyChainError; fn try_from(value: &str) -> Result { @@ -48,7 +48,7 @@ impl TryFrom<&str> for SymphonyChains { MAINNET_NAME => Ok(Self::Mainnet), DEVNET_NAME => Ok(Self::Devnet), TESTNET_NAME => Ok(Self::Testnet), - _ => Err(SymphonyChainError::UnrecognizedStr) + _ => Err(SymphonyChainError::UnrecognizedStr), } } } @@ -58,11 +58,11 @@ impl Display for SymphonyChains { let chain_name = match self { SymphonyChains::Mainnet => MAINNET_NAME, SymphonyChains::Devnet => DEVNET_NAME, - SymphonyChains::Testnet => TESTNET_NAME + SymphonyChains::Testnet => TESTNET_NAME, }; let chain_name = chain_name.to_lowercase(); - write!(f, "symphony-{chain_name}") + write!(f, "symphony-{chain_name}") } -} \ No newline at end of file +} diff --git a/crates/symphony-primitives/src/constants.rs b/crates/symphony-primitives/src/constants.rs index d6e92b7e824c..7bc030c1c0b6 100644 --- a/crates/symphony-primitives/src/constants.rs +++ b/crates/symphony-primitives/src/constants.rs @@ -1,8 +1,7 @@ - pub const MAINNET_NAME: &str = "MAINNET"; pub const DEVNET_NAME: &str = "DEVNET"; pub const TESTNET_NAME: &str = "TESTNET"; pub const MAINNET_ID: u64 = 70047; pub const DEVNET_ID: u64 = 70048; -pub const TESTNET_ID: u64 = 70049; \ No newline at end of file +pub const TESTNET_ID: u64 = 70049; diff --git a/crates/symphony-primitives/src/lib.rs b/crates/symphony-primitives/src/lib.rs index 97e96848011c..4c7001e6091c 100644 --- a/crates/symphony-primitives/src/lib.rs +++ b/crates/symphony-primitives/src/lib.rs @@ -1,4 +1,4 @@ -pub mod constants; pub mod chains; +pub mod constants; -pub use chains::{SymphonyChains, SymphonyChainError}; \ No newline at end of file +pub use chains::{SymphonyChainError, SymphonyChains}; From 11944a84eed4d3420f4173dd413137f132a25000 Mon Sep 17 00:00:00 2001 From: Derek Sione <95190961+dereksione@users.noreply.github.com> Date: Mon, 3 Jul 2023 09:57:10 -0400 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Richard Janis Goldschmidt --- Cargo.toml | 2 +- bin/reth/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1be46fbaa22d..2fcf531789f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,7 +108,7 @@ thiserror = "1.0.37" serde_json = "1.0.94" serde = { version = "1.0", default-features = false } rand = "0.8.5" -parse-display = { version = "0.8.1"} +parse-display = "0.8.1" ## tokio diff --git a/bin/reth/Cargo.toml b/bin/reth/Cargo.toml index 5cfc3ee038cf..18c65d607580 100644 --- a/bin/reth/Cargo.toml +++ b/bin/reth/Cargo.toml @@ -86,7 +86,7 @@ hex = "0.4" thiserror = { workspace = true } pretty_assertions = "1.3.0" humantime = "2.1.0" -parse-display.workspace = true +parse-display = { workspace = true } [features] jemalloc = ["dep:jemallocator", "dep:jemalloc-ctl"]