aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / store / genesis.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Canonical System Store v1 genesis units (ADR 0015 ยง5). |
| 17 | |
| 18 | use alloc::vec::Vec; |
| 19 | |
| 20 | use super::v1::{ |
| 21 | object_unit_count, Catalog, CommitRecord, ObjectId, Superblock, OBJECT_KIND_CATALOG, |
| 22 | OBJECT_VERSION_V1, STORE_UNIT_BYTES, |
| 23 | }; |
| 24 | |
| 25 | /// Units 0..4 of a freshly formatted Store: Superblock A, an all-zero slot B |
| 26 | /// (so the first transaction exercises the "inactive slot initially zero" |
| 27 | /// case), the empty catalog at unit 2 and the genesis CommitRecord at unit 3. |
| 28 | pub fn genesis_units(store_uuid: [u8; 16], region_units: u64) -> [[u8; STORE_UNIT_BYTES]; 4] { |
| 29 | let catalog = Catalog { |
| 30 | entries: Vec::new(), |
| 31 | }; |
| 32 | let catalog_bytes = catalog.encode().expect("empty catalog encodes"); |
| 33 | let catalog_id = ObjectId::calculate(OBJECT_KIND_CATALOG, OBJECT_VERSION_V1, &catalog_bytes) |
| 34 | .expect("catalog id"); |
| 35 | let catalog_units = object_unit_count(catalog_bytes.len() as u64).expect("catalog units"); |
| 36 | let catalog_first = 2u64; |
| 37 | let commit_unit = catalog_first + u64::from(catalog_units); |
| 38 | let high_water = commit_unit + 1; |
| 39 | |
| 40 | let commit = CommitRecord { |
| 41 | store_uuid, |
| 42 | region_units, |
| 43 | generation: 1, |
| 44 | previous_generation: 0, |
| 45 | previous_commit_id: ObjectId([0u8; 32]), |
| 46 | previous_catalog_id: ObjectId([0u8; 32]), |
| 47 | catalog_id, |
| 48 | catalog_first_unit: catalog_first, |
| 49 | catalog_byte_length: catalog_bytes.len() as u64, |
| 50 | catalog_unit_count: catalog_units, |
| 51 | catalog_entry_count: 0, |
| 52 | committed_high_water: high_water, |
| 53 | }; |
| 54 | let commit_bytes = commit.encode().expect("genesis commit encodes"); |
| 55 | let commit_id = commit.object_id().expect("commit id"); |
| 56 | |
| 57 | let superblock = Superblock { |
| 58 | store_uuid, |
| 59 | slot_id: 0, |
| 60 | region_units, |
| 61 | generation: 1, |
| 62 | commit_record_id: commit_id, |
| 63 | commit_record_unit: commit_unit, |
| 64 | catalog_id, |
| 65 | catalog_first_unit: catalog_first, |
| 66 | catalog_byte_length: catalog_bytes.len() as u64, |
| 67 | catalog_unit_count: catalog_units, |
| 68 | catalog_entry_count: 0, |
| 69 | committed_high_water: high_water, |
| 70 | }; |
| 71 | |
| 72 | let mut out = [[0u8; STORE_UNIT_BYTES]; 4]; |
| 73 | out[0].copy_from_slice(&superblock.encode().expect("genesis superblock encodes")); |
| 74 | out[2][..catalog_bytes.len()].copy_from_slice(&catalog_bytes); |
| 75 | out[3][..commit_bytes.len()].copy_from_slice(&commit_bytes); |
| 76 | out |
| 77 | } |