Skip to content

Commit 2cc1188

Browse files
committed
refactor: improve structure, calc to utils
1 parent ba05f08 commit 2cc1188

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub mod utils {
3232

3333
/// Tracing utilities.
3434
pub mod tracing;
35+
36+
/// Slot calculator for determining the current slot and timepoint within a
37+
/// slot.
38+
pub mod calc;
3539
}
3640

3741
/// Re-exports of common dependencies.

src/perms/auth.rs

Whitespace-only changes.

src/perms/builders.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
//! Simply update the included `builders.json` file with the new builders.
1111
1212
use crate::{
13-
perms::{SlotAuthzConfig, SlotAuthzConfigError, SlotCalculator},
14-
utils::from_env::{FromEnv, FromEnvErr, FromEnvVar},
13+
perms::{SlotAuthzConfig, SlotAuthzConfigError},
14+
utils::{
15+
calc::SlotCalculator,
16+
from_env::{FromEnv, FromEnvErr, FromEnvVar},
17+
},
1518
};
1619

1720
/// The builder list env var.
@@ -101,6 +104,11 @@ impl Builders {
101104
self.config.calc()
102105
}
103106

107+
/// Get the slot authorization configuration.
108+
pub const fn config(&self) -> &SlotAuthzConfig {
109+
&self.config
110+
}
111+
104112
/// Get the builder at a specific index.
105113
///
106114
/// # Panics
@@ -132,25 +140,24 @@ impl Builders {
132140
self.builder_at(self.index_now() as usize)
133141
}
134142

135-
/// Checks if a builder is allowed to perform an action.
136-
/// This is based on the current timestamp and the builder's sub. It's a
137-
/// round-robin design, where each builder is allowed to perform an action
138-
/// at a specific slot, and what builder is allowed changes with each slot.
139-
pub fn is_builder_permissioned(&self, sub: &str) -> Result<(), BuilderPermissionError> {
140-
// Get the current timestamp.
141-
142-
// Calculate the current slot time, which is a number between 0 and 11.
143+
/// Check the query bounds for the current timestamp.
144+
fn check_query_bounds(&self) -> Result<(), BuilderPermissionError> {
143145
let current_slot_time = self.calc().current_timepoint_within_slot();
144-
145-
// Builders can only perform actions between the configured start and cutoff times, to prevent any timing games.
146146
if current_slot_time < self.config.block_query_start() {
147-
tracing::debug!("Action attempt too early");
148147
return Err(BuilderPermissionError::ActionAttemptTooEarly);
149148
}
150149
if current_slot_time > self.config.block_query_cutoff() {
151-
tracing::debug!("Action attempt too late");
152150
return Err(BuilderPermissionError::ActionAttemptTooLate);
153151
}
152+
Ok(())
153+
}
154+
155+
/// Checks if a builder is allowed to perform an action.
156+
/// This is based on the current timestamp and the builder's sub. It's a
157+
/// round-robin design, where each builder is allowed to perform an action
158+
/// at a specific slot, and what builder is allowed changes with each slot.
159+
pub fn is_builder_permissioned(&self, sub: &str) -> Result<(), BuilderPermissionError> {
160+
self.check_query_bounds()?;
154161

155162
if sub != self.current_builder().sub {
156163
tracing::debug!(
@@ -183,16 +190,16 @@ impl FromEnv for Builders {
183190
mod test {
184191

185192
use super::*;
186-
use crate::perms;
193+
use crate::{perms, utils::calc};
187194

188195
#[test]
189196
fn load_builders() {
190197
unsafe {
191198
std::env::set_var(BUILDERS, "0,1,2,3,4,5");
192199

193-
std::env::set_var(perms::calc::START_TIMESTAMP, "1");
194-
std::env::set_var(perms::calc::SLOT_OFFSET, "0");
195-
std::env::set_var(perms::calc::SLOT_DURATION, "12");
200+
std::env::set_var(calc::START_TIMESTAMP, "1");
201+
std::env::set_var(calc::SLOT_OFFSET, "0");
202+
std::env::set_var(calc::SLOT_DURATION, "12");
196203

197204
std::env::set_var(perms::config::BLOCK_QUERY_START, "1");
198205
std::env::set_var(perms::config::BLOCK_QUERY_CUTOFF, "11");

src/perms/config.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use crate::{
2-
perms::SlotCalcEnvError,
3-
utils::from_env::{FromEnv, FromEnvErr, FromEnvVar},
1+
use crate::utils::{
2+
calc::{SlotCalcEnvError, SlotCalculator},
3+
from_env::{FromEnv, FromEnvErr, FromEnvVar},
44
};
55
use core::num;
66

7-
use super::SlotCalculator;
8-
97
// Environment variable names for configuration
108
pub(crate) const BLOCK_QUERY_CUTOFF: &str = "BLOCK_QUERY_CUTOFF";
119
pub(crate) const BLOCK_QUERY_START: &str = "BLOCK_QUERY_START";

src/perms/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
pub(crate) mod builders;
22
pub use builders::{Builder, BuilderPermissionError, Builders, ETHEREUM_SLOT_TIME};
33

4-
pub(crate) mod calc;
5-
pub use calc::{SlotCalcEnvError, SlotCalculator};
6-
74
pub(crate) mod config;
85
pub use config::{SlotAuthzConfig, SlotAuthzConfigError};

src/perms/calc.rs renamed to src/utils/calc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct SlotCalculator {
2727
/// The start timestamp.
2828
start_timestamp: u64,
2929

30-
/// This is the number of the slot containing the block which contains the[]
30+
/// This is the number of the slot containing the block which contains the
3131
/// `start_timestamp`.
3232
///
3333
/// This is needed for chains that contain a merge (like Ethereum Mainnet),

0 commit comments

Comments
 (0)