use std::collections::BTreeMap;
use std::sync::{Mutex, MutexGuard};
use crate::v2::challenge_issuance::V2ChallengeIssuanceStore;
use crate::v2::claim_store::V2StoreError;
use crate::v2::context::V2Context;
use crate::v2::operation_context::challenge_claim_digest_v2;
use crate::v2::preflight::ExpectedChallengeV2;
pub const ERR_PERSIST_FAILED: &str = "v2reference: scripted issuance persistence failure";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PersistFault {
#[default]
Ok,
FailBeforeCommit,
FailAfterCommit,
}
struct State {
records: BTreeMap<String, ExpectedChallengeV2>,
fault: PersistFault,
}
pub struct TestOnlyChallengeIssuanceStore {
state: Mutex<State>,
}
impl std::fmt::Debug for TestOnlyChallengeIssuanceStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TestOnlyChallengeIssuanceStore")
.field("records", &self.len())
.finish()
}
}
impl Default for TestOnlyChallengeIssuanceStore {
fn default() -> Self {
Self::new()
}
}
pub(crate) fn issuance_key(namespace: &str, epoch: i64, digest: &[u8]) -> String {
format!("{}:{}|{}|{}", namespace.len(), namespace, epoch, hex::encode(digest))
}
impl TestOnlyChallengeIssuanceStore {
pub fn new() -> Self {
TestOnlyChallengeIssuanceStore {
state: Mutex::new(State {
records: BTreeMap::new(),
fault: PersistFault::Ok,
}),
}
}
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub fn fail_next_persist(&self, fault: PersistFault) {
self.lock().fault = fault;
}
pub fn len(&self) -> usize {
self.lock().records.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn records(&self) -> BTreeMap<String, ExpectedChallengeV2> {
self.lock().records.clone()
}
pub(crate) fn set_records(&self, records: BTreeMap<String, ExpectedChallengeV2>) {
self.lock().records = records;
}
}
impl V2ChallengeIssuanceStore for TestOnlyChallengeIssuanceStore {
fn persist_v2_challenge(&self, _ctx: &V2Context, record: ExpectedChallengeV2) -> Result<(), V2StoreError> {
let mut s = self.lock();
let fault = std::mem::take(&mut s.fault);
if fault == PersistFault::FailBeforeCommit {
return Err(V2StoreError::Unavailable(ERR_PERSIST_FAILED.into()));
}
let digest = challenge_claim_digest_v2(&record.value)?;
let key = issuance_key(&record.verifier_namespace, record.replay_epoch, &digest);
if s.records.contains_key(&key) {
return Err(V2StoreError::ChallengeIssuanceConflict);
}
s.records.insert(key, record);
if fault == PersistFault::FailAfterCommit {
return Err(V2StoreError::Unavailable(ERR_PERSIST_FAILED.into()));
}
Ok(())
}
fn load_v2_challenge(
&self,
_ctx: &V2Context,
namespace: &str,
replay_epoch: i64,
claim_digest: &[u8],
) -> Result<ExpectedChallengeV2, V2StoreError> {
self.lock()
.records
.get(&issuance_key(namespace, replay_epoch, claim_digest))
.cloned()
.ok_or(V2StoreError::ChallengeIssuanceNotFound)
}
}