exochain-catapult 0.2.0-beta

EXOCHAIN Catapult — franchise business incubator with FM 3-05 operational doctrine
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Franchise blueprints — immutable templates from which newcos are instantiated.

use std::collections::BTreeMap;

use exo_core::{Hash256, Timestamp, Version};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
    budget::BudgetTemplate,
    error::{CatapultError, Result},
    goal::GoalTemplate,
    oda::OdaSlot,
};

/// Domain tag for franchise blueprint content hashing.
pub const FRANCHISE_BLUEPRINT_HASH_DOMAIN: &str = "exo.catapult.franchise_blueprint.v1";
const FRANCHISE_BLUEPRINT_SCHEMA_VERSION: &str = "1.0.0";

/// The business model classification for a franchise.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum BusinessModel {
    SaaS,
    Marketplace,
    Agency,
    MediaPublisher,
    ConsultingFirm,
    Custom { description: String },
}

/// An immutable franchise blueprint — the template from which newcos are created.
///
/// Once published, a blueprint is content-addressed and cannot be modified.
/// New versions create new blueprints.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FranchiseBlueprint {
    pub id: Uuid,
    pub name: String,
    pub version: Version,
    pub description: String,
    pub business_model: BusinessModel,
    /// Hash of the constitutional corpus that governs newcos from this blueprint.
    pub constitution_hash: Hash256,
    /// Which ODA slots are mandatory for this franchise type.
    pub required_slots: Vec<OdaSlot>,
    /// Default budget configuration for newcos.
    pub budget_template: BudgetTemplate,
    /// Default goal structure for newcos.
    pub goal_template: GoalTemplate,
    /// When this blueprint was created.
    pub created: Timestamp,
    /// Content-address of the serialized blueprint.
    pub content_hash: Hash256,
}

/// Caller-supplied deterministic metadata for a franchise blueprint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FranchiseBlueprintInput {
    pub id: Uuid,
    pub name: String,
    pub version: Version,
    pub description: String,
    pub business_model: BusinessModel,
    pub constitution_hash: Hash256,
    pub required_slots: Vec<OdaSlot>,
    pub budget_template: BudgetTemplate,
    pub goal_template: GoalTemplate,
    pub created: Timestamp,
}

impl FranchiseBlueprint {
    /// Build a franchise blueprint from caller-supplied deterministic metadata.
    ///
    /// # Errors
    /// Returns [`CatapultError`] if the blueprint contains placeholder metadata
    /// or if canonical CBOR hashing fails.
    pub fn new(input: FranchiseBlueprintInput) -> Result<Self> {
        validate_blueprint_input(&input)?;
        let content_hash = franchise_blueprint_content_hash(&input)?;
        Ok(Self {
            id: input.id,
            name: input.name,
            version: input.version,
            description: input.description,
            business_model: input.business_model,
            constitution_hash: input.constitution_hash,
            required_slots: input.required_slots,
            budget_template: input.budget_template,
            goal_template: input.goal_template,
            created: input.created,
            content_hash,
        })
    }

    /// Recompute and compare this blueprint's canonical content hash.
    ///
    /// # Errors
    /// Returns [`CatapultError`] if the blueprint metadata is invalid or if
    /// canonical CBOR hashing fails.
    pub fn verify_content_hash(&self) -> Result<bool> {
        let expected = franchise_blueprint_content_hash(&self.input())?;
        Ok(self.content_hash == expected)
    }

    fn validate(&self) -> Result<()> {
        validate_blueprint_input(&self.input())?;
        if self.content_hash == Hash256::ZERO {
            return Err(CatapultError::InvalidFranchiseBlueprint {
                reason: "blueprint content hash must not be zero".into(),
            });
        }
        if !self.verify_content_hash()? {
            return Err(CatapultError::InvalidFranchiseBlueprint {
                reason: format!(
                    "blueprint {} content hash does not match canonical payload",
                    self.id
                ),
            });
        }
        Ok(())
    }

    fn input(&self) -> FranchiseBlueprintInput {
        FranchiseBlueprintInput {
            id: self.id,
            name: self.name.clone(),
            version: self.version,
            description: self.description.clone(),
            business_model: self.business_model.clone(),
            constitution_hash: self.constitution_hash,
            required_slots: self.required_slots.clone(),
            budget_template: self.budget_template.clone(),
            goal_template: self.goal_template.clone(),
            created: self.created,
        }
    }
}

/// Compute the canonical content hash for a franchise blueprint input.
///
/// # Errors
/// Returns [`CatapultError`] if the input is invalid or canonical CBOR hashing
/// fails.
pub fn franchise_blueprint_content_hash(input: &FranchiseBlueprintInput) -> Result<Hash256> {
    validate_blueprint_input(input)?;
    exo_core::hash::hash_structured(&FranchiseBlueprintHashPayload::from_input(input)).map_err(
        |e| CatapultError::InvalidFranchiseBlueprint {
            reason: format!("blueprint hash CBOR serialization failed: {e}"),
        },
    )
}

#[derive(Serialize)]
struct FranchiseBlueprintHashPayload<'a> {
    domain: &'static str,
    schema_version: &'static str,
    id: Uuid,
    name: &'a str,
    version: Version,
    description: &'a str,
    business_model: &'a BusinessModel,
    constitution_hash: Hash256,
    required_slots: &'a [OdaSlot],
    budget_template: &'a BudgetTemplate,
    goal_template: &'a GoalTemplate,
    created: Timestamp,
}

impl<'a> FranchiseBlueprintHashPayload<'a> {
    fn from_input(input: &'a FranchiseBlueprintInput) -> Self {
        Self {
            domain: FRANCHISE_BLUEPRINT_HASH_DOMAIN,
            schema_version: FRANCHISE_BLUEPRINT_SCHEMA_VERSION,
            id: input.id,
            name: &input.name,
            version: input.version,
            description: &input.description,
            business_model: &input.business_model,
            constitution_hash: input.constitution_hash,
            required_slots: &input.required_slots,
            budget_template: &input.budget_template,
            goal_template: &input.goal_template,
            created: input.created,
        }
    }
}

fn validate_blueprint_input(input: &FranchiseBlueprintInput) -> Result<()> {
    if input.id.is_nil() {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint id must be caller-supplied and non-nil".into(),
        });
    }
    if input.name.trim().is_empty() {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint name must not be empty".into(),
        });
    }
    if input.version == Version::ZERO {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint version must be greater than zero".into(),
        });
    }
    if input.description.trim().is_empty() {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint description must not be empty".into(),
        });
    }
    if let BusinessModel::Custom { description } = &input.business_model
        && description.trim().is_empty()
    {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "custom business-model description must not be empty".into(),
        });
    }
    if input.constitution_hash == Hash256::ZERO {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint constitution hash must not be zero".into(),
        });
    }
    if input.required_slots.is_empty() {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint required slots must not be empty".into(),
        });
    }
    let mut canonical_slots = input.required_slots.clone();
    canonical_slots.sort();
    canonical_slots.dedup();
    if canonical_slots != input.required_slots {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint required slots must be sorted and unique".into(),
        });
    }
    if input.created == Timestamp::ZERO {
        return Err(CatapultError::InvalidFranchiseBlueprint {
            reason: "blueprint created timestamp must be caller-supplied HLC".into(),
        });
    }
    Ok(())
}

/// In-memory registry of published franchise blueprints.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FranchiseRegistry {
    pub blueprints: BTreeMap<Uuid, FranchiseBlueprint>,
}

impl FranchiseRegistry {
    /// Create an empty franchise registry.
    #[must_use]
    pub fn new() -> Self {
        Self {
            blueprints: BTreeMap::new(),
        }
    }

    /// Publish a new franchise blueprint.
    pub fn publish(&mut self, blueprint: FranchiseBlueprint) -> Result<Uuid> {
        blueprint.validate()?;
        let id = blueprint.id;
        if self.blueprints.contains_key(&id) {
            return Err(CatapultError::FranchiseAlreadyExists(id));
        }
        self.blueprints.insert(id, blueprint);
        Ok(id)
    }

    /// Look up a blueprint by ID.
    #[must_use]
    pub fn get(&self, id: &Uuid) -> Option<&FranchiseBlueprint> {
        self.blueprints.get(id)
    }

    /// List all published blueprints.
    #[must_use]
    pub fn list(&self) -> Vec<&FranchiseBlueprint> {
        self.blueprints.values().collect()
    }

    /// Number of published blueprints.
    #[must_use]
    pub fn len(&self) -> usize {
        self.blueprints.len()
    }

    /// Whether the registry is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.blueprints.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{budget::BudgetTemplate, goal::GoalTemplate};

    fn test_uuid(byte: u8) -> Uuid {
        Uuid::from_bytes([byte; 16])
    }

    fn test_hash(label: &str) -> Hash256 {
        Hash256::digest(label.as_bytes())
    }

    fn test_timestamp() -> Timestamp {
        Timestamp {
            physical_ms: 1_765_000_000_000,
            logical: 7,
        }
    }

    fn test_blueprint_input() -> FranchiseBlueprintInput {
        FranchiseBlueprintInput {
            id: test_uuid(1),
            name: "Test SaaS Franchise".into(),
            version: Version::ZERO.next(),
            description: "A governed SaaS franchise blueprint".into(),
            business_model: BusinessModel::SaaS,
            constitution_hash: test_hash("constitution"),
            required_slots: OdaSlot::ALL.to_vec(),
            budget_template: BudgetTemplate::default(),
            goal_template: GoalTemplate::default(),
            created: test_timestamp(),
        }
    }

    fn test_blueprint() -> FranchiseBlueprint {
        FranchiseBlueprint::new(test_blueprint_input()).unwrap()
    }

    #[test]
    fn blueprint_new_computes_and_verifies_canonical_hash() {
        let blueprint = FranchiseBlueprint::new(test_blueprint_input()).unwrap();

        assert_ne!(blueprint.content_hash, Hash256::ZERO);
        assert!(blueprint.verify_content_hash().unwrap());

        let same = FranchiseBlueprint::new(test_blueprint_input()).unwrap();
        assert_eq!(blueprint.content_hash, same.content_hash);

        let mut changed = test_blueprint_input();
        changed.required_slots = vec![OdaSlot::HrPeopleOps1];
        let changed = FranchiseBlueprint::new(changed).unwrap();
        assert_ne!(blueprint.content_hash, changed.content_hash);
    }

    #[test]
    fn blueprint_rejects_placeholder_metadata() {
        let mut input = test_blueprint_input();
        input.id = Uuid::nil();
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.name = "   ".into();
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.version = Version::ZERO;
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.description = " ".into();
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.business_model = BusinessModel::Custom {
            description: " ".into(),
        };
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.constitution_hash = Hash256::ZERO;
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.required_slots = Vec::new();
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.required_slots = vec![OdaSlot::HrPeopleOps1, OdaSlot::DeepResearcher];
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.required_slots = vec![OdaSlot::HrPeopleOps1, OdaSlot::HrPeopleOps1];
        assert!(FranchiseBlueprint::new(input).is_err());

        let mut input = test_blueprint_input();
        input.created = Timestamp::ZERO;
        assert!(FranchiseBlueprint::new(input).is_err());
    }

    #[test]
    fn publish_rejects_tampered_or_placeholder_blueprint_hash() {
        let mut reg = FranchiseRegistry::new();
        let mut blueprint = test_blueprint();
        blueprint.content_hash = test_hash("tampered");

        assert!(reg.publish(blueprint).is_err());

        let mut blueprint = test_blueprint();
        blueprint.id = Uuid::nil();
        assert!(reg.publish(blueprint).is_err());

        let mut blueprint = test_blueprint();
        blueprint.content_hash = Hash256::ZERO;
        assert!(reg.publish(blueprint).is_err());
    }

    #[test]
    fn publish_and_get() {
        let mut reg = FranchiseRegistry::new();
        let bp = test_blueprint();
        let id = bp.id;
        reg.publish(bp).unwrap();
        assert_eq!(reg.len(), 1);
        assert!(reg.get(&id).is_some());
    }

    #[test]
    fn duplicate_rejected() {
        let mut reg = FranchiseRegistry::new();
        let bp = test_blueprint();
        let bp2 = bp.clone();
        reg.publish(bp).unwrap();
        assert!(reg.publish(bp2).is_err());
    }

    #[test]
    fn list() {
        let mut reg = FranchiseRegistry::new();
        reg.publish(test_blueprint()).unwrap();
        let mut second = test_blueprint_input();
        second.id = test_uuid(2);
        reg.publish(FranchiseBlueprint::new(second).unwrap())
            .unwrap();
        assert_eq!(reg.list().len(), 2);
    }

    #[test]
    fn empty() {
        let reg = FranchiseRegistry::new();
        assert!(reg.is_empty());
        assert_eq!(reg.len(), 0);
    }

    #[test]
    fn business_model_serde() {
        let models = [
            BusinessModel::SaaS,
            BusinessModel::Marketplace,
            BusinessModel::Agency,
            BusinessModel::MediaPublisher,
            BusinessModel::ConsultingFirm,
            BusinessModel::Custom {
                description: "test".into(),
            },
        ];
        for m in &models {
            let j = serde_json::to_string(m).unwrap();
            let rt: BusinessModel = serde_json::from_str(&j).unwrap();
            assert_eq!(&rt, m);
        }
    }

    #[test]
    fn blueprint_serde_roundtrip() {
        let bp = test_blueprint();
        let j = serde_json::to_string(&bp).unwrap();
        let rt: FranchiseBlueprint = serde_json::from_str(&j).unwrap();
        assert_eq!(rt.name, bp.name);
        assert_eq!(rt.business_model, bp.business_model);
    }
}