1- use crate :: utils:: from_env:: FromEnv ;
1+ use crate :: utils:: from_env:: { EnvItemInfo , FromEnv , FromEnvErr , FromEnvVar } ;
22use 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 ) ]
5655pub 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,51 @@ 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: "CHAIN_NAME" ,
272+ description: "The name of the chain. If set, the other environment variables are ignored." ,
273+ optional: true ,
274+ } ,
275+ & EnvItemInfo {
276+ var: "START_TIMESTAMP" ,
277+ description: "The start timestamp of the chain in seconds. Required if CHAIN_NAME is not set." ,
278+ optional: true ,
279+ } ,
280+ & EnvItemInfo {
281+ var: "SLOT_OFFSET" ,
282+ description: "The number of the slot containing the start timestamp. Required if CHAIN_NAME is not set." ,
283+ optional: true ,
284+ } ,
285+ & EnvItemInfo {
286+ var: "SLOT_DURATION" ,
287+ description: "The slot duration of the chain in seconds. Required if CHAIN_NAME is not set." ,
288+ optional: true ,
289+ } ,
290+ ]
291+ }
292+
293+ fn from_env ( ) -> Result < Self , FromEnvErr < Self :: Error > > {
294+ if let Ok ( slot_calculator) = SlotCalculator :: from_env_var ( "CHAIN_NAME" ) {
295+ return Ok ( slot_calculator) ;
296+ }
297+
298+ let start_timestamp = FromEnvVar :: from_env_var ( "START_TIMESTAMP" ) ?;
299+ let slot_offset = FromEnvVar :: from_env_var ( "SLOT_OFFSET" ) ?;
300+ let slot_duration = FromEnvVar :: from_env_var ( "SLOT_DURATION" ) ?;
301+
302+ Ok ( Self {
303+ start_timestamp,
304+ slot_offset,
305+ slot_duration,
306+ } )
307+ }
308+ }
309+
278310impl From < KnownChains > for SlotCalculator {
279311 fn from ( value : KnownChains ) -> Self {
280312 match value {
0 commit comments