use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct GateConfig {
pub posting_threshold: i64,
pub rate_limit: u32,
pub rate_window_ms: u64,
}
impl Default for GateConfig {
fn default() -> Self {
Self {
posting_threshold: 1,
rate_limit: 20,
rate_window_ms: 60_000, }
}
}
#[derive(Clone, Debug, Default)]
pub struct TesseraFacts {
pub score: i64,
pub recent_action_ms: Vec<u64>,
pub vouched: bool,
pub is_member: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DenyReason {
NoCapability,
BelowThreshold,
NotAMember,
RateLimited,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GateDecision {
Allow,
Deny(DenyReason),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Policy {
OpenWithFloor(GateConfig),
VouchedOrScore(GateConfig),
MembersOnly {
rate_limit: u32,
rate_window_ms: u64,
},
}
pub fn authorize(
policy: &Policy,
cap_covers: bool,
facts: &TesseraFacts,
now_ms: u64,
) -> GateDecision {
if !cap_covers {
return GateDecision::Deny(DenyReason::NoCapability);
}
let (rate_limit, rate_window_ms) = match policy {
Policy::OpenWithFloor(config) => {
if facts.score < config.posting_threshold {
return GateDecision::Deny(DenyReason::BelowThreshold);
}
(config.rate_limit, config.rate_window_ms)
}
Policy::VouchedOrScore(config) => {
if facts.score < config.posting_threshold && !facts.vouched {
return GateDecision::Deny(DenyReason::BelowThreshold);
}
(config.rate_limit, config.rate_window_ms)
}
Policy::MembersOnly {
rate_limit,
rate_window_ms,
} => {
if !facts.is_member {
return GateDecision::Deny(DenyReason::NotAMember);
}
(*rate_limit, *rate_window_ms)
}
};
let in_window = facts
.recent_action_ms
.iter()
.filter(|&&t| now_ms.saturating_sub(t) < rate_window_ms)
.count() as u32;
if in_window >= rate_limit {
return GateDecision::Deny(DenyReason::RateLimited);
}
GateDecision::Allow
}
pub fn may_act(
cap_covers: bool,
facts: &TesseraFacts,
now_ms: u64,
config: &GateConfig,
) -> GateDecision {
authorize(
&Policy::OpenWithFloor(config.clone()),
cap_covers,
facts,
now_ms,
)
}
#[cfg(test)]
mod tests {
use super::*;
fn facts(score: i64, recent_action_ms: Vec<u64>) -> TesseraFacts {
TesseraFacts {
score,
recent_action_ms,
..Default::default()
}
}
#[test]
fn allows_when_cap_and_facts_pass() {
assert_eq!(
may_act(
true,
&facts(50, vec![1, 2, 3]),
1_000,
&GateConfig::default()
),
GateDecision::Allow
);
}
#[test]
fn denies_without_a_capability_before_consulting_reputation() {
assert_eq!(
may_act(false, &facts(10_000, vec![]), 1_000, &GateConfig::default()),
GateDecision::Deny(DenyReason::NoCapability)
);
}
#[test]
fn a_fresh_chain_is_below_threshold() {
assert_eq!(
may_act(true, &facts(0, vec![]), 1_000, &GateConfig::default()),
GateDecision::Deny(DenyReason::BelowThreshold)
);
}
#[test]
fn rate_limit_throttles_a_burst() {
let config = GateConfig {
posting_threshold: 1,
rate_limit: 3,
rate_window_ms: 1_000,
};
assert_eq!(
may_act(true, &facts(100, vec![10, 20, 30]), 100, &config),
GateDecision::Deny(DenyReason::RateLimited)
);
}
#[test]
fn actions_outside_the_window_do_not_count() {
let config = GateConfig {
posting_threshold: 1,
rate_limit: 3,
rate_window_ms: 1_000,
};
assert_eq!(
may_act(true, &facts(100, vec![10, 20, 30]), 5_000, &config),
GateDecision::Allow
);
}
#[test]
fn vouched_or_score_lets_a_vouched_newcomer_in() {
let policy = Policy::VouchedOrScore(GateConfig::default());
let vouched = TesseraFacts {
score: 0,
vouched: true,
..Default::default()
};
let stranger = TesseraFacts {
score: 0,
vouched: false,
..Default::default()
};
assert_eq!(authorize(&policy, true, &vouched, 0), GateDecision::Allow);
assert_eq!(
authorize(&policy, true, &stranger, 0),
GateDecision::Deny(DenyReason::BelowThreshold)
);
}
#[test]
fn members_only_admits_only_members() {
let policy = Policy::MembersOnly {
rate_limit: 20,
rate_window_ms: 60_000,
};
let member = TesseraFacts {
score: 0,
is_member: true,
..Default::default()
};
let outsider = TesseraFacts {
score: 10_000,
is_member: false,
..Default::default()
};
assert_eq!(authorize(&policy, true, &member, 0), GateDecision::Allow);
assert_eq!(
authorize(&policy, true, &outsider, 0),
GateDecision::Deny(DenyReason::NotAMember)
);
}
}