Skip to main content

datumesh_http/
mesh.rs

1//! [`Mesh`]: the engine, its store and everything the endpoints share; the blocking
2//! operations each endpoint is built on, usable as library calls.
3
4use std::collections::{BTreeMap, HashMap, HashSet};
5use std::path::PathBuf;
6use std::sync::{Arc, Mutex, RwLock};
7use std::time::{Duration, Instant};
8
9use datumesh_core::json::{self, Value};
10use datumesh_core::{keys_from_response, verify, ErrorCode, FunctionDescriptor, KeyDoc, Keys, Receipt, SigningKey};
11use datumesh_engine::{Answer, Clock, Engine, EngineError, Runtime, SystemClock};
12use datumesh_formula::runtime::FormulaRuntime;
13use datumesh_store::Store;
14
15use crate::apikeys::Principal;
16use crate::call::{self, Bindings, Bound, Rejection, Target};
17use crate::config::{Config, ConfigError, Rule};
18use crate::envelope::{self, Cache, Reply};
19use crate::foreign::{host_of, CrossMesh, Foreign, ForeignError, KeyCache};
20use crate::jobs::Jobs;
21use crate::limit::Limiter;
22use crate::metrics::Metrics;
23use crate::pool::Pool;
24
25/// Why a mesh could not be built.
26#[derive(Debug)]
27#[non_exhaustive]
28pub enum BuildError {
29    /// The configuration.
30    Config(ConfigError),
31    /// The engine's configuration (the key, its publication in the store, the runtimes).
32    Engine(datumesh_engine::ConfigError),
33    /// The base URL ([`Config::base_url`]). Since 0.2.0.
34    BaseUrl(crate::config::BaseUrlError),
35}
36
37impl std::fmt::Display for BuildError {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            BuildError::Config(e) => write!(f, "{e}"),
41            BuildError::Engine(e) => write!(f, "engine: {e}"),
42            BuildError::BaseUrl(e) => write!(f, "configuration: base URL: {e}"),
43        }
44    }
45}
46
47impl std::error::Error for BuildError {}
48
49/// Builds a [`Mesh`].
50pub struct MeshBuilder {
51    config: Config,
52    store: Arc<dyn Store + Send + Sync>,
53    key: Option<SigningKey>,
54    key_file: Option<PathBuf>,
55    runtimes: Vec<Arc<dyn Runtime>>,
56    formula: bool,
57    clock: Arc<dyn Clock>,
58    fuel: Option<u64>,
59    licence: Option<String>,
60    memo_capacity: Option<usize>,
61}
62
63impl std::fmt::Debug for MeshBuilder {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        f.debug_struct("MeshBuilder").field("host", &self.config.host).finish_non_exhaustive()
66    }
67}
68
69impl MeshBuilder {
70    /// The mesh's signing key.
71    pub fn signing_key(mut self, key: SigningKey) -> Self {
72        self.key = Some(key);
73        self.key_file = None;
74        self
75    }
76
77    /// The mesh's signing key, from a file of its 32-byte seed (32 bytes or 64 hex digits),
78    /// which must have mode 0600.
79    pub fn signing_key_file(mut self, path: impl Into<PathBuf>) -> Self {
80        self.key_file = Some(path.into());
81        self.key = None;
82        self
83    }
84
85    /// Adds a runtime (the formula runtime is added unless [`MeshBuilder::without_formula`]).
86    pub fn runtime(mut self, runtime: Arc<dyn Runtime>) -> Self {
87        self.runtimes.push(runtime);
88        self
89    }
90
91    /// No formula runtime.
92    pub fn without_formula(mut self) -> Self {
93        self.formula = false;
94        self
95    }
96
97    /// The clock (default: the system clock).
98    pub fn clock(mut self, clock: Arc<dyn Clock>) -> Self {
99        self.clock = clock;
100        self
101    }
102
103    /// The engine's fuel per request.
104    pub fn fuel(mut self, fuel: u64) -> Self {
105        self.fuel = Some(fuel);
106        self
107    }
108
109    /// The licence of the receipts the engine signs.
110    pub fn licence(mut self, licence: impl Into<String>) -> Self {
111        self.licence = Some(licence.into());
112        self
113    }
114
115    /// The engine's memo capacity.
116    pub fn memo_capacity(mut self, n: usize) -> Self {
117        self.memo_capacity = Some(n);
118        self
119    }
120
121    /// Checks the configuration, builds the engine (its key must be published in the store's
122    /// namespace record for the host) and the mesh.
123    pub fn build(self) -> Result<Mesh, BuildError> {
124        if let Some(u) = &self.config.base_url {
125            crate::config::check_base_url(&self.config.host, u).map_err(BuildError::BaseUrl)?;
126        }
127        self.config.check().map_err(BuildError::Config)?;
128        let keys = Arc::new(KeyCache::default());
129        let foreign = Arc::new(Foreign::new(&self.config, self.store.clone(), keys.clone(), self.clock.clone()));
130        let mut b = Engine::builder(self.store.clone(), self.config.host.clone()).clock(self.clock.clone());
131        b = match (self.key, &self.key_file) {
132            (Some(k), _) => b.signing_key(k),
133            (None, Some(p)) => {
134                crate::config::read_secret_file(p).map_err(BuildError::Config)?;
135                b.signing_key_file(p.clone())
136            }
137            (None, None) => b,
138        };
139        let mut runtimes = self.runtimes;
140        if self.formula {
141            runtimes.push(Arc::new(FormulaRuntime::new(self.store.clone())));
142        }
143        if self.config.test_cells {
144            crate::testrun::install_test_cases(&*self.store, &self.config.host)
145                .map_err(|e| BuildError::Config(ConfigError::new("DATUMESH_TEST_CELLS", e)))?;
146            runtimes.push(Arc::new(crate::testrun::TestCases { store: self.store.clone() }));
147        }
148        for rt in runtimes {
149            b = b.runtime(Arc::new(CrossMesh::new(rt, foreign.clone())));
150        }
151        if let Some(f) = self.fuel {
152            b = b.fuel(f);
153        }
154        if let Some(l) = self.licence {
155            b = b.licence(l);
156        }
157        if let Some(n) = self.memo_capacity {
158            b = b.memo_capacity(n);
159        }
160        let engine = b.build().map_err(BuildError::Engine)?;
161        let config = self.config;
162        let embed = config.embed_url.as_deref().map(crate::embed::EmbedClient::new);
163        let inner = Inner {
164            generic: RwLock::new(Arc::new(crate::find::stopwords())),
165            embed,
166            reindex: Mutex::new(()),
167            publish_limits: Mutex::new(crate::bundles::Limits::default()),
168            relays: Mutex::new(HashMap::new()),
169            name_cells: Mutex::new(HashMap::new()),
170            series: Mutex::new(crate::series::Memo::default()),
171            pool: Arc::new(Pool::new(config.pool_size)),
172            jobs: Jobs::new(config.ttl.job, config.max_jobs),
173            limiter: Limiter::new(config.rate.clone()),
174            metrics: Metrics::default(),
175            engine: Arc::new(engine),
176            store: self.store,
177            clock: self.clock,
178            keys,
179            foreign,
180            descriptors: Mutex::new(HashMap::new()),
181            last_good: Mutex::new(HashMap::new()),
182            infos: (0..32).map(|_| Mutex::new(HashMap::new())).collect(),
183            by_hash: (0..32).map(|_| Mutex::new(HashMap::new())).collect(),
184            latest: Latest::new(),
185            targets: Targets::new(),
186            slices: Mutex::new(Vec::new()),
187            known: Mutex::new(crate::describe::Index::default()),
188            started: Instant::now(),
189            config,
190        };
191        let mesh = Mesh { inner: Arc::new(inner) };
192        // The operator's catalogue, then the index: now, and every 30 s (a bundle or a publish
193        // from another process reaches the index within that). The thread stops with the mesh.
194        // On a thread of its own: the store's synchronous client must not run on a runtime's thread.
195        std::thread::scope(|s| {
196            let inner = mesh.inner.clone();
197            let _ = s.spawn(move || crate::find::sync_operator_catalog(&inner)).join();
198        });
199        let weak = Arc::downgrade(&mesh.inner);
200        let spawned = std::thread::Builder::new().name("datumesh-index".into()).spawn(move || loop {
201            let Some(inner) = weak.upgrade() else { break };
202            crate::find::reindex(&inner);
203            drop(inner);
204            std::thread::sleep(Duration::from_secs(30));
205        });
206        if let Err(e) = spawned {
207            tracing::warn!(error = %e, "the index thread is not started; find sees the catalogue as it is at start");
208        }
209        Ok(mesh)
210    }
211}
212
213/// The last `ok` receipt served for a cell, for `stale` answers.
214struct LastGood {
215    info: Arc<ReceiptInfo>,
216    at: Instant,
217}
218
219/// What the mesh needs of a receipt it serves, kept by its bytes (content-addressed: never
220/// stale): its hash, `asof`, trust and its `ok` envelope.
221pub(crate) struct ReceiptInfo {
222    pub(crate) hash: String,
223    pub(crate) asof: String,
224    pub(crate) receipt: Vec<u8>,
225    pub(crate) parsed: Receipt,
226    pub(crate) trust: datumesh_core::TrustLabel,
227    /// The `ok` envelope, the function hash echoed.
228    pub(crate) ok: crate::envelope::Bytes,
229}
230
231pub(crate) struct Inner {
232    pub(crate) config: Config,
233    pub(crate) store: Arc<dyn Store + Send + Sync>,
234    pub(crate) engine: Arc<Engine>,
235    pub(crate) clock: Arc<dyn Clock>,
236    pub(crate) pool: Arc<Pool>,
237    pub(crate) jobs: Jobs,
238    pub(crate) limiter: Limiter,
239    pub(crate) metrics: Metrics,
240    pub(crate) keys: Arc<KeyCache>,
241    pub(crate) foreign: Arc<Foreign>,
242    descriptors: Mutex<HashMap<String, Arc<FunctionDescriptor>>>,
243    last_good: Mutex<HashMap<String, LastGood>>,
244    infos: Vec<Mutex<HashMap<u64, Arc<ReceiptInfo>>>>,
245    by_hash: Vec<Mutex<HashMap<String, Arc<ReceiptInfo>>>>,
246    latest: Latest,
247    targets: Targets,
248    #[allow(clippy::type_complexity)]
249    pub(crate) slices: Mutex<Vec<(String, String, Arc<Vec<datumesh_store::SliceValue>>)>>,
250    /// The index of the objects the mesh knows (describe, short ids).
251    pub(crate) known: Mutex<crate::describe::Index>,
252    pub(crate) started: Instant,
253    /// The generic words `find` never matches values by (CONTRACT-build2-part2-read R3).
254    pub(crate) generic: RwLock<Arc<HashSet<String>>>,
255    /// The embedding service, when `DATUMESH_EMBED_URL` is set (R2).
256    pub(crate) embed: Option<crate::embed::EmbedClient>,
257    /// One index pass at a time.
258    pub(crate) reindex: Mutex<()>,
259    /// The attempts at `POST /bundles`, per host and for the mesh (Build 2 Part 4, P1).
260    pub(crate) publish_limits: Mutex<crate::bundles::Limits>,
261    /// Where a relayed function's source was read, by function name and hash (Build 2 Part 5, F5).
262    pub(crate) relays: Mutex<HashMap<String, (Instant, Option<crate::fnpage::Relay>)>>,
263    /// The cells of a function by name, where its hash is shared (http 0.4.6, D23): name and hash to (when, count).
264    pub(crate) name_cells: Mutex<HashMap<String, (Instant, u64)>>,
265    /// The overviews and dictionary orders of the series pages (Build 2 Part 6, G5).
266    pub(crate) series: Mutex<crate::series::Memo>,
267}
268
269/// A Datumesh mesh: the engine behind the endpoints. Cheap to clone (shared).
270#[derive(Clone)]
271pub struct Mesh {
272    pub(crate) inner: Arc<Inner>,
273}
274
275impl std::fmt::Debug for Mesh {
276    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277        f.debug_struct("Mesh").field("host", &self.inner.config.host).finish_non_exhaustive()
278    }
279}
280
281/// Bindings looked up once per name and asof within one batch.
282pub(crate) struct Memo<'a> {
283    inner: &'a Inner,
284    seen: std::borrow::Cow<'a, Seen>,
285}
286
287/// A binding a batch looks up: the lookup's result once one succeeded, and the lock held
288/// during a lookup, so the batch's chunks, which start together and ask for the same names,
289/// make one store round trip per name and asof, not one each.
290#[derive(Default)]
291struct SeenEntry {
292    found: std::sync::OnceLock<Option<Arc<Bound>>>,
293    lookup: Mutex<()>,
294}
295
296/// The bindings a batch has looked up (shared by its chunks), by name, then asof.
297#[derive(Default)]
298pub(crate) struct Seen(RwLock<HashMap<String, SeenAsofs>>);
299
300/// A name's entries in [`Seen`], by asof.
301type SeenAsofs = Vec<(String, Arc<SeenEntry>)>;
302
303impl Clone for Seen {
304    fn clone(&self) -> Seen {
305        let m = self.0.read().unwrap_or_else(|p| p.into_inner());
306        let copy = m
307            .iter()
308            .map(|(name, v)| {
309                let v = v
310                    .iter()
311                    .map(|(a, e)| (a.clone(), Arc::new(SeenEntry { found: e.found.clone(), lookup: Mutex::new(()) })))
312                    .collect();
313                (name.clone(), v)
314            })
315            .collect();
316        Seen(RwLock::new(copy))
317    }
318}
319
320/// What [`Seen`] has of a name as of an asof.
321enum Held {
322    Found(Option<Arc<Bound>>),
323    Entry(Arc<SeenEntry>),
324    Full,
325}
326
327impl Seen {
328    /// The binding of `fn_name` as of `asof` if found, else its entry (made if there is room:
329    /// 64 asofs per name).
330    fn held(&self, fn_name: &str, asof: &str) -> Held {
331        if let Some(v) = self.0.read().unwrap_or_else(|p| p.into_inner()).get(fn_name) {
332            if let Some((_, e)) = v.iter().find(|(a, _)| a == asof) {
333                return match e.found.get() {
334                    Some(b) => Held::Found(b.clone()),
335                    None => Held::Entry(e.clone()),
336                };
337            }
338        }
339        let mut m = self.0.write().unwrap_or_else(|p| p.into_inner());
340        let v = m.entry(fn_name.to_string()).or_default();
341        if let Some((_, e)) = v.iter().find(|(a, _)| a == asof) {
342            return Held::Entry(e.clone());
343        }
344        if v.len() >= 64 {
345            return Held::Full;
346        }
347        let e = Arc::new(SeenEntry::default());
348        v.push((asof.to_string(), e.clone()));
349        Held::Entry(e)
350    }
351
352    /// The binding of `fn_name` as of `asof`: as found before, else looked up by `look`, at
353    /// most one lookup at a time per name and asof (the others wait for its result). A failed
354    /// lookup is not kept: the next request looks the name up again.
355    pub(crate) fn lookup(
356        &self,
357        fn_name: &str,
358        asof: &str,
359        look: impl FnOnce() -> Result<Option<Arc<Bound>>, ErrorCode>,
360    ) -> Result<Option<Arc<Bound>>, ErrorCode> {
361        let e = match self.held(fn_name, asof) {
362            Held::Found(b) => return Ok(b),
363            Held::Full => return look(),
364            Held::Entry(e) => e,
365        };
366        let _looking = e.lookup.lock().unwrap_or_else(|p| p.into_inner());
367        if let Some(b) = e.found.get() {
368            return Ok(b.clone());
369        }
370        let b = look()?;
371        let _ = e.found.set(b.clone());
372        Ok(b)
373    }
374}
375
376impl<'a> Memo<'a> {
377    pub(crate) fn new(inner: &'a Inner) -> Memo<'a> {
378        Memo { inner, seen: std::borrow::Cow::Owned(Seen::default()) }
379    }
380
381    pub(crate) fn shared(inner: &'a Inner, seen: &'a Seen) -> Memo<'a> {
382        Memo { inner, seen: std::borrow::Cow::Borrowed(seen) }
383    }
384}
385
386impl Bindings for Memo<'_> {
387    fn pinned(&self, fn_hash: &str) -> Result<Option<Arc<Bound>>, ErrorCode> {
388        Bindings::pinned(self.inner, fn_hash)
389    }
390
391    fn alias(&self, alias: &str) -> Option<String> {
392        Bindings::alias(self.inner, alias)
393    }
394
395    fn class(&self, name: &str, asof: &str) -> Result<Option<Arc<call::BoundClass>>, ErrorCode> {
396        Bindings::class(self.inner, name, asof)
397    }
398
399    fn aliases_tx(&self) -> Option<String> {
400        Bindings::aliases_tx(self.inner)
401    }
402
403    fn lookup(&self, class: &str, label: &str, asof: &str) -> Result<Option<String>, ErrorCode> {
404        Bindings::lookup(self.inner, class, label, asof)
405    }
406
407    fn cached(&self, fn_hash: &str, rest: &str) -> Option<Arc<Target>> {
408        self.inner.targets.get(fn_hash, rest)
409    }
410
411    fn remember(&self, rest: &str, target: &Arc<Target>) {
412        self.inner.targets.put(rest, target);
413    }
414
415    fn binding(&self, fn_name: &str, asof: &str) -> Result<Option<Arc<Bound>>, ErrorCode> {
416        self.seen.lookup(fn_name, asof, || Bindings::binding(self.inner, fn_name, asof))
417    }
418}
419
420impl Bindings for Inner {
421    fn pinned(&self, fn_hash: &str) -> Result<Option<Arc<Bound>>, ErrorCode> {
422        match self.descriptor(fn_hash) {
423            Ok(d) => Ok(Some(Arc::new(Bound::new(fn_hash, "", d)))),
424            Err(ErrorCode::FnUnavailable) => Ok(None),
425            Err(c) => Err(c),
426        }
427    }
428
429    fn alias(&self, alias: &str) -> Option<String> {
430        self.config.aliases.get(alias).cloned()
431    }
432
433    fn class(&self, name: &str, asof: &str) -> Result<Option<Arc<call::BoundClass>>, ErrorCode> {
434        let c = self.class_binding(name, asof)?;
435        if c.is_some() {
436            self.hear_of_class(name);
437        }
438        Ok(c)
439    }
440
441    /// The mesh's configured alias map has no snapshot.
442    fn aliases_tx(&self) -> Option<String> {
443        None
444    }
445
446    fn lookup(&self, class: &str, label: &str, asof: &str) -> Result<Option<String>, ErrorCode> {
447        let Some(c) = Bindings::class(self, class, asof)? else { return Ok(None) };
448        let Some(method) = &c.descriptor.lookup else { return Ok(None) };
449        let name = format!("{class}/{method}");
450        let Some(b) = Bindings::binding(self, &name, asof)? else { return Ok(None) };
451        let (ns, fn_name) = name.rsplit_once('/').unwrap_or((&name, ""));
452        let mut args = datumesh_core::Args::new();
453        args.insert("label".into(), datumesh_core::Arg::Value(datumesh_core::Scalar::String(label.to_string())));
454        let cell = datumesh_core::Cell { ns: ns.into(), fn_name: fn_name.into(), fn_hash: b.fn_hash.clone(), args };
455        match self.engine.get_cell(&cell, Some(asof)) {
456            Ok(Answer::Receipt(bytes)) => match Receipt::from_json(&bytes).map(|r| r.value().clone()) {
457                Ok(datumesh_core::Scalar::String(k)) => Ok(Some(k)),
458                _ => Ok(None),
459            },
460            Err(EngineError::Store(_)) => Err(ErrorCode::InputUnavailable),
461            _ => Ok(None),
462        }
463    }
464
465    fn cached(&self, fn_hash: &str, rest: &str) -> Option<Arc<Target>> {
466        self.targets.get(fn_hash, rest)
467    }
468
469    fn remember(&self, rest: &str, target: &Arc<Target>) {
470        self.targets.put(rest, target);
471    }
472
473    fn binding(&self, fn_name: &str, asof: &str) -> Result<Option<Arc<Bound>>, ErrorCode> {
474        let mut failed = None;
475        if self.foreign.is_foreign(fn_name) {
476            if let Err(e) = self.foreign.ensure_name(fn_name) {
477                tracing::warn!(target: "datumesh_http", name = fn_name, detail = %e.detail, "cross-mesh binding");
478                // `Bindings::binding` answers a bare code: the detail of E_FOREIGN_UNAVAILABLE
479                // waits on this thread for `rejection`, which answers on the same thread.
480                FOREIGN_DETAIL.with(|d| d.set(e.foreign_detail()));
481                failed = Some(e.code);
482            }
483        }
484        let b = self.store.resolve_binding(fn_name, asof).map_err(|e| {
485            tracing::warn!(target: "datumesh_http", error = %e, "resolve_binding");
486            ErrorCode::InputUnavailable
487        })?;
488        // The other mesh is unreachable: a binding imported before still names the cell
489        // (whose answer is then stale or an error), else the name cannot be resolved.
490        if let (Some(code), None) = (failed, &b) {
491            return Err(code);
492        }
493        match b {
494            None => Ok(None),
495            Some(b) => {
496                let d = self.descriptor(&b.fn_hash)?;
497                Ok(Some(Arc::new(Bound::new(b.fn_hash, b.tx, d))))
498            }
499        }
500    }
501}
502
503thread_local! {
504    /// Why the other mesh could not be reached, from the last cross-mesh name resolution on
505    /// this thread (see `Inner::binding` and `Inner::rejection`).
506    static FOREIGN_DETAIL: std::cell::Cell<Option<datumesh_core::ForeignDetail>> = const { std::cell::Cell::new(None) };
507}
508
509/// Latest answers by key: the receipt, the cell id and the asof answered at.
510type LatestShard = RwLock<HashMap<String, (Arc<ReceiptInfo>, String, String)>>;
511
512/// The latest answers of local stored cells, as of when they were answered: such an answer
513/// stands for every later asof until a version of the cell is added, which the mesh is told
514/// of ([`Mesh::stored_version_added`]), as the engine's memo is. It saves the engine's work
515/// (the cell id, the memo) on the hottest path: warm gets of stored cells.
516struct Latest {
517    shards: Vec<LatestShard>,
518    by_cell: Mutex<HashMap<String, Vec<String>>>,
519}
520
521impl Latest {
522    fn new() -> Latest {
523        Latest { shards: (0..32).map(|_| RwLock::new(HashMap::new())).collect(), by_cell: Mutex::new(HashMap::new()) }
524    }
525
526    fn shard(&self, key: &str) -> &LatestShard {
527        use std::hash::{Hash, Hasher};
528        let mut h = std::collections::hash_map::DefaultHasher::new();
529        key.hash(&mut h);
530        &self.shards[(h.finish() % self.shards.len() as u64) as usize]
531    }
532
533    /// The answer for `key` as of `a`, if one answered as of `a` or earlier is held.
534    fn get(&self, key: &str, a: &str) -> Option<Arc<ReceiptInfo>> {
535        let m = self.shard(key).read().unwrap_or_else(|p| p.into_inner());
536        let (info, _, at) = m.get(key)?;
537        (a >= at.as_str()).then(|| info.clone())
538    }
539
540    fn put(&self, key: String, cell_id: String, info: Arc<ReceiptInfo>, at: String) {
541        {
542            let mut b = self.by_cell.lock().unwrap_or_else(|p| p.into_inner());
543            if b.len() > 200_000 {
544                b.clear();
545                for s in &self.shards {
546                    s.write().unwrap_or_else(|p| p.into_inner()).clear();
547                }
548            }
549            b.entry(cell_id.clone()).or_default().push(key.clone());
550        }
551        self.shard(&key).write().unwrap_or_else(|p| p.into_inner()).insert(key, (info, cell_id, at));
552    }
553
554    fn forget(&self, cell_id: &str) {
555        let keys = self.by_cell.lock().unwrap_or_else(|p| p.into_inner()).remove(cell_id).unwrap_or_default();
556        for k in keys {
557            self.shard(&k).write().unwrap_or_else(|p| p.into_inner()).remove(&k);
558        }
559    }
560
561    fn clear(&self) {
562        self.by_cell.lock().unwrap_or_else(|p| p.into_inner()).clear();
563        for s in &self.shards {
564            s.write().unwrap_or_else(|p| p.into_inner()).clear();
565        }
566    }
567}
568
569/// Cell URI rests already parsed, by function hash (parsing depends on the signature
570/// alone): resolving a hot cell costs a binding lookup and no parse.
571struct Targets {
572    #[allow(clippy::type_complexity)]
573    shards: Vec<RwLock<HashMap<String, HashMap<String, Arc<Target>>>>>,
574}
575
576impl Targets {
577    fn new() -> Targets {
578        Targets { shards: (0..32).map(|_| RwLock::new(HashMap::new())).collect() }
579    }
580
581    #[allow(clippy::type_complexity)]
582    fn shard(&self, rest: &str) -> &RwLock<HashMap<String, HashMap<String, Arc<Target>>>> {
583        use std::hash::{Hash, Hasher};
584        let mut h = std::collections::hash_map::DefaultHasher::new();
585        rest.hash(&mut h);
586        &self.shards[(h.finish() % self.shards.len() as u64) as usize]
587    }
588
589    fn get(&self, fn_hash: &str, rest: &str) -> Option<Arc<Target>> {
590        self.shard(rest).read().unwrap_or_else(|p| p.into_inner()).get(rest)?.get(fn_hash).cloned()
591    }
592
593    fn put(&self, rest: &str, t: &Arc<Target>) {
594        let mut m = self.shard(rest).write().unwrap_or_else(|p| p.into_inner());
595        if m.len() >= 4_096 {
596            m.clear();
597        }
598        let v = m.entry(rest.to_string()).or_default();
599        if v.len() >= 8 {
600            v.clear();
601        }
602        v.insert(t.bound.fn_hash.clone(), t.clone());
603    }
604}
605
606/// The key of a cell's last good answer: the function hash and the URI (without asof: the
607/// last good answer is kept for requests about now only).
608fn last_good_key(t: &Target) -> String {
609    t.cell_key().to_string()
610}
611
612/// The engine's error as an envelope code (the engine's table, "For the HTTP layer").
613pub(crate) fn engine_code(e: &EngineError) -> (bool, ErrorCode) {
614    match e {
615        EngineError::Invalid(e) if envelope::is_request_code(e.code()) => (true, e.code()),
616        EngineError::Invalid(e) if envelope::is_error_code(e.code()) => (false, e.code()),
617        EngineError::NameUnresolved { .. } => (false, ErrorCode::FnUnavailable),
618        EngineError::UndeclaredInput { .. } => (false, ErrorCode::InputUndeclared),
619        EngineError::Sign(e) if envelope::is_error_code(e.code()) => (false, e.code()),
620        EngineError::Store(_) => (false, ErrorCode::InputUnavailable),
621        _ => (false, ErrorCode::EvalUnavailable),
622    }
623}
624
625impl Inner {
626    pub(crate) fn now(&self) -> String {
627        self.clock.now()
628    }
629
630    /// A descriptor by hash (cached: content-addressed).
631    pub(crate) fn descriptor(&self, fn_hash: &str) -> Result<Arc<FunctionDescriptor>, ErrorCode> {
632        if let Some(d) = self.descriptors.lock().unwrap_or_else(|p| p.into_inner()).get(fn_hash) {
633            return Ok(d.clone());
634        }
635        let bytes = self.store.descriptor(fn_hash).ok_or(ErrorCode::FnUnavailable)?;
636        let d = Arc::new(FunctionDescriptor::from_json(&bytes).map_err(|_| ErrorCode::FnUnavailable)?);
637        let mut m = self.descriptors.lock().unwrap_or_else(|p| p.into_inner());
638        if m.len() > 10_000 {
639            m.clear();
640        }
641        m.insert(fn_hash.to_string(), d.clone());
642        Ok(d)
643    }
644
645    /// The `ok` reply with a receipt, remembered for `stale` answers.
646    fn ok_reply(
647        &self,
648        target: &Target,
649        info: Arc<ReceiptInfo>,
650        a: &str,
651        now: &str,
652        private: bool,
653        link: bool,
654    ) -> Reply {
655        let mut reply = Reply::json(200, info.ok.clone(), Cache::Immutable);
656        if target.asof().is_none() {
657            let mut lg = self.last_good.lock().unwrap_or_else(|p| p.into_inner());
658            if lg.len() > 100_000 {
659                lg.clear();
660            }
661            lg.insert(last_good_key(target), LastGood { info, at: Instant::now() });
662        }
663        self.cache_rules(&mut reply, target, a, now, private, link);
664        reply
665    }
666
667    /// Remembers the answer as of now `a` for a local stored cell, when it is the cell's
668    /// latest version (a version with a later `tx` would answer later asofs).
669    fn remember_latest(&self, target: &Target, info: &Arc<ReceiptInfo>, a: &str) {
670        match self.store.latest(target.cell_id()) {
671            Ok(Some(v)) if v.receipt_hash == info.hash => {
672                self.latest.put(
673                    target.cell_key().to_string(),
674                    target.cell_id().to_string(),
675                    info.clone(),
676                    a.to_string(),
677                );
678            }
679            _ => {}
680        }
681    }
682
683    /// A stored version was added for `cell_id`: the engine's memo, the latest answers and
684    /// the slice cache forget what rests on it.
685    pub(crate) fn version_added(&self, cell_id: &str) {
686        self.engine.stored_version_added(cell_id);
687        self.latest.forget(cell_id);
688        self.slices.lock().unwrap_or_else(|p| p.into_inner()).clear();
689        self.known.lock().unwrap_or_else(|p| p.into_inner()).announce(cell_id);
690    }
691
692    /// The receipt's hash, asof, trust and `ok` envelope (cached by its bytes).
693    fn receipt_info(&self, bytes: &[u8]) -> Option<Arc<ReceiptInfo>> {
694        use std::hash::{Hash, Hasher};
695        let mut h = std::collections::hash_map::DefaultHasher::new();
696        bytes.hash(&mut h);
697        let key = h.finish();
698        let shard = &self.infos[(key % self.infos.len() as u64) as usize];
699        if let Some(i) = shard.lock().unwrap_or_else(|p| p.into_inner()).get(&key) {
700            if i.receipt == bytes {
701                return Some(i.clone());
702            }
703        }
704        let r = Receipt::from_json(bytes).ok()?;
705        let trust = envelope::trust_label_of(&r, &self.config.host);
706        let ok = envelope::with_fn_hash(datumesh_core::Answer::ok(r.clone(), trust), Some(&r.cell().fn_hash)).to_json();
707        let info = Arc::new(ReceiptInfo {
708            hash: r.hash().to_string(),
709            asof: r.asof().to_string(),
710            receipt: bytes.to_vec(),
711            parsed: r,
712            trust,
713            ok: ok.into(),
714        });
715        let mut m = shard.lock().unwrap_or_else(|p| p.into_inner());
716        if m.len() >= 4_096 {
717            m.clear();
718        }
719        m.insert(key, info.clone());
720        Some(info)
721    }
722
723    /// A receipt's info by its hash: from the cache, else the store.
724    pub(crate) fn receipt_info_by_hash(&self, hash: &str) -> Option<Arc<ReceiptInfo>> {
725        use std::hash::{Hash, Hasher};
726        let mut h = std::collections::hash_map::DefaultHasher::new();
727        hash.hash(&mut h);
728        let shard = &self.by_hash[(h.finish() % self.by_hash.len() as u64) as usize];
729        if let Some(i) = shard.lock().unwrap_or_else(|p| p.into_inner()).get(hash) {
730            return Some(i.clone());
731        }
732        let bytes = self.store.receipt(hash)?;
733        let info = self.receipt_info(&bytes)?;
734        let mut m = shard.lock().unwrap_or_else(|p| p.into_inner());
735        if m.len() >= 2_048 {
736            m.clear();
737        }
738        m.insert(hash.to_string(), info.clone());
739        Some(info)
740    }
741
742    /// The reply to a request the call parser rejected.
743    pub(crate) fn rejection(&self, r: Rejection, asof: Option<&str>) -> Reply {
744        match r {
745            Rejection::BadRequest(c) => envelope::bad_request_reply(c),
746            Rejection::NotCovered => {
747                let _ = asof;
748                Reply::json(404, envelope::not_covered(None), Cache::Public(self.config.ttl.latest_max_age.as_secs()))
749            }
750            Rejection::Error(ErrorCode::NameUnbound) => {
751                envelope::name_unbound_reply(self.config.ttl.latest_max_age.as_secs())
752            }
753            Rejection::Error(ErrorCode::ForeignUnavailable) => {
754                // The other mesh could not be reached while resolving the name (spec-0.4.3).
755                let d = FOREIGN_DETAIL.with(|d| d.take()).unwrap_or(datumesh_core::ForeignDetail::Network);
756                envelope::answer_reply(&datumesh_core::Answer::foreign_unavailable(d), Cache::NoStore)
757            }
758            Rejection::Error(c) => envelope::error_reply(c),
759        }
760    }
761
762    /// The licence and payment rules for a target and principal: a refusal, and whether the
763    /// answer depends on who asks.
764    pub(crate) fn policy(&self, target: &Target, principal: Option<&Principal>) -> (Option<Reply>, bool) {
765        if self.config.policy.rules.is_empty() {
766            return (None, false);
767        }
768        match self.config.policy.rule(&target.fn_name()) {
769            None => (None, false),
770            Some(Rule::Principals(ps)) => {
771                if principal.is_some_and(|p| ps.contains(&p.id)) {
772                    (None, true)
773                } else {
774                    let a = envelope::with_fn_hash(datumesh_core::Answer::unlicensed(), Some(&target.bound.fn_hash));
775                    (Some(envelope::answer_reply(&a, Cache::Private)), true)
776                }
777            }
778            Some(Rule::Price { amount, currency }) => {
779                let authorise = format!("{}/pay/{}", self.config.origin(), target.cell_id());
780                let quote = datumesh_core::Quote { amount: amount.clone(), currency: currency.clone(), authorise };
781                let a =
782                    envelope::with_fn_hash(datumesh_core::Answer::payment_required(quote), Some(&target.bound.fn_hash));
783                (Some(envelope::answer_reply(&a, Cache::Private)), true)
784            }
785        }
786    }
787
788    /// Answers a `/cell` request (blocking; no budget: see [`Mesh::cell`]). `on_target` is
789    /// told the request description as soon as it is known (for `pending`).
790    pub(crate) fn answer(
791        &self,
792        rest: &str,
793        principal: Option<&Principal>,
794        bindings: &dyn Bindings,
795        now: &str,
796        on_target: &mut dyn FnMut(&Target),
797    ) -> Reply {
798        self.answer_with(rest, principal, bindings, now, on_target, true)
799    }
800
801    /// As [`Inner::answer`]; without the canonical link when `link` is false (a batch's
802    /// envelopes have no headers).
803    pub(crate) fn answer_with(
804        &self,
805        rest: &str,
806        principal: Option<&Principal>,
807        bindings: &dyn Bindings,
808        now: &str,
809        on_target: &mut dyn FnMut(&Target),
810        link: bool,
811    ) -> Reply {
812        let target = match call::resolve(rest, now, &self.config.aliases, bindings, false) {
813            Ok(t) => t,
814            Err(r) => return self.rejection(r, None),
815        };
816        on_target(&target);
817        let (refused, private) = self.policy(&target, principal);
818        let reply = match refused {
819            Some(r) => r,
820            None => self.answer_target(&target, now, private, link),
821        };
822        // What the request resolved is per request: the answers are built from cached bytes.
823        match &target.resolved {
824            Some(r) => envelope::with_resolved(reply, r),
825            None => reply,
826        }
827    }
828
829    /// Answers a resolved target as of its asof (or `now`).
830    pub(crate) fn answer_target(&self, target: &Target, now: &str, private: bool, link: bool) -> Reply {
831        let a = target.asof().unwrap_or(now);
832        let stored = target.bound.descriptor.lang == "stored";
833        let local = stored && !self.foreign.is_foreign(&target.cell.ns);
834        if local {
835            if let Some(info) = self.latest.get(target.cell_key(), a) {
836                let mut reply = Reply::json(200, info.ok.clone(), Cache::Immutable);
837                self.cache_rules(&mut reply, target, a, now, private, link);
838                return reply;
839            }
840        }
841        if let Err(e) = self.foreign.prepare(&target.cell, stored, target.asof()) {
842            return self.unavailable(target, e);
843        }
844        // Since 0.2.1: before computing another mesh's function, its publisher's key document
845        // (its declarations, SPEC 8.5), fetched again after its TTL.
846        if !stored {
847            if let Err(e) = self.foreign.ensure_publisher(&target.cell.ns) {
848                return self.unavailable(target, e);
849            }
850        }
851        let mut attempt = 0;
852        loop {
853            let r = self.engine.get_cell(&target.cell, Some(a));
854            let inval = self.foreign.take_invalidated();
855            for id in &inval {
856                self.version_added(id);
857            }
858            if !inval.is_empty() && attempt < 2 {
859                attempt += 1;
860                continue;
861            }
862            // A formula cell answered: the describe index learns of it (CONTRACT-1.8b H4).
863            if !stored && matches!(r, Ok(Answer::Receipt(_))) {
864                self.known.lock().unwrap_or_else(|p| p.into_inner()).note_computed(&target.cell);
865            }
866            return match r {
867                Ok(Answer::Receipt(bytes)) => {
868                    let Some(info) = self.receipt_info(&bytes) else {
869                        return envelope::error_answer_reply(ErrorCode::EvalUnavailable, Some(&target.bound.fn_hash));
870                    };
871                    if attempt < 2 {
872                        match self.foreign.refresh_deps(&info.hash, a) {
873                            Ok(true) => {
874                                for id in self.foreign.take_invalidated() {
875                                    self.version_added(&id);
876                                }
877                                attempt += 1;
878                                continue;
879                            }
880                            Ok(false) => {}
881                            Err(e) => return self.unavailable(target, e),
882                        }
883                    }
884                    if local && target.asof().is_none() {
885                        self.remember_latest(target, &info, a);
886                    }
887                    self.ok_reply(target, info, a, now, private, link)
888                }
889                Ok(Answer::NotCovered { fault }) => {
890                    let mut ans =
891                        envelope::with_fn_hash(datumesh_core::Answer::not_covered(), Some(&target.bound.fn_hash));
892                    if let Some(f) = fault {
893                        ans = ans.with_fault(f);
894                    }
895                    let mut reply = envelope::answer_reply(&ans, Cache::Public(0));
896                    self.cache_rules(&mut reply, target, a, now, private, link);
897                    if target.asof().is_some() {
898                        reply.cache = if private {
899                            Cache::Private
900                        } else {
901                            Cache::Public(self.config.ttl.latest_max_age.as_secs())
902                        };
903                    }
904                    reply
905                }
906                Ok(_) => envelope::error_answer_reply(ErrorCode::EvalUnavailable, Some(&target.bound.fn_hash)),
907                Err(EngineError::Unlicensed { cell_id, reason }) => {
908                    // Licence inheritance refuses the cell (SPEC 8.5, spec-0.4.3): `unlicensed`,
909                    // no new code. Not immutable: a declaration may come later.
910                    tracing::info!(target: "datumesh_http", cell_id = %cell_id, reason = reason.as_str(), "unlicensed");
911                    let a = envelope::with_fn_hash(datumesh_core::Answer::unlicensed(), Some(&target.bound.fn_hash));
912                    let cache =
913                        if private { Cache::Private } else { Cache::Public(self.config.ttl.latest_max_age.as_secs()) };
914                    envelope::answer_reply(&a, cache)
915                }
916                Err(EngineError::Cycle { path }) => {
917                    return self.cycle_reply(&path, Some(a), Some(&target.bound.fn_hash))
918                }
919                Err(e) => {
920                    let (bad, code) = engine_code(&e);
921                    tracing::info!(target: "datumesh_http", error = %e, code = code.as_str(), "engine");
922                    if code == ErrorCode::ForeignUnavailable {
923                        // A cross-mesh fetch inside the evaluation got no response (ForeignError::eval).
924                        let detail = match &e {
925                            EngineError::Invalid(c) => {
926                                c.detail().strip_prefix("cross-mesh: ").unwrap_or(c.detail()).to_string()
927                            }
928                            other => other.to_string(),
929                        };
930                        return self.unavailable(target, ForeignError { code, detail });
931                    }
932                    if bad {
933                        envelope::bad_request_reply(code)
934                    } else if code == ErrorCode::InputUnavailable {
935                        self.unavailable(target, ForeignError { code, detail: e.to_string() })
936                    } else {
937                        envelope::error_answer_reply(code, Some(&target.bound.fn_hash))
938                    }
939                }
940            };
941        }
942    }
943
944    fn cache_rules(&self, reply: &mut Reply, target: &Target, a: &str, now: &str, private: bool, link: bool) {
945        let latest = self.config.ttl.latest_max_age.as_secs();
946        reply.cache = if private {
947            Cache::Private
948        } else {
949            match target.asof() {
950                Some(x) if x <= now => Cache::Immutable,
951                Some(_) => Cache::Public(latest),
952                None => Cache::Public(latest),
953            }
954        };
955        if link && target.asof().is_none() {
956            reply.canonical = Some(format!("{}/cell/{}", self.config.origin(), target.rest_asof(a)));
957        }
958    }
959
960    /// The answer when something the mesh depends on failed: `stale` with the last receipt
961    /// it served for the cell, when it has one that answers the request; else `error`.
962    fn unavailable(&self, target: &Target, e: ForeignError) -> Reply {
963        tracing::info!(target: "datumesh_http", code = e.code.as_str(), detail = %e.detail, "unavailable");
964        // A suspended namespace (9.9, 5.5): the import is refused, so `not_covered` with its error,
965        // and not the last receipt served (a cell served before is served as before, not imported).
966        if e.code == crate::foreign::NAMESPACE_SUSPENDED {
967            if let Ok(a) = datumesh_core::Answer::not_covered().with_error(ErrorCode::NamespaceSuspended) {
968                return envelope::answer_reply(&envelope::with_fn_hash(a, Some(&target.bound.fn_hash)), Cache::NoStore);
969            }
970        }
971        let lg = self.last_good.lock().unwrap_or_else(|p| p.into_inner());
972        if let Some(g) = lg.get(&last_good_key(target)) {
973            if target.asof().is_none_or(|a| g.info.asof.as_str() <= a) {
974                let age = g.at.elapsed().as_secs();
975                let a = datumesh_core::Answer::stale(g.info.parsed.clone(), g.info.trust, age);
976                let a = envelope::with_fn_hash(a, Some(&g.info.parsed.cell().fn_hash));
977                return envelope::answer_reply(&a, Cache::NoStore);
978            }
979        }
980        if let Some(d) = e.foreign_detail() {
981            let a = envelope::with_fn_hash(datumesh_core::Answer::foreign_unavailable(d), Some(&target.bound.fn_hash));
982            return envelope::answer_reply(&a, Cache::NoStore);
983        }
984        envelope::error_answer_reply(e.code, Some(&target.bound.fn_hash))
985    }
986
987    /// The keys to verify `receipt` with: the documents of its signer and of the signers of
988    /// its inputs and slice members held in the store, from the cache or the store's
989    /// namespace records.
990    pub(crate) fn keys_for_receipt(&self, receipt: &[u8]) -> Keys {
991        let mut hosts = std::collections::BTreeSet::new();
992        if let Ok(v) = json::parse(receipt) {
993            if let Some(h) = v.get("computed_by").and_then(Value::as_str) {
994                hosts.insert(h.split('#').next().unwrap_or("").to_string());
995            }
996            if let Some(Value::Array(ins)) = v.get("inputs") {
997                let mut members = Vec::new();
998                for i in ins.iter().filter_map(Value::as_str).take(10_000) {
999                    if i.starts_with("slice:") {
1000                        if let Some(m) =
1001                            self.store.manifest(i).and_then(|m| datumesh_core::Manifest::from_json(&m).ok())
1002                        {
1003                            members.extend(m.members().iter().take(10_000).cloned());
1004                        }
1005                    } else {
1006                        members.push(i.to_string());
1007                    }
1008                }
1009                for h in members.iter().take(20_000) {
1010                    if let Some(r) = self.store.receipt(h).and_then(|b| Receipt::from_json(&b).ok()) {
1011                        hosts.insert(r.signer_host().to_string());
1012                    }
1013                }
1014            }
1015        }
1016        let mut keys = Keys::new();
1017        for h in hosts {
1018            if let Some(r) = self.keys.result(&h) {
1019                keys.insert_result(&h, r);
1020            } else if let Some(doc) = self.key_doc(&h) {
1021                keys.insert(doc);
1022            }
1023        }
1024        keys
1025    }
1026
1027    /// A host's key document: the cache's, else the store's namespace record.
1028    fn key_doc(&self, host: &str) -> Option<KeyDoc> {
1029        if let Some((d, _, _)) = self.keys.get(host) {
1030            return Some(d);
1031        }
1032        let ns = self.store.namespace(host).ok()??;
1033        keys_from_response(host, 200, Some(&ns.keydoc)).ok()
1034    }
1035}
1036
1037fn verified(r: datumesh_core::VerifyResult) -> Verified {
1038    let s = |v: Option<&str>| v.map(|x| Value::String(x.to_string())).unwrap_or(Value::Null);
1039    let body = Value::Object(vec![
1040        ("blob_checked".into(), Value::Bool(r.blob_checked)),
1041        ("code".into(), s(r.code.map(|c| c.as_str()))),
1042        ("computed_by".into(), s(r.computed_by.as_deref())),
1043        ("detail".into(), Value::String(r.detail.clone())),
1044        ("hash".into(), s(r.hash.as_deref())),
1045        ("level".into(), s(r.level.map(|l| l.as_str()))),
1046        ("ok".into(), Value::Bool(r.ok)),
1047        ("path".into(), Value::String(r.path.clone())),
1048    ]);
1049    Verified { body: body.to_jcs() }
1050}
1051
1052/// What `POST /verify` reports.
1053#[derive(Clone, Debug, PartialEq, Eq)]
1054pub struct Verified {
1055    /// The result as JSON (`ok`, `level`, `code`, `hash`, `computed_by`, `path`, `detail`).
1056    pub body: Vec<u8>,
1057}
1058
1059impl Mesh {
1060    /// A builder for a mesh on `store` with `config`.
1061    pub fn builder(config: Config, store: Arc<dyn Store + Send + Sync>) -> MeshBuilder {
1062        MeshBuilder {
1063            config,
1064            store,
1065            key: None,
1066            key_file: None,
1067            runtimes: Vec::new(),
1068            formula: true,
1069            clock: Arc::new(SystemClock),
1070            fuel: None,
1071            licence: None,
1072            memo_capacity: None,
1073        }
1074    }
1075
1076    /// The configuration.
1077    pub fn config(&self) -> &Config {
1078        &self.inner.config
1079    }
1080
1081    /// The engine.
1082    pub fn engine(&self) -> &Arc<Engine> {
1083        &self.inner.engine
1084    }
1085
1086    /// Forgets every memoised answer (the engine's memo and the mesh's), for a process that
1087    /// wrote versions without telling the mesh.
1088    pub fn clear_memo(&self) {
1089        self.inner.engine.clear_memo();
1090        self.inner.latest.clear();
1091        self.inner.slices.lock().unwrap_or_else(|p| p.into_inner()).clear();
1092    }
1093
1094    /// The cross-mesh resolver.
1095    pub fn foreign(&self) -> &Foreign {
1096        &self.inner.foreign
1097    }
1098
1099    /// The store.
1100    pub fn store(&self) -> &Arc<dyn Store + Send + Sync> {
1101        &self.inner.store
1102    }
1103
1104    /// The key documents the mesh holds for other hosts (fetched, or inserted here): what
1105    /// `POST /verify` verifies with, besides the store's namespace records.
1106    pub fn keys(&self) -> &KeyCache {
1107        &self.inner.keys
1108    }
1109
1110    /// Tells the engine that a stored version was added for `cell_id` outside the mesh.
1111    pub fn stored_version_added(&self, cell_id: &str) {
1112        self.inner.version_added(cell_id);
1113    }
1114
1115    /// The envelope for `rest` (a call or a cell URI's rest, query included, as received at
1116    /// `/cell/`), for `principal`. Blocking, and without a budget: what a `/cell` request
1117    /// gets when it finishes within its budget, or its job's result.
1118    pub fn cell(&self, rest: &str, principal: Option<&Principal>) -> Reply {
1119        let now = self.inner.now();
1120        self.inner.answer(rest, principal, &*self.inner, &now, &mut |_| {})
1121    }
1122
1123    /// The envelopes of a batch, in order (blocking, no budget).
1124    pub fn cells(&self, rests: &[String], principal: Option<&Principal>) -> Vec<Reply> {
1125        let now = self.inner.now();
1126        let memo = Memo::new(&self.inner);
1127        rests.iter().map(|r| self.inner.answer(r, principal, &memo, &now, &mut |_| {})).collect()
1128    }
1129
1130    /// Resolves a request to its cell, as `/cell` does (or to its slice, with `slice`).
1131    pub fn resolve(&self, rest: &str, slice: bool) -> Result<Arc<Target>, Rejection> {
1132        let now = self.inner.now();
1133        call::resolve(rest, &now, &self.inner.config.aliases, &*self.inner, slice)
1134    }
1135
1136    /// The describe answer (SPEC 16.8, 9.6) for `id`: a class name, an object's key encoding, an
1137    /// object id, a cell id or a short id (12 hex digits). A class or an object: the body is a
1138    /// `describe-class` or `describe-object` document, `200`; an id the mesh does not know, `404`;
1139    /// a short id naming more than one object or cell the mesh knows, `400` with
1140    /// `E_SHORT_ID_AMBIGUOUS`. A class hash is not an id (9.6). Blocking. Since 0.2.2.
1141    pub fn describe(&self, id: &str) -> Reply {
1142        self.inner.describe(id)
1143    }
1144
1145    /// `POST /whatif` (5.9): the scenario answer to the request's body (see [`crate::scenario`]).
1146    pub(crate) fn whatif(&self, body: &[u8], principal: Option<&Principal>) -> Reply {
1147        self.inner.whatif(body, principal)
1148    }
1149
1150    /// `POST /x/calc` (Build 2 Part 3, `calc`): see [`crate::calc`].
1151    pub(crate) fn calc(&self, body: &[u8], principal: Option<&Principal>) -> Reply {
1152        self.inner.calc(body, principal)
1153    }
1154
1155    /// Verifies a receipt (SPEC 10.1) with the mesh's keys; at level `recomputed` with the
1156    /// engine as the resolver unless `signed_only`; `blob` is content for step 7.7.
1157    pub fn verify(&self, receipt: &[u8], signed_only: bool, blob: Option<&[u8]>) -> Verified {
1158        let keys = self.inner.keys_for_receipt(receipt);
1159        let resolver: Option<&dyn datumesh_core::Resolver> =
1160            if signed_only { None } else { Some(&*self.inner.engine as &dyn datumesh_core::Resolver) };
1161        verified(verify(receipt, &keys, resolver, blob))
1162    }
1163
1164    /// Verifies a receipt with the mesh's keys and what the caller offers for step 7: a
1165    /// `recompute` object as SPEC 11 describes it (`descriptor`, `inputs`, `manifests`,
1166    /// `lineage`, `value`), the core's `MapResolver` over it. Without one, steps 1-6 only.
1167    pub fn verify_offered(&self, receipt: &[u8], recompute: Option<&Value>, blob: Option<&[u8]>) -> Verified {
1168        let mut keys = self.inner.keys_for_receipt(receipt);
1169        // The signers of the receipts offered for step 7 too.
1170        let mut hosts = std::collections::BTreeSet::new();
1171        for (_, r) in recompute.and_then(|rc| rc.get("inputs")).and_then(Value::as_object).unwrap_or(&[]) {
1172            if let Some(h) = r
1173                .as_str()
1174                .and_then(|t| json::parse(t.as_bytes()).ok())
1175                .and_then(|v| v.get("computed_by").and_then(Value::as_str).map(str::to_string))
1176            {
1177                hosts.insert(h.split('#').next().unwrap_or("").to_string());
1178            }
1179        }
1180        for h in hosts {
1181            if let Some(r) = self.inner.keys.result(&h) {
1182                keys.insert_result(&h, r);
1183            } else if let Some(doc) = self.inner.key_doc(&h) {
1184                keys.insert(doc);
1185            }
1186        }
1187        let Some(rc) = recompute else {
1188            return verified(verify(receipt, &keys, None, blob));
1189        };
1190        let mut m = datumesh_core::MapResolver::new();
1191        let cell = json::parse(receipt).ok().and_then(|r| r.get("cell").cloned());
1192        if let (Some(d), Some(h)) =
1193            (rc.get("descriptor"), cell.as_ref().and_then(|c| c.get("fn_hash")).and_then(Value::as_str))
1194        {
1195            m.insert_descriptor(h, d.to_jcs());
1196        }
1197        for (h, r) in rc.get("inputs").and_then(Value::as_object).unwrap_or(&[]) {
1198            m.insert_receipt(h.as_str(), r.as_str().unwrap_or("").as_bytes());
1199        }
1200        for (e, t) in rc.get("manifests").and_then(Value::as_object).unwrap_or(&[]) {
1201            m.insert_manifest(e.as_str(), t.as_str().unwrap_or("").as_bytes());
1202        }
1203        let value = rc.get("value").and_then(|v| datumesh_core::Scalar::from_json(&v.to_jcs()).ok());
1204        if let (Some(value), Some(c)) = (value, cell.as_ref().and_then(|c| datumesh_core::Cell::from_value(c).ok())) {
1205            m.insert_value(c.id(), value);
1206            if let Some(Value::Array(l)) = rc.get("lineage") {
1207                m.insert_lineage(c.id(), l.iter().filter_map(Value::as_str));
1208            }
1209        }
1210        verified(verify(receipt, &keys, Some(&m), blob))
1211    }
1212
1213    /// The mesh document (SPEC 9.6) this mesh serves: its URLs on the base URL when one is
1214    /// set ([`Config::base_url`]), else on `https://<host>`.
1215    pub fn mesh_document(&self) -> Vec<u8> {
1216        let o = self.inner.config.mesh_document_base();
1217        let ep: BTreeMap<&str, String> = [
1218            ("blob", format!("{o}/blob/{{sha256}}")),
1219            ("cell", format!("{o}/cell/{{cell}}")),
1220            ("cells", format!("{o}/cells")),
1221            ("x_bundles", format!("{o}/bundles")),
1222            ("x_calc", format!("{o}/x/calc")),
1223            ("x_interval", format!("{o}/x/interval/{{function}}")),
1224            ("describe", format!("{o}/describe/{{id}}")),
1225            ("descriptor", format!("{o}/descriptor/{{hash}}")),
1226            ("explain", format!("{o}/explain/{{cell}}")),
1227            ("find", format!("{o}/find")),
1228            ("jobs", format!("{o}/jobs/{{job}}")),
1229            ("manifest", format!("{o}/manifest/{{slice_id}}/{{members_hash}}")),
1230            ("receipt", format!("{o}/receipt/{{hash}}")),
1231            ("slice", format!("{o}/slice/{{cell}}")),
1232            ("verify", format!("{o}/verify")),
1233            ("whatif", format!("{o}/whatif")),
1234            ("versions", format!("{o}/versions/{{cell}}")),
1235        ]
1236        .into_iter()
1237        .collect();
1238        Value::Object(vec![
1239            ("bindings".into(), Value::String(format!("{o}/bindings"))),
1240            (
1241                "endpoints".into(),
1242                Value::Object(ep.into_iter().map(|(k, v)| (k.to_string(), Value::String(v))).collect()),
1243            ),
1244            ("host".into(), Value::String(self.inner.config.host.clone())),
1245            ("proto".into(), Value::String("datumesh/1".into())),
1246        ])
1247        .to_jcs()
1248    }
1249
1250    /// The key document this mesh serves: the store's namespace record for its host.
1251    pub fn key_document(&self) -> Option<Vec<u8>> {
1252        self.inner.store.namespace(&self.inner.config.host).ok().flatten().map(|n| n.keydoc)
1253    }
1254
1255    /// The Prometheus text of `/metrics`.
1256    pub fn metrics(&self) -> String {
1257        crate::http::metrics_text(&self.inner)
1258    }
1259
1260    /// The number of requests counted for a route template (e.g. `/cell/{cell}`).
1261    pub fn requests_of(&self, route: &str) -> u64 {
1262        self.inner.metrics.requests_of(route)
1263    }
1264
1265    /// The host of a function name (its first segment).
1266    pub fn host_of(name: &str) -> &str {
1267        host_of(name)
1268    }
1269
1270    /// How long the mesh has run.
1271    pub fn uptime(&self) -> Duration {
1272        self.inner.started.elapsed()
1273    }
1274}
1275
1276impl Inner {
1277    /// The class `name` is bound to as of `asof`, with its flattened descriptor (not noted for the
1278    /// describe index: see [`Bindings::class`]).
1279    pub(crate) fn class_binding(&self, name: &str, asof: &str) -> Result<Option<Arc<call::BoundClass>>, ErrorCode> {
1280        let store_failed = |e: datumesh_store::Error| {
1281            tracing::warn!(target: "datumesh_http", error = %e, "resolve_class_binding");
1282            ErrorCode::InputUnavailable
1283        };
1284        let Some(b) = self.store.resolve_class_binding(name, asof).map_err(store_failed)? else { return Ok(None) };
1285        let Some(bytes) = self.store.class(&b.class_hash).map_err(store_failed)? else {
1286            return Err(ErrorCode::InputUnavailable);
1287        };
1288        let d = datumesh_core::ClassDescriptor::from_json(&bytes).map_err(|_| ErrorCode::InputUnavailable)?;
1289        Ok(Some(Arc::new(call::BoundClass { class_hash: b.class_hash, since: b.tx, descriptor: Arc::new(d) })))
1290    }
1291}
1292
1293#[cfg(test)]
1294mod tests {
1295    use super::*;
1296    use std::sync::atomic::{AtomicUsize, Ordering};
1297
1298    /// A batch's chunks start together and ask for the same names: one store round trip per
1299    /// name and asof for the whole batch, not one per chunk.
1300    #[test]
1301    fn a_batch_looks_each_binding_up_once() {
1302        let seen = Seen::default();
1303        let lookups = AtomicUsize::new(0);
1304        let start = std::sync::Barrier::new(6);
1305        let look = || {
1306            lookups.fetch_add(1, Ordering::SeqCst);
1307            std::thread::sleep(Duration::from_millis(50));
1308            Ok(None)
1309        };
1310        std::thread::scope(|s| {
1311            for _ in 0..6 {
1312                s.spawn(|| {
1313                    start.wait();
1314                    assert!(matches!(seen.lookup("a.example/f", "2026-01-01T00:00:00Z", look), Ok(None)));
1315                });
1316            }
1317        });
1318        assert_eq!(lookups.load(Ordering::SeqCst), 1, "one lookup for six chunks");
1319        // Found: no lookup.
1320        assert!(matches!(seen.lookup("a.example/f", "2026-01-01T00:00:00Z", look), Ok(None)));
1321        assert_eq!(lookups.load(Ordering::SeqCst), 1);
1322        // Another asof, another name: looked up.
1323        assert!(matches!(seen.lookup("a.example/f", "2026-01-02T00:00:00Z", look), Ok(None)));
1324        assert!(matches!(seen.lookup("a.example/g", "2026-01-01T00:00:00Z", look), Ok(None)));
1325        assert_eq!(lookups.load(Ordering::SeqCst), 3);
1326        // A failed lookup is not kept.
1327        let fail = || Err(ErrorCode::InputUnavailable);
1328        assert!(matches!(seen.lookup("a.example/h", "2026-01-01T00:00:00Z", fail), Err(ErrorCode::InputUnavailable)));
1329        assert!(matches!(seen.lookup("a.example/h", "2026-01-01T00:00:00Z", look), Ok(None)));
1330        assert_eq!(lookups.load(Ordering::SeqCst), 4);
1331    }
1332}