From 55df0eef915b33de2e522136f2b0d8bc14455a4a Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Tue, 6 Aug 2024 15:44:42 +0800 Subject: [PATCH 01/10] add skip_difficulty --- src/mine.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index 46cf1da1..40dde2bd 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -110,7 +110,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; } @@ -126,8 +126,15 @@ impl Miner { nonce += 1; } + // If best_difficulty is below min_difficulty, skip submitting the result + let mut skip_difficulty = 20; + if best_difficulty < skip_difficulty { + println!("Thread {}: Skip (difficulty: {} < {})", i, best_difficulty, skip_difficulty); + return None; + } + // Return the best nonce - (best_nonce, best_difficulty, best_hash) + Some((best_nonce, best_difficulty, best_hash)) } }) }) @@ -138,11 +145,13 @@ impl Miner { 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; + if let Ok(result) = h.join() { + if let Some((nonce, difficulty, hash)) = result { + if difficulty > best_difficulty { + best_difficulty = difficulty; + best_nonce = nonce; + best_hash = hash; + } } } } From 0985ad5fbe557d30bc924eb8db86b527554a3d89 Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Tue, 6 Aug 2024 15:58:36 +0800 Subject: [PATCH 02/10] add MIN_DIFFICULTY --- src/mine.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index 40dde2bd..b2e8a7c4 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -20,6 +20,7 @@ use crate::{ utils::{amount_u64_to_string, get_clock, get_config, get_proof_with_authority, proof_pubkey}, Miner, }; +const MIN_DIFFICULTY: u32 = 20; impl Miner { pub async fn mine(&self, args: MineArgs) { @@ -110,7 +111,7 @@ impl Miner { // Exit if time has elapsed if nonce % 100 == 0 { if timer.elapsed().as_secs().ge(&cutoff_time) { - if best_difficulty.ge(&min_difficulty) { + if best_difficulty.gt(&MIN_DIFFICULTY) { // Mine until min difficulty has been met break; } @@ -126,15 +127,8 @@ impl Miner { nonce += 1; } - // If best_difficulty is below min_difficulty, skip submitting the result - let mut skip_difficulty = 20; - if best_difficulty < skip_difficulty { - println!("Thread {}: Skip (difficulty: {} < {})", i, best_difficulty, skip_difficulty); - return None; - } - // Return the best nonce - Some((best_nonce, best_difficulty, best_hash)) + (best_nonce, best_difficulty, best_hash) } }) }) @@ -145,13 +139,11 @@ impl Miner { let mut best_difficulty = 0; let mut best_hash = Hash::default(); for h in handles { - if let Ok(result) = h.join() { - if let Some((nonce, difficulty, hash)) = result { - if difficulty > best_difficulty { - best_difficulty = difficulty; - best_nonce = nonce; - best_hash = hash; - } + if let Ok((nonce, difficulty, hash)) = h.join() { + if difficulty > best_difficulty { + best_difficulty = difficulty; + best_nonce = nonce; + best_hash = hash; } } } From 1f39d894f95823212653ec1be346491d0de2d204 Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Tue, 6 Aug 2024 16:21:56 +0800 Subject: [PATCH 03/10] change_difficulty.sh --- change_difficulty.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 change_difficulty.sh diff --git a/change_difficulty.sh b/change_difficulty.sh new file mode 100644 index 00000000..c8ca6185 --- /dev/null +++ b/change_difficulty.sh @@ -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 \ No newline at end of file From 5e5cc1781409afee3a3f63286a312b4c55f6a2fb Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 10:04:08 +0800 Subject: [PATCH 04/10] skip when MIN_DIFFICULTY --- src/mine.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index b2e8a7c4..d34f1396 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -20,7 +20,8 @@ use crate::{ utils::{amount_u64_to_string, get_clock, get_config, get_proof_with_authority, proof_pubkey}, Miner, }; -const MIN_DIFFICULTY: u32 = 20; + +const MIN_DIFFICULTY: u32 = 16; impl Miner { pub async fn mine(&self, args: MineArgs) { @@ -34,24 +35,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 == Default::default() && 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; @@ -70,6 +78,7 @@ impl Miner { .await .ok(); } + } async fn find_hash_par( @@ -111,7 +120,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; } @@ -148,6 +157,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(Default::default(), [0u8; 8]); // Return a "default" Solution + } + // Update log progress_bar.finish_with_message(format!( "Best hash: {} (difficulty: {})", @@ -191,8 +206,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] -} +} \ No newline at end of file From a50ed9c852801d88d723757573b026db15407e04 Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 10:16:32 +0800 Subject: [PATCH 05/10] skip when MIN_DIFFICULTY --- src/mine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index d34f1396..d44626f2 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -55,7 +55,7 @@ impl Miner { ).await; // Check if the solution is "default" (indicating it was skipped) - if solution.d == Default::default() && solution.n == [0u8; 8] { + 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; @@ -160,7 +160,7 @@ 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(Default::default(), [0u8; 8]); // Return a "default" Solution + return Solution::new([0u8; 16], [0u8; 8]); // Return a "default" Solution } // Update log From 5669554d694be5759eac3f4a6e22d87a45a1866d Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 11:02:09 +0800 Subject: [PATCH 06/10] nonce with time --- src/mine.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mine.rs b/src/mine.rs index d44626f2..bb72af8f 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -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, @@ -98,7 +99,8 @@ impl Miner { let mut memory = equix::SolverMemory::new(); move || { let timer = Instant::now(); - let mut nonce = u64::MAX.saturating_div(threads).saturating_mul(i); + let time_now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + let mut nonce = time_now ^ (u64::MAX.saturating_div(threads).saturating_mul(i)); let mut best_nonce = nonce; let mut best_difficulty = 0; let mut best_hash = Hash::default(); From fc7423eb53d36a174a3ac6cf4f84ccfefe0e9583 Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 11:21:57 +0800 Subject: [PATCH 07/10] random nonce --- src/mine.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index bb72af8f..af18759b 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -13,7 +13,6 @@ 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, @@ -99,8 +98,10 @@ impl Miner { let mut memory = equix::SolverMemory::new(); move || { let timer = Instant::now(); - let time_now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); - let mut nonce = time_now ^ (u64::MAX.saturating_div(threads).saturating_mul(i)); + let mut nonce = u64::MAX.saturating_div(threads).saturating_mul(i); + // 生成随机 nonce + let mut rng = rand::thread_rng(); + let random_nonce: u64 = rng.gen(); let mut best_nonce = nonce; let mut best_difficulty = 0; let mut best_hash = Hash::default(); @@ -109,11 +110,11 @@ impl Miner { if let Ok(hx) = drillx::hash_with_memory( &mut memory, &proof.challenge, - &nonce.to_le_bytes(), + &random_nonce.to_le_bytes(), ) { let difficulty = hx.difficulty(); if difficulty.gt(&best_difficulty) { - best_nonce = nonce; + best_nonce = random_nonce; best_difficulty = difficulty; best_hash = hx; } From f21afa0a6c2bb5ae4f36805ae636b6bde499d65f Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 11:28:02 +0800 Subject: [PATCH 08/10] random nonce --- src/mine.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/mine.rs b/src/mine.rs index af18759b..9a8ddb65 100644 --- a/src/mine.rs +++ b/src/mine.rs @@ -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, @@ -98,10 +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 - let mut rng = rand::thread_rng(); - let random_nonce: u64 = rng.gen(); + nonce = time_now ^ nonce; let mut best_nonce = nonce; let mut best_difficulty = 0; let mut best_hash = Hash::default(); @@ -110,11 +112,11 @@ impl Miner { if let Ok(hx) = drillx::hash_with_memory( &mut memory, &proof.challenge, - &random_nonce.to_le_bytes(), + &nonce.to_le_bytes(), ) { let difficulty = hx.difficulty(); if difficulty.gt(&best_difficulty) { - best_nonce = random_nonce; + best_nonce = nonce; best_difficulty = difficulty; best_hash = hx; } From 0723ae8d00d5e41b656224f49989b7014a37b9af Mon Sep 17 00:00:00 2001 From: root Date: Wed, 7 Aug 2024 03:36:43 +0000 Subject: [PATCH 09/10] tool --- change_gateway_retry.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 change_gateway_retry.sh diff --git a/change_gateway_retry.sh b/change_gateway_retry.sh new file mode 100755 index 00000000..fd6f86fe --- /dev/null +++ b/change_gateway_retry.sh @@ -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 From 2a2b641e8a34bc6208bc381ecc92bbc455d2b88b Mon Sep 17 00:00:00 2001 From: flareskyqq Date: Wed, 7 Aug 2024 11:46:51 +0800 Subject: [PATCH 10/10] max --- change_gateway_retry.sh | 0 src/send_and_confirm.rs | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 change_gateway_retry.sh diff --git a/change_gateway_retry.sh b/change_gateway_retry.sh old mode 100755 new mode 100644 diff --git a/src/send_and_confirm.rs b/src/send_and_confirm.rs index b18eb3e8..6d0a3e84 100644 --- a/src/send_and_confirm.rs +++ b/src/send_and_confirm.rs @@ -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