ore-cli 3.4.15

A command line interface for ORE cryptocurrency mining.
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use std::{
    io::stdout,
    sync::{Arc, RwLock},
    thread::sleep,
    time::{Duration, Instant},
    usize,
};

use b64::FromBase64;
use colored::*;
use crossterm::{
    cursor::MoveTo,
    execute,
    terminal::{Clear, ClearType},
};
use drillx::{
    equix::{self},
    Hash, Solution,
};
use ore_api::{
    consts::{BUS_ADDRESSES, BUS_COUNT, EPOCH_DURATION},
    event::MineEvent,
    state::{proof_pda, Bus, Config},
};
use rand::Rng;
use solana_program::pubkey::Pubkey;
use solana_rpc_client::spinner;
use solana_sdk::{signature::Signature, signer::Signer};
use solana_transaction_status::{option_serializer::OptionSerializer, UiTransactionEncoding};
use steel::AccountDeserialize;
use tabled::{
    settings::{
        object::{Columns, Rows},
        style::BorderColor,
        Alignment, Border, Color, Highlight, Remove, Style,
    },
    Table,
};

use crate::{
    args::MineArgs,
    error::Error,
    utils::{
        amount_u64_to_f64, format_duration, format_timestamp, get_clock, get_config,
        get_updated_proof_with_authority, ComputeBudget, PoolMiningData, SoloMiningData,
    },
    Miner,
};

use super::pool::Pool;

impl Miner {
    pub async fn mine(&self, args: MineArgs) -> Result<(), Error> {
        match args.pool_url {
            Some(ref pool_url) => {
                let pool = &Pool {
                    http_client: reqwest::Client::new(),
                    pool_url: pool_url.clone(),
                };
                self.mine_pool(args, pool).await?;
            }
            None => {
                self.mine_solo(args).await;
            }
        }
        Ok(())
    }

    async fn mine_solo(&self, args: MineArgs) {
        // Open account, if needed.
        self.open().await;

        // Check num threads
        let cores_str = args.cores;
        let cores = if cores_str == "ALL" {
            num_cpus::get() as u64
        } else {
            cores_str.parse::<u64>().unwrap()
        };
        self.check_num_cores(cores);

        // Get verbose flag
        let verbose = args.verbose;

        // Generate addresses
        let signer = self.signer();
        let _proof_address = proof_pda(signer.pubkey()).0;
        let boost_config_address = ore_boost_api::state::config_pda().0;

        // Start mining loop
        let mut last_hash_at = 0;
        loop {
            // Fetch accounts
            let config = get_config(&self.rpc_client).await;
            let proof =
                get_updated_proof_with_authority(&self.rpc_client, signer.pubkey(), last_hash_at)
                    .await
                    .expect("Failed to fetch proof account");

            // Log mining table
            self.update_solo_mining_table(verbose);

            // Track timestamp
            last_hash_at = proof.last_hash_at;

            // Calculate cutoff time
            let cutoff_time = self.get_cutoff(proof.last_hash_at, args.buffer_time).await;

            // Build nonce indices
            let mut nonce_indices = Vec::with_capacity(cores as usize);
            for n in 0..(cores) {
                let nonce = u64::MAX.saturating_div(cores).saturating_mul(n);
                nonce_indices.push(nonce);
            }

            // Run drillx
            let solution = Self::find_hash_par(
                proof.challenge,
                cutoff_time,
                cores,
                config.min_difficulty as u32,
                nonce_indices.as_slice(),
                None,
            )
            .await;

            // Build instruction set
            let mut ixs = vec![ore_api::sdk::auth(proof_pda(signer.pubkey()).0)];
            let mut compute_budget = 750_000;

            // Check for reset
            if self.should_reset(config).await
            // && rand::thread_rng().gen_range(0..100).eq(&0)
            {
                compute_budget += 100_000;
                ixs.push(ore_api::sdk::reset(signer.pubkey()));
            }

            // Build mine ix
            let mine_ix = ore_api::sdk::mine(
                signer.pubkey(),
                signer.pubkey(),
                self.find_bus().await,
                solution,
                boost_config_address,
            );
            ixs.push(mine_ix);

            // Submit transaction
            match self
                .send_and_confirm(&ixs, ComputeBudget::Fixed(compute_budget), false)
                .await
            {
                Ok(sig) => self.fetch_solo_mine_event(sig, verbose).await,
                Err(err) => {
                    let mining_data = SoloMiningData::failed();
                    let mut data = self.solo_mining_data.write().unwrap();
                    if !data.is_empty() {
                        data.remove(0);
                    }
                    data.insert(0, mining_data);
                    drop(data);

                    // Log mining table
                    self.update_solo_mining_table(verbose);
                    println!("{}: {}", "ERROR".bold().red(), err);

                    return;
                }
            }
        }
    }

    async fn mine_pool(&self, args: MineArgs, pool: &Pool) -> Result<(), Error> {
        // Register, if needed
        let pool_member = pool.post_pool_register(self).await?;
        let nonce_index = pool_member.id as u64;

        // Get device id
        let device_id = args.device_id.unwrap_or(0);

        // Get verbose flag
        let verbose = args.verbose;

        // Check num threads
        let cores = self.parse_cores(args.cores);
        self.check_num_cores(cores);

        // Init channel for continuous submission
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<Solution>();
        tokio::spawn({
            let miner = self.clone();
            let pool = pool.clone();
            async move {
                while let Some(solution) = rx.recv().await {
                    if let Err(err) = pool.post_pool_solution(&miner, &solution).await {
                        println!("error submitting solution: {:?}", err);
                    }
                }
            }
        });

        // Start mining loop
        let mut last_hash_at = 0;
        loop {
            // Fetch latest challenge
            let member_challenge = match pool.get_updated_pool_challenge(self, last_hash_at).await {
                Err(_err) => {
                    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
                    continue;
                }
                Ok(member_challenge) => member_challenge,
            };

            // Log mining table
            self.update_pool_mining_table(verbose);

            // Increment last balance and hash
            last_hash_at = member_challenge.challenge.lash_hash_at;

            // Compute cutoff time
            let cutoff_time = self.get_cutoff(last_hash_at, args.buffer_time).await;

            // Build nonce indices
            let num_total_members = member_challenge.num_total_members.max(1);
            let member_search_space_size = u64::MAX.saturating_div(num_total_members);
            let device_search_space_size =
                member_search_space_size.saturating_div(member_challenge.num_devices as u64);

            // Check device id doesn't go beyond pool limit
            if (device_id as u8) > member_challenge.num_devices {
                return Err(Error::TooManyDevices);
            }

            // Calculate bounds on nonce space
            let left_bound = member_search_space_size.saturating_mul(nonce_index)
                + device_id.saturating_mul(device_search_space_size);

            // Split nonce-device space for muliple cores
            let range_per_core = device_search_space_size.saturating_div(cores);
            let mut nonce_indices = Vec::with_capacity(cores as usize);
            for n in 0..(cores) {
                let index = left_bound + n * range_per_core;
                nonce_indices.push(index);
            }

            // Run drillx
            let solution = Self::find_hash_par(
                member_challenge.challenge.challenge,
                cutoff_time,
                cores,
                member_challenge.challenge.min_difficulty as u32,
                nonce_indices.as_slice(),
                Some(tx.clone()),
            )
            .await;

            // Post solution to pool server
            match pool.post_pool_solution(self, &solution).await {
                Err(_err) => {
                    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
                    continue;
                }
                Ok(()) => {
                    self.fetch_pool_mine_event(pool, last_hash_at, verbose)
                        .await;
                }
            }
        }
    }

    async fn find_hash_par(
        challenge: [u8; 32],
        cutoff_time: u64,
        cores: u64,
        min_difficulty: u32,
        nonce_indices: &[u64],
        pool_channel: Option<tokio::sync::mpsc::UnboundedSender<Solution>>,
    ) -> Solution {
        // Dispatch job to each thread
        let progress_bar = Arc::new(spinner::new_progress_bar());
        let global_best_difficulty = Arc::new(RwLock::new(0u32));

        progress_bar.set_message("Mining...");
        let core_ids = core_affinity::get_core_ids().expect("Failed to fetch core count");
        let core_ids = core_ids.into_iter().filter(|id| id.id < (cores as usize));
        let handles: Vec<_> = core_ids
            .map(|i| {
                let global_best_difficulty = Arc::clone(&global_best_difficulty);
                std::thread::spawn({
                    let progress_bar = progress_bar.clone();
                    let nonce = nonce_indices[i.id];
                    let mut memory = equix::SolverMemory::new();
                    let pool_channel = pool_channel.clone();
                    move || {
                        // Pin to core
                        let _ = core_affinity::set_for_current(i);

                        // Start hashing
                        let timer = Instant::now();
                        let mut nonce = nonce;
                        let mut best_nonce = nonce;
                        let mut best_difficulty = 0;
                        let mut best_hash = Hash::default();
                        loop {
                            // Get hashes
                            let hxs = drillx::hashes_with_memory(
                                &mut memory,
                                &challenge,
                                &nonce.to_le_bytes(),
                            );

                            // Look for best difficulty score in all hashes
                            for hx in hxs {
                                let difficulty = hx.difficulty();
                                if difficulty.gt(&best_difficulty) {
                                    best_nonce = nonce;
                                    best_difficulty = difficulty;
                                    best_hash = hx;
                                    if best_difficulty.gt(&*global_best_difficulty.read().unwrap())
                                    {
                                        // Update best global difficulty
                                        *global_best_difficulty.write().unwrap() = best_difficulty;

                                        // Continuously upload best solution to pool
                                        if difficulty.ge(&min_difficulty) {
                                            if let Some(ref ch) = pool_channel {
                                                let digest = best_hash.d;
                                                let nonce = nonce.to_le_bytes();
                                                let solution = Solution {
                                                    d: digest,
                                                    n: nonce,
                                                };
                                                if let Err(err) = ch.send(solution) {
                                                    println!("{} {:?}", "ERROR".bold().red(), err);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // Exit if time has elapsed
                            if nonce % 100 == 0 {
                                let global_best_difficulty =
                                    *global_best_difficulty.read().unwrap();
                                if timer.elapsed().as_secs().ge(&cutoff_time) {
                                    if i.id == 0 {
                                        progress_bar.set_message(format!(
                                            "Mining...\n  Best score: {}",
                                            global_best_difficulty,
                                        ));
                                    }
                                    if global_best_difficulty.ge(&min_difficulty) {
                                        // Mine until min difficulty has been met
                                        break;
                                    }
                                } else if i.id == 0 {
                                    progress_bar.set_message(format!(
                                        "Mining...\n  Best score: {}\n  Time remaining: {}",
                                        global_best_difficulty,
                                        format_duration(
                                            cutoff_time.saturating_sub(timer.elapsed().as_secs())
                                                as u32
                                        ),
                                    ));
                                }
                            }

                            // Increment nonce
                            nonce += 1;
                        }

                        // Return the best nonce
                        (best_nonce, best_difficulty, best_hash)
                    }
                })
            })
            .collect();

        // Join handles and return best nonce
        let mut best_nonce: u64 = 0;
        let mut best_difficulty = 0;
        let mut best_hash = Hash::default();
        for h in handles {
            if let Ok((nonce, difficulty, hash)) = h.join() {
                if difficulty > best_difficulty {
                    best_difficulty = difficulty;
                    best_nonce = nonce;
                    best_hash = hash;
                }
            }
        }

        Solution::new(best_hash.d, best_nonce.to_le_bytes())
    }

    pub fn parse_cores(&self, cores: String) -> u64 {
        if cores == "ALL" {
            num_cpus::get() as u64
        } else {
            cores.parse::<u64>().unwrap()
        }
    }

    pub fn check_num_cores(&self, cores: u64) {
        let num_cores = num_cpus::get() as u64;
        if cores.gt(&num_cores) {
            println!(
                "{} Cannot exceeds available cores ({})",
                "WARNING".bold().yellow(),
                num_cores
            );
        }
    }

    async fn should_reset(&self, config: Config) -> bool {
        let clock = get_clock(&self.rpc_client)
            .await
            .expect("Failed to fetch clock account");
        config
            .last_reset_at
            .saturating_add(EPOCH_DURATION)
            .saturating_sub(5) // Buffer
            .le(&clock.unix_timestamp)
    }

    async fn get_cutoff(&self, last_hash_at: i64, buffer_time: u64) -> u64 {
        let clock = get_clock(&self.rpc_client)
            .await
            .expect("Failed to fetch clock account");
        last_hash_at
            .saturating_add(60)
            .saturating_sub(buffer_time as i64)
            .saturating_sub(clock.unix_timestamp)
            .max(0) as u64
    }

    async fn find_bus(&self) -> Pubkey {
        // Fetch the bus with the largest balance
        if let Ok(accounts) = self.rpc_client.get_multiple_accounts(&BUS_ADDRESSES).await {
            let mut top_bus_balance: u64 = 0;
            let mut top_bus = BUS_ADDRESSES[0];
            for account in accounts {
                if let Some(account) = account {
                    if let Ok(bus) = Bus::try_from_bytes(&account.data) {
                        if bus.rewards.gt(&top_bus_balance) {
                            top_bus_balance = bus.rewards;
                            top_bus = BUS_ADDRESSES[bus.id as usize];
                        }
                    }
                }
            }
            return top_bus;
        }

        // Otherwise return a random bus
        let i = rand::thread_rng().gen_range(0..BUS_COUNT);
        BUS_ADDRESSES[i]
    }

    async fn fetch_solo_mine_event(&self, sig: Signature, verbose: bool) {
        // Add loading row
        let mining_data = SoloMiningData::fetching(sig);
        let mut data = self.solo_mining_data.write().unwrap();
        data.insert(0, mining_data);
        if !data.is_empty() {
            data.remove(0);
        }
        drop(data);

        // Update table
        self.update_solo_mining_table(verbose);

        // Poll for transaction
        let mut tx;
        let mut attempts = 0;
        loop {
            tx = self
                .rpc_client
                .get_transaction(&sig, UiTransactionEncoding::Json)
                .await;
            if tx.is_ok() {
                break;
            }
            sleep(Duration::from_secs(1));
            attempts += 1;
            if attempts > 30 {
                break;
            }
        }

        // Parse transaction response
        if let Ok(tx) = tx {
            if let Some(meta) = tx.transaction.meta {
                if let OptionSerializer::Some(log_messages) = meta.log_messages {
                    if let Some(return_log) = log_messages
                        .iter()
                        .find(|log| log.starts_with("Program return: "))
                    {
                        if let Some(return_data) =
                            return_log.strip_prefix(&format!("Program return: {} ", ore_api::ID))
                        {
                            if let Ok(return_data) = return_data.from_base64() {
                                let mut data = self.solo_mining_data.write().unwrap();
                                let event = MineEvent::from_bytes(&return_data);
                                let mining_data = SoloMiningData {
                                    signature: if verbose {
                                        sig.to_string()
                                    } else {
                                        format!("{}...", sig.to_string()[..8].to_string())
                                    },
                                    block: tx.slot.to_string(),
                                    timestamp: format_timestamp(tx.block_time.unwrap_or_default()),
                                    difficulty: event.difficulty.to_string(),
                                    base_reward: if event.net_base_reward > 0 {
                                        format!("{:#.11}", amount_u64_to_f64(event.net_base_reward))
                                    } else {
                                        "0".to_string()
                                    },
                                    boost_reward: if event.net_miner_boost_reward > 0 {
                                        format!(
                                            "{:#.11}",
                                            amount_u64_to_f64(event.net_miner_boost_reward)
                                        )
                                    } else {
                                        "0".to_string()
                                    },
                                    total_reward: if event.net_reward > 0 {
                                        format!("{:#.11}", amount_u64_to_f64(event.net_reward))
                                    } else {
                                        "0".to_string()
                                    },
                                    timing: format!("{}s", event.timing),
                                    status: "Confirmed".bold().green().to_string(),
                                };
                                data.insert(0, mining_data);
                            }
                        }
                    }
                }
            }
        }
    }

    async fn fetch_pool_mine_event(&self, pool: &Pool, last_hash_at: i64, verbose: bool) {
        let mining_data = match pool
            .get_latest_pool_event(self.signer().pubkey(), last_hash_at)
            .await
        {
            Ok(event) => PoolMiningData {
                signature: if verbose {
                    event.signature.to_string()
                } else {
                    format!("{}...", event.signature.to_string()[..8].to_string())
                },
                block: event.block.to_string(),
                timestamp: format_timestamp(event.timestamp as i64),
                timing: format!("{}s", event.timing),
                difficulty: event.difficulty.to_string(),
                base_reward: if event.net_base_reward > 0 {
                    format!("{:#.11}", amount_u64_to_f64(event.net_base_reward))
                } else {
                    "0".to_string()
                },
                boost_reward: if event.net_miner_boost_reward > 0 {
                    format!("{:#.11}", amount_u64_to_f64(event.net_miner_boost_reward))
                } else {
                    "0".to_string()
                },
                total_reward: if event.net_reward > 0 {
                    format!("{:#.11}", amount_u64_to_f64(event.net_reward))
                } else {
                    "0".to_string()
                },
                my_difficulty: event.member_difficulty.to_string(),
                my_reward: if event.member_reward > 0 {
                    format!("{:#.11}", amount_u64_to_f64(event.member_reward))
                } else {
                    "0".to_string()
                },
            },
            Err(err) => PoolMiningData {
                signature: format!("Failed to fetch event: {:?}", err),
                block: "".to_string(),
                timestamp: "".to_string(),
                timing: "".to_string(),
                difficulty: "".to_string(),
                base_reward: "".to_string(),
                boost_reward: "".to_string(),
                total_reward: "".to_string(),
                my_difficulty: "".to_string(),
                my_reward: "".to_string(),
            },
        };

        // Add row
        let mut data = self.pool_mining_data.write().unwrap();
        data.insert(0, mining_data);
        if data.len() >= 12 {
            data.pop();
        }
        drop(data);
    }

    fn update_solo_mining_table(&self, verbose: bool) {
        execute!(stdout(), Clear(ClearType::All), MoveTo(0, 0)).unwrap();
        let mut rows: Vec<SoloMiningData> = vec![];
        let data = self.solo_mining_data.read().unwrap();
        rows.extend(data.iter().cloned());
        let mut table = Table::new(&rows);
        table.with(Style::blank());
        table.modify(Columns::new(1..), Alignment::right());
        table.modify(Rows::first(), Color::BOLD);
        table.with(
            Highlight::new(Rows::single(1)).color(BorderColor::default().top(Color::FG_WHITE)),
        );
        table.with(Highlight::new(Rows::single(1)).border(Border::new().top('')));
        if !verbose {
            table.with(Remove::column(Columns::new(1..3)));
        }
        println!("\n{}\n", table);
    }

    fn update_pool_mining_table(&self, verbose: bool) {
        execute!(stdout(), Clear(ClearType::All), MoveTo(0, 0)).unwrap();
        let mut rows: Vec<PoolMiningData> = vec![];
        let data = self.pool_mining_data.read().unwrap();
        rows.extend(data.iter().cloned());
        let mut table = Table::new(&rows);
        table.with(Style::blank());
        table.modify(Columns::new(1..), Alignment::right());
        table.modify(Rows::first(), Color::BOLD);
        table.with(
            Highlight::new(Rows::single(1)).color(BorderColor::default().top(Color::FG_WHITE)),
        );
        table.with(Highlight::new(Rows::single(1)).border(Border::new().top('')));
        if !verbose {
            table.with(Remove::column(Columns::new(1..3)));
        }
        println!("\n{}\n", table);
    }

    async fn open(&self) {
        // Register miner
        let mut ixs = Vec::new();
        let signer = self.signer();
        let fee_payer = self.fee_payer();
        let proof_address = proof_pda(signer.pubkey()).0;
        if self.rpc_client.get_account(&proof_address).await.is_err() {
            let ix = ore_api::sdk::open(signer.pubkey(), signer.pubkey(), fee_payer.pubkey());
            ixs.push(ix);
        }

        // Submit transaction
        if ixs.len() > 0 {
            self.send_and_confirm(&ixs, ComputeBudget::Fixed(400_000), false)
                .await
                .ok();
        }
    }
}