chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Configuration management for CHIE node settings.
//!
//! This module provides centralized configuration with validation, defaults,
//! and builder patterns for easy construction.
//!
//! # Example
//!
//! ```rust
//! use chie_core::config::{NodeSettings, StorageSettings, NetworkSettings};
//!
//! // Use builder pattern
//! let settings = NodeSettings::builder()
//!     .storage(StorageSettings::default())
//!     .network(NetworkSettings::default())
//!     .build()
//!     .expect("Invalid configuration");
//!
//! println!("Max storage: {} GB", settings.storage.max_bytes_gb());
//! ```

use std::path::PathBuf;

/// Storage configuration settings.
#[derive(Debug, Clone)]
pub struct StorageSettings {
    /// Base path for storing content chunks.
    pub base_path: PathBuf,
    /// Maximum storage in bytes.
    pub max_bytes: u64,
    /// Minimum free space to maintain (bytes).
    pub min_free_bytes: u64,
    /// Enable tiered storage (SSD/HDD).
    pub enable_tiering: bool,
    /// SSD tier path (if tiering enabled).
    pub ssd_path: Option<PathBuf>,
    /// HDD tier path (if tiering enabled).
    pub hdd_path: Option<PathBuf>,
}

impl StorageSettings {
    /// Create storage settings with specified path and max bytes.
    #[inline]
    #[must_use]
    pub fn new(base_path: PathBuf, max_bytes: u64) -> Self {
        Self {
            base_path,
            max_bytes,
            min_free_bytes: 1024 * 1024 * 1024, // 1 GB
            enable_tiering: false,
            ssd_path: None,
            hdd_path: None,
        }
    }

    /// Get max storage in gigabytes.
    #[inline]
    #[must_use]
    pub const fn max_bytes_gb(&self) -> f64 {
        self.max_bytes as f64 / (1024.0 * 1024.0 * 1024.0)
    }

    /// Validate storage settings.
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.max_bytes == 0 {
            return Err(ConfigError::InvalidValue(
                "max_bytes must be greater than 0".into(),
            ));
        }

        if self.min_free_bytes >= self.max_bytes {
            return Err(ConfigError::InvalidValue(
                "min_free_bytes must be less than max_bytes".into(),
            ));
        }

        if self.enable_tiering && (self.ssd_path.is_none() || self.hdd_path.is_none()) {
            return Err(ConfigError::InvalidValue(
                "Tiering enabled but SSD/HDD paths not specified".into(),
            ));
        }

        Ok(())
    }
}

impl Default for StorageSettings {
    #[inline]
    fn default() -> Self {
        Self::new(
            PathBuf::from("./chie-data"),
            50 * 1024 * 1024 * 1024, // 50 GB
        )
    }
}

/// Network configuration settings.
#[derive(Debug, Clone)]
pub struct NetworkSettings {
    /// Maximum bandwidth in bytes per second.
    pub max_bandwidth_bps: u64,
    /// Maximum concurrent connections.
    pub max_connections: usize,
    /// Connection timeout in seconds.
    pub connection_timeout_secs: u64,
    /// Request timeout in seconds.
    pub request_timeout_secs: u64,
    /// Enable rate limiting.
    pub enable_rate_limiting: bool,
    /// Rate limit (requests per second).
    pub rate_limit_rps: f64,
}

impl NetworkSettings {
    /// Create network settings with specified bandwidth.
    #[inline]
    #[must_use]
    pub fn new(max_bandwidth_bps: u64) -> Self {
        Self {
            max_bandwidth_bps,
            max_connections: 100,
            connection_timeout_secs: 10,
            request_timeout_secs: 30,
            enable_rate_limiting: true,
            rate_limit_rps: 100.0,
        }
    }

    /// Get max bandwidth in Mbps.
    #[inline]
    #[must_use]
    pub const fn max_bandwidth_mbps(&self) -> f64 {
        (self.max_bandwidth_bps * 8) as f64 / (1024.0 * 1024.0)
    }

    /// Validate network settings.
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.max_bandwidth_bps == 0 {
            return Err(ConfigError::InvalidValue(
                "max_bandwidth_bps must be greater than 0".into(),
            ));
        }

        if self.max_connections == 0 {
            return Err(ConfigError::InvalidValue(
                "max_connections must be greater than 0".into(),
            ));
        }

        if self.rate_limit_rps < 0.0 {
            return Err(ConfigError::InvalidValue(
                "rate_limit_rps must be non-negative".into(),
            ));
        }

        Ok(())
    }
}

impl Default for NetworkSettings {
    #[inline]
    fn default() -> Self {
        Self::new(100 * 1024 * 1024 / 8) // 100 Mbps
    }
}

/// Coordinator configuration settings.
#[derive(Debug, Clone)]
pub struct CoordinatorSettings {
    /// Coordinator base URL.
    pub url: String,
    /// API key for authentication.
    pub api_key: Option<String>,
    /// Proof submission interval in seconds.
    pub proof_submit_interval_secs: u64,
    /// Batch size for proof submissions.
    pub proof_batch_size: usize,
    /// Enable automatic proof submission.
    pub auto_submit: bool,
}

impl CoordinatorSettings {
    /// Create coordinator settings with specified URL.
    #[inline]
    #[must_use]
    pub fn new(url: String) -> Self {
        Self {
            url,
            api_key: None,
            proof_submit_interval_secs: 60,
            proof_batch_size: 10,
            auto_submit: true,
        }
    }

    /// Validate coordinator settings.
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.url.is_empty() {
            return Err(ConfigError::InvalidValue(
                "Coordinator URL cannot be empty".into(),
            ));
        }

        if !self.url.starts_with("http://") && !self.url.starts_with("https://") {
            return Err(ConfigError::InvalidValue(
                "Coordinator URL must start with http:// or https://".into(),
            ));
        }

        if self.proof_batch_size == 0 {
            return Err(ConfigError::InvalidValue(
                "proof_batch_size must be greater than 0".into(),
            ));
        }

        Ok(())
    }
}

impl Default for CoordinatorSettings {
    #[inline]
    fn default() -> Self {
        Self::new("https://coordinator.chie.network".to_string())
    }
}

/// Performance tuning settings.
#[derive(Debug, Clone)]
pub struct PerformanceSettings {
    /// Enable chunk prefetching.
    pub enable_prefetch: bool,
    /// Prefetch cache size (number of chunks).
    pub prefetch_cache_size: usize,
    /// Enable content compression.
    pub enable_compression: bool,
    /// Enable content deduplication.
    pub enable_deduplication: bool,
    /// Garbage collection interval in seconds.
    pub gc_interval_secs: u64,
    /// Enable performance profiling.
    pub enable_profiling: bool,
}

impl PerformanceSettings {
    /// Validate performance settings.
    pub fn validate(&self) -> Result<(), ConfigError> {
        if self.prefetch_cache_size == 0 {
            return Err(ConfigError::InvalidValue(
                "prefetch_cache_size must be greater than 0".into(),
            ));
        }

        Ok(())
    }
}

impl Default for PerformanceSettings {
    #[inline]
    fn default() -> Self {
        Self {
            enable_prefetch: true,
            prefetch_cache_size: 100,
            enable_compression: true,
            enable_deduplication: true,
            gc_interval_secs: 3600, // 1 hour
            enable_profiling: false,
        }
    }
}

/// Complete node settings.
#[derive(Debug, Clone, Default)]
pub struct NodeSettings {
    /// Storage configuration.
    pub storage: StorageSettings,
    /// Network configuration.
    pub network: NetworkSettings,
    /// Coordinator configuration.
    pub coordinator: CoordinatorSettings,
    /// Performance configuration.
    pub performance: PerformanceSettings,
}

impl NodeSettings {
    /// Create a builder for node settings.
    #[inline]
    #[must_use]
    pub fn builder() -> NodeSettingsBuilder {
        NodeSettingsBuilder::default()
    }

    /// Validate all settings.
    pub fn validate(&self) -> Result<(), ConfigError> {
        self.storage.validate()?;
        self.network.validate()?;
        self.coordinator.validate()?;
        self.performance.validate()?;
        Ok(())
    }

    /// Load settings from environment variables.
    pub fn from_env() -> Result<Self, ConfigError> {
        let mut settings = Self::default();

        // Storage
        if let Ok(path) = std::env::var("CHIE_STORAGE_PATH") {
            settings.storage.base_path = PathBuf::from(path);
        }
        if let Ok(max_bytes) = std::env::var("CHIE_STORAGE_MAX_BYTES") {
            settings.storage.max_bytes = max_bytes
                .parse()
                .map_err(|_| ConfigError::InvalidValue("Invalid CHIE_STORAGE_MAX_BYTES".into()))?;
        }

        // Network
        if let Ok(bandwidth) = std::env::var("CHIE_MAX_BANDWIDTH_BPS") {
            settings.network.max_bandwidth_bps = bandwidth
                .parse()
                .map_err(|_| ConfigError::InvalidValue("Invalid CHIE_MAX_BANDWIDTH_BPS".into()))?;
        }

        // Coordinator
        if let Ok(url) = std::env::var("CHIE_COORDINATOR_URL") {
            settings.coordinator.url = url;
        }
        if let Ok(api_key) = std::env::var("CHIE_API_KEY") {
            settings.coordinator.api_key = Some(api_key);
        }

        settings.validate()?;
        Ok(settings)
    }
}

/// Builder for node settings.
#[derive(Debug, Default)]
pub struct NodeSettingsBuilder {
    storage: Option<StorageSettings>,
    network: Option<NetworkSettings>,
    coordinator: Option<CoordinatorSettings>,
    performance: Option<PerformanceSettings>,
}

impl NodeSettingsBuilder {
    /// Set storage settings.
    #[inline]
    #[must_use]
    pub fn storage(mut self, storage: StorageSettings) -> Self {
        self.storage = Some(storage);
        self
    }

    /// Set network settings.
    #[inline]
    #[must_use]
    pub fn network(mut self, network: NetworkSettings) -> Self {
        self.network = Some(network);
        self
    }

    /// Set coordinator settings.
    #[inline]
    #[must_use]
    pub fn coordinator(mut self, coordinator: CoordinatorSettings) -> Self {
        self.coordinator = Some(coordinator);
        self
    }

    /// Set performance settings.
    #[inline]
    #[must_use]
    pub fn performance(mut self, performance: PerformanceSettings) -> Self {
        self.performance = Some(performance);
        self
    }

    /// Build the node settings.
    pub fn build(self) -> Result<NodeSettings, ConfigError> {
        let settings = NodeSettings {
            storage: self.storage.unwrap_or_default(),
            network: self.network.unwrap_or_default(),
            coordinator: self.coordinator.unwrap_or_default(),
            performance: self.performance.unwrap_or_default(),
        };

        settings.validate()?;
        Ok(settings)
    }
}

/// Configuration errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
    /// Invalid configuration value.
    InvalidValue(String),
    /// Missing required field.
    MissingField(String),
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidValue(msg) => write!(f, "Invalid configuration value: {}", msg),
            Self::MissingField(field) => write!(f, "Missing required field: {}", field),
        }
    }
}

impl std::error::Error for ConfigError {}

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

    #[test]
    fn test_storage_settings_default() {
        let settings = StorageSettings::default();
        assert_eq!(settings.max_bytes, 50 * 1024 * 1024 * 1024);
        assert!(settings.validate().is_ok());
    }

    #[test]
    fn test_storage_settings_validation() {
        let settings = StorageSettings {
            max_bytes: 0,
            ..Default::default()
        };
        assert!(settings.validate().is_err());

        let settings = StorageSettings {
            min_free_bytes: 50 * 1024 * 1024 * 1024 + 1,
            ..Default::default()
        };
        assert!(settings.validate().is_err());
    }

    #[test]
    fn test_network_settings_default() {
        let settings = NetworkSettings::default();
        assert_eq!(settings.max_connections, 100);
        assert!(settings.validate().is_ok());
    }

    #[test]
    fn test_network_settings_mbps() {
        let settings = NetworkSettings::new(100 * 1024 * 1024 / 8);
        assert_eq!(settings.max_bandwidth_mbps(), 100.0);
    }

    #[test]
    fn test_coordinator_settings_validation() {
        let settings = CoordinatorSettings {
            url: String::new(),
            ..Default::default()
        };
        assert!(settings.validate().is_err());

        let settings = CoordinatorSettings {
            url: "invalid-url".to_string(),
            ..Default::default()
        };
        assert!(settings.validate().is_err());
    }

    #[test]
    fn test_node_settings_builder() {
        let settings = NodeSettings::builder()
            .storage(StorageSettings::default())
            .network(NetworkSettings::default())
            .build();

        assert!(settings.is_ok());
    }

    #[test]
    fn test_node_settings_default() {
        let settings = NodeSettings::default();
        assert!(settings.validate().is_ok());
    }

    #[test]
    fn test_performance_settings_default() {
        let settings = PerformanceSettings::default();
        assert!(settings.enable_prefetch);
        assert!(settings.validate().is_ok());
    }

    #[test]
    fn test_config_error_display() {
        let err = ConfigError::InvalidValue("test".to_string());
        assert_eq!(err.to_string(), "Invalid configuration value: test");

        let err = ConfigError::MissingField("field1".to_string());
        assert_eq!(err.to_string(), "Missing required field: field1");
    }

    #[test]
    fn test_storage_tiering_validation() {
        let settings = StorageSettings {
            enable_tiering: true,
            ..Default::default()
        };
        assert!(settings.validate().is_err());

        let mut settings = settings;
        settings.ssd_path = Some(PathBuf::from("/ssd"));
        settings.hdd_path = Some(PathBuf::from("/hdd"));
        assert!(settings.validate().is_ok());
    }
}