thru-core 0.2.39

Shared implementation for the Thru CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Feature gate / chain param registry loading.
//!
//! The registry is the off-chain metadata layer: it gives account indices human
//! names, descriptions, lifecycle/status information, and value encodings. The
//! live values themselves are read from the on-chain global feature-gate account.

use crate::error::CliError;
use serde::Deserialize;
use std::collections::HashSet;
use std::path::Path;

// The default registry is bundled so installed CLI binaries do not depend on a
// source-checkout path. --registry remains available for local/dev overrides.
const DEFAULT_FEATURE_GATE_REGISTRY_TOML: &str =
    include_str!("../registry/feature-gates-registry.toml");

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureGateRegistryKind {
    FeatureGate,
    ChainParam,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureGateRegistryStatus {
    Reserved,
    PendingImplementation,
    Deployed,
    Armed,
    Activated,
    Deactivated,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureGateRegistryType {
    U8,
    U16,
    U32,
    U64,
    U128,
    U256,
    U512,
}

impl FeatureGateRegistryType {
    pub fn width_bytes(&self) -> usize {
        match self {
            FeatureGateRegistryType::U8 => 1,
            FeatureGateRegistryType::U16 => 2,
            FeatureGateRegistryType::U32 => 4,
            FeatureGateRegistryType::U64 => 8,
            FeatureGateRegistryType::U128 => 16,
            FeatureGateRegistryType::U256 => 32,
            FeatureGateRegistryType::U512 => 64,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureGateRegistryCategory {
    ConsensusCritical,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureGateRegistryEntry {
    pub index: u32,
    pub name: String,
    pub kind: FeatureGateRegistryKind,
    pub description: String,
    pub status: Option<FeatureGateRegistryStatus>,
    pub category: Option<FeatureGateRegistryCategory>,
    pub value_type: Option<FeatureGateRegistryType>,
    pub tracking: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureGateRegistry {
    entries: Vec<FeatureGateRegistryEntry>,
}

impl FeatureGateRegistry {
    pub fn entries(&self) -> &[FeatureGateRegistryEntry] {
        &self.entries
    }

    pub fn get_by_index(&self, index: u32) -> Option<&FeatureGateRegistryEntry> {
        self.entries
            .get(index as usize)
            .filter(|entry| entry.index == index)
    }

    pub fn get_by_name(&self, name: &str) -> Option<&FeatureGateRegistryEntry> {
        self.entries.iter().find(|entry| entry.name == name)
    }
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRegistry {
    entry: Vec<RawRegistryEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawRegistryEntry {
    index: u32,
    name: String,
    kind: String,
    description: String,
    status: Option<String>,
    category: Option<String>,
    #[serde(rename = "type")]
    value_type: Option<String>,
    tracking: String,
}

pub fn load_feature_gate_registry(path: Option<&Path>) -> Result<FeatureGateRegistry, CliError> {
    if let Some(path) = path {
        let content = std::fs::read_to_string(path).map_err(|err| {
            CliError::Io(std::io::Error::new(
                err.kind(),
                format!(
                    "failed to read feature gate registry {}: {}",
                    path.display(),
                    err
                ),
            ))
        })?;
        return parse_feature_gate_registry(&content);
    }

    parse_feature_gate_registry(DEFAULT_FEATURE_GATE_REGISTRY_TOML)
}

pub fn parse_feature_gate_registry(content: &str) -> Result<FeatureGateRegistry, CliError> {
    let raw: RawRegistry = toml::from_str(content).map_err(|err| {
        CliError::Validation(format!("invalid feature gate registry TOML: {}", err))
    })?;

    if raw.entry.is_empty() {
        return Err(CliError::Validation(
            "feature gate registry must contain at least one entry".to_string(),
        ));
    }

    let mut seen_names = HashSet::new();
    let mut entries = Vec::with_capacity(raw.entry.len());

    for (position, raw_entry) in raw.entry.into_iter().enumerate() {
        // Registry order is the canonical on-chain table order.  Requiring
        // index == position prevents holes and accidental index reuse.
        let expected_index = u32::try_from(position).map_err(|_| {
            CliError::Validation("feature gate registry has too many entries".to_string())
        })?;
        if raw_entry.index != expected_index {
            return Err(CliError::Validation(format!(
                "feature gate registry entry '{}' has index {}, expected {}",
                raw_entry.name, raw_entry.index, expected_index
            )));
        }

        validate_non_empty("name", &raw_entry.name)?;
        validate_registry_name(&raw_entry.name)?;
        validate_non_empty("description", &raw_entry.description)?;
        validate_non_empty("tracking", &raw_entry.tracking)?;

        if !seen_names.insert(raw_entry.name.clone()) {
            return Err(CliError::Validation(format!(
                "duplicate feature gate registry name '{}'",
                raw_entry.name
            )));
        }

        let kind = parse_kind(&raw_entry.kind)?;
        // Feature gates and chain params share the same account table, but their
        // metadata fields are intentionally disjoint.
        let (status, category, value_type) = match kind {
            FeatureGateRegistryKind::FeatureGate => {
                if raw_entry.category.is_some() || raw_entry.value_type.is_some() {
                    return Err(CliError::Validation(format!(
                        "feature gate registry entry '{}' must not set category or type",
                        raw_entry.name
                    )));
                }
                let status = raw_entry.status.as_deref().ok_or_else(|| {
                    CliError::Validation(format!(
                        "feature gate registry entry '{}' is missing status",
                        raw_entry.name
                    ))
                })?;
                (Some(parse_status(status)?), None, None)
            }
            FeatureGateRegistryKind::ChainParam => {
                if raw_entry.status.is_some() {
                    return Err(CliError::Validation(format!(
                        "chain param registry entry '{}' must not set status",
                        raw_entry.name
                    )));
                }
                let category = raw_entry.category.as_deref().ok_or_else(|| {
                    CliError::Validation(format!(
                        "chain param registry entry '{}' is missing category",
                        raw_entry.name
                    ))
                })?;
                let value_type = raw_entry.value_type.as_deref().ok_or_else(|| {
                    CliError::Validation(format!(
                        "chain param registry entry '{}' is missing type",
                        raw_entry.name
                    ))
                })?;
                (
                    None,
                    Some(parse_category(category)?),
                    Some(parse_type(value_type)?),
                )
            }
        };

        entries.push(FeatureGateRegistryEntry {
            index: raw_entry.index,
            name: raw_entry.name,
            kind,
            description: raw_entry.description,
            status,
            category,
            value_type,
            tracking: raw_entry.tracking,
        });
    }

    Ok(FeatureGateRegistry { entries })
}

fn validate_non_empty(field: &str, value: &str) -> Result<(), CliError> {
    if value.trim().is_empty() {
        return Err(CliError::Validation(format!(
            "feature gate registry field '{}' must not be empty",
            field
        )));
    }
    Ok(())
}

fn validate_registry_name(name: &str) -> Result<(), CliError> {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return Err(CliError::Validation(
            "feature gate registry name must not be empty".to_string(),
        ));
    };
    if !first.is_ascii_lowercase() {
        return Err(CliError::Validation(format!(
            "feature gate registry name '{}' must start with a lowercase letter",
            name
        )));
    }
    if !chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {
        return Err(CliError::Validation(format!(
            "feature gate registry name '{}' must contain only lowercase letters, digits, or underscores",
            name
        )));
    }
    Ok(())
}

fn parse_kind(kind: &str) -> Result<FeatureGateRegistryKind, CliError> {
    match kind {
        "feature-gate" => Ok(FeatureGateRegistryKind::FeatureGate),
        "chain-param" => Ok(FeatureGateRegistryKind::ChainParam),
        other => Err(CliError::Validation(format!(
            "unsupported feature gate registry kind '{}'",
            other
        ))),
    }
}

fn parse_status(status: &str) -> Result<FeatureGateRegistryStatus, CliError> {
    match status {
        "reserved" => Ok(FeatureGateRegistryStatus::Reserved),
        "pending-implementation" => Ok(FeatureGateRegistryStatus::PendingImplementation),
        "deployed" => Ok(FeatureGateRegistryStatus::Deployed),
        "armed" => Ok(FeatureGateRegistryStatus::Armed),
        "activated" => Ok(FeatureGateRegistryStatus::Activated),
        "deactivated" => Ok(FeatureGateRegistryStatus::Deactivated),
        other => Err(CliError::Validation(format!(
            "unsupported feature gate registry status '{}'",
            other
        ))),
    }
}

fn parse_category(category: &str) -> Result<FeatureGateRegistryCategory, CliError> {
    match category {
        "consensus-critical" => Ok(FeatureGateRegistryCategory::ConsensusCritical),
        other => Err(CliError::Validation(format!(
            "unsupported feature gate registry category '{}'",
            other
        ))),
    }
}

fn parse_type(value_type: &str) -> Result<FeatureGateRegistryType, CliError> {
    match value_type {
        "u8" => Ok(FeatureGateRegistryType::U8),
        "u16" => Ok(FeatureGateRegistryType::U16),
        "u32" => Ok(FeatureGateRegistryType::U32),
        "u64" => Ok(FeatureGateRegistryType::U64),
        "u128" => Ok(FeatureGateRegistryType::U128),
        "u256" => Ok(FeatureGateRegistryType::U256),
        "u512" => Ok(FeatureGateRegistryType::U512),
        other => Err(CliError::Validation(format!(
            "unsupported feature gate registry type '{}'",
            other
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const VALID_REGISTRY: &str = r#"
[[entry]]
index = 0
name = "parallel_exec"
kind = "feature-gate"
description = "Reserved binary feature gate used by genesis tests."
status = "reserved"
tracking = "UNTO-1818"

[[entry]]
index = 1
name = "max_compute_units_per_txn"
kind = "chain-param"
description = "Reserved consensus-critical runtime parameter encoded as a u64."
category = "consensus-critical"
type = "u64"
tracking = "UNTO-1818"
"#;

    #[test]
    fn parses_valid_registry() {
        let registry = parse_feature_gate_registry(VALID_REGISTRY).expect("valid registry");

        assert_eq!(registry.entries().len(), 2);
        assert_eq!(registry.get_by_index(0).unwrap().name, "parallel_exec");
        assert_eq!(
            registry
                .get_by_name("max_compute_units_per_txn")
                .unwrap()
                .kind,
            FeatureGateRegistryKind::ChainParam
        );
        assert_eq!(
            registry
                .get_by_name("max_compute_units_per_txn")
                .unwrap()
                .value_type,
            Some(FeatureGateRegistryType::U64)
        );
    }

    #[test]
    fn loads_bundled_default_registry() {
        let registry = load_feature_gate_registry(None).expect("bundled registry parses");

        assert!(!registry.entries().is_empty());
    }

    #[test]
    fn rejects_non_monotonic_index() {
        let content = VALID_REGISTRY.replace("index = 1", "index = 2");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("expected 1"));
    }

    #[test]
    fn rejects_duplicate_name() {
        let content = VALID_REGISTRY.replace("max_compute_units_per_txn", "parallel_exec");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("duplicate"));
    }

    #[test]
    fn rejects_invalid_registry_name() {
        let content = VALID_REGISTRY.replace("parallel_exec", "Parallel-Exec");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("lowercase"));
    }

    #[test]
    fn accepts_documented_feature_gate_statuses() {
        for status in [
            "reserved",
            "pending-implementation",
            "deployed",
            "armed",
            "activated",
            "deactivated",
        ] {
            let content = VALID_REGISTRY
                .replace("status = \"reserved\"", &format!("status = \"{}\"", status));
            parse_feature_gate_registry(&content).unwrap_or_else(|err| {
                panic!("status {} should parse: {}", status, err);
            });
        }
    }

    #[test]
    fn rejects_feature_gate_without_status() {
        let content = VALID_REGISTRY.replace("status = \"reserved\"\n", "");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("missing status"));
    }

    #[test]
    fn rejects_chain_param_without_category() {
        let content = VALID_REGISTRY.replace("category = \"consensus-critical\"\n", "");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("missing category"));
    }

    #[test]
    fn accepts_documented_chain_param_types() {
        for (value_type, width) in [
            ("u8", 1),
            ("u16", 2),
            ("u32", 4),
            ("u64", 8),
            ("u128", 16),
            ("u256", 32),
            ("u512", 64),
        ] {
            let content =
                VALID_REGISTRY.replace("type = \"u64\"", &format!("type = \"{}\"", value_type));
            let registry = parse_feature_gate_registry(&content).unwrap_or_else(|err| {
                panic!("type {} should parse: {}", value_type, err);
            });
            assert_eq!(
                registry
                    .get_by_name("max_compute_units_per_txn")
                    .unwrap()
                    .value_type
                    .as_ref()
                    .unwrap()
                    .width_bytes(),
                width
            );
        }
    }

    #[test]
    fn rejects_unsupported_chain_param_type() {
        let content = VALID_REGISTRY.replace("type = \"u64\"", "type = \"i64\"");
        let err = parse_feature_gate_registry(&content).unwrap_err();

        assert!(err.to_string().contains("unsupported"));
    }
}