Skip to content

Commit

Permalink
PoS: refactor usages of Epoch::checked_sub
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic authored and brentstone committed Sep 28, 2023
1 parent ad9c2d1 commit 95ef243
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
42 changes: 15 additions & 27 deletions proof_of_stake/src/epoched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,10 @@ where
(last_update, oldest_epoch)
{
let oldest_to_keep = current_epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default();
if oldest_epoch.0 < oldest_to_keep {
let diff = oldest_to_keep - oldest_epoch.0;
if oldest_epoch < oldest_to_keep {
let diff = u64::from(oldest_to_keep - oldest_epoch);
// Go through the epochs before the expected oldest epoch and
// keep the latest one
tracing::debug!(
Expand Down Expand Up @@ -266,12 +265,9 @@ where
}

fn sub_past_epochs(params: &PosParams, epoch: Epoch) -> Epoch {
Epoch(
epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default(),
)
epoch
.checked_sub(PastEpochs::value(params))
.unwrap_or_default()
}

fn get_oldest_epoch_storage_key(&self) -> storage::Key {
Expand Down Expand Up @@ -400,12 +396,9 @@ where
}

fn sub_past_epochs(params: &PosParams, epoch: Epoch) -> Epoch {
Epoch(
epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default(),
)
epoch
.checked_sub(PastEpochs::value(params))
.unwrap_or_default()
}

/// Update data by removing old epochs
Expand All @@ -426,11 +419,10 @@ where
(last_update, oldest_epoch)
{
let oldest_to_keep = current_epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default();
if oldest_epoch.0 < oldest_to_keep {
let diff = oldest_to_keep - oldest_epoch.0;
if oldest_epoch < oldest_to_keep {
let diff = u64::from(oldest_to_keep - oldest_epoch);
// Go through the epochs before the expected oldest epoch and
// keep the latest one
tracing::debug!(
Expand Down Expand Up @@ -610,11 +602,10 @@ where
(last_update, oldest_epoch)
{
let oldest_to_keep = current_epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default();
if oldest_epoch.0 < oldest_to_keep {
let diff = oldest_to_keep - oldest_epoch.0;
if oldest_epoch < oldest_to_keep {
let diff = u64::from(oldest_to_keep - oldest_epoch);
// Go through the epochs before the expected oldest epoch and
// sum them into it
tracing::debug!(
Expand Down Expand Up @@ -713,12 +704,9 @@ where
}

fn sub_past_epochs(params: &PosParams, epoch: Epoch) -> Epoch {
Epoch(
epoch
.0
.checked_sub(PastEpochs::value(params))
.unwrap_or_default(),
)
epoch
.checked_sub(PastEpochs::value(params))
.unwrap_or_default()
}

fn get_oldest_epoch_storage_key(&self) -> storage::Key {
Expand Down
12 changes: 6 additions & 6 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3215,10 +3215,10 @@ fn make_unbond_details(
if slash.epoch >= start
&& slash.epoch
< withdraw
.checked_sub(Epoch(
.checked_sub(
params.unbonding_len
+ params.cubic_slashing_window_length,
))
)
.unwrap_or_default()
{
let cur_rate = slash_rates_by_epoch.entry(slash.epoch).or_default();
Expand Down Expand Up @@ -3751,10 +3751,10 @@ where
start,
Some(
infraction_epoch
.checked_sub(Epoch(
.checked_sub(
params.unbonding_len
+ params.cubic_slashing_window_length,
))
)
.unwrap_or_default(),
),
&validator,
Expand Down Expand Up @@ -3813,10 +3813,10 @@ where
start,
Some(
infraction_epoch
.checked_sub(Epoch(
.checked_sub(
params.unbonding_len
+ params.cubic_slashing_window_length,
))
)
.unwrap_or_default(),
),
&validator,
Expand Down

0 comments on commit 95ef243

Please sign in to comment.