@@ -2,7 +2,11 @@ use self::boosted_hex_eligibility::BoostedHexEligibility;
2
2
use crate :: {
3
3
boosting_oracles:: db:: check_for_unprocessed_data_sets,
4
4
coverage, data_session,
5
- heartbeats:: { self , location_cache:: LocationCache , HeartbeatReward } ,
5
+ heartbeats:: {
6
+ self ,
7
+ location_cache:: { self , LocationCache } ,
8
+ HeartbeatReward ,
9
+ } ,
6
10
radio_location_estimates, radio_threshold,
7
11
reward_shares:: {
8
12
self , CalculatedPocRewardShares , CoverageShares , DataTransferAndPocAllocatedRewardBuckets ,
@@ -21,6 +25,7 @@ use file_store::{
21
25
traits:: { FileSinkCommitStrategy , FileSinkRollTime , FileSinkWriteExt , TimestampEncode } ,
22
26
} ;
23
27
use futures_util:: TryFutureExt ;
28
+ use h3o:: { LatLng , Resolution } ;
24
29
use helium_proto:: {
25
30
reward_manifest:: RewardData :: MobileRewardData ,
26
31
services:: poc_mobile:: {
@@ -428,7 +433,7 @@ async fn reward_poc(
428
433
speedtest_avg_sink : & FileSinkClient < proto:: SpeedtestAvg > ,
429
434
reward_period : & Range < DateTime < Utc > > ,
430
435
reward_shares : DataTransferAndPocAllocatedRewardBuckets ,
431
- _location_cache : & LocationCache ,
436
+ location_cache : & LocationCache ,
432
437
) -> anyhow:: Result < ( Decimal , CalculatedPocRewardShares ) > {
433
438
let heartbeats = HeartbeatReward :: validated ( pool, reward_period) ;
434
439
let speedtest_averages =
@@ -455,6 +460,29 @@ async fn reward_poc(
455
460
)
456
461
. await ?;
457
462
463
+ {
464
+ let locations = location_cache. get_all ( ) . await ;
465
+ for ( key, value) in locations. iter ( ) {
466
+ let entity = location_cache:: key_to_entity ( key. clone ( ) ) ;
467
+ // Estimates are ordered by bigger radius first it should allow us to do less calculation
468
+ // and find a match faster
469
+ let estimates =
470
+ radio_location_estimates:: get_valid_estimates ( pool, & entity, dec ! ( 0.75 ) ) . await ?;
471
+ if estimates. is_empty ( ) {
472
+ // TODO we ban that key
473
+ todo ! ( )
474
+ } else {
475
+ match is_within_radius ( value. lat , value. lon , estimates) {
476
+ Ok ( true ) => todo ! ( ) ,
477
+ // TODO we ban that key
478
+ Ok ( false ) => todo ! ( ) ,
479
+ // TODO we ban that key
480
+ Err ( _) => todo ! ( ) ,
481
+ }
482
+ }
483
+ }
484
+ }
485
+
458
486
let coverage_shares = CoverageShares :: new (
459
487
pool,
460
488
heartbeats,
@@ -501,6 +529,46 @@ async fn reward_poc(
501
529
Ok ( ( unallocated_poc_amount, calculated_poc_rewards_per_share) )
502
530
}
503
531
532
+ fn is_within_radius (
533
+ loc_lat : f64 ,
534
+ loc_lon : f64 ,
535
+ estimates : Vec < ( Decimal , Decimal , Decimal ) > ,
536
+ ) -> anyhow:: Result < bool > {
537
+ let resolution = Resolution :: Twelve ;
538
+
539
+ let point_a = LatLng :: new ( loc_lat, loc_lon)
540
+ . map_err ( |e| anyhow:: anyhow!( "Invalid LatLng for A: {}" , e) ) ?;
541
+ let h3_index_a = point_a. to_cell ( resolution) ;
542
+
543
+ for ( radius_meters, lat, lon) in estimates {
544
+ let lat_f64 = lat
545
+ . to_f64 ( )
546
+ . ok_or_else ( || anyhow:: anyhow!( "Failed to convert lat_b to f64" ) ) ?;
547
+ let lon_f64 = lon
548
+ . to_f64 ( )
549
+ . ok_or_else ( || anyhow:: anyhow!( "Failed to convert lon_b to f64" ) ) ?;
550
+ let radius_meters_f64 = radius_meters
551
+ . to_f64 ( )
552
+ . ok_or_else ( || anyhow:: anyhow!( "Failed to convert radius_meters to f64" ) ) ?;
553
+
554
+ let point_b = LatLng :: new ( lat_f64, lon_f64)
555
+ . map_err ( |e| anyhow:: anyhow!( "Invalid LatLng for B: {}" , e) ) ?;
556
+ let h3_index_b = point_b. to_cell ( resolution) ;
557
+
558
+ let grid_distance = h3_index_a
559
+ . grid_distance ( h3_index_b)
560
+ . map_err ( |e| anyhow:: anyhow!( "Failed to calculate grid distance: {}" , e) ) ?;
561
+
562
+ let max_grid_distance = ( radius_meters_f64 / 9.0 ) . round ( ) as i32 ;
563
+
564
+ if grid_distance <= max_grid_distance {
565
+ return Ok ( true ) ;
566
+ }
567
+ }
568
+
569
+ Ok ( false )
570
+ }
571
+
504
572
pub async fn reward_dc (
505
573
mobile_rewards : & FileSinkClient < proto:: MobileRewardShare > ,
506
574
reward_period : & Range < DateTime < Utc > > ,
0 commit comments