Skip to content

Commit

Permalink
move mobile device type out of metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgrunewald committed Dec 14, 2023
1 parent c24422f commit 165a2f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 50 deletions.
4 changes: 2 additions & 2 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 @@ -60,14 +60,14 @@ sqlx = {version = "0", features = [
"runtime-tokio-rustls"
]}
helium-crypto = {version = "0.8.1", features=["sqlx-postgres", "multisig"]}
helium-proto = {git = "https://github.com/helium/proto", branch = "andymck/verify-hb-cell-type", features = ["services"]}
helium-proto = {git = "https://github.com/helium/proto", branch = "master", 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 = "andymck/verify-hb-cell-type" }
beacon = { git = "https://github.com/helium/proto", branch = "master" }
humantime = "2"
metrics = "0"
metrics-exporter-prometheus = "0"
Expand Down
67 changes: 29 additions & 38 deletions mobile_config/src/gateway_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,30 @@ pub type GatewayInfoStream = BoxStream<'static, GatewayInfo>;

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

#[derive(Clone, Debug)]
pub struct GatewayInfo {
pub address: PublicKeyBinary,
pub metadata: GatewayMetadata,
pub metadata: Option<GatewayMetadata>,
pub device_type: DeviceType,
}

impl From<GatewayInfoProto> for GatewayInfo {
fn from(info: GatewayInfoProto) -> Self {
let location = if info
.metadata
.as_ref()
.is_some_and(|meta| meta.location.is_empty())
{
None
let metadata = if let Some(ref metadata) = info.metadata {
u64::from_str_radix(&metadata.location, 26)
.map(|location| GatewayMetadata { location })
.ok()
} else {
info.metadata
.as_ref()
.and_then(|meta| u64::from_str_radix(&meta.location, 16).ok())
None
};
let device_type = info.device_type().into();
Self {
address: info.address.into(),
metadata: GatewayMetadata {
location,
device_type: info
.metadata
.map(|meta| meta.device_type().into())
.unwrap_or(DeviceType::Cbrs),
},
metadata,
device_type,
}
}
}
Expand All @@ -49,19 +41,17 @@ impl TryFrom<GatewayInfo> for GatewayInfoProto {
type Error = hextree::Error;

fn try_from(info: GatewayInfo) -> Result<Self, Self::Error> {
let location = info
.metadata
.location
.map(hextree::Cell::from_raw)
.transpose()?
.map(|cell| cell.to_string())
.unwrap_or_default();
let metadata = if let Some(metadata) = info.metadata {
Some(GatewayMetadataProto {
location: hextree::Cell::from_raw(metadata.location)?.to_string(),
})
} else {
None
};
Ok(Self {
address: info.address.into(),
metadata: Some(GatewayMetadataProto {
location,
device_type: info.metadata.device_type as i32,
}),
metadata,
device_type: info.device_type as i32,
})
}
}
Expand Down Expand Up @@ -93,8 +83,8 @@ impl std::str::FromStr for DeviceType {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = match s {
"cbrs" => Self::Cbrs,
"wifi_indoor" => Self::WifiIndoor,
"wifi_outdoor" => Self::WifiOutdoor,
"wifiIndoor" => Self::WifiIndoor,
"wifiOutdoor" => Self::WifiOutdoor,
_ => return Err(DeviceTypeParseError),
};
Ok(result)
Expand Down Expand Up @@ -140,23 +130,24 @@ pub(crate) mod db {

impl sqlx::FromRow<'_, sqlx::postgres::PgRow> for GatewayInfo {
fn from_row(row: &sqlx::postgres::PgRow) -> sqlx::Result<Self> {
let metadata = GatewayMetadata {
location: row
let metadata = row
.get::<Option<i64>, &str>("location")
.map(|loc| loc as u64),
device_type: DeviceType::from_str(
.map(|loc| GatewayMetadata {
location: loc as u64,
});
let device_type = DeviceType::from_str(
row.get::<Json<String>, &str>("device_type")
.to_string()
.as_ref(),
)
.map_err(|err| sqlx::Error::Decode(Box::new(err)))?,
};
.map_err(|err| sqlx::Error::Decode(Box::new(err)))?;
Ok(Self {
address: PublicKeyBinary::from_str(
&bs58::encode(row.get::<&[u8], &str>("entity_key")).into_string(),
)
.map_err(|err| sqlx::Error::Decode(Box::new(err)))?,
metadata,
device_type,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion mobile_config/src/gateway_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl mobile_config::Gateway for GatewayService {
Err(Status::not_found(pubkey.to_string()))
},
|info| {
if info.metadata.location.is_some() {
if info.metadata.is_some() {
telemetry::count_gateway_chain_lookup("asserted");
} else {
telemetry::count_gateway_chain_lookup("not-asserted");
Expand Down
10 changes: 3 additions & 7 deletions mobile_verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ impl GatewayResolver for mobile_config::GatewayClient {
address: &helium_crypto::PublicKeyBinary,
) -> Result<GatewayResolution, Self::Error> {
use mobile_config::client::gateway_client::GatewayInfoResolver;
use mobile_config::gateway_info::{GatewayInfo, GatewayMetadata};
use mobile_config::gateway_info::GatewayInfo;
match self.resolve_gateway_info(address).await? {
None => Ok(GatewayResolution::GatewayNotFound),
Some(GatewayInfo {
metadata:
GatewayMetadata {
location: Some(location),
..
},
metadata: Some(metadata),
..
}) => Ok(GatewayResolution::AssertedLocation(location)),
}) => Ok(GatewayResolution::AssertedLocation(metadata.location)),
Some(_) => Ok(GatewayResolution::GatewayNotAsserted),
}
}
Expand Down

0 comments on commit 165a2f1

Please sign in to comment.