rialo_feature_management_interface/lib.rs
1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Feature Management Program Interface
5//!
6//! This program provides a system-level mechanism for managing feature flags
7//! with time-based activation and deactivation schedules.
8
9#![no_std]
10
11extern crate alloc;
12
13use alloc::vec::Vec;
14
15use rialo_s_pubkey::Pubkey;
16
17pub mod error;
18pub mod instruction;
19pub mod state;
20
21rialo_s_pubkey::declare_id!("Feature1111111111111111111111111111111111111");
22
23/// Storage account PDA seed for the features map
24pub const STORAGE_ACCOUNT_SEED: &[u8] = b"features_storage";
25
26/// Derive the storage account address
27pub fn get_storage_account_address() -> (rialo_s_pubkey::Pubkey, u8) {
28 rialo_s_pubkey::Pubkey::find_program_address(&[STORAGE_ACCOUNT_SEED], &id())
29}
30
31/// Maximum length for a feature name
32pub const MAX_FEATURE_NAME_LENGTH: usize = 512;
33
34/// Maximum number of features allowed in the system
35pub const MAX_FEATURE_COUNT: usize = 10000;
36
37/// Maximum total size of serialized features state (100 KB)
38pub const MAX_FEATURES_STATE_SIZE: usize = 100 * 1024;
39
40/// Validates a feature name
41///
42/// Feature names must:
43/// - Not be empty
44/// - Not exceed MAX_FEATURE_NAME_LENGTH
45/// - Contain only alphanumeric characters, underscores, and hyphens
46/// - Not start or end with whitespace
47pub fn validate_feature_name(name: &str) -> bool {
48 !name.is_empty()
49 && name.len() <= MAX_FEATURE_NAME_LENGTH
50 && name
51 .chars()
52 .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
53 && !name.starts_with(char::is_whitespace)
54 && !name.ends_with(char::is_whitespace)
55}
56
57/// Create genesis storage data with initial authority
58///
59/// Creates the initial FeaturesState with the provided authority and serializes it
60/// for use in genesis configuration.
61///
62/// # Arguments
63/// * `authority` - The pubkey that will have authority over the feature management system
64///
65/// # Returns
66/// A `Vec<u8>` containing the serialized FeaturesState
67pub fn genesis_storage(authority: Pubkey) -> Vec<u8> {
68 use state::FeaturesState;
69
70 let state = FeaturesState::new(authority);
71 state
72 .serialize()
73 .expect("Failed to serialize genesis state")
74}