tenzro-tee 0.1.0

TEE abstraction layer for Tenzro Network — Intel TDX, AMD SEV-SNP, AWS Nitro, NVIDIA GPU, Intel Tiber Trust Authority
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
//! TEE provider registry for Tenzro Network.
//!
//! This module maintains a registry of all TEE providers participating in the
//! Tenzro Network, tracking their attestations, availability, and capacity.

use chrono::Duration;
use dashmap::DashMap;
use std::sync::Arc;
use uuid::Uuid;

use tenzro_types::tee::{TeeProviderInfo, TeeProviderStatus, TeeVendor, TeeCapacity, AttestationReport, TeeProviderPricing, AttestationResult};
use crate::error::{Result, TeeError};
use crate::traits::TeeProvider;

/// Registry for tracking TEE providers on the Tenzro Network.
///
/// The registry maintains:
/// - Provider information and attestations
/// - Health status and heartbeats
/// - Capacity metrics
/// - Attestation verification state
#[derive(Debug, Clone)]
pub struct TeeRegistry {
    /// Map of provider ID to provider info
    providers: Arc<DashMap<Uuid, TeeProviderInfo>>,
    /// Heartbeat timeout duration
    heartbeat_timeout: Duration,
}

impl TeeRegistry {
    /// Creates a new TEE provider registry.
    ///
    /// # Arguments
    /// - `heartbeat_timeout_secs`: Seconds before a provider is considered offline
    pub fn new(heartbeat_timeout_secs: i64) -> Self {
        Self {
            providers: Arc::new(DashMap::new()),
            heartbeat_timeout: Duration::seconds(heartbeat_timeout_secs),
        }
    }

    /// Registers a new TEE provider.
    ///
    /// # Arguments
    /// - `address`: Network address of the provider
    /// - `public_key`: Provider's public key
    /// - `attestation`: Initial attestation report
    /// - `capacity`: Provider's capacity metrics
    ///
    /// # Returns
    /// The provider ID
    pub fn register_provider(
        &self,
        address: String,
        public_key: Vec<u8>,
        attestation: AttestationReport,
        capacity: TeeCapacity,
    ) -> Result<Uuid> {
        let provider_id = Uuid::new_v4();
        let now = tenzro_types::Timestamp::now();

        let info = TeeProviderInfo {
            id: provider_id,
            vendor: attestation.vendor,
            address,
            public_key,
            attestation,
            registered_at: now,
            last_heartbeat: now,
            capacity,
            status: TeeProviderStatus::Active,
            attestation_result: None,
            pricing: TeeProviderPricing::default(),
            reputation: 0,
            total_jobs_completed: 0,
            last_attestation_update: now,
        };

        let vendor = info.vendor;
        self.providers.insert(provider_id, info);

        tracing::info!(
            "Registered TEE provider: {} (vendor: {:?})",
            provider_id,
            vendor
        );

        Ok(provider_id)
    }

    /// Unregisters a TEE provider.
    ///
    /// # Arguments
    /// - `provider_id`: The provider to unregister
    pub fn unregister_provider(&self, provider_id: &Uuid) -> Result<()> {
        self.providers
            .remove(provider_id)
            .ok_or_else(|| TeeError::ProviderNotFound(provider_id.to_string()))?;

        tracing::info!("Unregistered TEE provider: {}", provider_id);
        Ok(())
    }

    /// Updates a provider's attestation.
    ///
    /// # Arguments
    /// - `provider_id`: The provider to update
    /// - `attestation`: New attestation report
    pub fn update_attestation(
        &self,
        provider_id: &Uuid,
        attestation: AttestationReport,
    ) -> Result<()> {
        let mut provider = self
            .providers
            .get_mut(provider_id)
            .ok_or_else(|| TeeError::ProviderNotFound(provider_id.to_string()))?;

        provider.attestation = attestation;
        provider.last_heartbeat = tenzro_types::Timestamp::now();

        tracing::debug!("Updated attestation for provider: {}", provider_id);
        Ok(())
    }

    /// Records a heartbeat from a provider.
    ///
    /// # Arguments
    /// - `provider_id`: The provider sending the heartbeat
    /// - `capacity`: Updated capacity metrics
    pub fn heartbeat(&self, provider_id: &Uuid, capacity: TeeCapacity) -> Result<()> {
        let mut provider = self
            .providers
            .get_mut(provider_id)
            .ok_or_else(|| TeeError::ProviderNotFound(provider_id.to_string()))?;

        provider.last_heartbeat = tenzro_types::Timestamp::now();
        provider.capacity = capacity;

        // If provider was offline, mark it active
        if provider.status == TeeProviderStatus::Offline {
            provider.status = TeeProviderStatus::Active;
            tracing::info!("Provider {} is back online", provider_id);
        }

        Ok(())
    }

    /// Updates a provider's status.
    ///
    /// # Arguments
    /// - `provider_id`: The provider to update
    /// - `status`: New status
    pub fn update_status(&self, provider_id: &Uuid, status: TeeProviderStatus) -> Result<()> {
        let mut provider = self
            .providers
            .get_mut(provider_id)
            .ok_or_else(|| TeeError::ProviderNotFound(provider_id.to_string()))?;

        provider.status = status;

        tracing::info!("Provider {} status changed to: {:?}", provider_id, status);
        Ok(())
    }

    /// Gets information about a provider.
    pub fn get_provider(&self, provider_id: &Uuid) -> Option<TeeProviderInfo> {
        self.providers.get(provider_id).map(|p| p.clone())
    }

    /// Gets all registered providers.
    pub fn get_all_providers(&self) -> Vec<TeeProviderInfo> {
        self.providers.iter().map(|entry| entry.value().clone()).collect()
    }

    /// Gets all providers of a specific vendor.
    pub fn get_providers_by_vendor(&self, vendor: TeeVendor) -> Vec<TeeProviderInfo> {
        self.providers
            .iter()
            .filter(|entry| entry.value().vendor == vendor)
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Gets all active providers (accepting requests).
    pub fn get_active_providers(&self) -> Vec<TeeProviderInfo> {
        self.providers
            .iter()
            .filter(|entry| entry.value().status == TeeProviderStatus::Active)
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Gets providers with available capacity.
    ///
    /// # Arguments
    /// - `min_available_operations`: Minimum available operation slots required
    pub fn get_available_providers(&self, min_available_operations: u32) -> Vec<TeeProviderInfo> {
        self.providers
            .iter()
            .filter(|entry| {
                let info = entry.value();
                info.status == TeeProviderStatus::Active
                    && info.capacity.max_operations - info.capacity.current_operations
                        >= min_available_operations
            })
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Checks and updates status of providers based on heartbeat timeouts.
    ///
    /// Providers that haven't sent a heartbeat within the timeout period
    /// are marked as offline.
    ///
    /// # Returns
    /// Number of providers marked offline
    pub fn check_heartbeats(&self) -> usize {
        let now = tenzro_types::Timestamp::now();
        let timeout_millis = self.heartbeat_timeout.num_milliseconds();
        let timeout_threshold = tenzro_types::Timestamp::new(now.as_millis() - timeout_millis);
        let mut offline_count = 0;

        for mut entry in self.providers.iter_mut() {
            let provider = entry.value_mut();
            if provider.last_heartbeat < timeout_threshold
                && provider.status == TeeProviderStatus::Active
            {
                provider.status = TeeProviderStatus::Offline;
                offline_count += 1;
                tracing::warn!("Provider {} marked offline (no heartbeat)", provider.id);
            }
        }

        offline_count
    }

    /// Verifies a provider's attestation using the given verifier.
    ///
    /// If verification fails, marks the provider as untrusted.
    pub async fn verify_provider_attestation(
        &self,
        provider_id: &Uuid,
        verifier: &dyn TeeProvider,
    ) -> Result<AttestationResult> {
        let provider = self
            .providers
            .get(provider_id)
            .ok_or_else(|| TeeError::ProviderNotFound(provider_id.to_string()))?;

        let attestation = provider.attestation.clone();
        drop(provider); // Release the lock

        let result = verifier.verify_attestation(&attestation).await?;

        if !result.valid {
            self.update_status(provider_id, TeeProviderStatus::Untrusted)?;
            tracing::warn!("Provider {} failed attestation verification", provider_id);
        }

        Ok(result)
    }

    /// Returns statistics about the registry.
    pub fn stats(&self) -> RegistryStats {
        let mut stats = RegistryStats {
            total_providers: 0,
            active_providers: 0,
            draining_providers: 0,
            offline_providers: 0,
            untrusted_providers: 0,
            providers_by_vendor: std::collections::HashMap::new(),
        };

        for entry in self.providers.iter() {
            let provider = entry.value();
            stats.total_providers += 1;

            match provider.status {
                TeeProviderStatus::Active => stats.active_providers += 1,
                TeeProviderStatus::Draining => stats.draining_providers += 1,
                TeeProviderStatus::Offline => stats.offline_providers += 1,
                TeeProviderStatus::Untrusted => stats.untrusted_providers += 1,
                TeeProviderStatus::Pending
                | TeeProviderStatus::Inactive
                | TeeProviderStatus::Suspended
                | TeeProviderStatus::AttestationExpired => {
                    // These statuses are not tracked separately in stats
                }
            }

            *stats.providers_by_vendor.entry(provider.vendor).or_insert(0) += 1;
        }

        stats
    }
}

impl Default for TeeRegistry {
    fn default() -> Self {
        Self::new(300) // 5 minute default timeout
    }
}

/// Statistics about the TEE provider registry.
#[derive(Debug, Clone)]
pub struct RegistryStats {
    pub total_providers: usize,
    pub active_providers: usize,
    pub draining_providers: usize,
    pub offline_providers: usize,
    pub untrusted_providers: usize,
    pub providers_by_vendor: std::collections::HashMap<TeeVendor, usize>,
}

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

    fn create_test_attestation(vendor: TeeVendor) -> AttestationReport {
        AttestationReport {
            id: Uuid::new_v4(),
            vendor,
            user_data: vec![],
            attestation_data: vec![],
            certificates: vec![],
            timestamp: tenzro_types::Timestamp::now(),
            metadata: std::collections::HashMap::new(),
            quote: vec![0x01; 32],
            measurement: vec![0x02; 32],
            signature: vec![0x03; 64],
            vendor_data: vec![],
        }
    }

    fn create_test_capacity() -> TeeCapacity {
        TeeCapacity {
            max_concurrent_jobs: 10,
            active_jobs: 2,
            max_operations: 100,
            current_operations: 10,
            total_memory: 2 * 1024 * 1024 * 1024,
            available_memory: 1024 * 1024 * 1024,
            total_cpu_cores: 8,
            available_cpu_cores: 6,
            supported_vendors: vec![TeeVendor::IntelTdx],
        }
    }

    #[test]
    fn test_register_provider() {
        let registry = TeeRegistry::default();

        let id = registry
            .register_provider(
                "127.0.0.1:8080".to_string(),
                vec![0x01; 32],
                create_test_attestation(TeeVendor::IntelTdx),
                create_test_capacity(),
            )
            .unwrap();

        assert!(registry.get_provider(&id).is_some());
    }

    #[test]
    fn test_unregister_provider() {
        let registry = TeeRegistry::default();

        let id = registry
            .register_provider(
                "127.0.0.1:8080".to_string(),
                vec![0x01; 32],
                create_test_attestation(TeeVendor::IntelTdx),
                create_test_capacity(),
            )
            .unwrap();

        registry.unregister_provider(&id).unwrap();
        assert!(registry.get_provider(&id).is_none());
    }

    #[test]
    fn test_heartbeat() {
        let registry = TeeRegistry::default();

        let id = registry
            .register_provider(
                "127.0.0.1:8080".to_string(),
                vec![0x01; 32],
                create_test_attestation(TeeVendor::IntelTdx),
                create_test_capacity(),
            )
            .unwrap();

        let new_capacity = TeeCapacity {
            max_concurrent_jobs: 10,
            active_jobs: 5,
            max_operations: 100,
            current_operations: 50,
            total_memory: 2 * 1024 * 1024 * 1024,
            available_memory: 512 * 1024 * 1024,
            total_cpu_cores: 8,
            available_cpu_cores: 3,
            supported_vendors: vec![TeeVendor::IntelTdx],
        };

        registry.heartbeat(&id, new_capacity).unwrap();

        let provider = registry.get_provider(&id).unwrap();
        assert_eq!(provider.capacity.current_operations, 50);
    }

    #[test]
    fn test_get_active_providers() {
        let registry = TeeRegistry::default();

        let id1 = registry
            .register_provider(
                "127.0.0.1:8080".to_string(),
                vec![0x01; 32],
                create_test_attestation(TeeVendor::IntelTdx),
                create_test_capacity(),
            )
            .unwrap();

        let id2 = registry
            .register_provider(
                "127.0.0.1:8081".to_string(),
                vec![0x02; 32],
                create_test_attestation(TeeVendor::AmdSevSnp),
                create_test_capacity(),
            )
            .unwrap();

        registry.update_status(&id2, TeeProviderStatus::Offline).unwrap();

        let active = registry.get_active_providers();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].id, id1);
    }

    #[test]
    fn test_stats() {
        let registry = TeeRegistry::default();

        registry
            .register_provider(
                "127.0.0.1:8080".to_string(),
                vec![0x01; 32],
                create_test_attestation(TeeVendor::IntelTdx),
                create_test_capacity(),
            )
            .unwrap();

        registry
            .register_provider(
                "127.0.0.1:8081".to_string(),
                vec![0x02; 32],
                create_test_attestation(TeeVendor::AmdSevSnp),
                create_test_capacity(),
            )
            .unwrap();

        let stats = registry.stats();
        assert_eq!(stats.total_providers, 2);
        assert_eq!(stats.active_providers, 2);
    }
}