diff --git a/roles/test-utils/mining-device/src/lib/mod.rs b/roles/test-utils/mining-device/src/lib/mod.rs index abfa1d9691..b1ce7a0498 100644 --- a/roles/test-utils/mining-device/src/lib/mod.rs +++ b/roles/test-utils/mining-device/src/lib/mod.rs @@ -42,6 +42,7 @@ pub async fn connect( device_id: Option, user_id: Option, handicap: u32, + nominal_hashrate_multiplier: Option, ) { let address = address .clone() @@ -77,7 +78,16 @@ pub async fn connect( .await .unwrap(); info!("Pool noise connection established at {}", address); - Device::start(receiver, sender, address, device_id, user_id, handicap).await + Device::start( + receiver, + sender, + address, + device_id, + user_id, + handicap, + nominal_hashrate_multiplier, + ) + .await } pub type Message = MiningDeviceMessages<'static>; @@ -193,13 +203,22 @@ pub struct Device { notify_changes_to_mining_thread: NewWorkNotifier, } -fn open_channel(device_id: Option) -> OpenStandardMiningChannel<'static> { +fn open_channel( + device_id: Option, + nominal_hashrate_multiplier: Option, + handicap: u32, +) -> OpenStandardMiningChannel<'static> { let user_identity = device_id.unwrap_or_default().try_into().unwrap(); let id: u32 = 10; info!("Measuring CPU hashrate"); let p = std::thread::available_parallelism().unwrap().get() as u32 - 3; - let nominal_hash_rate = measure_hashrate(5) as f32 * p as f32; - info!("Pc hashrate is {}", nominal_hash_rate); + let measured_hashrate = measure_hashrate(5, handicap) as f32 * p as f32; + info!("Measured CPU hashrate is {}", measured_hashrate); + let nominal_hash_rate = match nominal_hashrate_multiplier { + Some(m) => measured_hashrate * m, + None => measured_hashrate, + }; + info!("MINING DEVICE: send open channel with request id {}", id); OpenStandardMiningChannel { request_id: id.into(), @@ -217,6 +236,7 @@ impl Device { device_id: Option, user_id: Option, handicap: u32, + nominal_hashrate_multiplier: Option, ) { let setup_connection_handler = Arc::new(Mutex::new(SetupConnectionHandler::new())); SetupConnectionHandler::setup( @@ -244,8 +264,9 @@ impl Device { sender: notify_changes_to_mining_thread, }, }; - let open_channel = - MiningDeviceMessages::Mining(Mining::OpenStandardMiningChannel(open_channel(user_id))); + let open_channel = MiningDeviceMessages::Mining(Mining::OpenStandardMiningChannel( + open_channel(user_id, nominal_hashrate_multiplier, handicap), + )); let frame: StdFrame = open_channel.try_into().unwrap(); self_.sender.send(frame.into()).await.unwrap(); let self_mutex = std::sync::Arc::new(Mutex::new(self_)); @@ -597,7 +618,7 @@ impl NextShareOutcome { } // returns hashrate based on how fast the device hashes over the given duration -fn measure_hashrate(duration_secs: u64) -> f64 { +fn measure_hashrate(duration_secs: u64, handicap: u32) -> f64 { let mut rng = thread_rng(); let prev_hash: [u8; 32] = generate_random_32_byte_array().to_vec().try_into().unwrap(); let prev_hash = Hash::from_inner(prev_hash); @@ -619,7 +640,7 @@ fn measure_hashrate(duration_secs: u64) -> f64 { let start_time = Instant::now(); let mut hashes: u64 = 0; let duration = Duration::from_secs(duration_secs); - let mut miner = Miner::new(0); + let mut miner = Miner::new(handicap); // We put the target to 0 we are only interested in how many hashes per unit of time we can do // and do not want to be botherd by messages about valid shares found. miner.new_target(vec![0_u8; 32]); diff --git a/roles/test-utils/mining-device/src/main.rs b/roles/test-utils/mining-device/src/main.rs index 02a19c12f0..74bff1b3f2 100644 --- a/roles/test-utils/mining-device/src/main.rs +++ b/roles/test-utils/mining-device/src/main.rs @@ -39,6 +39,14 @@ struct Args { help = "User id, used when a new channel is opened, it can be used by the pool to identify the miner" )] id_user: Option, + #[arg( + long, + help = "This floating point number is used to modify the advertised nominal hashrate when opening a channel with the upstream.\ + \nIf 0.0 < nominal_hashrate_multiplier < 1.0, the CPU miner will advertise a nominal hashrate that is smaller than its real capacity.\ + \nIf nominal_hashrate_multiplier > 1.0, the CPU miner will advertise a nominal hashrate that is bigger than its real capacity.\ + \nIf empty, the CPU miner will simply advertise its real capacity." + )] + nominal_hashrate_multiplier: Option, } #[tokio::main(flavor = "current_thread")] @@ -52,6 +60,7 @@ async fn main() { args.id_device, args.id_user, args.handicap, + args.nominal_hashrate_multiplier, ) .await; }