-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* HIP 103 the second try * Use more of the terminology from the HIP * Adjust assignments and urbanization code to be more testable * Get everything working with tests * Hopefully improve speed of assigning * Fix for timeout * Address comments * Add test for oracle boosting * Fmt * Best I could come up with * Fix * Update mobile_verifier/tests/boosting_oracles.rs Co-authored-by: andymck <[email protected]> * PR comments * Switch to master for helium-proto --------- Co-authored-by: andymck <[email protected]>
- Loading branch information
Showing
22 changed files
with
785 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE TYPE oracle_assignment AS ENUM ('a', 'b', 'c'); | ||
|
||
ALTER TABLE hexes ADD COLUMN urbanized oracle_assignment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use helium_proto::services::poc_mobile::oracle_boosting_hex_assignment; | ||
use rust_decimal::Decimal; | ||
use rust_decimal_macros::dec; | ||
use std::fmt; | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Debug, sqlx::Type)] | ||
#[sqlx(type_name = "oracle_assignment")] | ||
#[sqlx(rename_all = "lowercase")] | ||
pub enum Assignment { | ||
A, | ||
B, | ||
C, | ||
} | ||
|
||
impl From<Assignment> for oracle_boosting_hex_assignment::Assignment { | ||
fn from(assignment: Assignment) -> Self { | ||
match assignment { | ||
Assignment::A => Self::A, | ||
Assignment::B => Self::B, | ||
Assignment::C => Self::C, | ||
} | ||
} | ||
} | ||
|
||
impl From<Assignment> for i32 { | ||
fn from(assignment: Assignment) -> i32 { | ||
oracle_boosting_hex_assignment::Assignment::from(assignment) as i32 | ||
} | ||
} | ||
|
||
impl fmt::Display for Assignment { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use Assignment::*; | ||
|
||
match self { | ||
A => write!(f, "a"), | ||
B => write!(f, "b"), | ||
C => write!(f, "c"), | ||
} | ||
} | ||
} | ||
|
||
pub fn urbanization_multiplier(urbanization: Assignment) -> Decimal { | ||
use Assignment::*; | ||
|
||
match urbanization { | ||
A => dec!(1.0), | ||
B => dec!(0.25), | ||
C => dec!(0.0), | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn urbanization_and_footfall_multiplier( | ||
footfall: Assignment, | ||
urbanization: Assignment, | ||
) -> Decimal { | ||
use Assignment::*; | ||
|
||
match (footfall, urbanization) { | ||
(A, A) => dec!(1.0), | ||
(A, B) => dec!(1.0), | ||
(B, A) => dec!(0.75), | ||
(B, B) => dec!(0.50), | ||
(C, A) => dec!(0.40), | ||
(C, B) => dec!(0.10), | ||
(A, C) => dec!(0.00), | ||
(B, C) => dec!(0.00), | ||
(C, C) => dec!(0.00), | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn boosting_oracles_multiplier( | ||
footfall: Assignment, | ||
landtype: Assignment, | ||
urbanization: Assignment, | ||
) -> Decimal { | ||
use Assignment::*; | ||
|
||
match (footfall, landtype, urbanization) { | ||
// POI ≥ 1 Urbanized | ||
(A, A, A) => dec!(1.00), | ||
(A, B, A) => dec!(1.00), | ||
(A, C, A) => dec!(1.00), | ||
// POI ≥ 1 Not Urbanized | ||
(A, A, B) => dec!(1.00), | ||
(A, B, B) => dec!(1.00), | ||
(A, C, B) => dec!(1.00), | ||
// Point of Interest Urbanized | ||
(B, A, A) => dec!(0.70), | ||
(B, B, A) => dec!(0.70), | ||
(B, C, A) => dec!(0.70), | ||
// Point of Interest Not Urbanized | ||
(B, A, B) => dec!(0.50), | ||
(B, B, B) => dec!(0.50), | ||
(B, C, B) => dec!(0.50), | ||
// No POI Urbanized | ||
(C, A, A) => dec!(0.40), | ||
(C, B, A) => dec!(0.30), | ||
(C, C, A) => dec!(0.05), | ||
// No POI Not Urbanized | ||
(C, A, B) => dec!(0.20), | ||
(C, B, B) => dec!(0.15), | ||
(C, C, B) => dec!(0.03), | ||
// Outside of USA | ||
(_, _, C) => dec!(0.00), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
pub mod assignment; | ||
|
||
use std::collections::HashMap; | ||
|
||
use crate::geofence::GeofenceValidator; | ||
pub use assignment::Assignment; | ||
use hextree::disktree::DiskTreeMap; | ||
|
||
pub trait DiskTreeLike: Send + Sync + 'static { | ||
fn get(&self, cell: hextree::Cell) -> hextree::Result<Option<(hextree::Cell, &[u8])>>; | ||
} | ||
|
||
impl DiskTreeLike for DiskTreeMap { | ||
fn get(&self, cell: hextree::Cell) -> hextree::Result<Option<(hextree::Cell, &[u8])>> { | ||
self.get(cell) | ||
} | ||
} | ||
|
||
impl DiskTreeLike for HashMap<hextree::Cell, Vec<u8>> { | ||
fn get(&self, cell: hextree::Cell) -> hextree::Result<Option<(hextree::Cell, &[u8])>> { | ||
Ok(self.get(&cell).map(|x| (cell, x.as_slice()))) | ||
} | ||
} | ||
|
||
pub struct MockDiskTree; | ||
|
||
impl DiskTreeLike for MockDiskTree { | ||
fn get(&self, cell: hextree::Cell) -> hextree::Result<Option<(hextree::Cell, &[u8])>> { | ||
Ok(Some((cell, &[]))) | ||
} | ||
} | ||
|
||
pub struct Urbanization<DT, GF> { | ||
urbanized: DT, | ||
usa_geofence: GF, | ||
} | ||
|
||
impl<DT, GF> Urbanization<DT, GF> { | ||
pub fn new(urbanized: DT, usa_geofence: GF) -> Self { | ||
Self { | ||
urbanized, | ||
usa_geofence, | ||
} | ||
} | ||
} | ||
|
||
impl<DT, GF> Urbanization<DT, GF> | ||
where | ||
DT: DiskTreeLike, | ||
GF: GeofenceValidator<u64>, | ||
{ | ||
fn is_urbanized(&self, location: u64) -> anyhow::Result<bool> { | ||
let cell = hextree::Cell::from_raw(location)?; | ||
let result = self.urbanized.get(cell)?; | ||
Ok(result.is_some()) | ||
} | ||
|
||
pub fn hex_assignment(&self, hex: u64) -> anyhow::Result<Assignment> { | ||
let assignment = if self.usa_geofence.in_valid_region(&hex) { | ||
if self.is_urbanized(hex)? { | ||
Assignment::A | ||
} else { | ||
Assignment::B | ||
} | ||
} else { | ||
Assignment::C | ||
}; | ||
Ok(assignment) | ||
} | ||
} |
Oops, something went wrong.