hanzo-mining 1.1.21

Hanzo AI - Mining Library
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use std::sync::Arc;
use tokio::sync::RwLock;
use serde::{Deserialize, Serialize};

pub mod bridge;
pub mod consensus;
pub mod evm;
pub mod ledger;
pub mod wallet;

pub use bridge::*;
pub use consensus::*;
pub use evm::*;
pub use ledger::*;
pub use wallet::*;

/// AI Mining configuration for Hanzo Node
/// Enables mining AI coins by offering GPU/CPU compute to the network
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiningConfig {
    // Network selection
    pub network: NetworkType,
    pub enabled: bool,
    pub auto_start: bool,

    // Compute offering
    pub offer_gpu: bool,
    pub offer_cpu: bool,
    pub max_gpu_usage: f32,  // 0.0 to 1.0
    pub max_cpu_usage: f32,  // 0.0 to 1.0
    pub reserved_ram_gb: f32, // RAM to keep free

    // Mining wallet
    pub wallet_address: String,
    pub wallet_private_key: Option<String>, // Encrypted

    // Earnings & rewards
    pub payout_threshold: f64,
    pub auto_withdraw: bool,
    pub withdrawal_address: Option<String>,

    // Performance settings
    pub benchmark_on_start: bool,
    pub adaptive_performance: bool,
    pub min_job_reward: f64, // Minimum AI coin reward to accept job

    // Connection settings
    pub rpc_endpoints: Vec<String>,
    pub p2p_port: u16,
    pub api_port: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum NetworkType {
    HanzoMainnet,     // hanzo.network - Main AI network
    HanzoTestnet,     // hanzo-test.network - Test network
    ZooMainnet,       // zoo.network - Zoo mainnet with AI protocol
    ZooTestnet,       // zoo-test.network - Zoo testnet
    Custom(String),   // Custom network endpoint
}

impl NetworkType {
    pub fn rpc_endpoint(&self) -> String {
        match self {
            Self::HanzoMainnet => "https://rpc.hanzo.network".to_string(),
            Self::HanzoTestnet => "https://rpc.hanzo-test.network".to_string(),
            Self::ZooMainnet => "https://rpc.zoo.network".to_string(),
            Self::ZooTestnet => "https://rpc.zoo-test.network".to_string(),
            Self::Custom(endpoint) => endpoint.clone(),
        }
    }

    pub fn chain_id(&self) -> u64 {
        match self {
            Self::HanzoMainnet => 36963,  // Hanzo mainnet chain ID
            Self::HanzoTestnet => 36964,  // Hanzo testnet chain ID
            Self::ZooMainnet => 200200,   // Zoo mainnet chain ID
            Self::ZooTestnet => 200201,   // Zoo testnet chain ID
            Self::Custom(_) => 0,
        }
    }

    pub fn native_token(&self) -> &str {
        match self {
            Self::HanzoMainnet | Self::HanzoTestnet => "HAI",  // Hanzo AI coin
            Self::ZooMainnet | Self::ZooTestnet => "ZOO",      // Zoo coin
            Self::Custom(_) => "TOKEN",
        }
    }
}

impl Default for MiningConfig {
    fn default() -> Self {
        Self {
            network: NetworkType::HanzoMainnet,
            enabled: true,
            auto_start: true,

            offer_gpu: true,
            offer_cpu: true,
            max_gpu_usage: 0.8,  // Use up to 80% GPU
            max_cpu_usage: 0.6,  // Use up to 60% CPU
            reserved_ram_gb: 4.0, // Keep 4GB RAM free

            wallet_address: String::new(),
            wallet_private_key: None,

            payout_threshold: 10.0,  // Withdraw after 10 AI coins
            auto_withdraw: true,
            withdrawal_address: None,

            benchmark_on_start: true,
            adaptive_performance: true,
            min_job_reward: 0.001,  // Accept jobs paying at least 0.001 AI coins

            rpc_endpoints: vec![
                "https://rpc.hanzo.network".to_string(),
                "https://rpc2.hanzo.network".to_string(),
            ],
            p2p_port: 3691,  // P2P consensus port (3690 + 1)
            api_port: 3690,   // Main hanzod API port
        }
    }
}

/// AI Mining Manager - handles compute job execution and rewards
pub struct MiningManager {
    config: Arc<RwLock<MiningConfig>>,
    is_mining: Arc<RwLock<bool>>,
    current_jobs: Arc<RwLock<Vec<ComputeJob>>>,
    total_earned: Arc<RwLock<f64>>,
    performance_stats: Arc<RwLock<PerformanceStats>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComputeJob {
    pub job_id: String,
    pub job_type: JobType,
    pub requester: String,
    pub reward: f64,
    pub deadline: u64,
    pub input_data: Vec<u8>,
    pub status: JobStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JobType {
    Embedding(EmbeddingJob),
    Reranking(RerankingJob),
    Inference(InferenceJob),
    Training(TrainingJob),
    Custom(Vec<u8>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingJob {
    pub model: String,
    pub texts: Vec<String>,
    pub batch_size: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RerankingJob {
    pub model: String,
    pub query: String,
    pub documents: Vec<String>,
    pub top_k: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InferenceJob {
    pub model: String,
    pub prompt: String,
    pub max_tokens: usize,
    pub temperature: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingJob {
    pub model: String,
    pub dataset_url: String,
    pub epochs: usize,
    pub checkpoint_interval: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum JobStatus {
    Pending,
    Running,
    Completed,
    Failed,
    Cancelled,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceStats {
    pub gpu_tflops: f32,
    pub cpu_gflops: f32,
    pub ram_gb: f32,
    pub vram_gb: f32,
    pub network_mbps: f32,
    pub jobs_completed: u64,
    pub jobs_failed: u64,
    pub uptime_hours: f64,
    pub reputation_score: f64,
}

impl MiningManager {
    pub fn new(config: MiningConfig) -> Self {
        Self {
            config: Arc::new(RwLock::new(config)),
            is_mining: Arc::new(RwLock::new(false)),
            current_jobs: Arc::new(RwLock::new(Vec::new())),
            total_earned: Arc::new(RwLock::new(0.0)),
            performance_stats: Arc::new(RwLock::new(PerformanceStats::default())),
        }
    }

    /// Start mining on the configured network
    pub async fn start_mining(&self) -> Result<(), Box<dyn std::error::Error>> {
        let config = self.config.read().await;

        if !config.enabled {
            return Err("Mining is disabled in config".into());
        }

        println!("🚀 Starting AI mining on {}", config.network.rpc_endpoint());
        println!("⛏️  Offering: GPU={}, CPU={}", config.offer_gpu, config.offer_cpu);
        println!("💰 Wallet: {}", config.wallet_address);

        *self.is_mining.write().await = true;

        // Connect to network
        self.connect_to_network(&config).await?;

        // Benchmark if needed
        if config.benchmark_on_start {
            self.run_benchmark().await?;
        }

        // Start job listener
        self.start_job_listener().await;

        Ok(())
    }

    /// Stop mining
    pub async fn stop_mining(&self) -> Result<(), Box<dyn std::error::Error>> {
        println!("⛔ Stopping AI mining...");
        *self.is_mining.write().await = false;

        // Cancel current jobs
        let mut jobs = self.current_jobs.write().await;
        for job in jobs.iter_mut() {
            job.status = JobStatus::Cancelled;
        }

        Ok(())
    }

    /// Connect to the blockchain network
    async fn connect_to_network(&self, config: &MiningConfig) -> Result<(), Box<dyn std::error::Error>> {
        println!("🔗 Connecting to {} network...", match &config.network {
            NetworkType::HanzoMainnet => "Hanzo Mainnet",
            NetworkType::HanzoTestnet => "Hanzo Testnet",
            NetworkType::ZooMainnet => "Zoo Mainnet",
            NetworkType::ZooTestnet => "Zoo Testnet",
            NetworkType::Custom(url) => url,
        });

        // TODO: Implement actual blockchain connection
        // This would connect to the RPC endpoint and register as a compute provider

        Ok(())
    }

    /// Run performance benchmark
    async fn run_benchmark(&self) -> Result<(), Box<dyn std::error::Error>> {
        println!("📊 Running performance benchmark...");

        let mut stats = self.performance_stats.write().await;

        // Detect GPU
        if self.config.read().await.offer_gpu {
            stats.gpu_tflops = self.benchmark_gpu().await?;
            println!("  GPU: {:.2} TFLOPS", stats.gpu_tflops);
        }

        // Benchmark CPU
        if self.config.read().await.offer_cpu {
            stats.cpu_gflops = self.benchmark_cpu().await?;
            println!("  CPU: {:.2} GFLOPS", stats.cpu_gflops);
        }

        // Check RAM
        stats.ram_gb = self.get_available_ram();
        println!("  RAM: {:.2} GB available", stats.ram_gb);

        // Check VRAM
        if self.config.read().await.offer_gpu {
            stats.vram_gb = self.get_available_vram();
            println!("  VRAM: {:.2} GB available", stats.vram_gb);
        }

        // Test network speed
        stats.network_mbps = self.benchmark_network().await?;
        println!("  Network: {:.2} Mbps", stats.network_mbps);

        Ok(())
    }

    /// Start listening for compute jobs
    async fn start_job_listener(&self) {
        let is_mining = self.is_mining.clone();
        let _current_jobs = self.current_jobs.clone();
        let config = self.config.clone();

        tokio::spawn(async move {
            while *is_mining.read().await {
                // Poll for new jobs from the network
                // This would connect to the P2P network or smart contract

                // Simulate receiving a job
                tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;

                // Check if we should accept jobs
                let cfg = config.read().await;
                if !cfg.enabled {
                    continue;
                }

                // TODO: Implement actual job fetching from blockchain
                // For now, this is a placeholder

                println!("👀 Checking for new compute jobs...");
            }
        });
    }

    /// Execute a compute job
    pub async fn execute_job(&self, job: ComputeJob) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        println!("🔧 Executing job: {}", job.job_id);

        let result = match job.job_type {
            JobType::Embedding(ref params) => {
                self.execute_embedding_job(params).await?
            }
            JobType::Reranking(ref params) => {
                self.execute_reranking_job(params).await?
            }
            JobType::Inference(ref params) => {
                self.execute_inference_job(params).await?
            }
            JobType::Training(ref params) => {
                self.execute_training_job(params).await?
            }
            JobType::Custom(ref data) => {
                self.execute_custom_job(data).await?
            }
        };

        // Update stats
        let mut stats = self.performance_stats.write().await;
        stats.jobs_completed += 1;

        // Add to earnings
        *self.total_earned.write().await += job.reward;
        println!("💰 Earned {} {} for job {}", job.reward,
            self.config.read().await.network.native_token(), job.job_id);

        Ok(result)
    }

    async fn execute_embedding_job(&self, params: &EmbeddingJob) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        // TODO: Use hanzo-engine to generate embeddings
        println!("  Generating embeddings with model: {}", params.model);
        Ok(vec![])
    }

    async fn execute_reranking_job(&self, params: &RerankingJob) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        // TODO: Use hanzo-engine for reranking
        println!("  Reranking {} documents", params.documents.len());
        Ok(vec![])
    }

    async fn execute_inference_job(&self, params: &InferenceJob) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        // TODO: Use hanzo-engine for inference
        println!("  Running inference with model: {}", params.model);
        Ok(vec![])
    }

    async fn execute_training_job(&self, params: &TrainingJob) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        // TODO: Use hanzo-engine for training
        println!("  Training model: {} for {} epochs", params.model, params.epochs);
        Ok(vec![])
    }

    async fn execute_custom_job(&self, data: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        println!("  Executing custom job ({} bytes)", data.len());
        Ok(vec![])
    }

    /// Get mining statistics
    pub async fn get_stats(&self) -> MiningStats {
        MiningStats {
            is_mining: *self.is_mining.read().await,
            network: self.config.read().await.network.clone(),
            total_earned: *self.total_earned.read().await,
            current_jobs: self.current_jobs.read().await.len(),
            performance: self.performance_stats.read().await.clone(),
        }
    }

    // Benchmark helpers
    async fn benchmark_gpu(&self) -> Result<f32, Box<dyn std::error::Error>> {
        // TODO: Actual GPU benchmark using hanzo-engine
        Ok(15.7) // Example: 15.7 TFLOPS for RTX 3080
    }

    async fn benchmark_cpu(&self) -> Result<f32, Box<dyn std::error::Error>> {
        // TODO: Actual CPU benchmark
        Ok(450.0) // Example: 450 GFLOPS
    }

    fn get_available_ram(&self) -> f32 {
        // TODO: Get actual available RAM
        16.0 // Example: 16 GB
    }

    fn get_available_vram(&self) -> f32 {
        // TODO: Get actual VRAM from GPU
        10.0 // Example: 10 GB for RTX 3080
    }

    async fn benchmark_network(&self) -> Result<f32, Box<dyn std::error::Error>> {
        // TODO: Actual network speed test
        Ok(100.0) // Example: 100 Mbps
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiningStats {
    pub is_mining: bool,
    pub network: NetworkType,
    pub total_earned: f64,
    pub current_jobs: usize,
    pub performance: PerformanceStats,
}

impl Default for PerformanceStats {
    fn default() -> Self {
        Self {
            gpu_tflops: 0.0,
            cpu_gflops: 0.0,
            ram_gb: 0.0,
            vram_gb: 0.0,
            network_mbps: 0.0,
            jobs_completed: 0,
            jobs_failed: 0,
            uptime_hours: 0.0,
            reputation_score: 100.0,
        }
    }
}

/// Initialize mining with default settings
pub async fn init_mining() -> Result<MiningManager, Box<dyn std::error::Error>> {
    let config_path = dirs::home_dir()
        .expect("Could not find home directory")
        .join(".hanzo/config/mining.toml");

    let config = if config_path.exists() {
        let contents = std::fs::read_to_string(&config_path)?;
        toml::from_str(&contents)?
    } else {
        let config = MiningConfig::default();
        std::fs::create_dir_all(config_path.parent().unwrap())?;
        std::fs::write(&config_path, toml::to_string_pretty(&config)?)?;
        config
    };

    let manager = MiningManager::new(config);

    if manager.config.read().await.auto_start {
        let _ = manager.start_mining().await;
    }

    Ok(manager)
}

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

    #[test]
    fn test_network_endpoints() {
        assert_eq!(NetworkType::HanzoMainnet.rpc_endpoint(), "https://rpc.hanzo.network");
        assert_eq!(NetworkType::ZooMainnet.rpc_endpoint(), "https://rpc.zoo.network");
        assert_eq!(NetworkType::HanzoMainnet.native_token(), "HAI");
        assert_eq!(NetworkType::ZooMainnet.native_token(), "ZOO");
        assert_eq!(NetworkType::HanzoMainnet.chain_id(), 36963);
        assert_eq!(NetworkType::ZooMainnet.chain_id(), 200200);
    }

    #[tokio::test]
    async fn test_mining_manager() {
        let config = MiningConfig {
            enabled: false, // Don't actually start mining in tests
            ..Default::default()
        };

        let manager = MiningManager::new(config);
        let stats = manager.get_stats().await;
        assert!(!stats.is_mining);
        assert_eq!(stats.total_earned, 0.0);
    }
}