From 9da46ab3f3bbe28ca7680e02a7005b8aa3bed6ed Mon Sep 17 00:00:00 2001 From: Andrew McKenzie Date: Tue, 9 Jan 2024 17:29:00 +0000 Subject: [PATCH] use service provider proto string impls --- Cargo.lock | 4 +-- .../src/client/carrier_service_client.rs | 33 +++++++++++-------- mobile_config/src/client/mod.rs | 4 +-- mobile_verifier/src/data_session.rs | 2 +- mobile_verifier/src/reward_shares.rs | 31 ++++++++--------- mobile_verifier/tests/rewarder_sp_rewards.rs | 25 +++++++++----- reward_index/src/indexer.rs | 14 ++------ 7 files changed, 57 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 920e14d35..c85e2dce6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1197,7 +1197,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "beacon" version = "0.1.0" -source = "git+https://github.com/helium/proto?branch=master#e3258b53a170315fb321d38dc9f451fd80538de8" +source = "git+https://github.com/helium/proto?branch=master#585704c871d32846a6e35d186a08443883545687" dependencies = [ "base64 0.21.0", "byteorder", @@ -3047,7 +3047,7 @@ dependencies = [ [[package]] name = "helium-proto" version = "0.1.0" -source = "git+https://github.com/helium/proto?branch=master#e3258b53a170315fb321d38dc9f451fd80538de8" +source = "git+https://github.com/helium/proto?branch=master#585704c871d32846a6e35d186a08443883545687" dependencies = [ "bytes", "prost", diff --git a/mobile_config/src/client/carrier_service_client.rs b/mobile_config/src/client/carrier_service_client.rs index 6bff7afd6..672612595 100644 --- a/mobile_config/src/client/carrier_service_client.rs +++ b/mobile_config/src/client/carrier_service_client.rs @@ -4,23 +4,24 @@ use file_store::traits::MsgVerify; use helium_crypto::{Keypair, PublicKey, Sign}; use helium_proto::{ services::{mobile_config, Channel}, - Message, + Message, ServiceProvider, }; use retainer::Cache; -use std::{sync::Arc, time::Duration}; - +use std::{str::FromStr, sync::Arc, time::Duration}; #[async_trait] pub trait CarrierServiceVerifier { type Error; - async fn key_to_rewardable_entity<'a>(&self, entity_id: &'a str) - -> Result; + async fn payer_key_to_service_provider<'a>( + &self, + payer: &str, + ) -> Result; } #[derive(Clone)] pub struct CarrierServiceClient { client: mobile_config::CarrierServiceClient, signing_key: Arc, config_pubkey: PublicKey, - cache: Arc>, + cache: Arc>, cache_ttl: Duration, } @@ -28,31 +29,35 @@ pub struct CarrierServiceClient { impl CarrierServiceVerifier for CarrierServiceClient { type Error = ClientError; - async fn key_to_rewardable_entity<'a>(&self, pubkey: &'a str) -> Result { - if let Some(carrier_found) = self.cache.get(&pubkey.to_string()).await { - return Ok(carrier_found.value().clone()); + async fn payer_key_to_service_provider<'a>( + &self, + payer: &str, + ) -> Result { + if let Some(carrier_found) = self.cache.get(&payer.to_string()).await { + return Ok(*carrier_found.value()); } let mut request = mobile_config::CarrierKeyToEntityReqV1 { - pubkey: pubkey.to_string(), + pubkey: payer.to_string(), signer: self.signing_key.public_key().into(), signature: vec![], }; request.signature = self.signing_key.sign(&request.encode_to_vec())?; - tracing::debug!(?pubkey, "getting entity key for carrier on-chain"); + tracing::debug!(?payer, "getting service provider for payer key"); let response = match call_with_retry!(self.client.clone().key_to_entity(request.clone())) { Ok(verify_res) => { let response = verify_res.into_inner(); response.verify(&self.config_pubkey)?; - response.entity_key + ServiceProvider::from_str(&response.entity_key) + .map_err(|_| ClientError::UnknownServiceProvider(payer.to_string()))? } Err(status) if status.code() == tonic::Code::NotFound => { - Err(ClientError::UnknownServiceProvider)? + Err(ClientError::UnknownServiceProvider(payer.to_string()))? } Err(status) => Err(status)?, }; self.cache - .insert(pubkey.to_string(), response.clone(), self.cache_ttl) + .insert(payer.to_string(), response, self.cache_ttl) .await; Ok(response) } diff --git a/mobile_config/src/client/mod.rs b/mobile_config/src/client/mod.rs index 93f8e2b63..68699dd0d 100644 --- a/mobile_config/src/client/mod.rs +++ b/mobile_config/src/client/mod.rs @@ -24,8 +24,8 @@ pub enum ClientError { VerificationError(#[from] file_store::Error), #[error("error parsing gateway location {0}")] LocationParseError(#[from] std::num::ParseIntError), - #[error("unknown service provider name")] - UnknownServiceProvider, + #[error("unknown service provider {0}")] + UnknownServiceProvider(String), } macro_rules! call_with_retry { diff --git a/mobile_verifier/src/data_session.rs b/mobile_verifier/src/data_session.rs index cac66a9d9..6e2edf636 100644 --- a/mobile_verifier/src/data_session.rs +++ b/mobile_verifier/src/data_session.rs @@ -5,7 +5,7 @@ use futures::{ TryFutureExt, }; use helium_crypto::PublicKeyBinary; -use helium_proto::services::poc_mobile::ServiceProvider; +use helium_proto::ServiceProvider; use rust_decimal::Decimal; use sqlx::{PgPool, Postgres, Row, Transaction}; use std::{collections::HashMap, ops::Range, time::Instant}; diff --git a/mobile_verifier/src/reward_shares.rs b/mobile_verifier/src/reward_shares.rs index e5f930827..b2425dc41 100644 --- a/mobile_verifier/src/reward_shares.rs +++ b/mobile_verifier/src/reward_shares.rs @@ -5,18 +5,20 @@ use crate::{ speedtests_average::{SpeedtestAverage, SpeedtestAverages}, subscriber_location::SubscriberValidatedLocations, }; -use anyhow::bail; use chrono::{DateTime, Duration, Utc}; use file_store::traits::TimestampEncode; use futures::{Stream, StreamExt}; use helium_crypto::PublicKeyBinary; -use helium_proto::services::{ - poc_mobile as proto, - poc_mobile::{ - mobile_reward_share::Reward as ProtoReward, ServiceProvider, UnallocatedReward, - UnallocatedRewardType, +use helium_proto::{ + services::{ + poc_mobile as proto, + poc_mobile::{ + mobile_reward_share::Reward as ProtoReward, UnallocatedReward, UnallocatedRewardType, + }, }, + ServiceProvider, }; + use mobile_config::client::{carrier_service_client::CarrierServiceVerifier, ClientError}; use rust_decimal::prelude::*; use rust_decimal_macros::dec; @@ -330,16 +332,9 @@ impl ServiceProviderShares { payer: &str, client: &impl CarrierServiceVerifier, ) -> anyhow::Result { - tracing::info!(payer, "getting entity key for service provider"); - let entity_key = client.key_to_rewardable_entity(payer).await?; - Self::entity_key_to_service_provider(&entity_key) - } - - fn entity_key_to_service_provider(key: &str) -> anyhow::Result { - match key { - "Helium Mobile" => Ok(ServiceProvider::HeliumMobile), - _ => bail!("unknown service provider name"), - } + tracing::info!(payer, "getting service provider for payer"); + let sp = client.payer_key_to_service_provider(payer).await?; + Ok(sp) } } @@ -606,8 +601,8 @@ mod test { use chrono::{Duration, Utc}; use file_store::speedtest::CellSpeedtest; use futures::stream::{self, BoxStream}; - use helium_proto::services::{ - poc_mobile::mobile_reward_share::Reward as MobileReward, poc_mobile::ServiceProvider, + use helium_proto::{ + services::poc_mobile::mobile_reward_share::Reward as MobileReward, ServiceProvider, }; use prost::Message; use std::collections::HashMap; diff --git a/mobile_verifier/tests/rewarder_sp_rewards.rs b/mobile_verifier/tests/rewarder_sp_rewards.rs index 65d242a47..e542b8095 100644 --- a/mobile_verifier/tests/rewarder_sp_rewards.rs +++ b/mobile_verifier/tests/rewarder_sp_rewards.rs @@ -3,8 +3,9 @@ use std::string::ToString; use async_trait::async_trait; use chrono::{DateTime, Duration as ChronoDuration, Utc}; -use helium_proto::services::poc_mobile::{ - ServiceProviderReward, UnallocatedReward, UnallocatedRewardType, +use helium_proto::{ + services::poc_mobile::{ServiceProviderReward, UnallocatedReward, UnallocatedRewardType}, + ServiceProvider, }; use rust_decimal::prelude::*; use rust_decimal_macros::dec; @@ -35,11 +36,14 @@ impl MockCarrierServiceClient { impl CarrierServiceVerifier for MockCarrierServiceClient { type Error = ClientError; - async fn key_to_rewardable_entity<'a>(&self, pubkey: &'a str) -> Result { + async fn payer_key_to_service_provider<'a>( + &self, + pubkey: &str, + ) -> Result { match self.valid_sps.get(pubkey) { - Some(v) => Ok(v.clone()), - - None => Err(ClientError::UnknownServiceProvider), + Some(v) => Ok(ServiceProvider::from_str(v) + .map_err(|_| ClientError::UnknownServiceProvider(pubkey.to_string()))?), + None => Err(ClientError::UnknownServiceProvider(pubkey.to_string())), } } } @@ -70,7 +74,12 @@ async fn test_service_provider_rewards(pool: PgPool) -> anyhow::Result<()> { receive_expected_rewards(&mut mobile_rewards) ); if let Ok((sp_reward, unallocated_reward)) = rewards { - // assert_eq!(SP_1.to_string(), ServiceProvider::from_i32(sp_reward.service_provider_id).unwrap().as_str_name()); + assert_eq!( + SP_1.to_string(), + ServiceProvider::from_i32(sp_reward.service_provider_id) + .unwrap() + .to_string() + ); assert_eq!(6000, sp_reward.amount); assert_eq!( @@ -123,7 +132,7 @@ async fn test_service_provider_rewards_invalid_sp(pool: PgPool) -> anyhow::Resul .await; assert_eq!( resp.unwrap_err().to_string(), - "unknown service provider name".to_string() + "unknown service provider ".to_string() + PAYER_2 ); // confirm we get no msgs as rewards halted diff --git a/reward_index/src/indexer.rs b/reward_index/src/indexer.rs index 97bd13091..64afd4a5d 100644 --- a/reward_index/src/indexer.rs +++ b/reward_index/src/indexer.rs @@ -8,10 +8,8 @@ use futures::{stream, StreamExt, TryStreamExt}; use helium_crypto::PublicKeyBinary; use helium_proto::{ services::poc_lora::{iot_reward_share::Reward as IotReward, IotRewardShare}, - services::poc_mobile::{ - mobile_reward_share::Reward as MobileReward, MobileRewardShare, ServiceProvider, - }, - Message, + services::poc_mobile::{mobile_reward_share::Reward as MobileReward, MobileRewardShare}, + Message, ServiceProvider, }; use poc_metrics::record_duration; use sqlx::{Pool, Postgres, Transaction}; @@ -163,7 +161,7 @@ impl Indexer { if let Some(sp) = ServiceProvider::from_i32(r.service_provider_id) { Ok(( RewardKey { - key: service_provider_to_entity_key(sp)?, + key: sp.to_string(), reward_type: RewardType::MobileServiceProvider, }, r.amount, @@ -212,9 +210,3 @@ impl Indexer { } } } - -fn service_provider_to_entity_key(sp: ServiceProvider) -> anyhow::Result { - match sp { - ServiceProvider::HeliumMobile => Ok("Helium Mobile".to_string()), - } -}