use crate::{
declaration::{AllocationDeclaration, DeclarationSnapshot},
schema::SchemaMetadata,
slot::{
IC_MEMORY_AUTHORITY_OWNER, IC_MEMORY_LEDGER_LABEL, IC_MEMORY_LEDGER_STABLE_KEY,
MEMORY_MANAGER_LEDGER_ID, is_ic_memory_stable_key,
},
text::validate_diagnostic_text,
};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, sync::Arc};
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct MemoryRequest {
authority: String,
stable_key: crate::StableKey,
schema: SchemaMetadata,
}
impl<'de> Deserialize<'de> for MemoryRequest {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error as _;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Request {
authority: String,
stable_key: crate::StableKey,
schema: SchemaMetadata,
}
let request = Request::deserialize(deserializer)?;
Self::new(
request.authority,
request.stable_key.as_str(),
request.schema,
)
.map_err(D::Error::custom)
}
}
impl MemoryRequest {
pub fn new(
authority: impl Into<String>,
stable_key: &str,
schema: SchemaMetadata,
) -> Result<Self, MemoryDeclarationError> {
let authority = authority.into();
validate_external_authority(&authority)?;
let stable_key =
crate::StableKey::parse(stable_key).map_err(crate::DeclarationSnapshotError::Key)?;
if is_ic_memory_stable_key(stable_key.as_str()) {
return Err(MemoryDeclarationError::ReservedStableKey {
stable_key: stable_key.as_str().to_string(),
});
}
Ok(Self {
authority,
stable_key,
schema,
})
}
pub(crate) const fn with_schema(mut self, schema: SchemaMetadata) -> Self {
self.schema = schema;
self
}
#[must_use]
pub const fn stable_key(&self) -> &crate::StableKey {
&self.stable_key
}
#[must_use]
pub const fn schema(&self) -> &SchemaMetadata {
&self.schema
}
#[must_use]
pub fn authority(&self) -> &str {
&self.authority
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum MemoryDeclarationError {
#[error("at most 254 external requests are supported")]
TooManyDeclarations,
#[error("duplicate requested stable key {stable_key}")]
DuplicateRequest { stable_key: crate::StableKey },
#[error(transparent)]
Declaration(#[from] crate::DeclarationSnapshotError),
#[error("authority {reason}")]
InvalidAuthority {
reason: &'static str,
},
#[error("authority '{authority}' is reserved for ic-memory runtime internals")]
ReservedAuthority {
authority: String,
},
#[error("stable key '{stable_key}' is reserved for ic-memory runtime internals")]
ReservedStableKey {
stable_key: String,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SealedDeclarationSnapshot {
inner: Arc<SealedDeclarationSnapshotInner>,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SealedDeclarationFingerprint {
algorithm_version: u8,
value: u64,
}
impl SealedDeclarationFingerprint {
#[must_use]
pub const fn algorithm_version(&self) -> u8 {
self.algorithm_version
}
#[must_use]
pub const fn value(&self) -> u64 {
self.value
}
}
#[derive(Debug, Eq, PartialEq)]
struct SealedDeclarationSnapshotInner {
requests: Vec<MemoryRequest>,
fingerprint: SealedDeclarationFingerprint,
}
impl SealedDeclarationSnapshot {
pub fn new(mut requests: Vec<MemoryRequest>) -> Result<Self, MemoryDeclarationError> {
if requests.len() > 254 {
return Err(MemoryDeclarationError::TooManyDeclarations);
}
requests.sort_unstable_by(|a, b| a.stable_key.cmp(&b.stable_key));
for pair in requests.windows(2) {
if pair[0].stable_key == pair[1].stable_key {
return Err(MemoryDeclarationError::DuplicateRequest {
stable_key: pair[1].stable_key.clone(),
});
}
}
let fingerprint = sealed_declaration_fingerprint(&requests);
Ok(Self {
inner: Arc::new(SealedDeclarationSnapshotInner {
requests,
fingerprint,
}),
})
}
#[must_use]
pub fn requests(&self) -> &[MemoryRequest] {
&self.inner.requests
}
pub(crate) fn resolve(
&self,
ledger: &crate::AllocationLedger,
historical: Vec<MemoryRequest>,
pool: &crate::MemoryAllocationPool,
) -> Result<DeclarationSnapshot, crate::MemoryResolutionError> {
if self.requests().len() + historical.len() > 254 {
return Err(MemoryDeclarationError::TooManyDeclarations.into());
}
let mut declarations = Vec::with_capacity(self.requests().len() + historical.len() + 1);
declarations.push(internal_ledger_declaration());
let mut occupied = [false; 255];
for record in ledger.records() {
occupied[usize::from(record.slot().id())] = true;
}
for request in self
.requests()
.iter()
.map(Cow::Borrowed)
.chain(historical.into_iter().map(Cow::Owned))
{
let historical = ledger
.records()
.iter()
.find(|record| record.stable_key() == &request.stable_key);
pool.validate_authority(&request.stable_key, &request.authority)?;
let id = if let Some(record) = historical {
pool.validate_id(record.slot().id())?;
record.slot().id()
} else {
(0..crate::MEMORY_MANAGER_INVALID_ID)
.find(|id| pool.contains(*id) && !occupied[usize::from(*id)])
.ok_or_else(|| crate::MemoryResolutionError::Exhausted {
stable_key: request.stable_key.clone(),
authority: request.authority.clone(),
})?
};
let slot = crate::MemoryManagerSlot::new(id).expect("usable id");
occupied[usize::from(id)] = true;
let request = request.into_owned();
declarations.push(AllocationDeclaration {
stable_key: request.stable_key,
slot,
label: None,
schema: request.schema,
});
}
declarations.sort_unstable_by(|a, b| a.stable_key().cmp(b.stable_key()));
Ok(DeclarationSnapshot::new(declarations).map_err(MemoryDeclarationError::from)?)
}
#[must_use]
pub fn fingerprint(&self) -> SealedDeclarationFingerprint {
self.inner.fingerprint
}
#[cfg(test)]
pub(crate) fn shares_storage_with(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
fn validate_external_authority(value: &str) -> Result<(), MemoryDeclarationError> {
reject_internal_authority(value)?;
validate_diagnostic_text(value).map_err(|error| MemoryDeclarationError::InvalidAuthority {
reason: error.reason(),
})
}
fn reject_internal_authority(value: &str) -> Result<(), MemoryDeclarationError> {
if value == IC_MEMORY_AUTHORITY_OWNER {
return Err(MemoryDeclarationError::ReservedAuthority {
authority: value.to_string(),
});
}
Ok(())
}
#[derive(Serialize)]
struct SealedDeclarationFingerprintMaterial<'a> {
format: &'static str,
requests: &'a [MemoryRequest],
}
struct FingerprintWriter(u64);
impl std::io::Write for FingerprintWriter {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0 = crate::hash::fnv64(self.0, bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
fn sealed_declaration_fingerprint(requests: &[MemoryRequest]) -> SealedDeclarationFingerprint {
let material = SealedDeclarationFingerprintMaterial {
format: "ic-memory.sealed-declaration-fingerprint.v1",
requests,
};
let mut writer = FingerprintWriter(crate::hash::FNV_OFFSET);
ciborium::into_writer(&material, &mut writer)
.expect("sealed declaration fingerprint encodes into hash");
SealedDeclarationFingerprint {
algorithm_version: SEALED_DECLARATION_FINGERPRINT_VERSION,
value: writer.0,
}
}
const SEALED_DECLARATION_FINGERPRINT_VERSION: u8 = 1;
fn internal_ledger_declaration() -> AllocationDeclaration {
AllocationDeclaration::memory_manager(
IC_MEMORY_LEDGER_STABLE_KEY,
MEMORY_MANAGER_LEDGER_ID,
IC_MEMORY_LEDGER_LABEL,
)
.unwrap_or_else(|_| unreachable!("built-in ledger declaration constants are valid"))
}
#[cfg(test)]
mod tests;