Skip to content

Commit ad711bc

Browse files
committed
chore: finish the node components migration
1 parent 4e9485a commit ad711bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+5709
-1
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,21 @@ incremental = false
3737
signet-blobber = { version = "0.11", path = "crates/blobber" }
3838
signet-block-processor = { version = "0.11", path = "crates/block-processor" }
3939
signet-db = { version = "0.11", path = "crates/db" }
40+
signet-genesis = { version = "0.11", path = "crates/genesis" }
41+
signet-node = { version = "0.11", path = "crates/node" }
42+
signet-node-config = { version = "0.11", path = "crates/node-config" }
43+
signet-node-tests = { version = "0.11", path = "crates/node-tests" }
4044
signet-node-types = { version = "0.11", path = "crates/node-types" }
4145
signet-rpc = { version = "0.11", path = "crates/rpc" }
4246

47+
4348
init4-bin-base = { version = "0.13.1", features = ["alloy"] }
4449

4550
signet-bundle = "0.11.1"
4651
signet-constants = "0.11.1"
4752
signet-evm = "0.11.1"
4853
signet-extract = "0.11.1"
54+
signet-test-utils = "0.11.1"
4955
signet-tx-cache = "0.11.1"
5056
signet-types = "0.11.1"
5157
signet-zenith = "0.11.1"
@@ -124,6 +130,7 @@ tempfile = "3.17.0"
124130
# signet-constants = { path = "../sdk/crates/constants"}
125131
# signet-evm = { path = "../sdk/crates/evm"}
126132
# signet-extract = { path = "../sdk/crates/extract"}
133+
# signet-test-utils = { path = "../sdk/crates/test-utils"}
127134
# signet-tx-cache = { path = "../sdk/crates/tx-cache"}
128135
# signet-types = { path = "../sdk/crates/types"}
129136
# signet-zenith = { path = "../sdk/crates/zenith"}

crates/genesis/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "signet-genesis"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
authors.workspace = true
7+
license.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
11+
[dependencies]
12+
alloy = { workspace = true, features = ["genesis"] }
13+
init4-bin-base.workspace = true
14+
serde = { workspace = true, features = ["derive"] }
15+
serde_json.workspace = true
16+
signet-constants.workspace = true
17+
thiserror.workspace = true

crates/genesis/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Signet Genesis
2+
3+
Genesis configuration and utilities for the Signet Node.
4+
5+
This library contains the following:
6+
7+
- `GenesisSpec` - An enum representing different genesis specifications, either
8+
Pecorino, Test, or a custom genesis file path, which can be used to load
9+
genesis data.
10+
- `PECORINO_GENESIS` - The Pecorino genesis data.
11+
- `TEST_GENESIS` - A local test genesis for testing purposes.
12+
- `GenesisError` - Errors that can occur when loading or parsing genesis data.
13+
14+
## Example
15+
16+
```
17+
# use signet_genesis::GenesisSpec;
18+
# fn _main() -> Result<(), Box<dyn std::error::Error>> {
19+
let genesis = GenesisSpec::Pecorino.load_genesis()?;
20+
# Ok(())
21+
# }
22+
```

crates/genesis/src/lib.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#[doc = include_str!("../README.md")]
2+
use alloy::genesis::Genesis;
3+
use init4_bin_base::utils::from_env::{
4+
EnvItemInfo, FromEnv, FromEnvErr, FromEnvVar, parse_env_if_present,
5+
};
6+
use signet_constants::KnownChains;
7+
use std::{borrow::Cow, path::PathBuf, str::FromStr, sync::LazyLock};
8+
9+
/// Pecorino genesis file.
10+
pub const PECORINO_GENESIS_JSON: &str = include_str!("./pecorino.genesis.json");
11+
12+
/// Local genesis file for testing purposes.
13+
pub const TEST_GENESIS_JSON: &str = include_str!("./local.genesis.json");
14+
15+
/// Genesis for the Pecorino testnet.
16+
pub static PECORINO_GENESIS: LazyLock<Genesis> = LazyLock::new(|| {
17+
serde_json::from_str(PECORINO_GENESIS_JSON).expect("Failed to parse pecorino genesis")
18+
});
19+
20+
/// Test genesis for local testing.
21+
pub static TEST_GENESIS: LazyLock<Genesis> = LazyLock::new(|| {
22+
serde_json::from_str(TEST_GENESIS_JSON).expect("Failed to parse test genesis")
23+
});
24+
25+
/// Environment variable for specifying the genesis JSON file path.
26+
const GENSIS_JSON_PATH: &str = "GENSIS_JSON_PATH";
27+
28+
/// Result type for genesis operations.
29+
pub type Result<T, E = GenesisError> = std::result::Result<T, E>;
30+
31+
#[derive(Debug, thiserror::Error)]
32+
pub enum GenesisError {
33+
#[error(transparent)]
34+
Io(#[from] std::io::Error),
35+
#[error(transparent)]
36+
Json(#[from] serde_json::Error),
37+
}
38+
39+
/// Different genesis configurations available.
40+
#[derive(Debug, Clone, serde::Deserialize)]
41+
#[serde(untagged)]
42+
pub enum GenesisSpec {
43+
Pecorino,
44+
Test,
45+
Path(PathBuf),
46+
}
47+
48+
impl GenesisSpec {
49+
/// Load the genesis JSON from the specified source.
50+
///
51+
/// This will alwys return a valid string for [`KnownChains`].
52+
pub fn load_raw_genesis(&self) -> Result<Cow<'static, str>> {
53+
match self {
54+
GenesisSpec::Pecorino => Ok(Cow::Borrowed(PECORINO_GENESIS_JSON)),
55+
GenesisSpec::Test => Ok(Cow::Borrowed(TEST_GENESIS_JSON)),
56+
GenesisSpec::Path(path) => {
57+
std::fs::read_to_string(path).map(Cow::Owned).map_err(Into::into)
58+
}
59+
}
60+
}
61+
62+
/// Load the genesis from the specified source.
63+
///
64+
/// This will always return a valid genesis for [`KnownChains`].
65+
pub fn load_genesis(&self) -> Result<alloy::genesis::Genesis> {
66+
match self {
67+
GenesisSpec::Pecorino => Ok(PECORINO_GENESIS.clone()),
68+
GenesisSpec::Test => Ok(TEST_GENESIS.clone()),
69+
GenesisSpec::Path(_) => self
70+
.load_raw_genesis()
71+
.and_then(|raw| serde_json::from_str(&raw).map_err(Into::into)),
72+
}
73+
}
74+
}
75+
76+
impl FromStr for GenesisSpec {
77+
type Err = <PathBuf as FromStr>::Err;
78+
79+
fn from_str(s: &str) -> Result<Self, Self::Err> {
80+
if let Ok(known) = KnownChains::from_str(s) {
81+
return Ok(known.into());
82+
}
83+
84+
Ok(GenesisSpec::Path(s.parse()?))
85+
}
86+
}
87+
88+
impl FromEnvVar for GenesisSpec {
89+
type Error = <GenesisSpec as FromStr>::Err;
90+
91+
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
92+
parse_env_if_present(env_var)
93+
}
94+
}
95+
96+
impl FromEnv for GenesisSpec {
97+
type Error = <GenesisSpec as FromStr>::Err;
98+
99+
fn inventory() -> Vec<&'static init4_bin_base::utils::from_env::EnvItemInfo> {
100+
vec![
101+
&EnvItemInfo {
102+
var: "CHAIN_NAME",
103+
description: "The name of the chain. If set, the other environment variables are ignored.",
104+
optional: true,
105+
},
106+
&EnvItemInfo {
107+
var: GENSIS_JSON_PATH,
108+
description: "A filepath to the genesis JSON file. Required if CHAIN_NAME is not set.",
109+
optional: true,
110+
},
111+
]
112+
}
113+
114+
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
115+
parse_env_if_present::<KnownChains>("CHAIN_NAME")
116+
.map(Into::into)
117+
.or_else(|_| parse_env_if_present::<PathBuf>(GENSIS_JSON_PATH).map(Into::into))
118+
}
119+
}
120+
121+
impl From<KnownChains> for GenesisSpec {
122+
fn from(known: KnownChains) -> Self {
123+
match known {
124+
KnownChains::Pecorino => GenesisSpec::Pecorino,
125+
KnownChains::Test => GenesisSpec::Test,
126+
}
127+
}
128+
}
129+
130+
impl From<PathBuf> for GenesisSpec {
131+
fn from(path: PathBuf) -> Self {
132+
GenesisSpec::Path(path)
133+
}
134+
}

crates/genesis/src/local.genesis.json

Lines changed: 186 additions & 0 deletions
Large diffs are not rendered by default.

crates/genesis/src/pecorino.genesis.json

Lines changed: 143 additions & 0 deletions
Large diffs are not rendered by default.

crates/node-config/Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "signet-node-config"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
authors.workspace = true
7+
license.workspace = true
8+
homepage.workspace = true
9+
repository.workspace = true
10+
11+
[dependencies]
12+
signet-blobber.workspace = true
13+
signet-types.workspace = true
14+
15+
init4-bin-base.workspace = true
16+
17+
reth.workspace = true
18+
reth-chainspec.workspace = true
19+
reth-exex.workspace = true
20+
reth-node-api.workspace = true
21+
reth-db = { workspace = true, optional = true}
22+
23+
alloy.workspace = true
24+
25+
eyre.workspace = true
26+
reqwest.workspace = true
27+
serde.workspace = true
28+
serde_json.workspace = true
29+
tracing.workspace = true
30+
trevm.workspace = true
31+
signet-genesis.workspace = true
32+
33+
[features]
34+
test_utils = ["dep:reth-db", "reth-db/test-utils"]

crates/node-config/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Signet Node Config
2+
3+
Configuration objects for the Signet Node
4+
5+
This library contains the following:
6+
7+
- `SignetNodeConfig` - The main configuration object for a Signet Node. This
8+
struct can be loaded from the environment, or deserialized from a JSON file.

0 commit comments

Comments
 (0)