use super::attestation_framework::INTENT_PREFIX_LENGTH;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum RecordKind {
Handover,
Snapshot,
WriteSet,
OrderedBlock,
EpochComplete,
}
impl RecordKind {
pub const fn digest_prefix(self) -> &'static [u8] {
match self {
RecordKind::Handover => b"RIALO_HANDOVER_RECORD_V1",
RecordKind::Snapshot => b"RIALO_SNAPSHOT_RECORD_V1",
RecordKind::WriteSet => b"RIALO_WRITE_SET_RECORD_V1",
RecordKind::OrderedBlock => b"RIALO_ORDERED_BLOCK_RECORD_V1",
RecordKind::EpochComplete => b"RIALO_EPOCH_COMPLETE_V1",
}
}
pub const fn intent_scope(self) -> u8 {
match self {
RecordKind::Handover => 11,
RecordKind::Snapshot => 10,
RecordKind::WriteSet => 12,
RecordKind::OrderedBlock => 15,
RecordKind::EpochComplete => 13,
}
}
pub const fn intent_prefix(self) -> [u8; INTENT_PREFIX_LENGTH] {
[self.intent_scope(), 0, 2]
}
}
#[cfg(test)]
impl RecordKind {
const ALL: &[RecordKind] = &[
RecordKind::Handover,
RecordKind::Snapshot,
RecordKind::WriteSet,
RecordKind::OrderedBlock,
RecordKind::EpochComplete,
];
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
#[test]
fn all_covers_every_variant() {
assert_eq!(
RecordKind::ALL.len(),
5,
"RecordKind::ALL must list every variant; update it when adding a new record type"
);
}
#[test]
fn digest_prefixes_are_unique() {
let prefixes: HashSet<&[u8]> = RecordKind::ALL.iter().map(|k| k.digest_prefix()).collect();
assert_eq!(
prefixes.len(),
RecordKind::ALL.len(),
"two record kinds share a digest domain prefix"
);
}
#[test]
fn intent_scopes_are_unique() {
let scopes: HashSet<u8> = RecordKind::ALL.iter().map(|k| k.intent_scope()).collect();
assert_eq!(
scopes.len(),
RecordKind::ALL.len(),
"two record kinds share an intent scope"
);
}
#[test]
fn intent_prefixes_match_shared_crypto() {
use shared_crypto::intent::IntentScope;
assert_eq!(
RecordKind::Handover.intent_scope(),
IntentScope::HandoverAttestation as u8,
);
assert_eq!(
RecordKind::Snapshot.intent_scope(),
IntentScope::SnapshotAttestation as u8,
);
assert_eq!(
RecordKind::WriteSet.intent_scope(),
IntentScope::WriteSetAttestation as u8,
);
assert_eq!(
RecordKind::OrderedBlock.intent_scope(),
IntentScope::OrderedBlockAttestation as u8,
);
assert_eq!(
RecordKind::EpochComplete.intent_scope(),
IntentScope::EpochComplete as u8,
);
}
}