Skip to content

Commit

Permalink
Add hashrate estimation for cpuminer
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3 authored and plebhash committed Apr 9, 2024
1 parent 47cf5da commit ba0fb5d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions roles/test-utils/mining-device/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ key-utils = { version = "^1.0.0", path = "../../../utils/key-utils" }
clap = { version = "^4.5.4", features = ["derive"] }
tracing = { version = "0.1" }
tracing-subscriber = "0.3"
sha2 = "0.10.6"
45 changes: 42 additions & 3 deletions roles/test-utils/mining-device/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::{net::SocketAddr, sync::Arc, thread::sleep, time::Duration};

use async_std::net::ToSocketAddrs;
use clap::Parser;
use rand::{thread_rng, Rng};
use sha2::{Digest, Sha256};
use std::time::Instant;
use stratum_common::bitcoin::{
blockdata::block::BlockHeader, hash_types::BlockHash, hashes::Hash, util::uint::Uint256,
};
Expand Down Expand Up @@ -107,7 +110,7 @@ async fn main() {
}

use async_channel::{Receiver, Sender};
use binary_sv2::u256_from_int;
use binary_sv2::{u256_from_int, U256};
use codec_sv2::{Frame, Initiator, StandardEitherFrame, StandardSv2Frame};
use roles_logic_sv2::{
common_messages_sv2::{Protocol, SetupConnection, SetupConnectionSuccess},
Expand Down Expand Up @@ -233,11 +236,14 @@ pub struct Device {
fn open_channel(device_id: Option<String>) -> OpenStandardMiningChannel<'static> {
let user_identity = device_id.unwrap_or_default().try_into().unwrap();
let id: u32 = 10;
info!("Misuring pc hashrate");
let nominal_hash_rate = measure_hashrate(5) as f32;
info!("Pc hashrate is {}", nominal_hash_rate);
info!("MINING DEVICE: send open channel with request id {}", id);
OpenStandardMiningChannel {
request_id: id.into(),
user_identity,
nominal_hash_rate: 1000.0, // use 1000 or 10000 to test group channels
nominal_hash_rate,
max_target: u256_from_int(567_u64),
}
}
Expand Down Expand Up @@ -308,7 +314,7 @@ impl Device {

loop {
let mut incoming: StdFrame = receiver.recv().await.unwrap().try_into().unwrap();
let message_type = dbg!(incoming.get_header().unwrap().msg_type());
let message_type = incoming.get_header().unwrap().msg_type();
let payload = incoming.payload();
let next = Device::handle_message_mining(
self_mutex.clone(),
Expand Down Expand Up @@ -605,3 +611,36 @@ impl Miner {
}
}
}

// returns hashrate based on how fast the device hashes over the given duration
fn measure_hashrate(duration_secs: u64) -> f64 {
let mut share = generate_random_80_byte_array();
let start_time = Instant::now();
let mut hashes: u64 = 0;
let duration = Duration::from_secs(duration_secs);

while start_time.elapsed() < duration {
for _ in 0..10000 {
hash(&mut share);
hashes += 1;
}
}

let elapsed_secs = start_time.elapsed().as_secs_f64();
hashes as f64 / elapsed_secs
}
fn generate_random_80_byte_array() -> [u8; 80] {
let mut rng = thread_rng();
let mut arr = [0u8; 80];
rng.fill(&mut arr[..]);
arr
}
fn hash(share: &mut [u8; 80]) -> Target {
let nonce: [u8; 8] = share[0..8].try_into().unwrap();
let mut nonce = u64::from_le_bytes(nonce);
nonce += 1;
share[0..8].copy_from_slice(&nonce.to_le_bytes());
let hash = Sha256::digest(&share).to_vec();
let hash: U256<'static> = hash.try_into().unwrap();
hash.into()
}

0 comments on commit ba0fb5d

Please sign in to comment.