Skip to main content

vela_protocol/objects/
execution_binding.rs

1//! Exact execution identity attached to a current Submission.
2//!
3//! The binding is a producer-side description of the packet, profile,
4//! verifier capsule, and result contract used for bounded work. It carries no
5//! verification or decision authority.
6
7use serde::{Deserialize, Serialize};
8
9pub const EXECUTION_BINDING_SCHEMA: &str = "vela.execution-binding.v1";
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct ExecutionBindingV1 {
14    pub schema: String,
15    pub packet_root: String,
16    pub profile_root: String,
17    pub verifier_capsule_root: String,
18    pub result_contract_root: String,
19}
20
21impl ExecutionBindingV1 {
22    pub fn validate(&self) -> Result<(), String> {
23        if self.schema != EXECUTION_BINDING_SCHEMA {
24            return Err(format!("schema must be {EXECUTION_BINDING_SCHEMA}"));
25        }
26        for (field, value) in [
27            ("packet_root", &self.packet_root),
28            ("profile_root", &self.profile_root),
29            ("verifier_capsule_root", &self.verifier_capsule_root),
30            ("result_contract_root", &self.result_contract_root),
31        ] {
32            if !is_full_sha256_root(value) {
33                return Err(format!("{field} must be a full lowercase sha256 root"));
34            }
35        }
36        Ok(())
37    }
38}
39
40pub fn is_full_sha256_root(value: &str) -> bool {
41    value.strip_prefix("sha256:").is_some_and(|digest| {
42        digest.len() == 64
43            && digest
44                .bytes()
45                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
46    })
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn execution_binding_requires_exact_roots() {
55        let binding = ExecutionBindingV1 {
56            schema: EXECUTION_BINDING_SCHEMA.into(),
57            packet_root: format!("sha256:{}", "1".repeat(64)),
58            profile_root: format!("sha256:{}", "2".repeat(64)),
59            verifier_capsule_root: format!("sha256:{}", "3".repeat(64)),
60            result_contract_root: format!("sha256:{}", "4".repeat(64)),
61        };
62        assert!(binding.validate().is_ok());
63
64        let mut short = binding;
65        short.packet_root = "sha256:1".into();
66        assert!(short.validate().is_err());
67    }
68}