Skip to main content

tembra_core/host/
mod.rs

1use crate::Reason;
2mod contexts;
3mod disposal;
4pub use disposal::Disposal;
5mod lifecycle;
6mod orbs;
7mod references;
8mod resources;
9mod tasks;
10#[cfg(test)]
11mod tests;
12
13use crate::{
14    Code, Error, Result,
15    index::{Index, VersionCounts},
16    model::*,
17    program::WaitKey,
18    state::Snapshot,
19};
20use std::{
21    cell::RefCell,
22    collections::{BTreeMap, BTreeSet, VecDeque},
23    rc::Rc,
24};
25
26#[derive(Clone)]
27pub struct Host {
28    pub(crate) inner: Rc<RefCell<Inner>>,
29}
30
31pub(crate) use storage::Inner;
32use storage::Pending;
33
34mod ownership;
35mod storage;
36
37impl Default for Host {
38    /// Create a fresh platform-independent kernel.
39    fn default() -> Self {
40        Self::new()
41    }
42}
43
44/// Describe a record that no longer exists.
45pub(crate) fn gone(kind: Reason) -> Error {
46    Error::new(Code::Gone, kind, format!("{kind} is no longer available"))
47}
48
49/// Describe an operation rejected by the current lifecycle state.
50pub(crate) fn conflict(reason: Reason) -> Error {
51    Error::new(Code::Conflict, reason, reason.as_str().replace('_', " "))
52}
53
54/// Report identity or revision exhaustion without wrapping counters.
55fn exhausted() -> Error {
56    Error::new(
57        Code::Exhausted,
58        Reason::Identities,
59        "identity or revision space exhausted",
60    )
61}
62
63/// Issue the next identity within the shared safe-integer limit.
64fn reserve(next: &mut Id) -> Result<Id> {
65    let id = *next;
66    if id > MAX_ID {
67        return Err(exhausted());
68    }
69    *next += 1;
70    Ok(id)
71}
72
73/// Reject invalid declarations before constructing a replacement graph.
74fn validate_orb(orb: &Orb) -> Result<()> {
75    if orb.name.is_empty() || orb.api_version == 0 {
76        return Err(Error::invalid(
77            Reason::Orb,
78            "orb name and api version are required",
79        ));
80    }
81    if !cfg!(feature = "hooks")
82        && (!orb.hooks.is_empty() || orb.requires.iter().any(|r| !r.hooks.is_empty()))
83    {
84        return Err(Error::new(
85            Code::Unsupported,
86            Reason::Hooks,
87            "hooks were excluded from this build",
88        ));
89    }
90    if orb
91        .hook_signatures
92        .iter()
93        .any(|(hook, signature)| !orb.hooks.contains(hook) || signature.is_empty())
94        || orb.requires.iter().any(|requirement| {
95            requirement
96                .hook_signatures
97                .iter()
98                .any(|(hook, signature)| !requirement.hooks.contains(hook) || signature.is_empty())
99        })
100    {
101        return Err(Error::invalid(
102            Reason::Hook,
103            "hook signatures require declared hooks and nonempty contracts",
104        ));
105    }
106    let mut services = BTreeSet::new();
107    for service in &orb.services {
108        if service.name.is_empty()
109            || !services.insert(&service.name)
110            || service
111                .methods
112                .iter()
113                .any(|m| m.is_empty() || reserved_member(m))
114        {
115            return Err(Error::invalid(
116                Reason::Service,
117                "invalid or duplicate service/member name",
118            ));
119        }
120        if !admits_linger(service.lifetime, service.linger_ms) {
121            return Err(Error::invalid(
122                Reason::Linger,
123                "host-lifetime objects cannot linger",
124            ));
125        }
126    }
127    Ok(())
128}