Skip to content

Commit

Permalink
verify asserted device type against wifi HB cell type
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Oct 16, 2023
1 parent 44a9af8 commit 0d02a5e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ sqlx = {version = "0", features = [
]}

helium-crypto = {version = "0.8.1", features=["sqlx-postgres", "multisig"]}
helium-proto = {git = "https://github.com/helium/proto", branch = "master", features = ["services"]}
helium-proto = {git = "https://github.com/helium/proto", branch = "andymck/verify-hb-cell-type", features = ["services"]}
hextree = "*"
solana-client = "1.14"
solana-sdk = "1.14"
solana-program = "1.11"
spl-token = "3.5.0"
reqwest = {version = "0", default-features=false, features = ["gzip", "json", "rustls-tls"]}
beacon = { git = "https://github.com/helium/proto", branch = "master" }
beacon = { git = "https://github.com/helium/proto", branch = "andymck/verify-hb-cell-type" }
humantime = "2"
metrics = "0"
metrics-exporter-prometheus = "0"
Expand Down
17 changes: 14 additions & 3 deletions mobile_config/src/gateway_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use helium_crypto::PublicKeyBinary;
use helium_proto::services::mobile_config::{
GatewayInfo as GatewayInfoProto, GatewayMetadata as GatewayMetadataProto,
};

pub type GatewayInfoStream = BoxStream<'static, GatewayInfo>;

#[derive(Clone, Debug)]
pub struct GatewayMetadata {
pub location: u64,
pub device_type: Option<String>,
}

#[derive(Clone, Debug)]
Expand All @@ -32,12 +32,17 @@ pub trait GatewayInfoResolver {
impl From<GatewayInfoProto> for GatewayInfo {
fn from(info: GatewayInfoProto) -> Self {
let metadata = if let Some(metadata) = info.metadata {
let device_type = metadata.device_type.parse().ok();
u64::from_str_radix(&metadata.location, 16)
.map(|location| GatewayMetadata { location })
.map(|location| GatewayMetadata {
location,
device_type,
})
.ok()
} else {
None
};

Self {
address: info.address.into(),
metadata,
Expand All @@ -52,6 +57,7 @@ impl TryFrom<GatewayInfo> for GatewayInfoProto {
let metadata = if let Some(metadata) = info.metadata {
Some(GatewayMetadataProto {
location: hextree::Cell::from_raw(metadata.location)?.to_string(),
device_type: metadata.device_type.unwrap_or_default(),
})
} else {
None
Expand All @@ -67,11 +73,12 @@ pub(crate) mod db {
use super::{GatewayInfo, GatewayMetadata};
use futures::stream::{Stream, StreamExt};
use helium_crypto::PublicKeyBinary;
use sqlx::types::Json;
use sqlx::{PgExecutor, Row};
use std::str::FromStr;

const GET_METADATA_SQL: &str = r#"
select kta.entity_key, infos.location::bigint
select kta.entity_key, infos.location::bigint, infos.device_type
from mobile_hotspot_infos infos
join key_to_assets kta on infos.asset = kta.asset
"#;
Expand Down Expand Up @@ -102,10 +109,14 @@ pub(crate) mod db {

impl sqlx::FromRow<'_, sqlx::postgres::PgRow> for GatewayInfo {
fn from_row(row: &sqlx::postgres::PgRow) -> sqlx::Result<Self> {
let device_type = row
.get::<Option<Json<String>>, &str>("device_type")
.map(|s| s.to_string());
let metadata = row
.get::<Option<i64>, &str>("location")
.map(|loc| GatewayMetadata {
location: loc as u64,
device_type,
});
Ok(Self {
address: PublicKeyBinary::from_str(
Expand Down
9 changes: 9 additions & 0 deletions mobile_verifier/src/cell_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ impl CellType {
_ => dec!(1.0),
}
}

pub fn from_asserted(s: &Option<String>) -> Option<Self> {
// TODO: currently only handling wifi indoor, handle other cell types
// when foundation device type values are in use
match s {
Some(s) if s.eq("wifiIndoor") => Some(CellType::NovaGenericWifiIndoor),
_ => None,
}
}
}

impl From<CellType> for CellTypeProto {
Expand Down
17 changes: 17 additions & 0 deletions mobile_verifier/src/heartbeats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ pub async fn validate_heartbeat(
));
};

// verify the HB cell type matches that on chain
// TODO: currently only handling wifi indoor
// make this check generic and applicable to all cell types
// when device data is available in the db
match (
heartbeat.hb_type.clone(),
CellType::from_asserted(&metadata.device_type),
) {
(HBType::Wifi, Some(asserted_celltype)) if asserted_celltype != cell_type => {
return Ok((cell_type, proto::HeartbeatValidity::BadCellType, None));
}
(HBType::Wifi, None) => {
return Ok((cell_type, proto::HeartbeatValidity::BadCellType, None))
}
_ => (),
};

let distance_to_asserted = if heartbeat.hb_type == HBType::Wifi {
Some(heartbeat.asserted_distance(metadata.location)?)
} else {
Expand Down

0 comments on commit 0d02a5e

Please sign in to comment.