Skip to main content

rialo_s_program_runtime/
active_features.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Per-block view of currently active features.
5//!
6//! `ActiveFeatures` is the immutable snapshot the bank installs once per block
7//! after the subdag-fed clock update, and from which every hot-path gating site
8//! reads. The map value is the **current bank slot** at the moment the
9//! snapshot was computed; it is informational (debugging, metrics) and is not
10//! persisted — historical activation slots would require on-chain state the
11//! design intentionally avoids.
12//!
13//! This type lives in `rialo-s-program-runtime` so it can be threaded
14//! through `EnvironmentConfig` → `InvokeContext`, reachable by
15//! every builtin processor. The on-chain wire crate
16//! `feature-management-program-interface` stays `#![no_std]` and free of
17//! the `ahash` dependency.
18
19use ahash::AHashMap;
20use rialo_feature_management_interface::state::FeaturesState;
21
22/// Slot number alias used by the runtime view.
23///
24/// Aliased locally rather than imported from `rialo-s-clock` because the
25/// inherited Solana SDK is not the canonical source for this concept in
26/// Rialo's runtime.
27pub type Slot = u64;
28
29/// Immutable per-block snapshot of active features.
30///
31/// Built once per bank inside `execute_blob`, immediately after
32/// `update_clock`, and held under `Arc` so every consumer in the block sees
33/// the same view. There is no mid-block mutation, no lazy refresh, and no
34/// cross-block caching.
35#[derive(Clone, Debug, Default, PartialEq, Eq)]
36pub struct ActiveFeatures {
37    /// Active feature name → bank slot at snapshot time.
38    active: AHashMap<String, Slot>,
39}
40
41impl ActiveFeatures {
42    /// Empty snapshot — useful at genesis and in tests.
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Build the snapshot for a bank from `FeaturesState`. Every entry name
48    /// in the state is added to the snapshot, keyed by `snapshot_slot` (the
49    /// current bank slot — informational, not the historical activation
50    /// slot, which would require on-chain state the design intentionally
51    /// avoids).
52    ///
53    /// Activation is presence-based: a feature is active iff its name is
54    /// in `state.entries()`. No clock consult.
55    ///
56    /// This is the canonical producer the bank calls inside
57    /// `recompute_active_features`. Construction lives here (not in the
58    /// caller) so the producer surface stays in this crate; `from_map` can
59    /// remain `pub(crate)`.
60    pub fn from_features_state(state: &FeaturesState, snapshot_slot: Slot) -> Self {
61        let active: AHashMap<String, Slot> = state
62            .entries()
63            .iter()
64            .map(|name| (name.clone(), snapshot_slot))
65            .collect();
66        Self::from_map(active)
67    }
68
69    /// Snapshot from a pre-built map of the **complete** active set.
70    ///
71    /// This is the canonical producer used by `Bank::recompute_active_features`:
72    /// the bank reads `FeaturesState.entries` and hands the full map (name
73    /// → current bank slot) here. A feature absent from the map is
74    /// inactive — there is no "missing-means-default" fallback. Tests that
75    /// want sugar for "mark these names as active in an otherwise empty
76    /// snapshot" should use `with_overrides` instead.
77    ///
78    /// The strict `KNOWN_FEATURES` check lives in
79    /// `Bank::can_process_block` — running the same check
80    /// here would only fire on test fixtures that legitimately use
81    /// fabricated names. The producer paths
82    /// (`Self::from_features_state`, `with_overrides`) supply the names;
83    /// the readiness check is what halts a release node on an unknown one.
84    ///
85    /// `pub(crate)` so the only producer is `from_features_state` /
86    /// `with_overrides` inside this crate, matching the NORTHSTAR contract
87    /// "the only producer of `Arc<ActiveFeatures>` is the per-block
88    /// recomputation inside `execute_blob`." Cross-crate construction goes
89    /// through `from_features_state`, which takes already-validated
90    /// `FeaturesState` bytes.
91    pub(crate) fn from_map(active: AHashMap<String, Slot>) -> Self {
92        Self { active }
93    }
94
95    /// Test affordance — marks the named features as active in an
96    /// otherwise-empty snapshot.
97    ///
98    /// All overrides share the same snapshot slot `0`. Tests that need
99    /// distinct slots per feature should build the `AHashMap` themselves
100    /// and use `from_map`. Semantics intentionally differ from `from_map`:
101    /// `with_overrides` is "start from empty, add these"; `from_map` is
102    /// "this *is* the complete active set."
103    ///
104    /// Gated on `cfg(test)` or the `dev-context-only-utils` feature so the
105    /// affordance never reaches a release binary.
106    #[cfg(any(test, feature = "dev-context-only-utils"))]
107    pub fn with_overrides(features: &[&str]) -> Self {
108        let active: AHashMap<String, Slot> = features
109            .iter()
110            .map(|name| ((*name).to_string(), 0))
111            .collect();
112        Self::from_map(active)
113    }
114
115    /// Returns `true` if `feature` is active in this snapshot.
116    pub fn is_active(&self, feature: &str) -> bool {
117        self.active.contains_key(feature)
118    }
119
120    /// Returns the recorded snapshot slot for `feature`, if active.
121    ///
122    /// This is the bank slot at the moment the snapshot was computed, not
123    /// the historical activation slot.
124    pub fn snapshot_slot(&self, feature: &str) -> Option<Slot> {
125        self.active.get(feature).copied()
126    }
127
128    /// Number of active features in the snapshot.
129    pub fn len(&self) -> usize {
130        self.active.len()
131    }
132
133    /// `true` iff no features are active.
134    pub fn is_empty(&self) -> bool {
135        self.active.is_empty()
136    }
137
138    /// Iterator over `(feature_name, snapshot_slot)`.
139    pub fn iter(&self) -> impl Iterator<Item = (&str, Slot)> + '_ {
140        self.active.iter().map(|(k, v)| (k.as_str(), *v))
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    fn make(entries: &[(&str, Slot)]) -> ActiveFeatures {
149        let mut m = AHashMap::new();
150        for (k, v) in entries {
151            m.insert((*k).to_string(), *v);
152        }
153        ActiveFeatures::from_map(m)
154    }
155
156    #[test]
157    fn test_empty_is_empty() {
158        let a = ActiveFeatures::new();
159        assert!(a.is_empty());
160        assert_eq!(a.len(), 0);
161        assert!(!a.is_active("anything"));
162        assert_eq!(a.snapshot_slot("anything"), None);
163    }
164
165    #[test]
166    fn test_is_active_reflects_membership() {
167        let a = make(&[("foo", 42), ("bar", 100)]);
168        assert!(a.is_active("foo"));
169        assert!(a.is_active("bar"));
170        assert!(!a.is_active("baz"));
171    }
172
173    #[test]
174    fn test_snapshot_slot_returns_recorded_value() {
175        let a = make(&[("foo", 42)]);
176        assert_eq!(a.snapshot_slot("foo"), Some(42));
177        assert_eq!(a.snapshot_slot("missing"), None);
178    }
179
180    #[test]
181    fn test_iter_covers_all_entries() {
182        let a = make(&[("foo", 1), ("bar", 2)]);
183        let mut seen: Vec<(&str, Slot)> = a.iter().collect();
184        seen.sort_by_key(|(k, _)| *k);
185        assert_eq!(seen, vec![("bar", 2), ("foo", 1)]);
186    }
187
188    #[test]
189    fn test_with_overrides_marks_listed_features_active() {
190        let a = ActiveFeatures::with_overrides(&["foo", "bar"]);
191        assert!(a.is_active("foo"));
192        assert!(a.is_active("bar"));
193        assert!(!a.is_active("baz"));
194        assert_eq!(a.snapshot_slot("foo"), Some(0));
195    }
196
197    #[test]
198    fn test_with_overrides_empty_yields_empty() {
199        let a = ActiveFeatures::with_overrides(&[]);
200        assert!(a.is_empty());
201    }
202}