Skip to content

Commit

Permalink
chore: Implement saturating sub for AmountOf (dfinity#1740)
Browse files Browse the repository at this point in the history
Since we're using saturating arithmetic in many places (including
`Time`), it might be good to have a blanket implementation for the
phantom types.

---------

Co-authored-by: IDX GitHub Automation <infra+github-automation@dfinity.org>
  • Loading branch information
2 people authored and levifeldman committed Oct 1, 2024
1 parent 96f8b20 commit b48c0e1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions rs/consensus/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ DEPENDENCIES = [
"//rs/types/error_types",
"//rs/types/management_canister_types",
"//rs/types/types",
"@crate_index//:num-traits",
"@crate_index//:prometheus",
"@crate_index//:rand",
"@crate_index//:rand_chacha",
Expand Down
1 change: 1 addition & 0 deletions rs/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ic-registry-subnet-features = { path = "../registry/subnet_features" }
ic-registry-subnet-type = { path = "../registry/subnet_type" }
ic-replicated-state = { path = "../replicated_state" }
ic-types = { path = "../types/types" }
num-traits = { workspace = true }
phantom_newtype = { path = "../phantom_newtype" }
prometheus = { workspace = true }
rand = { workspace = true }
Expand Down
8 changes: 2 additions & 6 deletions rs/consensus/src/consensus/block_maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use ic_types::{
time::current_time,
CountBytes, Height, NodeId, RegistryVersion, SubnetId,
};
use num_traits::ops::saturating::SaturatingSub;
use std::{
sync::{Arc, RwLock},
time::Duration,
Expand Down Expand Up @@ -540,12 +541,7 @@ const DYNAMIC_DELAY_EXTRA_DURATION: Duration = Duration::from_secs(10);

fn count_non_rank_0_blocks(pool: &PoolReader, block: Block) -> usize {
let max_height = block.height();
let min_height = if max_height > DYNAMIC_DELAY_LOOK_BACK_DISTANCE {
max_height - DYNAMIC_DELAY_LOOK_BACK_DISTANCE
} else {
Height::new(0)
};

let min_height = max_height.saturating_sub(&DYNAMIC_DELAY_LOOK_BACK_DISTANCE);
pool.get_range(block, min_height, max_height)
.filter(|block| block.rank > Rank(0))
.count()
Expand Down
11 changes: 11 additions & 0 deletions rs/phantom_newtype/src/amountof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ where
}
}

impl<Unit, Repr> num_traits::SaturatingSub for AmountOf<Unit, Repr>
where
Repr: num_traits::SaturatingSub,
{
/// Saturating subtraction. Computes `self - other`, saturating
/// at the relevant high or low boundary of the type.
fn saturating_sub(&self, rhs: &Self) -> Self {
Self(self.0.saturating_sub(&rhs.0), PhantomData)
}
}

impl<Unit, Repr> AddAssign for AmountOf<Unit, Repr>
where
Repr: AddAssign,
Expand Down

0 comments on commit b48c0e1

Please sign in to comment.