anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};
use lazy_static::lazy_static;
use log::{error, warn, info};
use serde::{Deserialize, Serialize};

use crate::monitoring::blockchain_metrics;

lazy_static! {
    static ref ALERTS: Mutex<BlockchainAlerts> = Mutex::new(BlockchainAlerts::new());
}

/// Alert severity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AlertSeverity {
    Info,
    Warning,
    Error,
    Critical,
}

/// Alert status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlertStatus {
    Active,
    Acknowledged,
    Resolved,
}

/// Blockchain metric alert definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Alert {
    /// Alert ID
    id: String,
    
    /// Alert name
    name: String,
    
    /// Alert description
    description: String,
    
    /// Severity level
    severity: AlertSeverity,
    
    /// Current status
    status: AlertStatus,
    
    /// When the alert was triggered
    triggered_at: u64,
    
    /// When the alert was resolved (if applicable)
    resolved_at: Option<u64>,
    
    /// Metric name that triggered the alert
    metric_name: String,
    
    /// Threshold value that triggered the alert
    threshold: f64,
    
    /// Current value of the metric
    current_value: f64,
    
    /// Comparison operator (e.g., ">", "<", "==", ">=", "<=")
    comparison_operator: String,
}

/// Blockchain alerts manager
#[derive(Debug)]
pub struct BlockchainAlerts {
    /// Active alerts
    active_alerts: HashMap<String, Alert>,
    
    /// Alert definitions (name -> threshold config)
    alert_definitions: HashMap<String, AlertDefinition>,
    
    /// Last check time
    last_check: Instant,
    
    /// Alert history (last 100 alerts)
    alert_history: Vec<Alert>,
}

/// Alert threshold definition
#[derive(Debug, Clone)]
struct AlertDefinition {
    /// Alert name
    name: String,
    
    /// Alert description template
    description_template: String,
    
    /// Metric name to monitor
    metric_name: String,
    
    /// Threshold value
    threshold: f64,
    
    /// Comparison operator
    comparison_operator: String,
    
    /// Default severity level
    severity: AlertSeverity,
    
    /// Duration the threshold must be exceeded before an alert is triggered (in seconds)
    duration_threshold_seconds: u64,
    
    /// Consecutive readings that must exceed threshold
    consecutive_readings: u32,
    
    /// Current consecutive readings count
    current_consecutive: u32,
    
    /// First exceeded at timestamp
    first_exceeded_at: Option<Instant>,
}

impl BlockchainAlerts {
    /// Create new blockchain alerts manager
    pub fn new() -> Self {
        let mut alerts = Self {
            active_alerts: HashMap::new(),
            alert_definitions: HashMap::new(),
            last_check: Instant::now(),
            alert_history: Vec::new(),
        };
        
        // Define default alert thresholds
        alerts.define_default_alerts();
        
        alerts
    }
    
    /// Define default alert thresholds
    fn define_default_alerts(&mut self) {
        // SegWit adoption alerts
        self.add_alert_definition(
            "segwit_adoption_low",
            "SegWit Adoption Below Threshold",
            "SegWit adoption percentage is below {threshold}% (currently {value}%)",
            "segwit_percentage",
            80.0,
            "<",
            AlertSeverity::Warning,
            300, // 5 minutes
            3,
        );
        
        // Taproot adoption alerts
        self.add_alert_definition(
            "taproot_adoption_low",
            "Taproot Adoption Below Threshold",
            "Taproot adoption percentage is below {threshold}% (currently {value}%)",
            "taproot_percentage",
            5.0,
            "<",
            AlertSeverity::Warning,
            300, // 5 minutes
            3,
        );
        
        // Error rate alerts
        self.add_alert_definition(
            "high_error_rate",
            "High Error Rate",
            "Error rate is above {threshold}% (currently {value}%)",
            "error_rates.connection_failure", 
            5.0,
            ">",
            AlertSeverity::Error,
            60, // 1 minute
            2,
        );
        
        // Block propagation alerts
        self.add_alert_definition(
            "slow_block_propagation",
            "Slow Block Propagation",
            "Block propagation time is above {threshold}ms (currently {value}ms)",
            "block_propagation_ms",
            1000.0, // 1 second
            ">",
            AlertSeverity::Warning,
            0, // Immediate
            1,
        );
        
        // Mempool size alerts
        self.add_alert_definition(
            "mempool_size_high",
            "High Mempool Size",
            "Mempool size is above {threshold}MB (currently {value}MB)",
            "mempool_size_bytes",
            50.0 * 1024.0 * 1024.0, // 50MB
            ">", 
            AlertSeverity::Warning,
            300, // 5 minutes
            3,
        );
        
        // Fee rate alerts
        self.add_alert_definition(
            "high_fee_rate",
            "High Fee Rate",
            "Average fee rate is above {threshold} sats/vB (currently {value} sats/vB)",
            "avg_fee_rate_sats_per_vb",
            100.0,
            ">",
            AlertSeverity::Warning,
            300, // 5 minutes
            2,
        );
    }
    
    /// Add a new alert definition
    fn add_alert_definition(
        &mut self,
        id: &str,
        name: &str,
        description_template: &str,
        metric_name: &str,
        threshold: f64,
        comparison_operator: &str,
        severity: AlertSeverity,
        duration_threshold_seconds: u64,
        consecutive_readings: u32,
    ) {
        self.alert_definitions.insert(
            id.to_string(),
            AlertDefinition {
                name: name.to_string(),
                description_template: description_template.to_string(),
                metric_name: metric_name.to_string(),
                threshold,
                comparison_operator: comparison_operator.to_string(),
                severity,
                duration_threshold_seconds,
                consecutive_readings,
                current_consecutive: 0,
                first_exceeded_at: None,
            },
        );
    }
    
    /// Check metrics against all defined alert thresholds
    pub fn check_alerts(&mut self) {
        self.last_check = Instant::now();

        // Get current metrics
        let metrics_json = blockchain_metrics::get_metrics_json();

        let mut checks = Vec::new();

        for (alert_id, definition) in &self.alert_definitions {
            let metric_value = self.get_metric_value(&metrics_json, &definition.metric_name);

            match metric_value {
                Some(value) => {
                    let threshold_exceeded = self.is_threshold_exceeded(
                        value,
                        definition.threshold,
                        &definition.comparison_operator,
                    );

                    checks.push((alert_id.clone(), value, threshold_exceeded));
                },
                None => {
                    checks.push((alert_id.clone(), 0.0, false));
                }
            }
        }

        for (alert_id, value, threshold_exceeded) in checks {
            let definition = match self.alert_definitions.get_mut(&alert_id) {
                Some(def) => def,
                None => continue,
            };

            if threshold_exceeded {
                definition.current_consecutive += 1;

                if definition.first_exceeded_at.is_none() {
                    definition.first_exceeded_at = Some(Instant::now());
                }

                let duration_exceeded = definition
                    .first_exceeded_at
                    .map(|time| time.elapsed().as_secs() >= definition.duration_threshold_seconds)
                    .unwrap_or(false);

                if duration_exceeded && definition.current_consecutive >= definition.consecutive_readings {
                    let description = definition.description_template
                        .replace("{threshold}", &definition.threshold.to_string())
                        .replace("{value}", &value.to_string());

                    let now = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs();

                    let alert = Alert {
                        id: alert_id.clone(),
                        name: definition.name.clone(),
                        description: description.clone(),
                        severity: definition.severity,
                        status: AlertStatus::Active,
                        triggered_at: now,
                        resolved_at: None,
                        metric_name: definition.metric_name.clone(),
                        threshold: definition.threshold,
                        current_value: value,
                        comparison_operator: definition.comparison_operator.clone(),
                    };

                    self.active_alerts.insert(alert_id.clone(), alert.clone());
                    self.alert_history.push(alert);

                    if self.alert_history.len() > 100 {
                        self.alert_history.remove(0);
                    }

                    match definition.severity {
                        AlertSeverity::Info => info!("ALERT: {}", description),
                        AlertSeverity::Warning => warn!("ALERT: {}", description),
                        AlertSeverity::Error => error!("ALERT: {}", description),
                        AlertSeverity::Critical => error!("CRITICAL ALERT: {}", description),
                    }
                }
            } else {
                definition.current_consecutive = 0;
                definition.first_exceeded_at = None;

                if let Some(mut alert) = self.active_alerts.remove(&alert_id) {
                    let now = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs();

                    alert.status = AlertStatus::Resolved;
                    alert.resolved_at = Some(now);
                    
                    let description = alert.description.clone();
                    self.alert_history.push(alert);

                    if self.alert_history.len() > 100 {
                        self.alert_history.remove(0);
                    }

                    info!("RESOLVED: {}", description);
                }
            }
        }
    }
    
    /// Get a metric value from the metrics JSON structure
    fn get_metric_value(&self, metrics: &serde_json::Value, path: &str) -> Option<f64> {
        let parts: Vec<&str> = path.split('.').collect();
        
        let mut current = metrics;
        for part in &parts[0..parts.len() - 1] {
            current = &current[*part];
            if current.is_null() {
                return None;
            }
        }
        
        let last_part = parts.last()?;
        let value = &current[*last_part];
        
        if value.is_f64() {
            Some(value.as_f64().unwrap())
        } else if value.is_i64() {
            Some(value.as_i64().unwrap() as f64)
        } else if value.is_u64() {
            Some(value.as_u64().unwrap() as f64)
        } else {
            None
        }
    }
    
    /// Check if a threshold is exceeded based on the comparison operator
    fn is_threshold_exceeded(&self, value: f64, threshold: f64, operator: &str) -> bool {
        match operator {
            ">" => value > threshold,
            "<" => value < threshold,
            ">=" => value >= threshold,
            "<=" => value <= threshold,
            "==" => (value - threshold).abs() < f64::EPSILON,
            "!=" => (value - threshold).abs() > f64::EPSILON,
            _ => false,
        }
    }
    
    /// Acknowledge an alert
    pub fn acknowledge_alert(&mut self, alert_id: &str) {
        if let Some(alert) = self.active_alerts.get_mut(alert_id) {
            alert.status = AlertStatus::Acknowledged;
            info!("Alert '{}' acknowledged", alert_id);
        }
    }
    
    /// Get all active alerts
    pub fn get_active_alerts(&self) -> Vec<&Alert> {
        self.active_alerts.values().collect()
    }
    
    /// Get alert history
    pub fn get_alert_history(&self) -> &Vec<Alert> {
        &self.alert_history
    }
}

/// Check all alert thresholds against current metrics
pub fn check_alerts() {
    let mut alerts: std::sync::MutexGuard<BlockchainAlerts> = ALERTS.lock().unwrap();
    alerts.check_alerts();
}

/// Acknowledge an alert
pub fn acknowledge_alert(alert_id: &str) {
    let mut alerts: std::sync::MutexGuard<BlockchainAlerts> = ALERTS.lock().unwrap();
    let _ = alerts.acknowledge_alert(alert_id);
}

/// Get all active alerts
pub fn get_active_alerts() -> Vec<Alert> {
    let alerts: std::sync::MutexGuard<BlockchainAlerts> = ALERTS.lock().unwrap();
    alerts.get_active_alerts().iter().map(|a| (*a).clone()).collect()
}

/// Get alert history
pub fn get_alert_history() -> Vec<Alert> {
    let alerts: std::sync::MutexGuard<BlockchainAlerts> = ALERTS.lock().unwrap();
    alerts.get_alert_history().clone()
}

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

    #[test]
    fn test_blockchain_alerts() {
        // This is a minimal test as we can't easily modify the metrics
        let alerts = get_active_alerts();
        assert!(alerts.is_empty(), "Should have no active alerts initially");
    }
}