Skip to content

Commit 0676b9b

Browse files
committed
feat(calc): manual FromEnv impl
Implements `FromEnv` manuall for the calculator. It now has the following behavior: - First, the `CHAIN_NAME` environment variable is checked. If it's present, instantiation from the chain name is attempted. - If the `CHAIN_NAME` environment variable is not present, the individual environment variables used to configure the calculator are looked up, and the calculator is instantiated. Any of these missing results in a hard error.
1 parent 093d4bb commit 0676b9b

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

src/utils/calc.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::utils::from_env::FromEnv;
1+
use crate::utils::from_env::{EnvItemInfo, FromEnv, FromEnvErr, FromEnvVar};
22
use signet_constants::KnownChains;
3-
use std::str::FromStr;
3+
use std::{num::ParseIntError, str::FromStr};
44

55
/// A slot calculator, which can calculate slot numbers, windows, and offsets
66
/// for a given chain.
@@ -51,16 +51,11 @@ use std::str::FromStr;
5151
/// The `+ 1` is added because the first slot is the slot at `slot_offset`,
5252
/// which ENDS at `start_timestamp`. I.e. a timestamp at `start_timestamp` is
5353
/// in slot `slot_offset + 1`.
54-
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, FromEnv)]
55-
#[from_env(crate)]
54+
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize)]
5655
pub struct SlotCalculator {
5756
/// The start timestamp. This is the timestamp of the header to start the
5857
/// PoS chain. That header occupies a specific slot (the `slot_offset`). The
5958
/// `start_timestamp` is the END of that slot.
60-
#[from_env(
61-
var = "START_TIMESTAMP",
62-
desc = "The start timestamp of the chain in seconds"
63-
)]
6459
start_timestamp: u64,
6560

6661
/// This is the number of the slot containing the block which contains the
@@ -69,17 +64,9 @@ pub struct SlotCalculator {
6964
/// This is needed for chains that contain a merge (like Ethereum Mainnet),
7065
/// or for chains with missed slots at the start of the chain (like
7166
/// Holesky).
72-
#[from_env(
73-
var = "SLOT_OFFSET",
74-
desc = "The number of the slot containing the start timestamp"
75-
)]
7667
slot_offset: usize,
7768

7869
/// The slot duration (in seconds).
79-
#[from_env(
80-
var = "SLOT_DURATION",
81-
desc = "The slot duration of the chain in seconds"
82-
)]
8370
slot_duration: u64,
8471
}
8572

@@ -275,6 +262,47 @@ impl SlotCalculator {
275262
}
276263
}
277264

265+
impl FromEnv for SlotCalculator {
266+
type Error = FromEnvErr<ParseIntError>;
267+
268+
fn inventory() -> Vec<&'static EnvItemInfo> {
269+
vec![
270+
&EnvItemInfo {
271+
var: "START_TIMESTAMP",
272+
description: "The start timestamp of the chain in seconds",
273+
optional: false,
274+
},
275+
&EnvItemInfo {
276+
var: "SLOT_OFFSET",
277+
description: "The number of the slot containing the start timestamp",
278+
optional: false,
279+
},
280+
&EnvItemInfo {
281+
var: "SLOT_DURATION",
282+
description: "The slot duration of the chain in seconds",
283+
optional: false,
284+
},
285+
]
286+
}
287+
288+
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
289+
if let Ok(slot_calculator) = SlotCalculator::from_env_var("CHAIN_NAME") {
290+
return Ok(slot_calculator);
291+
}
292+
293+
// Else, look for the individual chain constants
294+
let start_timestamp = FromEnvVar::from_env_var("START_TIMESTAMP")?;
295+
let slot_offset = FromEnvVar::from_env_var("SLOT_OFFSET")?;
296+
let slot_duration = FromEnvVar::from_env_var("SLOT_DURATION")?;
297+
298+
Ok(Self {
299+
start_timestamp,
300+
slot_offset,
301+
slot_duration,
302+
})
303+
}
304+
}
305+
278306
impl From<KnownChains> for SlotCalculator {
279307
fn from(value: KnownChains) -> Self {
280308
match value {

0 commit comments

Comments
 (0)