|
| 1 | +//! #Signet Quincey builder permissioning system. |
| 2 | +//! |
| 3 | +//! The permissioning system decides which builder can perform a certain action at a given time. |
| 4 | +//! The permissioning system uses a simple round-robin design, where each builder is allowed to perform an action at a specific slot. |
| 5 | +//! Builders are permissioned based on their sub, which is present in the JWT token they acquire from our OAuth service. |
| 6 | +//! They are rotated every 12 seconds, which is Ethereum's slot time. |
| 7 | +//! As the logic is timestamp based, the system is deterministic. |
| 8 | +//! |
| 9 | +//! For updating the currently permissioned builders, |
| 10 | +//! Simply update the included `builders.json` file with the new builders. |
| 11 | +
|
| 12 | +use crate::utils::from_env::{FromEnvErr, FromEnvVar}; |
| 13 | + |
| 14 | +/// The start timestamp for the permissioned builders, in seconds. |
| 15 | +const EPOCH_START: u64 = 0; |
| 16 | + |
| 17 | +/// Ethereum's slot time in seconds. |
| 18 | +pub const ETHEREUM_SLOT_TIME: u64 = 12; |
| 19 | + |
| 20 | +fn now() -> u64 { |
| 21 | + chrono::Utc::now().timestamp().try_into().unwrap() |
| 22 | +} |
| 23 | + |
| 24 | +/// Possible errors when permissioning a builder. |
| 25 | +#[derive(Debug, thiserror::Error)] |
| 26 | +pub enum BuilderPermissionError { |
| 27 | + /// Action attempt too early. |
| 28 | + #[error("action attempt too early")] |
| 29 | + ActionAttemptTooEarly, |
| 30 | + |
| 31 | + /// Action attempt too late. |
| 32 | + #[error("action attempt too late")] |
| 33 | + ActionAttemptTooLate, |
| 34 | + |
| 35 | + /// Builder not permissioned for this slot. |
| 36 | + #[error("builder not permissioned for this slot")] |
| 37 | + NotPermissioned, |
| 38 | + |
| 39 | + /// Error loading the environment variable. |
| 40 | + #[error( |
| 41 | + "failed to parse environment variable. Expected a comma-seperated list of UUIDs. Got: {input}" |
| 42 | + )] |
| 43 | + ParseError { |
| 44 | + /// The environment variable name. |
| 45 | + env_var: String, |
| 46 | + /// The contents of the environment variable. |
| 47 | + input: String, |
| 48 | + }, |
| 49 | +} |
| 50 | + |
| 51 | +/// An individual builder. |
| 52 | +#[derive(Clone, Debug)] |
| 53 | +pub struct Builder { |
| 54 | + /// The sub of the builder. |
| 55 | + pub sub: String, |
| 56 | +} |
| 57 | + |
| 58 | +impl Builder { |
| 59 | + /// Create a new builder. |
| 60 | + pub fn new(sub: impl AsRef<str>) -> Self { |
| 61 | + Self { |
| 62 | + sub: sub.as_ref().to_owned(), |
| 63 | + } |
| 64 | + } |
| 65 | + /// Get the sub of the builder. |
| 66 | + pub fn sub(&self) -> &str { |
| 67 | + &self.sub |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Builders struct to keep track of the builders that are allowed to perform actions. |
| 72 | +#[derive(Clone, Debug)] |
| 73 | +pub struct Builders { |
| 74 | + /// The list of builders. |
| 75 | + pub builders: Vec<Builder>, |
| 76 | +} |
| 77 | + |
| 78 | +impl Builders { |
| 79 | + /// Create a new Builders struct. |
| 80 | + pub const fn new(builders: Vec<Builder>) -> Self { |
| 81 | + Self { builders } |
| 82 | + } |
| 83 | + |
| 84 | + /// Get the builder at a specific index. |
| 85 | + /// |
| 86 | + /// # Panics |
| 87 | + /// |
| 88 | + /// Panics if the index is out of bounds from the builders array. |
| 89 | + pub fn builder_at(&self, index: usize) -> &Builder { |
| 90 | + &self.builders[index] |
| 91 | + } |
| 92 | + |
| 93 | + /// Get the builder permissioned at a specific timestamp. |
| 94 | + pub fn builder_at_timestamp(&self, timestamp: u64) -> &Builder { |
| 95 | + self.builder_at(self.index(timestamp) as usize) |
| 96 | + } |
| 97 | + |
| 98 | + /// Get the index of the builder that is allowed to sign a block for a |
| 99 | + /// particular timestamp. |
| 100 | + pub fn index(&self, timestamp: u64) -> u64 { |
| 101 | + ((timestamp - EPOCH_START) / ETHEREUM_SLOT_TIME) % self.builders.len() as u64 |
| 102 | + } |
| 103 | + |
| 104 | + /// Get the index of the builder that is allowed to sign a block at the |
| 105 | + /// current timestamp. |
| 106 | + pub fn index_now(&self) -> u64 { |
| 107 | + self.index(now()) |
| 108 | + } |
| 109 | + |
| 110 | + /// Get the builder that is allowed to sign a block at the current timestamp. |
| 111 | + pub fn current_builder(&self) -> &Builder { |
| 112 | + self.builder_at(self.index_now() as usize) |
| 113 | + } |
| 114 | + |
| 115 | + /// Checks if a builder is allowed to perform an action. |
| 116 | + /// This is based on the current timestamp and the builder's sub. It's a |
| 117 | + /// round-robin design, where each builder is allowed to perform an action |
| 118 | + /// at a specific slot, and what builder is allowed changes with each slot. |
| 119 | + pub fn is_builder_permissioned( |
| 120 | + &self, |
| 121 | + config: &crate::perms::SlotAuthzConfig, |
| 122 | + sub: &str, |
| 123 | + ) -> Result<(), BuilderPermissionError> { |
| 124 | + // Get the current timestamp. |
| 125 | + let curr_timestamp = now(); |
| 126 | + |
| 127 | + // Calculate the current slot time, which is a number between 0 and 11. |
| 128 | + let current_slot_time = (curr_timestamp - config.chain_offset()) % ETHEREUM_SLOT_TIME; |
| 129 | + |
| 130 | + // Builders can only perform actions between the configured start and cutoff times, to prevent any timing games. |
| 131 | + if current_slot_time < config.block_query_start() { |
| 132 | + tracing::debug!("Action attempt too early"); |
| 133 | + return Err(BuilderPermissionError::ActionAttemptTooEarly); |
| 134 | + } |
| 135 | + if current_slot_time > config.block_query_cutoff() { |
| 136 | + tracing::debug!("Action attempt too late"); |
| 137 | + return Err(BuilderPermissionError::ActionAttemptTooLate); |
| 138 | + } |
| 139 | + |
| 140 | + if sub != self.current_builder().sub { |
| 141 | + tracing::debug!( |
| 142 | + builder = %sub, |
| 143 | + permissioned_builder = %self.current_builder().sub, |
| 144 | + "Builder not permissioned for this slot" |
| 145 | + ); |
| 146 | + return Err(BuilderPermissionError::NotPermissioned); |
| 147 | + } |
| 148 | + |
| 149 | + Ok(()) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl FromIterator<Builder> for Builders { |
| 154 | + fn from_iter<T: IntoIterator<Item = Builder>>(iter: T) -> Self { |
| 155 | + Self::new(iter.into_iter().collect()) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl FromEnvVar for Builders { |
| 160 | + type Error = BuilderPermissionError; |
| 161 | + |
| 162 | + fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> { |
| 163 | + let s = String::from_env_var(env_var) |
| 164 | + .map_err(FromEnvErr::infallible_into::<BuilderPermissionError>)?; |
| 165 | + |
| 166 | + Ok(s.split(',').map(Builder::new).collect()) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(test)] |
| 171 | +mod test { |
| 172 | + use super::*; |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn load_builders() { |
| 176 | + unsafe { std::env::set_var("TEST", "0,1,2,3,4,5") }; |
| 177 | + |
| 178 | + let builders = Builders::from_env_var("TEST").unwrap(); |
| 179 | + assert_eq!(builders.builder_at(0).sub, "0"); |
| 180 | + assert_eq!(builders.builder_at(1).sub, "1"); |
| 181 | + assert_eq!(builders.builder_at(2).sub, "2"); |
| 182 | + assert_eq!(builders.builder_at(3).sub, "3"); |
| 183 | + assert_eq!(builders.builder_at(4).sub, "4"); |
| 184 | + assert_eq!(builders.builder_at(5).sub, "5"); |
| 185 | + } |
| 186 | +} |
0 commit comments