Skip to main content

ai3_lib/
mining.rs

1use crate::tensor::Tensor;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct MiningTask {
8    pub id: String,
9    pub operation_type: String,
10    pub input_tensors: Vec<Tensor>,
11    pub difficulty: u64,
12    pub reward: u64,
13    pub max_computation_time: u64,
14    pub requester: String,
15    pub created_at: DateTime<Utc>,
16    pub expires_at: DateTime<Utc>,
17}
18
19impl MiningTask {
20    pub fn new(
21        operation_type: String,
22        input_tensors: Vec<Tensor>,
23        difficulty: u64,
24        reward: u64,
25        max_computation_time: u64,
26        requester: String,
27    ) -> Self {
28        let now = Utc::now();
29        Self {
30            id: uuid::Uuid::new_v4().to_string(),
31            operation_type,
32            input_tensors,
33            difficulty,
34            reward,
35            max_computation_time,
36            requester,
37            created_at: now,
38            expires_at: now + chrono::Duration::seconds(max_computation_time as i64),
39        }
40    }
41
42    pub fn is_expired(&self) -> bool {
43        Utc::now() > self.expires_at
44    }
45
46    pub fn meets_difficulty(&self, hash: &str) -> bool {
47        let leading_zeros = self.difficulty as usize;
48        hash.chars().take(leading_zeros).all(|c| c == '0')
49    }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct MiningResult {
54    pub task_id: String,
55    pub miner_id: String,
56    pub nonce: u64,
57    pub hash: String,
58    pub output_tensor: Tensor,
59    pub computation_time: u64,
60    pub timestamp: DateTime<Utc>,
61    pub is_valid: bool,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct MinerCapabilities {
66    pub supported_operations: Vec<String>,
67    pub max_tensor_size: usize,
68    pub is_esp_device: bool,
69    pub max_computation_time: u64,
70}
71
72impl Default for MinerCapabilities {
73    fn default() -> Self {
74        Self {
75            supported_operations: vec![
76                "matrix_multiply".into(),
77                "convolution".into(),
78                "relu".into(),
79                "sigmoid".into(),
80                "tanh".into(),
81                "dot_product".into(),
82                "normalize".into(),
83            ],
84            max_tensor_size: 64 * 64 * 4, // 64x64 f32
85            is_esp_device: false,
86            max_computation_time: 300,
87        }
88    }
89}
90
91#[derive(Debug, Clone, Default, Serialize, Deserialize)]
92pub struct MinerStats {
93    pub tasks_completed: u64,
94    pub tasks_failed: u64,
95    pub total_compute_time: u64,
96    pub average_compute_time: f64,
97}
98
99/// Distributes mining tasks to available miners (from .AI3)
100#[derive(Debug, Default)]
101pub struct TaskDistributor {
102    pub pending_tasks: HashMap<String, MiningTask>,
103}
104
105impl TaskDistributor {
106    pub fn new() -> Self {
107        Self::default()
108    }
109
110    pub fn add_task(&mut self, task: MiningTask) {
111        self.pending_tasks.insert(task.id.clone(), task);
112    }
113
114    pub fn get_pending_tasks(&self) -> Vec<&MiningTask> {
115        self.pending_tasks.values().collect()
116    }
117
118    pub fn remove_task(&mut self, task_id: &str) -> Option<MiningTask> {
119        self.pending_tasks.remove(task_id)
120    }
121
122    pub fn cleanup_expired_tasks(&mut self) {
123        self.pending_tasks.retain(|_, task| !task.is_expired());
124    }
125}