use super::SessionRecord;
use crate::account::Account;
use crate::account::session_key::{self, RENEW_BEFORE_MS, Status};
pub const INDEXER_GRACE_MS: u64 = 120_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyState {
Missing,
Unregistered,
Ready,
}
#[must_use]
pub fn key_state(record: Option<&SessionRecord>, account: Option<&Account>, now_ms: u64) -> KeyState {
let Some(record) = record else {
return KeyState::Missing;
};
if record.held_until_ms <= now_ms.saturating_add(RENEW_BEFORE_MS) {
return KeyState::Missing;
}
let Some(account) = account else {
return if record.registered_at_ms.is_some() { KeyState::Ready } else { KeyState::Unregistered };
};
let Ok(address) = record.address() else {
return KeyState::Missing;
};
match session_key::status(
address,
record.held_until_ms,
&account.session_keys,
account.indexed_through_timestamp,
now_ms,
) {
Status::Ready { .. } => KeyState::Ready,
Status::Unregistered
if record.registered_at_ms.is_some_and(|at| {
account.indexed_through_timestamp.saturating_mul(1000) < at.saturating_add(INDEXER_GRACE_MS)
}) =>
{
KeyState::Ready
}
Status::Unregistered | Status::Expiring { .. } => KeyState::Unregistered,
}
}
#[cfg(test)]
#[path = "tests/key_state.rs"]
mod tests;