Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions change_difficulty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
perl -pi -e "s|const MIN_DIFFICULTY: u32 =.*|const MIN_DIFFICULTY: u32 = $1;|g" src/mine.rs
cargo build --release
cp target/release/ore ~/.cargo/bin
3 changes: 3 additions & 0 deletions change_gateway_retry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
perl -pi -e "s|const GATEWAY_RETRIES: usize =.*|const GATEWAY_RETRIES: usize = $1;|g" src/send_and_confirm.rs
cargo build --release
cp target/release/ore ~/.cargo/bin
39 changes: 32 additions & 7 deletions src/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand::Rng;
use solana_program::pubkey::Pubkey;
use solana_rpc_client::spinner;
use solana_sdk::signer::Signer;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::{
args::MineArgs,
Expand All @@ -21,6 +22,8 @@ use crate::{
Miner,
};

const MIN_DIFFICULTY: u32 = 16;

impl Miner {
pub async fn mine(&self, args: MineArgs) {
// Register, if needed.
Expand All @@ -33,24 +36,31 @@ impl Miner {
// Start mining loop
loop {
// Fetch proof
let config = get_config(&self.rpc_client).await;
let proof = get_proof_with_authority(&self.rpc_client, signer.pubkey()).await;
println!(
"\nStake balance: {} ORE",
amount_u64_to_string(proof.balance)
"\nStake: {} ORE\n Multiplier: {:12}x",
amount_u64_to_string(proof.balance),
calculate_multiplier(proof.balance, config.top_balance)
);

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

// Run drillx
let config = get_config(&self.rpc_client).await;
let solution = Self::find_hash_par(
proof,
cutoff_time,
args.threads,
config.min_difficulty as u32,
)
.await;
).await;

// Check if the solution is "default" (indicating it was skipped)
if solution.d == [0u8; 16] && solution.n == [0u8; 8] {
// Print a skip message and continue to the next loop iteration
println!("Skipped: got a default solution.");
continue;
}

// Submit most difficult hash
let mut compute_budget = 500_000;
Expand All @@ -69,6 +79,7 @@ impl Miner {
.await
.ok();
}

}

async fn find_hash_par(
Expand All @@ -88,7 +99,11 @@ impl Miner {
let mut memory = equix::SolverMemory::new();
move || {
let timer = Instant::now();
let time_now = SystemTime::now().duration_since(UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH")
.as_micros() as u64;
let mut nonce = u64::MAX.saturating_div(threads).saturating_mul(i);
nonce = time_now ^ nonce;
let mut best_nonce = nonce;
let mut best_difficulty = 0;
let mut best_hash = Hash::default();
Expand All @@ -110,7 +125,7 @@ impl Miner {
// Exit if time has elapsed
if nonce % 100 == 0 {
if timer.elapsed().as_secs().ge(&cutoff_time) {
if best_difficulty.gt(&min_difficulty) {
if best_difficulty.ge(&min_difficulty) {
// Mine until min difficulty has been met
break;
}
Expand Down Expand Up @@ -147,6 +162,12 @@ impl Miner {
}
}

// Check if the best difficulty meets the minimum requirement
if best_difficulty < MIN_DIFFICULTY {
progress_bar.finish_with_message(format!("Skipped: (difficulty: {} < {})", best_difficulty, MIN_DIFFICULTY));
return Solution::new([0u8; 16], [0u8; 8]); // Return a "default" Solution
}

// Update log
progress_bar.finish_with_message(format!(
"Best hash: {} (difficulty: {})",
Expand Down Expand Up @@ -190,8 +211,12 @@ impl Miner {
}
}

fn calculate_multiplier(balance: u64, top_balance: u64) -> f64 {
1.0 + (balance as f64 / top_balance as f64).min(1.0f64)
}

// TODO Pick a better strategy (avoid draining bus)
fn find_bus() -> Pubkey {
let i = rand::thread_rng().gen_range(0..BUS_COUNT);
BUS_ADDRESSES[i]
}
}
2 changes: 1 addition & 1 deletion src/send_and_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Miner {
// Submit tx
let mut attempts = 0;
loop {
progress_bar.set_message(format!("Submitting transaction... (attempt {})", attempts));
progress_bar.set_message(format!("Submitting transaction... (attempt {}, max {})", attempts, GATEWAY_RETRIES));
match client.send_transaction_with_config(&tx, send_cfg).await {
Ok(sig) => {
// Skip confirmation
Expand Down