zc2 0.0.25

P2P compute broker with credit-based billing, WAL, and broker mesh support
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
//! Statistics tracking for the broker TUI.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::RwLock;
use std::time::Instant;

use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};

/// Maximum number of transactions to keep in history
const MAX_TRANSACTIONS: usize = 100;

/// Maximum number of data points for time-series metrics
const MAX_DATAPOINTS: usize = 60;

/// A recorded task offer (broadcast to peers) for the task pile view
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskOfferRecord {
    /// Task/request ID
    pub task_id: String,
    /// When the offer was broadcast
    pub timestamp: DateTime<Local>,
    /// User being charged
    pub requester_user_id: String,
    /// Max price (zkcr/hr) offered
    pub max_price_per_hour: f64,
    /// Originating broker node name
    pub source_broker: String,
}

/// A recorded transaction for display
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionRecord {
    /// Transaction number
    pub tx_num: u64,
    /// Timestamp
    pub timestamp: DateTime<Local>,
    /// User ID
    pub user_id: String,
    /// Action type (EXECUTE, CREDIT, etc.)
    pub action: String,
    /// Cost in credits
    pub cost: f64,
    /// Balance after transaction
    pub balance: f64,
    /// Target worker (if applicable)
    pub worker: Option<String>,
    /// Duration in milliseconds
    pub duration_ms: f64,
    /// Status (OK, FAIL, PENDING)
    pub status: TransactionStatus,
    /// Agreed price per hour (zkcr/hr) from the worker that executed
    #[serde(default)]
    pub price_per_hour: Option<f64>,
    /// Owner ID of the worker / executor (broker owner for P2P)
    #[serde(default)]
    pub owner_id: Option<String>,
    /// Worker process ID (from X-Zakuro-Pid if present)
    #[serde(default)]
    pub worker_pid: Option<String>,
    /// Worker IP (from X-Zakuro-IP or worker URI)
    #[serde(default)]
    pub worker_ip: Option<String>,
    /// Request/task ID (links to task_offers for execution trace)
    #[serde(default)]
    pub request_id: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum TransactionStatus {
    Ok,
    Fail,
    Pending,
}

impl TransactionStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Ok => "OK",
            Self::Fail => "FAIL",
            Self::Pending => "PENDING",
        }
    }
}

/// Time-series data point
#[derive(Debug, Clone, Copy)]
pub struct DataPoint {
    pub timestamp: Instant,
    pub value: f64,
}

/// Aggregated broker statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrokerMetrics {
    /// Total requests processed
    pub total_requests: u64,
    /// Successful requests
    pub successful_requests: u64,
    /// Failed requests
    pub failed_requests: u64,
    /// Total credits spent
    pub total_credits_spent: f64,
    /// Requests per second (rolling average)
    pub requests_per_sec: f64,
    /// Average latency (ms)
    pub avg_latency_ms: f64,
    /// P95 latency (ms)
    pub p95_latency_ms: f64,
    /// Active workers
    pub active_workers: usize,
    /// Total workers
    pub total_workers: usize,
    /// Uptime in seconds
    pub uptime_secs: u64,
    /// Local mode (free execution)
    pub local_mode: bool,
    /// Ledger connected
    pub ledger_connected: bool,
    /// This node's WireGuard IP (if connected)
    pub wireguard_ip: Option<String>,
    /// Whether WireGuard is connected
    pub wireguard_connected: bool,
}

impl Default for BrokerMetrics {
    fn default() -> Self {
        Self {
            total_requests: 0,
            successful_requests: 0,
            failed_requests: 0,
            total_credits_spent: 0.0,
            requests_per_sec: 0.0,
            avg_latency_ms: 0.0,
            p95_latency_ms: 0.0,
            active_workers: 0,
            total_workers: 0,
            uptime_secs: 0,
            local_mode: false,
            ledger_connected: false,
            wireguard_ip: None,
            wireguard_connected: false,
        }
    }
}

/// Max task offers to keep in the pile
const MAX_TASK_OFFERS: usize = 50;

/// Thread-safe statistics collector
pub struct StatsCollector {
    /// Start time
    start_time: Instant,
    /// Recent transactions (ring buffer)
    transactions: RwLock<VecDeque<TransactionRecord>>,
    /// Recent task offers (broadcast pile)
    task_offers: RwLock<VecDeque<TaskOfferRecord>>,
    /// Latency measurements (for percentiles)
    latencies: RwLock<VecDeque<f64>>,
    /// Requests per second history
    rps_history: RwLock<VecDeque<DataPoint>>,
    /// Credits spent history
    credits_history: RwLock<VecDeque<DataPoint>>,
    /// Counters
    total_requests: AtomicU64,
    successful_requests: AtomicU64,
    failed_requests: AtomicU64,
    /// Total credits spent (stored as u64 with 6 decimal precision)
    total_credits_spent: AtomicU64,
    /// Request counter for RPS calculation
    requests_this_second: AtomicU64,
    /// Last RPS calculation time
    last_rps_calc: RwLock<Instant>,
}

impl StatsCollector {
    /// Create a new stats collector
    pub fn new() -> Self {
        Self {
            start_time: Instant::now(),
            transactions: RwLock::new(VecDeque::with_capacity(MAX_TRANSACTIONS)),
            task_offers: RwLock::new(VecDeque::with_capacity(MAX_TASK_OFFERS)),
            latencies: RwLock::new(VecDeque::with_capacity(MAX_DATAPOINTS * 10)),
            rps_history: RwLock::new(VecDeque::with_capacity(MAX_DATAPOINTS)),
            credits_history: RwLock::new(VecDeque::with_capacity(MAX_DATAPOINTS)),
            total_requests: AtomicU64::new(0),
            successful_requests: AtomicU64::new(0),
            failed_requests: AtomicU64::new(0),
            total_credits_spent: AtomicU64::new(0),
            requests_this_second: AtomicU64::new(0),
            last_rps_calc: RwLock::new(Instant::now()),
        }
    }

    /// Record a transaction
    pub fn record_transaction(&self, record: TransactionRecord) {
        // Update counters
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        self.requests_this_second.fetch_add(1, Ordering::Relaxed);

        match record.status {
            TransactionStatus::Ok => {
                self.successful_requests.fetch_add(1, Ordering::Relaxed);
            }
            TransactionStatus::Fail => {
                self.failed_requests.fetch_add(1, Ordering::Relaxed);
            }
            TransactionStatus::Pending => {}
        }

        // Display-only series below use `try_write`: the hot path must never
        // block on these locks. The exact metrics (counts, credits) are the
        // atomics above; the latency/transaction/credits *history* feeds the
        // TUI and `/stats` only, so under contention dropping a sample is
        // correct — it keeps the request path lock-free at high RPS (the
        // blocking `write()` here was a primary throughput bottleneck).

        // Record latency (sampled under contention)
        if record.duration_ms > 0.0 {
            if let Ok(mut latencies) = self.latencies.try_write() {
                latencies.push_back(record.duration_ms);
                while latencies.len() > MAX_DATAPOINTS * 10 {
                    latencies.pop_front();
                }
            }
        }

        // Record credits spent
        if record.cost > 0.0 {
            let cost_micro = (record.cost * 1_000_000.0) as u64;
            self.total_credits_spent
                .fetch_add(cost_micro, Ordering::Relaxed);

            if let Ok(mut history) = self.credits_history.try_write() {
                history.push_back(DataPoint {
                    timestamp: Instant::now(),
                    value: record.cost,
                });
                while history.len() > MAX_DATAPOINTS {
                    history.pop_front();
                }
            }
        }

        // Add to transaction history (sampled under contention)
        if let Ok(mut txs) = self.transactions.try_write() {
            txs.push_back(record);
            while txs.len() > MAX_TRANSACTIONS {
                txs.pop_front();
            }
        }
    }

    /// Record a task offer (broadcast to peers) for the task pile view
    pub fn record_task_offer(&self, record: TaskOfferRecord) {
        if let Ok(mut offers) = self.task_offers.try_write() {
            offers.push_back(record);
            while offers.len() > MAX_TASK_OFFERS {
                offers.pop_front();
            }
        }
    }

    /// Recent task offers (newest last)
    pub fn recent_task_offers(&self, limit: usize) -> Vec<TaskOfferRecord> {
        if let Ok(offers) = self.task_offers.read() {
            offers.iter().rev().take(limit).cloned().collect()
        } else {
            Vec::new()
        }
    }

    /// Calculate and record requests per second
    pub fn tick_rps(&self) {
        let now = Instant::now();
        let elapsed = {
            let last = self.last_rps_calc.read().unwrap();
            now.duration_since(*last).as_secs_f64()
        };

        if elapsed >= 1.0 {
            let requests = self.requests_this_second.swap(0, Ordering::Relaxed);
            let rps = requests as f64 / elapsed;

            if let Ok(mut history) = self.rps_history.write() {
                history.push_back(DataPoint {
                    timestamp: now,
                    value: rps,
                });
                while history.len() > MAX_DATAPOINTS {
                    history.pop_front();
                }
            }

            *self.last_rps_calc.write().unwrap() = now;
        }
    }

    /// Get recent transactions
    pub fn recent_transactions(&self, limit: usize) -> Vec<TransactionRecord> {
        if let Ok(txs) = self.transactions.read() {
            txs.iter().rev().take(limit).cloned().collect()
        } else {
            Vec::new()
        }
    }

    /// Get RPS history for sparkline
    pub fn rps_history(&self) -> Vec<f64> {
        if let Ok(history) = self.rps_history.read() {
            history.iter().map(|dp| dp.value).collect()
        } else {
            Vec::new()
        }
    }

    /// Get current metrics
    pub fn metrics(
        &self,
        active_workers: usize,
        total_workers: usize,
        local_mode: bool,
        ledger_connected: bool,
        wireguard_ip: Option<String>,
    ) -> BrokerMetrics {
        let total = self.total_requests.load(Ordering::Relaxed);
        let successful = self.successful_requests.load(Ordering::Relaxed);
        let failed = self.failed_requests.load(Ordering::Relaxed);
        let credits_micro = self.total_credits_spent.load(Ordering::Relaxed);

        // Calculate average latency
        let (avg_latency, p95_latency) = if let Ok(latencies) = self.latencies.read() {
            if latencies.is_empty() {
                (0.0, 0.0)
            } else {
                let sum: f64 = latencies.iter().sum();
                let avg = sum / latencies.len() as f64;

                // P95 calculation
                let mut sorted: Vec<f64> = latencies.iter().cloned().collect();
                sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let p95_idx = (sorted.len() as f64 * 0.95) as usize;
                let p95 = sorted
                    .get(p95_idx.min(sorted.len() - 1))
                    .cloned()
                    .unwrap_or(0.0);

                (avg, p95)
            }
        } else {
            (0.0, 0.0)
        };

        // Calculate RPS from history
        let rps = if let Ok(history) = self.rps_history.read() {
            if history.is_empty() {
                0.0
            } else {
                history.iter().rev().take(5).map(|dp| dp.value).sum::<f64>()
                    / 5.0f64.min(history.len() as f64)
            }
        } else {
            0.0
        };

        BrokerMetrics {
            total_requests: total,
            successful_requests: successful,
            failed_requests: failed,
            total_credits_spent: credits_micro as f64 / 1_000_000.0,
            requests_per_sec: rps,
            avg_latency_ms: avg_latency,
            p95_latency_ms: p95_latency,
            active_workers,
            total_workers,
            uptime_secs: self.start_time.elapsed().as_secs(),
            local_mode,
            ledger_connected,
            wireguard_connected: wireguard_ip.is_some(),
            wireguard_ip,
        }
    }
}

impl Default for StatsCollector {
    fn default() -> Self {
        Self::new()
    }
}

/// Format duration for display
pub fn format_uptime(secs: u64) -> String {
    if secs < 60 {
        format!("{}s", secs)
    } else if secs < 3600 {
        format!("{}m {}s", secs / 60, secs % 60)
    } else if secs < 86400 {
        format!("{}h {}m", secs / 3600, (secs % 3600) / 60)
    } else {
        format!("{}d {}h", secs / 86400, (secs % 86400) / 3600)
    }
}

/// Format credits for display
pub fn format_credits(amount: f64) -> String {
    if amount >= 0.01 {
        format!("{:.4}", amount)
    } else {
        format!("{:.6}", amount)
    }
}

/// Worker info for stats response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerStats {
    pub id: String,
    pub name: String,
    pub uri: String,
    pub status: String,
    pub cpus_available: f64,
    pub memory_available_gib: f64,
    pub gpus_available: u32,
    pub price_per_hour: f64,
    pub active_requests: u32,
    pub avg_latency_ms: f64,
}

/// Complete stats response for remote TUI
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatsResponse {
    /// Host and port of the broker
    pub host: String,
    pub port: u16,
    /// Recent transactions
    pub transactions: Vec<TransactionRecord>,
    /// Recent task offers (broadcast pile)
    #[serde(default)]
    pub task_offers: Vec<TaskOfferRecord>,
    /// Worker list
    pub workers: Vec<WorkerStats>,
    /// Aggregated metrics
    pub metrics: BrokerMetrics,
    /// RPS history for sparkline
    pub rps_history: Vec<f64>,
    /// This node's WireGuard IP (if connected)
    pub wireguard_ip: Option<String>,
    /// Whether WireGuard is connected
    pub wireguard_connected: bool,
}