Skip to content

Commit 9f8e5c0

Browse files
authored
chore: finish the node components migration (#38)
1 parent 4e9485a commit 9f8e5c0

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

+5759
-2
lines changed

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["crates/*"]
33
resolver = "2"
44

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

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
tracing.workspace = true
29+
trevm.workspace = true
30+
signet-genesis.workspace = true
31+
32+
[features]
33+
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)