use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "persistent", derive(serde::Serialize, serde::Deserialize))]
pub struct VerificationCosts {
pub ed25519: u32,
pub certified: u32,
}
#[cfg(all(
test,
any(
feature = "portable-signature-verification",
feature = "certified-proof-verification"
)
))]
#[path = "admission_budget/tests.rs"]
mod tests;
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
pub enum AdmissionBudgetError {
#[error("invalid admission budget configuration")]
InvalidPolicy,
#[error("fresh verification budget is exhausted")]
Exhausted,
#[error("admission clock moved backwards")]
ClockRollback,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "persistent", derive(serde::Serialize, serde::Deserialize))]
pub struct SessionAdmissionBudget {
burst: u32,
available: u32,
refill_interval_ns: u64,
refill_at_ns: u64,
observed_at_ns: u64,
costs: Option<VerificationCosts>,
}
impl SessionAdmissionBudget {
#[cfg(feature = "persistent")]
pub(crate) fn stored_policy_valid(&self, burst: u32, interval: u64, observed: u64) -> bool {
self.burst == burst
&& self.refill_interval_ns == interval
&& burst > 0
&& interval > 0
&& self.available <= burst
&& self.refill_at_ns <= self.observed_at_ns
&& self.observed_at_ns <= observed
&& self.costs_valid()
}
fn costs_valid(&self) -> bool {
self.costs.is_none_or(|costs| {
costs.ed25519 > 0
&& costs.ed25519 <= self.burst
&& costs.certified > 0
&& costs.certified <= self.burst
})
}
pub fn for_verification(
burst: u32,
refill_interval_ns: u64,
costs: VerificationCosts,
now_ns: u64,
) -> Result<Self, AdmissionBudgetError> {
let mut budget = Self::for_operations(burst, refill_interval_ns, now_ns)?;
budget.costs = Some(costs);
if !budget.costs_valid() {
return Err(AdmissionBudgetError::InvalidPolicy);
}
Ok(budget)
}
#[cfg(feature = "persistent")]
pub(crate) fn has_costs(&self, expected: VerificationCosts) -> bool {
self.costs == Some(expected)
}
#[cfg(feature = "persistent")]
pub(crate) fn is_operations(&self) -> bool {
self.costs.is_none()
}
#[cfg(feature = "persistent")]
pub(crate) fn for_issuance(
burst: u32,
interval: u64,
now: u64,
) -> Result<Self, AdmissionBudgetError> {
let mut budget = Self::for_operations(burst, interval, now)?;
budget.available = burst;
Ok(budget)
}
#[cfg(any(
feature = "portable-signature-verification",
feature = "certified-proof-verification"
))]
pub(crate) fn consume_verification(
&mut self,
now_ns: u64,
mechanism: ic_auth_protocol_types::PortableMechanism,
) -> Result<(), AdmissionBudgetError> {
let costs = self.costs.ok_or(AdmissionBudgetError::InvalidPolicy)?;
let cost = match mechanism {
ic_auth_protocol_types::PortableMechanism::Ed25519 => costs.ed25519,
ic_auth_protocol_types::PortableMechanism::Certified => costs.certified,
};
self.consume_units(now_ns, cost)
}
pub fn for_operations(
burst: u32,
refill_interval_ns: u64,
now_ns: u64,
) -> Result<Self, AdmissionBudgetError> {
if burst == 0 || refill_interval_ns == 0 {
return Err(AdmissionBudgetError::InvalidPolicy);
}
Ok(Self {
burst,
available: 0,
refill_interval_ns,
refill_at_ns: now_ns,
observed_at_ns: now_ns,
costs: None,
})
}
pub(crate) fn consume(&mut self, now_ns: u64) -> Result<(), AdmissionBudgetError> {
self.consume_units(now_ns, 1)
}
fn consume_units(&mut self, now_ns: u64, cost: u32) -> Result<(), AdmissionBudgetError> {
if now_ns < self.observed_at_ns {
return Err(AdmissionBudgetError::ClockRollback);
}
self.observed_at_ns = now_ns;
let elapsed = now_ns - self.refill_at_ns;
let refill = elapsed / self.refill_interval_ns;
self.available = u64::from(self.available)
.saturating_add(refill)
.min(u64::from(self.burst)) as u32;
self.refill_at_ns = now_ns - elapsed % self.refill_interval_ns;
if self.available < cost {
return Err(AdmissionBudgetError::Exhausted);
}
self.available -= cost;
Ok(())
}
}