pocx_aggregator 1.0.3

High-performance mining aggregator for PoCX 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
// Copyright (c) 2025 Proof of Capacity Consortium
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;

// Re-export shared types from pocx_protocol for convenience
pub use pocx_protocol::{BasicAuthConfig, RpcAuth, RpcServerAuth, RpcTransport, SubmissionMode};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    /// Server configuration for downstream miner connections
    #[serde(default)]
    pub server: ServerConfig,

    /// Upstream pool or wallet configuration
    pub upstream: UpstreamConfig,

    /// Cache settings
    #[serde(default)]
    pub cache: CacheConfig,

    /// Database settings
    #[serde(default)]
    pub database: DatabaseConfig,

    /// Dashboard settings (optional)
    #[serde(default)]
    pub dashboard: Option<DashboardConfig>,

    /// Logging configuration
    #[serde(default)]
    pub logging: LoggingConfig,
}

/// Server configuration for downstream connections (miners connecting to aggregator).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// Address to listen on for miner connections
    #[serde(default = "default_listen_address")]
    pub listen_address: String,

    /// Optional authentication for downstream connections
    #[serde(default)]
    pub auth: RpcServerAuth,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            listen_address: default_listen_address(),
            auth: RpcServerAuth::default(),
        }
    }
}

/// Upstream configuration - aligned with miner's Chain config style.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpstreamConfig {
    /// Pool/wallet name
    pub name: String,

    /// Transport protocol (http, https)
    #[serde(default)]
    pub rpc_transport: RpcTransport,

    /// RPC host address
    #[serde(default = "default_rpc_host")]
    pub rpc_host: String,

    /// RPC port
    #[serde(default = "default_rpc_port")]
    pub rpc_port: u16,

    /// Authentication configuration
    #[serde(default)]
    pub rpc_auth: RpcAuth,

    /// Submission mode: Pool (per-account best) or Wallet (global best)
    #[serde(default)]
    pub submission_mode: SubmissionMode,

    /// Expected block time in seconds (used for capacity estimation and poc_time)
    #[serde(default = "default_block_time")]
    pub block_time_secs: u64,
}

impl UpstreamConfig {
    /// Build URL for HTTP/HTTPS transport.
    pub fn build_url(&self) -> Option<String> {
        match self.rpc_transport {
            RpcTransport::Http => Some(format!("http://{}:{}", self.rpc_host, self.rpc_port)),
            RpcTransport::Https => Some(format!("https://{}:{}", self.rpc_host, self.rpc_port)),
        }
    }

    /// Get endpoint description for logging.
    pub fn endpoint(&self) -> String {
        match self.rpc_transport {
            RpcTransport::Http => format!("http://{}:{}", self.rpc_host, self.rpc_port),
            RpcTransport::Https => format!("https://{}:{}", self.rpc_host, self.rpc_port),
        }
    }

    /// Validate upstream configuration.
    pub fn validate(&self) -> Result<()> {
        if self.name.is_empty() {
            return Err(Error::Config("Upstream name cannot be empty".to_string()));
        }

        if self.rpc_host.is_empty() {
            return Err(Error::Config("rpc_host cannot be empty".to_string()));
        }
        if self.rpc_port == 0 {
            return Err(Error::Config("rpc_port cannot be 0".to_string()));
        }
        // Validate URL format
        let url_str = self.build_url().unwrap();
        if let Err(e) = url::Url::parse(&url_str) {
            return Err(Error::Config(format!(
                "Upstream has invalid URL '{}': {}",
                url_str, e
            )));
        }

        Ok(())
    }

    /// Get auth token or exit on failure.
    pub fn get_auth_token_or_exit(&self) -> std::result::Result<Option<String>, String> {
        self.rpc_auth.get_token_or_exit(&self.name)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    /// How long to cache mining info (in seconds)
    #[serde(default = "default_mining_info_ttl")]
    pub mining_info_ttl_secs: u64,

    /// Pool request timeout (in seconds)
    #[serde(default = "default_pool_timeout")]
    pub pool_timeout_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
    /// Database file path
    #[serde(default = "default_database_path")]
    pub path: String,

    /// Database retention period in days (0 = keep forever)
    /// Submissions older than this will be automatically deleted
    #[serde(default = "default_db_retention_days")]
    pub retention_days: u64,
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            path: default_database_path(),
            retention_days: default_db_retention_days(),
        }
    }
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            mining_info_ttl_secs: default_mining_info_ttl(),
            pool_timeout_secs: default_pool_timeout(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardConfig {
    /// Enable dashboard
    #[serde(default = "default_true")]
    pub enabled: bool,

    /// Dashboard listen address
    #[serde(default = "default_dashboard_address")]
    pub listen_address: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Log level
    #[serde(default = "default_log_level")]
    pub level: String,

    /// Log file path
    #[serde(default = "default_log_file")]
    pub file: String,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: default_log_level(),
            file: default_log_file(),
        }
    }
}

impl Config {
    /// Load configuration from a YAML file
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let contents = std::fs::read_to_string(path)?;
        let config: Config = serde_yaml::from_str(&contents)?;
        config.validate()?;
        Ok(config)
    }

    /// Calculate genesis base target from block time
    /// Formula: 2^42 / block_time_seconds (for 1 TiB starting network capacity)
    pub fn genesis_base_target(&self) -> u64 {
        2u64.pow(42) / self.upstream.block_time_secs
    }

    /// Calculate retention period in blocks from retention days
    /// Formula: days × 86400 / block_time_secs
    /// Returns 0 if retention_days is 0 (keep forever)
    pub fn retention_blocks(&self) -> u64 {
        if self.database.retention_days == 0 {
            return 0;
        }
        self.database.retention_days * 86400 / self.upstream.block_time_secs
    }

    /// Validate configuration
    fn validate(&self) -> Result<()> {
        // Validate upstream config
        self.upstream.validate()?;

        // Validate cache TTL
        if self.cache.mining_info_ttl_secs == 0 {
            return Err(Error::Config(
                "mining_info_ttl_secs must be greater than 0".to_string(),
            ));
        }

        // Validate timeout
        if self.cache.pool_timeout_secs == 0 {
            return Err(Error::Config(
                "pool_timeout_secs must be greater than 0".to_string(),
            ));
        }

        // Validate server auth if enabled
        if self.server.auth.enabled && self.server.auth.basic_auth.is_none() {
            return Err(Error::Config(
                "Server auth is enabled but no credentials configured".to_string(),
            ));
        }

        Ok(())
    }
}

// Default values
fn default_listen_address() -> String {
    "0.0.0.0:8080".to_string()
}

fn default_rpc_host() -> String {
    "127.0.0.1".to_string()
}

fn default_rpc_port() -> u16 {
    8080
}

fn default_mining_info_ttl() -> u64 {
    5
}

fn default_pool_timeout() -> u64 {
    30
}

fn default_block_time() -> u64 {
    120 // PoCX uses 120 seconds (Burst uses 240)
}

fn default_database_path() -> String {
    "aggregator.db".to_string()
}

fn default_db_retention_days() -> u64 {
    7 // Keep submissions for 7 days by default
}

fn default_true() -> bool {
    true
}

fn default_dashboard_address() -> String {
    "0.0.0.0:8081".to_string()
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_log_file() -> String {
    "aggregator.log".to_string()
}

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

    #[test]
    fn test_config_validation_empty_name() {
        let upstream = UpstreamConfig {
            name: "".to_string(),
            rpc_transport: RpcTransport::Http,
            rpc_host: "localhost".to_string(),
            rpc_port: 8080,
            rpc_auth: RpcAuth::None,
            submission_mode: SubmissionMode::Pool,
            block_time_secs: 120,
        };

        assert!(upstream.validate().is_err());
    }

    #[test]
    fn test_valid_http_upstream() {
        let upstream = UpstreamConfig {
            name: "test".to_string(),
            rpc_transport: RpcTransport::Http,
            rpc_host: "localhost".to_string(),
            rpc_port: 8080,
            rpc_auth: RpcAuth::None,
            submission_mode: SubmissionMode::Pool,
            block_time_secs: 120,
        };

        assert!(upstream.validate().is_ok());
        assert_eq!(upstream.endpoint(), "http://localhost:8080");
    }

    #[test]
    fn test_invalid_upstream_empty_host() {
        let upstream = UpstreamConfig {
            name: "test".to_string(),
            rpc_transport: RpcTransport::Http,
            rpc_host: "".to_string(),
            rpc_port: 8080,
            rpc_auth: RpcAuth::None,
            submission_mode: SubmissionMode::Pool,
            block_time_secs: 120,
        };

        assert!(upstream.validate().is_err());
    }

    #[test]
    fn test_full_config_validation() {
        let config = Config {
            server: ServerConfig::default(),
            upstream: UpstreamConfig {
                name: "test".to_string(),
                rpc_transport: RpcTransport::Http,
                rpc_host: "localhost".to_string(),
                rpc_port: 8080,
                rpc_auth: RpcAuth::None,
                submission_mode: SubmissionMode::Pool,
                block_time_secs: 120,
            },
            cache: CacheConfig::default(),
            database: DatabaseConfig::default(),
            dashboard: None,
            logging: LoggingConfig::default(),
        };

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_with_enabled_auth_but_no_credentials() {
        let config = Config {
            server: ServerConfig {
                listen_address: "0.0.0.0:8080".to_string(),
                auth: RpcServerAuth {
                    enabled: true,
                    basic_auth: None, // No credentials
                },
            },
            upstream: UpstreamConfig {
                name: "test".to_string(),
                rpc_transport: RpcTransport::Http,
                rpc_host: "localhost".to_string(),
                rpc_port: 8080,
                rpc_auth: RpcAuth::None,
                submission_mode: SubmissionMode::Pool,
                block_time_secs: 120,
            },
            cache: CacheConfig::default(),
            database: DatabaseConfig::default(),
            dashboard: None,
            logging: LoggingConfig::default(),
        };

        assert!(config.validate().is_err());
    }
}