Skip to content

Commit

Permalink
review tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Dec 6, 2024
1 parent e45ac3c commit e8643f6
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 57 deletions.
2 changes: 0 additions & 2 deletions file_store/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ pub enum Error {
//Not recommended for internal use!
#[error("external error")]
ExternalError(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("general error")]
Anyhow(#[from] anyhow::Error),
}

#[derive(Error, Debug)]
Expand Down
6 changes: 4 additions & 2 deletions mobile_config/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pub enum ClientError {
UnknownServiceProvider(String),
#[error("Invalid GatewayInfo proto response {0}")]
InvalidGatewayInfoProto(#[from] crate::gateway_info::GatewayInfoProtoParseError),
#[error("error {0}")]
AnyhowError(#[from] anyhow::Error),
#[error("Invalid SubDaoRewardInfo proto response {0}")]
InvalidSubDaoRewardInfoProto(
#[from] crate::sub_dao_epoch_reward_info::SubDaoRewardInfoParseError,
),
}

macro_rules! call_with_retry {
Expand Down
19 changes: 6 additions & 13 deletions mobile_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::anyhow;
use chrono::{DateTime, Duration, TimeZone, Utc};
use chrono::{DateTime, Duration, Utc};
use helium_crypto::PublicKey;
use helium_proto::services::mobile_config::AdminKeyRole as ProtoKeyRole;
use serde::Serialize;
Expand Down Expand Up @@ -104,18 +103,12 @@ pub struct EpochPeriod {
pub period: Range<DateTime<Utc>>,
}

impl TryFrom<u64> for EpochPeriod {
type Error = anyhow::Error;

fn try_from(next_reward_epoch: u64) -> anyhow::Result<Self> {
let start_time = Utc
.timestamp_opt(0, 0)
.single()
.ok_or_else(|| anyhow!("Failed to get Unix epoch start time"))?
+ Duration::days(next_reward_epoch as i64);
impl From<u64> for EpochPeriod {
fn from(next_reward_epoch: u64) -> Self {
let start_time = DateTime::<Utc>::UNIX_EPOCH + Duration::days(next_reward_epoch as i64);
let end_time = start_time + Duration::days(1);
Ok(EpochPeriod {
EpochPeriod {
period: start_time..end_time,
})
}
}
}
22 changes: 13 additions & 9 deletions mobile_config/src/sub_dao_epoch_reward_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::Range;

#[derive(Clone, Debug)]
pub struct ResolvedSubDaoEpochRewardInfo {
pub epoch: u64,
pub epoch_day: u64,
pub epoch_address: String,
pub sub_dao_address: String,
pub epoch_period: Range<DateTime<Utc>>,
Expand All @@ -25,30 +25,34 @@ pub struct RawSubDaoEpochRewardInfo {
rewards_issued_at: DateTime<Utc>,
}

impl TryFrom<RawSubDaoEpochRewardInfo> for SubDaoEpochRewardInfoProto {
type Error = anyhow::Error;
#[derive(thiserror::Error, Debug)]
pub enum SubDaoRewardInfoParseError {
#[error("file_store: {0}")]
FileStore(#[from] file_store::Error),
}

fn try_from(info: RawSubDaoEpochRewardInfo) -> Result<Self, Self::Error> {
Ok(Self {
impl From<RawSubDaoEpochRewardInfo> for SubDaoEpochRewardInfoProto {
fn from(info: RawSubDaoEpochRewardInfo) -> Self {
Self {
epoch: info.epoch,
epoch_address: info.epoch_address,
sub_dao_address: info.sub_dao_address,
rewards_issued: info.rewards_issued,
delegation_rewards_issued: info.delegation_rewards_issued,
rewards_issued_at: info.rewards_issued_at.encode_timestamp(),
})
}
}
}

impl TryFrom<SubDaoEpochRewardInfoProto> for ResolvedSubDaoEpochRewardInfo {
type Error = anyhow::Error;
type Error = SubDaoRewardInfoParseError;

fn try_from(info: SubDaoEpochRewardInfoProto) -> Result<Self, Self::Error> {
let epoch_period: EpochPeriod = info.epoch.try_into()?;
let epoch_period: EpochPeriod = info.epoch.into();
let epoch_rewards = Decimal::from(info.rewards_issued + info.delegation_rewards_issued);

Ok(Self {
epoch: info.epoch,
epoch_day: info.epoch,
epoch_address: info.epoch_address,
sub_dao_address: info.sub_dao_address,
epoch_period: epoch_period.period,
Expand Down
4 changes: 1 addition & 3 deletions mobile_config/src/sub_dao_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ impl sub_dao::sub_dao_server::SubDao for SubDaoService {
Err(Status::not_found(epoch.to_string()))
},
|info| {
let info = info.try_into().map_err(|_| {
Status::internal("error serializing sub_dao epoch reward info")
})?;
let info = info.into();
let mut res = SubDaoEpochRewardInfoResV1 {
info: Some(info),
timestamp: Utc::now().encode_timestamp(),
Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/src/cli/reward_from_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Cmd {
let radio_rewards = reward_shares
.into_rewards(
DataTransferAndPocAllocatedRewardBuckets::new(reward_info.epoch_emissions),
&reward_info,
&reward_info.epoch_period,
)
.ok_or(anyhow::anyhow!("no rewardable events"))?
.1;
Expand Down
36 changes: 18 additions & 18 deletions mobile_verifier/src/reward_shares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl MapperShares {

pub fn into_subscriber_rewards(
self,
reward_info: &'_ ResolvedSubDaoEpochRewardInfo,
reward_period: &'_ Range<DateTime<Utc>>,
reward_per_share: Decimal,
) -> impl Iterator<Item = (u64, proto::MobileRewardShare)> + '_ {
let mut subscriber_rewards: HashMap<Vec<u8>, proto::SubscriberReward> = HashMap::new();
Expand Down Expand Up @@ -278,8 +278,8 @@ impl MapperShares {
(
total_reward_amount,
proto::MobileRewardShare {
start_period: reward_info.epoch_period.start.encode_timestamp(),
end_period: reward_info.epoch_period.end.encode_timestamp(),
start_period: reward_period.start.encode_timestamp(),
end_period: reward_period.end.encode_timestamp(),
reward: Some(ProtoReward::SubscriberReward(subscriber_reward)),
},
)
Expand All @@ -296,7 +296,7 @@ pub fn dc_to_hnt_bones(dc_amount: Decimal, hnt_bone_price: Decimal) -> Decimal {

pub fn coverage_point_to_mobile_reward_share(
coverage_points: coverage_point_calculator::CoveragePoints,
reward_info: &ResolvedSubDaoEpochRewardInfo,
reward_period: &Range<DateTime<Utc>>,
radio_id: &RadioId,
poc_reward: u64,
rewards_per_share: CalculatedPocRewardShares,
Expand Down Expand Up @@ -362,8 +362,8 @@ pub fn coverage_point_to_mobile_reward_share(
});

let base = proto::MobileRewardShare {
start_period: reward_info.epoch_period.start.encode_timestamp(),
end_period: reward_info.epoch_period.end.encode_timestamp(),
start_period: reward_period.start.encode_timestamp(),
end_period: reward_period.end.encode_timestamp(),
reward: None,
};

Expand Down Expand Up @@ -546,7 +546,7 @@ impl CoverageShares {
pub fn into_rewards(
self,
reward_shares: DataTransferAndPocAllocatedRewardBuckets,
reward_info: &'_ ResolvedSubDaoEpochRewardInfo,
reward_period: &Range<DateTime<Utc>>,
) -> Option<(
CalculatedPocRewardShares,
impl Iterator<Item = (u64, proto::MobileRewardShare, proto::MobileRewardShare)> + '_,
Expand Down Expand Up @@ -584,7 +584,7 @@ impl CoverageShares {
reward_shares,
processed_radios.iter().map(|radio| &radio.points),
) else {
tracing::info!(?reward_info.epoch_period, "could not calculate reward shares");
tracing::info!(?reward_period, "could not calculate reward shares");
return None;
};

Expand All @@ -604,7 +604,7 @@ impl CoverageShares {
let (mobile_reward_v1, mobile_reward_v2) =
coverage_point_to_mobile_reward_share(
points,
reward_info,
reward_period,
&radio_id,
poc_reward,
rewards_per_share,
Expand Down Expand Up @@ -808,7 +808,7 @@ mod test {
) -> ResolvedSubDaoEpochRewardInfo {
let now = Utc::now();
ResolvedSubDaoEpochRewardInfo {
epoch: 1,
epoch_day: 1,
epoch_address: EPOCH_ADDRESS.into(),
sub_dao_address: SUB_DAO_ADDRESS.into(),
epoch_period: (now - epoch_duration)..now,
Expand Down Expand Up @@ -885,7 +885,7 @@ mod test {
// get the summed rewards allocated to subscribers for discovery location
let mut allocated_mapper_rewards = 0_u64;
for (reward_amount, subscriber_share) in
shares.into_subscriber_rewards(&rewards_info, rewards_per_share)
shares.into_subscriber_rewards(&rewards_info.epoch_period, rewards_per_share)
{
if let Some(MobileReward::SubscriberReward(r)) = subscriber_share.reward {
assert_eq!(
Expand Down Expand Up @@ -1222,7 +1222,7 @@ mod test {
)
.await
.unwrap()
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.unwrap()
.1
.next()
Expand Down Expand Up @@ -1626,7 +1626,7 @@ mod test {
)
.await
.unwrap()
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.unwrap()
.1
{
Expand Down Expand Up @@ -1808,7 +1808,7 @@ mod test {
)
.await
.unwrap()
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.unwrap()
.1
{
Expand Down Expand Up @@ -1943,7 +1943,7 @@ mod test {
)
.await
.unwrap()
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.unwrap()
.1
{
Expand Down Expand Up @@ -2079,7 +2079,7 @@ mod test {
)
.await
.unwrap()
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.unwrap()
.1
{
Expand Down Expand Up @@ -2225,7 +2225,7 @@ mod test {
// gw2 does not have enough speedtests for a mulitplier
let expected_hotspot = gw1;
for (_reward_amount, _mobile_reward_v1, mobile_reward_v2) in coverage_shares
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.expect("rewards output")
.1
{
Expand All @@ -2250,7 +2250,7 @@ mod test {
let reward_shares =
DataTransferAndPocAllocatedRewardBuckets::new_poc_only(rewards_info.epoch_emissions);
assert!(coverage_shares
.into_rewards(reward_shares, &rewards_info)
.into_rewards(reward_shares, &rewards_info.epoch_period)
.is_none());
}

Expand Down
12 changes: 6 additions & 6 deletions mobile_verifier/src/rewarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where

loop {
let next_reward_epoch = next_reward_epoch(&self.pool).await?;
let next_reward_epoch_period = EpochPeriod::try_from(next_reward_epoch)?;
let next_reward_epoch_period = EpochPeriod::from(next_reward_epoch);

let scheduler = Scheduler::new(
self.reward_period_duration,
Expand Down Expand Up @@ -274,7 +274,7 @@ where

tracing::info!(
"Rewarding for epoch {} period: {} to {} with hnt bone price: {}",
reward_info.epoch,
reward_info.epoch_day,
reward_info.epoch_period.start,
reward_info.epoch_period.end,
hnt_price.price_per_hnt_bone
Expand Down Expand Up @@ -335,7 +335,7 @@ where
.await?;
// subscriber_location::clear_location_shares(&mut transaction, &reward_period.end).await?;

save_next_reward_epoch(&mut transaction, reward_info.epoch + 1).await?;
save_next_reward_epoch(&mut transaction, reward_info.epoch_day + 1).await?;

transaction.commit().await?;

Expand All @@ -356,7 +356,7 @@ where
end_timestamp: reward_info.epoch_period.end.encode_timestamp(),
written_files,
reward_data: Some(MobileRewardData(reward_data)),
epoch: reward_info.epoch,
epoch: reward_info.epoch_day,
price: hnt_price.hnt_price_in_bones,
},
[],
Expand Down Expand Up @@ -500,7 +500,7 @@ async fn reward_poc(

let (unallocated_poc_amount, calculated_poc_rewards_per_share) =
if let Some((calculated_poc_rewards_per_share, mobile_reward_shares)) =
coverage_shares.into_rewards(reward_shares, reward_info)
coverage_shares.into_rewards(reward_shares, &reward_info.epoch_period)
{
// handle poc reward outputs
let mut allocated_poc_rewards = 0_u64;
Expand Down Expand Up @@ -587,7 +587,7 @@ pub async fn reward_mappers(
let mut allocated_mapping_rewards = 0_u64;

for (reward_amount, mapping_share) in
mapping_shares.into_subscriber_rewards(reward_info, rewards_per_share)
mapping_shares.into_subscriber_rewards(&reward_info.epoch_period, rewards_per_share)
{
allocated_mapping_rewards += reward_amount;
mobile_rewards
Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/src/service_provider/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ mod tests {
) -> ResolvedSubDaoEpochRewardInfo {
let now = Utc::now();
ResolvedSubDaoEpochRewardInfo {
epoch: 1,
epoch_day: 1,
epoch_address: EPOCH_ADDRESS.into(),
sub_dao_address: SUB_DAO_ADDRESS.into(),
epoch_period: (now - epoch_duration)..now,
Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DATA_TRANSFER_REWARDS_SCALE: &str = "data_transfer_rewards_scale";

pub async fn initialize(db: &Pool<Postgres>) -> anyhow::Result<()> {
let next_reward_epoch = rewarder::next_reward_epoch(db).await?;
let epoch_period: EpochPeriod = next_reward_epoch.try_into()?;
let epoch_period: EpochPeriod = next_reward_epoch.into();
last_rewarded_end_time(epoch_period.period.start);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion mobile_verifier/tests/integrations/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ pub fn default_rewards_info(
) -> ResolvedSubDaoEpochRewardInfo {
let now = Utc::now();
ResolvedSubDaoEpochRewardInfo {
epoch: 1,
epoch_day: 1,
epoch_address: EPOCH_ADDRESS.into(),
sub_dao_address: SUB_DAO_ADDRESS.into(),
epoch_period: (now - epoch_duration)..now,
Expand Down

0 comments on commit e8643f6

Please sign in to comment.