@@ -291,6 +291,14 @@ fn diff_to_sv1_message(diff: f64) -> ProxyResult<'static, (json_rpc::Message, [u
291291 Ok ( ( message, target) )
292292}
293293
294+ pub fn nearest_power_of_10 ( x : f32 ) -> f32 {
295+ if x <= 0.0 {
296+ return 0.001 ;
297+ }
298+ let exponent = x. log10 ( ) . round ( ) as i32 ;
299+ 10f32 . powi ( exponent)
300+ }
301+
294302#[ cfg( test) ]
295303mod test {
296304 use super :: super :: super :: upstream:: diff_management:: UpstreamDifficultyConfig ;
@@ -314,7 +322,7 @@ mod test {
314322 let initial_nominal_hashrate = dbg ! ( measure_hashrate( 10 ) ) ;
315323 let target = match roles_logic_sv2:: utils:: hash_rate_to_target (
316324 initial_nominal_hashrate,
317- expected_shares_per_minute. into ( ) ,
325+ expected_shares_per_minute,
318326 ) {
319327 Ok ( target) => target,
320328 Err ( _) => panic ! ( ) ,
@@ -334,7 +342,7 @@ mod test {
334342 let calculated_share_per_min = count as f32 / ( elapsed. as_secs_f32 ( ) / 60.0 ) ;
335343 // This is the error margin for a confidence of 99% given the expect number of shares per
336344 // minute TODO the review the math under it
337- let error_margin = get_error ( expected_shares_per_minute. into ( ) ) ;
345+ let error_margin = get_error ( expected_shares_per_minute) ;
338346 let error =
339347 ( dbg ! ( calculated_share_per_min) - dbg ! ( expected_shares_per_minute as f32 ) ) . abs ( ) ;
340348 assert ! (
@@ -370,9 +378,7 @@ mod test {
370378 }
371379
372380 let elapsed_secs = start_time. elapsed ( ) . as_secs_f64 ( ) ;
373- let hashrate = hashes as f64 / elapsed_secs;
374- let nominal_hash_rate = hashrate;
375- nominal_hash_rate
381+ hashes as f64 / elapsed_secs
376382 }
377383
378384 fn hash ( share : & mut [ u8 ; 80 ] ) -> Target {
@@ -392,6 +398,12 @@ mod test {
392398 arr
393399 }
394400
401+ fn get_diff ( hashrate : f32 ) -> f32 {
402+ let share_per_second = crate :: SHARE_PER_MIN / 60.0 ;
403+ let initial_difficulty = hashrate / ( share_per_second * 2f32 . powf ( 32.0 ) ) ;
404+ crate :: translator:: downstream:: diff_management:: nearest_power_of_10 ( initial_difficulty)
405+ }
406+
395407 #[ tokio:: test]
396408 async fn test_converge_to_spm_from_low ( ) {
397409 test_converge_to_spm ( 1.0 ) . await
@@ -434,27 +446,16 @@ mod test {
434446 downstream. difficulty_mgmt . estimated_downstream_hash_rate = start_hashrate as f32 ;
435447
436448 let total_run_time = std:: time:: Duration :: from_secs ( 10 ) ;
437- let config_shares_per_minute = crate :: SHARE_PER_MIN ;
438449 let timer = std:: time:: Instant :: now ( ) ;
439450 let mut elapsed = std:: time:: Duration :: from_secs ( 0 ) ;
440451
441452 let expected_nominal_hashrate = measure_hashrate ( 5 ) ;
442- let expected_target = match roles_logic_sv2:: utils:: hash_rate_to_target (
443- expected_nominal_hashrate,
444- config_shares_per_minute. into ( ) ,
445- ) {
446- Ok ( target) => target,
447- Err ( _) => panic ! ( ) ,
448- } ;
453+ let expected_diff = get_diff ( expected_nominal_hashrate as f32 ) ;
454+ let expected_target: U256 = Downstream :: difficulty_to_target ( expected_diff) . into ( ) ;
449455
450456 let initial_nominal_hashrate = start_hashrate;
451- let mut initial_target = match roles_logic_sv2:: utils:: hash_rate_to_target (
452- initial_nominal_hashrate,
453- config_shares_per_minute. into ( ) ,
454- ) {
455- Ok ( target) => target,
456- Err ( _) => panic ! ( ) ,
457- } ;
457+ let initial_difficulty = get_diff ( initial_nominal_hashrate as f32 ) ;
458+ let mut initial_target: U256 = Downstream :: difficulty_to_target ( initial_difficulty) . into ( ) ;
458459 let downstream = Arc :: new ( Mutex :: new ( downstream) ) ;
459460 Downstream :: init_difficulty_management ( & downstream)
460461 . await
@@ -464,17 +465,8 @@ mod test {
464465 mock_mine ( initial_target. clone ( ) . into ( ) , & mut share) ;
465466 Downstream :: save_share ( downstream. clone ( ) ) . unwrap ( ) ;
466467 let _ = Downstream :: try_update_difficulty_settings ( & downstream) . await ;
467- initial_target = downstream
468- . safe_lock ( |d| {
469- match roles_logic_sv2:: utils:: hash_rate_to_target (
470- d. difficulty_mgmt . estimated_downstream_hash_rate . into ( ) ,
471- config_shares_per_minute. into ( ) ,
472- ) {
473- Ok ( target) => target,
474- Err ( _) => panic ! ( ) ,
475- }
476- } )
477- . unwrap ( ) ;
468+ initial_target =
469+ Downstream :: difficulty_to_target ( downstream_conf. current_difficulty ) . into ( ) ;
478470 elapsed = timer. elapsed ( ) ;
479471 }
480472 let expected_0s = trailing_0s ( expected_target. inner_as_ref ( ) . to_vec ( ) ) ;
@@ -492,11 +484,3 @@ mod test {
492484 // a share but we try to updated the estimated hash power every 2 seconds and updated the
493485 // target consequentially this shuold start to provide shares within a normal amount of time
494486}
495-
496- pub fn nearest_power_of_10 ( x : f32 ) -> f32 {
497- if x <= 0.0 {
498- return 0.001 ;
499- }
500- let exponent = x. log10 ( ) . round ( ) as i32 ;
501- 10f32 . powi ( exponent)
502- }
0 commit comments