|
10 | 10 | //! Simply update the included `builders.json` file with the new builders. |
11 | 11 |
|
12 | 12 | 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 | + }, |
15 | 18 | }; |
16 | 19 |
|
17 | 20 | /// The builder list env var. |
@@ -101,6 +104,11 @@ impl Builders { |
101 | 104 | self.config.calc() |
102 | 105 | } |
103 | 106 |
|
| 107 | + /// Get the slot authorization configuration. |
| 108 | + pub const fn config(&self) -> &SlotAuthzConfig { |
| 109 | + &self.config |
| 110 | + } |
| 111 | + |
104 | 112 | /// Get the builder at a specific index. |
105 | 113 | /// |
106 | 114 | /// # Panics |
@@ -132,25 +140,24 @@ impl Builders { |
132 | 140 | self.builder_at(self.index_now() as usize) |
133 | 141 | } |
134 | 142 |
|
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> { |
143 | 145 | 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. |
146 | 146 | if current_slot_time < self.config.block_query_start() { |
147 | | - tracing::debug!("Action attempt too early"); |
148 | 147 | return Err(BuilderPermissionError::ActionAttemptTooEarly); |
149 | 148 | } |
150 | 149 | if current_slot_time > self.config.block_query_cutoff() { |
151 | | - tracing::debug!("Action attempt too late"); |
152 | 150 | return Err(BuilderPermissionError::ActionAttemptTooLate); |
153 | 151 | } |
| 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()?; |
154 | 161 |
|
155 | 162 | if sub != self.current_builder().sub { |
156 | 163 | tracing::debug!( |
@@ -183,16 +190,16 @@ impl FromEnv for Builders { |
183 | 190 | mod test { |
184 | 191 |
|
185 | 192 | use super::*; |
186 | | - use crate::perms; |
| 193 | + use crate::{perms, utils::calc}; |
187 | 194 |
|
188 | 195 | #[test] |
189 | 196 | fn load_builders() { |
190 | 197 | unsafe { |
191 | 198 | std::env::set_var(BUILDERS, "0,1,2,3,4,5"); |
192 | 199 |
|
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"); |
196 | 203 |
|
197 | 204 | std::env::set_var(perms::config::BLOCK_QUERY_START, "1"); |
198 | 205 | std::env::set_var(perms::config::BLOCK_QUERY_CUTOFF, "11"); |
|
0 commit comments