use super::twin_statecell;
pub const TWIN_STATECELL_V1: &str = "statecell-v1";
#[derive(Debug, Clone, Copy)]
pub struct TwinEntry {
pub id: &'static str,
#[allow(dead_code)]
pub summary: &'static str,
pub detect: fn(&str) -> bool,
}
pub static REGISTRY: &[TwinEntry] = &[TwinEntry {
id: TWIN_STATECELL_V1,
summary: "StateCell-shaped: initialize/increment, not-guard, dropped get.or_use re-read",
detect: twin_statecell::looks_like_statecell_instructions,
}];
pub fn detect_twin_id(instructions: &str) -> Option<&'static str> {
REGISTRY
.iter()
.find(|e| (e.detect)(instructions))
.map(|e| e.id)
}
pub fn registered_ids() -> Vec<&'static str> {
REGISTRY.iter().map(|e| e.id).collect()
}
pub fn require_registered(instructions: &str) -> Result<&'static str, String> {
detect_twin_id(instructions).ok_or_else(|| {
format!(
"no registered Aleo packaging twin matches this program \
(registered: {}); refuse silent approximate twins — see D10",
registered_ids().join(", ")
)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn statecell_template_detects() {
let sample = r#"
mapping pf_state_0: as u8 => u64;
mapping initialized: as u8 => bool;
function initialize:
function increment:
not r1 into r2
get.or_use pf_state_0[0u8] 0u64 into r3
"#;
assert_eq!(detect_twin_id(sample), Some(TWIN_STATECELL_V1));
}
#[test]
fn unknown_fails_closed() {
assert!(require_registered("program foo.aleo;").is_err());
}
}