pub const FORMAT_VERSION: u32 = 1;
pub const CERT_SCHEMA_VERSION: u32 = 4;
pub const ARTIFACT_CERTIFICATE_ROOT: &str = "AverCert.Artifact.certificate";
pub const CURRENT_WALL_ID: &str =
"sha256:fc4bfb5d40f2638c7cb574093215cffed4e754729a78c0605aef229cc454659b";
pub const WASM_GC_CAPABILITIES: &[(&str, &str)] = &[
("aver", "console_print"),
("aver", "console_error"),
("aver", "console_warn"),
("aver", "time_unix_ms"),
("aver", "process_stop_requested"),
("aver", "provider_contract_violation"),
("aver", "request_method"),
("aver", "request_url"),
("aver", "request_query"),
("aver", "request_body"),
("aver", "request_headers_load"),
("aver", "response_text"),
("aver", "response_set_header"),
("aver", "http_send"),
("aver", "http_add_request_header"),
("aver", "http_clear_request_headers"),
("aver", "env_get"),
("aver", "env_set"),
("aver", "console_read_line"),
("aver", "args_len"),
("aver", "args_get"),
("aver", "random_float"),
("aver", "random_int"),
("aver", "time_sleep"),
("aver", "time_now"),
("aver", "float_sin"),
("aver", "float_cos"),
("aver", "float_atan2"),
("aver", "float_pow"),
("aver", "terminal_enable_raw_mode"),
("aver", "terminal_disable_raw_mode"),
("aver", "terminal_clear"),
("aver", "terminal_move_to"),
("aver", "terminal_print"),
("aver", "terminal_set_color"),
("aver", "terminal_reset_color"),
("aver", "terminal_read_key"),
("aver", "terminal_size"),
("aver", "terminal_hide_cursor"),
("aver", "terminal_show_cursor"),
("aver", "terminal_flush"),
("aver", "disk_read_text"),
("aver", "disk_write_text"),
("aver", "disk_append_text"),
("aver", "disk_read_bytes"),
("aver", "disk_read_bytes_at"),
("aver", "disk_write_bytes"),
("aver", "disk_append_bytes"),
("aver", "disk_size"),
("aver", "disk_exists"),
("aver", "disk_delete"),
("aver", "disk_delete_dir"),
("aver", "disk_list_dir"),
("aver", "disk_make_dir"),
("aver", "tcp_connect"),
("aver", "tcp_begin_connect"),
("aver", "tcp_dialled"),
("aver", "tcp_listen"),
("aver", "tcp_accept"),
("aver", "tcp_peer_address"),
("aver", "tcp_write_line"),
("aver", "tcp_write_bytes"),
("aver", "tcp_read_line"),
("aver", "tcp_read_bytes"),
("aver", "tcp_read_some"),
("aver", "tcp_poll"),
("aver", "tcp_close"),
("aver", "tcp_close_dial"),
("aver", "tcp_close_listener"),
("aver", "tcp_send"),
("aver", "tcp_send_bytes"),
("aver", "tcp_ping"),
("aver", "http_get"),
("aver", "http_head"),
("aver", "http_delete"),
("aver", "http_post"),
("aver", "http_put"),
("aver", "http_patch"),
("aver", "record_enter_group"),
("aver", "record_set_branch"),
("aver", "record_exit_group"),
];
pub fn is_wasm_gc_capability_import(module: &str, field: &str) -> bool {
WASM_GC_CAPABILITIES.contains(&(module, field))
|| is_custom_wasm_gc_capability_import(module, field)
}
fn is_custom_wasm_gc_capability_import(module: &str, field: &str) -> bool {
const MODULE_PREFIX: &str = "aver:user/cap-n";
const OP_PREFIX: &str = "op-n";
let Some(module_tail) = module.strip_prefix(MODULE_PREFIX) else {
return false;
};
let Some((module_hex, contract_hash)) = module_tail.split_once("-c") else {
return false;
};
let Some(operation_hex) = field.strip_prefix(OP_PREFIX) else {
return false;
};
is_nonempty_even_lower_hex(module_hex)
&& contract_hash.len() == 64
&& is_lower_hex(contract_hash)
&& is_nonempty_even_lower_hex(operation_hex)
}
fn is_nonempty_even_lower_hex(value: &str) -> bool {
!value.is_empty() && value.len().is_multiple_of(2) && is_lower_hex(value)
}
fn is_lower_hex(value: &str) -> bool {
value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capability_pairs_are_unique() {
let unique = WASM_GC_CAPABILITIES
.iter()
.copied()
.collect::<std::collections::BTreeSet<_>>();
assert_eq!(unique.len(), WASM_GC_CAPABILITIES.len());
}
#[test]
fn custom_capability_import_syntax_is_exact() {
let hash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
assert!(is_wasm_gc_capability_import(
&format!("aver:user/cap-n436c6f636b-c{hash}"),
"op-n6e6f77"
));
for (module, field) in [
(format!("aver:user/cap-n-c{hash}"), "op-n6e6f77"),
(format!("aver:user/cap-n436c6f636b-c{hash}0"), "op-n6e6f77"),
(format!("aver:user/cap-n436C6f636b-c{hash}"), "op-n6e6f77"),
(format!("aver:user/cap-n436c6f636b-c{hash}"), "op-n"),
(format!("aver:user/cap-n436c6f636b-c{hash}"), "op-nxyz"),
] {
assert!(!is_wasm_gc_capability_import(&module, field));
}
}
#[test]
fn format_spec_states_the_current_wall_identity_and_source_count() {
let spec = include_str!("../../docs/certificate-format.md");
assert!(
spec.contains(CURRENT_WALL_ID),
"certificate-format.md does not state the current wall id {CURRENT_WALL_ID}; \
update the wall identity printed in the spec"
);
let count_phrase = format!(
"{} embedded `.lean` wall sources",
crate::wall::SOURCES.len()
);
assert!(
spec.contains(&count_phrase),
"certificate-format.md does not state \"{count_phrase}\"; \
update the wall source count printed in the spec"
);
}
#[test]
fn format_spec_states_the_current_schema_version() {
let spec = include_str!("../../docs/certificate-format.md");
for phrase in [
format!("`{CERT_SCHEMA_VERSION}` (`CERT_SCHEMA_VERSION`)"),
format!("schema_version = {CERT_SCHEMA_VERSION}"),
] {
assert!(
spec.contains(&phrase),
"certificate-format.md does not state \"{phrase}\"; \
update the schema version printed in the spec"
);
}
}
#[test]
fn the_two_schema_version_constants_agree() {
let engine_src = include_str!("engine/mod.rs");
let expected = format!("pub const CERT_SCHEMA_VERSION: u32 = {CERT_SCHEMA_VERSION};");
assert!(
engine_src.contains(&expected),
"engine/mod.rs does not declare `{expected}`; the producer would emit a \
schema version the transport parser rejects"
);
}
#[test]
fn every_wall_contract_string_has_a_verbatim_producer_twin() {
let axes = include_str!("../assets/wall/current/ClaimAxes.lean");
let engine_src = include_str!("engine/mod.rs");
let mut seen = 0usize;
let mut lines = axes.lines();
while let Some(line) = lines.next() {
let trimmed = line.trim();
if !(trimmed.starts_with("def ") && trimmed.ends_with("Contract : String :=")) {
continue;
}
let literal = lines
.next()
.expect("a contract definition is followed by its literal")
.trim();
assert!(
literal.starts_with('"') && literal.ends_with('"'),
"expected a one-line string literal under `{trimmed}`, got: {literal}"
);
assert!(
engine_src.contains(literal),
"wall contract `{trimmed}` has no verbatim producer twin in \
engine/mod.rs: {literal}"
);
seen += 1;
}
assert!(
seen >= 12,
"only {seen} wall contract definitions were found; the scan shape has \
drifted and this test has stopped checking anything"
);
}
}