anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
//! [AIR-3][AIS-3][BPC-3][RES-3] Reliability and monitoring components for Anya Core

use crate::{AnyaError, AnyaResult};
use log::{error, info, warn};
use std::future::Future;
use std::time::{Duration, Instant};

/// Confidence assessment for AI verification
#[derive(Debug, Clone)]
pub struct ConfidenceAssessment<T> {
    pub output: AnyaResult<T>,
    pub confidence: f64,
    pub verification_steps: Vec<String>,
    pub reasoning: String,
}

/// Watchdog timer for monitoring operations
#[derive(Debug, Clone)]
pub struct Watchdog {
    name: String,
    timeout: Duration,
    start_time: Instant,
    is_active: bool,
}

impl Watchdog {
    /// Create a new watchdog with specified timeout
    pub fn new(name: &str, timeout: Duration) -> Self {
        Self {
            name: name.to_string(),
            timeout,
            start_time: Instant::now(),
            is_active: true,
        }
    }

    /// Stop the watchdog
    pub fn stop(&mut self) {
        self.is_active = false;
    }

    /// Trigger an alert for timeout
    pub fn trigger_alert(&self) {
        error!(
            "Watchdog '{}' triggered alert after {:?}",
            self.name, self.timeout
        );
    }

    /// Check if the watchdog has timed out
    pub fn has_timed_out(&self) -> bool {
        self.is_active && self.start_time.elapsed() > self.timeout
    }
}

/// Progress tracker for long-running operations
#[derive(Debug, Clone)]
pub struct ProgressTracker {
    name: String,
    timeout: Duration,
    verbose: bool,
    start_time: Instant,
}

impl ProgressTracker {
    /// Create a new progress tracker
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            timeout: Duration::from_secs(300), // Default 5 minutes
            verbose: false,
            start_time: Instant::now(),
        }
    }

    /// Set timeout for the operation
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Enable verbose logging
    pub fn with_verbosity(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Log progress if verbose mode is enabled
    pub fn log_progress(&self, message: &str) {
        if self.verbose {
            info!("[{}] {}", self.name, message);
        }
    }

    /// Get elapsed time since start
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Update progress with completion percentage
    pub fn update(&self, progress: f64) -> AnyaResult<()> {
        if !(0.0..=1.0).contains(&progress) {
            return Err(AnyaError::InvalidInput(
                "Progress must be between 0.0 and 1.0".to_string(),
            ));
        }

        if self.verbose {
            info!("[{}] Progress: {:.1}%", self.name, progress * 100.0);
        }

        Ok(())
    }

    /// Mark operation as complete
    pub fn complete(&self) {
        if self.verbose {
            info!(
                "[{}] Operation completed in {:?}",
                self.name,
                self.elapsed()
            );
        }
    }
}

/// AI verification component for blockchain operations
#[derive(Debug, Clone)]
pub struct AiVerification {
    min_confidence: f64,
    blockchain_verification: bool,
    external_data_verification: bool,
    human_verification: bool,
}

impl AiVerification {
    /// Create a new AI verification instance
    pub fn new() -> Self {
        Self {
            min_confidence: 0.95,
            blockchain_verification: true,
            external_data_verification: true,
            human_verification: false,
        }
    }

    /// Set minimum confidence threshold
    pub fn with_min_confidence(mut self, confidence: f64) -> Self {
        self.min_confidence = confidence;
        self
    }

    /// Enable/disable blockchain verification
    pub fn with_blockchain_verification(mut self, enabled: bool) -> Self {
        self.blockchain_verification = enabled;
        self
    }

    /// Enable/disable external data verification
    pub fn with_external_data_verification(mut self, enabled: bool) -> Self {
        self.external_data_verification = enabled;
        self
    }

    /// Enable/disable human verification requirement
    pub fn with_human_verification(mut self, enabled: bool) -> Self {
        self.human_verification = enabled;
        self
    }

    /// Verify data with AI analysis
    pub async fn verify(&self, data: &[u8]) -> AnyaResult<bool> {
        // Simulate AI verification process
        let confidence = self.calculate_confidence(data).await?;

        if confidence >= self.min_confidence {
            Ok(true)
        } else {
            Err(AnyaError::LowConfidence(format!(
                "Verification confidence {} below threshold {}",
                confidence, self.min_confidence
            )))
        }
    }

    /// Calculate confidence score for data
    async fn calculate_confidence(&self, _data: &[u8]) -> AnyaResult<f64> {
        // Placeholder for AI confidence calculation
        // In real implementation, this would use ML models
        Ok(0.98) // High confidence for now
    }
}

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

/// [AIR-3][AIS-3][BPC-3][RES-3] Execute an async operation with timeout and progress tracking
pub async fn execute_with_monitoring<T, F>(
    operation_name: &str,
    timeout_duration: Duration,
    operation: F,
) -> AnyaResult<T>
where
    F: Future<Output = AnyaResult<T>>,
{
    // Create watchdog
    let mut watchdog = Watchdog::new(operation_name, timeout_duration);

    // Execute with timeout
    match tokio::time::timeout(timeout_duration, operation).await {
        Ok(result) => {
            // Operation completed within timeout
            watchdog.stop();
            result
        }
        Err(_) => {
            // Operation timed out
            watchdog.trigger_alert();
            let error_msg =
                format!("Operation '{operation_name}' timed out after {timeout_duration:?}");
            error!("{error_msg}");
            Err(AnyaError::Timeout(error_msg))
        }
    }
}

/// [AIR-3][AIS-3][BPC-3][RES-3] Execute with recovery attempt on timeout
pub async fn execute_with_recovery<T, F, R>(
    operation_name: &str,
    primary_timeout: Duration,
    recovery_timeout: Duration,
    primary_operation: F,
    recovery_operation: R,
) -> AnyaResult<T>
where
    F: Future<Output = AnyaResult<T>>,
    R: Future<Output = AnyaResult<T>>,
{
    // Create watchdog for the entire operation
    let mut watchdog = Watchdog::new(
        operation_name,
        primary_timeout + recovery_timeout + Duration::from_secs(1),
    );

    // Try primary operation with timeout
    match tokio::time::timeout(primary_timeout, primary_operation).await {
        Ok(result) => {
            // Primary operation completed within timeout
            watchdog.stop();
            result
        }
        Err(_) => {
            // Primary operation timed out, try recovery
            warn!(
                "Operation '{operation_name}' timed out after {primary_timeout:?}, attempting recovery"
            );

            // Try recovery operation with timeout
            match tokio::time::timeout(recovery_timeout, recovery_operation).await {
                Ok(result) => {
                    // Recovery completed within timeout
                    watchdog.stop();
                    info!("Recovery for '{operation_name}' succeeded");
                    result
                }
                Err(_) => {
                    // Recovery also timed out
                    watchdog.trigger_alert();
                    let error_msg = format!(
                        "Operation '{operation_name}' and recovery both timed out (after {primary_timeout:?} and {recovery_timeout:?})"
                    );
                    error!("{error_msg}");
                    Err(AnyaError::Timeout(error_msg))
                }
            }
        }
    }
}