Skip to main content

ic_memory/runtime/
policy.rs

1use super::RuntimeBootstrapError;
2use crate::{
3    AllocationPolicy, MemoryManagerSlot, PolicyIdentity, PolicyIdentityError,
4    RuntimeBootstrapPolicy, StableKey, slot::IC_MEMORY_LEDGER_STABLE_KEY,
5};
6use std::convert::Infallible;
7
8pub(super) fn runtime_bootstrap_error_from_bootstrap<P>(
9    err: crate::BootstrapError<P>,
10) -> RuntimeBootstrapError<P> {
11    match err {
12        crate::BootstrapError::Ledger(err) => RuntimeBootstrapError::LedgerCommit(err),
13        crate::BootstrapError::Validation(err) => RuntimeBootstrapError::Validation(err),
14        crate::BootstrapError::Staging(err) => RuntimeBootstrapError::Staging(err),
15    }
16}
17
18// Resolution admits every external row under the host pool. This adapter only
19// keeps private governance outside application callbacks; it cannot accept raw
20// declarations from a public caller or supply a different allocation policy.
21pub(super) struct RuntimeMemoryManagerPolicy<'a, P> {
22    pub(super) custom_policy: &'a P,
23}
24
25impl<P: AllocationPolicy> AllocationPolicy for RuntimeMemoryManagerPolicy<'_, P> {
26    type Error = P::Error;
27
28    fn validate_key(&self, key: &StableKey) -> Result<(), Self::Error> {
29        if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
30            return Ok(());
31        }
32        self.custom_policy.validate_key(key)
33    }
34
35    fn validate_slot(&self, key: &StableKey, slot: &MemoryManagerSlot) -> Result<(), Self::Error> {
36        if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
37            return Ok(());
38        }
39        self.custom_policy.validate_slot(key, slot)
40    }
41}
42
43///
44/// GenericAllocationPolicy
45///
46/// Built-in bootstrap policy for hosts needing no additional application checks.
47/// The runtime enforces host namespace grants, the common pool and governance;
48/// this policy adds no application-specific restrictions. Passing it directly
49/// to allocation validation outside the runtime does not enforce host pool admission.
50///
51/// Use with configured bootstrap when the host does not require a custom
52/// policy. It retains the built-in policy identity and does not authorize
53/// replacing a different policy already bound to the runtime.
54///
55pub struct GenericAllocationPolicy;
56
57impl AllocationPolicy for GenericAllocationPolicy {
58    type Error = Infallible;
59
60    fn validate_key(&self, _key: &StableKey) -> Result<(), Self::Error> {
61        Ok(())
62    }
63
64    fn validate_slot(
65        &self,
66        _key: &StableKey,
67        _slot: &MemoryManagerSlot,
68    ) -> Result<(), Self::Error> {
69        Ok(())
70    }
71}
72
73impl RuntimeBootstrapPolicy for GenericAllocationPolicy {
74    fn runtime_bootstrap_identity(&self) -> Result<PolicyIdentity, PolicyIdentityError> {
75        PolicyIdentity::new("ic-memory.noop-policy", 1)
76    }
77}