1
- use crate :: utils:: from_env:: FromEnv ;
1
+ use crate :: utils:: from_env:: { EnvItemInfo , FromEnv , FromEnvErr , FromEnvVar } ;
2
2
use signet_constants:: KnownChains ;
3
- use std:: str:: FromStr ;
3
+ use std:: { num :: ParseIntError , str:: FromStr } ;
4
4
5
5
/// A slot calculator, which can calculate slot numbers, windows, and offsets
6
6
/// for a given chain.
@@ -51,16 +51,11 @@ use std::str::FromStr;
51
51
/// The `+ 1` is added because the first slot is the slot at `slot_offset`,
52
52
/// which ENDS at `start_timestamp`. I.e. a timestamp at `start_timestamp` is
53
53
/// 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 ) ]
56
55
pub struct SlotCalculator {
57
56
/// The start timestamp. This is the timestamp of the header to start the
58
57
/// PoS chain. That header occupies a specific slot (the `slot_offset`). The
59
58
/// `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
- ) ]
64
59
start_timestamp : u64 ,
65
60
66
61
/// This is the number of the slot containing the block which contains the
@@ -69,17 +64,9 @@ pub struct SlotCalculator {
69
64
/// This is needed for chains that contain a merge (like Ethereum Mainnet),
70
65
/// or for chains with missed slots at the start of the chain (like
71
66
/// Holesky).
72
- #[ from_env(
73
- var = "SLOT_OFFSET" ,
74
- desc = "The number of the slot containing the start timestamp"
75
- ) ]
76
67
slot_offset : usize ,
77
68
78
69
/// The slot duration (in seconds).
79
- #[ from_env(
80
- var = "SLOT_DURATION" ,
81
- desc = "The slot duration of the chain in seconds"
82
- ) ]
83
70
slot_duration : u64 ,
84
71
}
85
72
@@ -275,6 +262,47 @@ impl SlotCalculator {
275
262
}
276
263
}
277
264
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
+
278
306
impl From < KnownChains > for SlotCalculator {
279
307
fn from ( value : KnownChains ) -> Self {
280
308
match value {
0 commit comments