Skip to content

Commit 14daf26

Browse files
committed
use saturating math
1 parent b684810 commit 14daf26

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

pallets/drand/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use sp_runtime::{
5555
offchain::{http, Duration},
5656
traits::{Hash, One},
5757
transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction},
58-
KeyTypeId,
58+
KeyTypeId, Saturating,
5959
};
6060

6161
pub mod bls12_381;
@@ -331,7 +331,7 @@ pub mod pallet {
331331

332332
// Update the next unsigned block number
333333
let current_block = frame_system::Pallet::<T>::block_number();
334-
<NextUnsignedAt<T>>::put(current_block + One::one());
334+
<NextUnsignedAt<T>>::put(current_block.saturating_add(One::one()));
335335

336336
// Emit a single event with all new rounds
337337
if !new_rounds.is_empty() {
@@ -358,7 +358,7 @@ pub mod pallet {
358358

359359
// now increment the block number at which we expect next unsigned transaction.
360360
let current_block = frame_system::Pallet::<T>::block_number();
361-
<NextUnsignedAt<T>>::put(current_block + One::one());
361+
<NextUnsignedAt<T>>::put(current_block.saturating_add(One::one()));
362362

363363
Self::deposit_event(Event::BeaconConfigChanged {});
364364
Ok(())
@@ -455,7 +455,9 @@ impl<T: Config> Pallet<T> {
455455
);
456456
let mut pulses = Vec::new();
457457

458-
for round in (last_stored_round + 1)..=(last_stored_round + rounds_to_fetch) {
458+
for round in (last_stored_round.saturating_add(1))
459+
..=(last_stored_round.saturating_add(rounds_to_fetch))
460+
{
459461
let pulse_body = Self::fetch_drand_by_round(round)
460462
.map_err(|_| "Drand: Failed to query drand for round")?;
461463
let unbounded_pulse: DrandResponseBody = serde_json::from_str(&pulse_body)
@@ -485,7 +487,7 @@ impl<T: Config> Pallet<T> {
485487
Ok(()) => log::debug!(
486488
"Drand: [{:?}] Submitted new pulses up to round: {:?}",
487489
acc.id,
488-
last_stored_round + rounds_to_fetch
490+
last_stored_round.saturating_add(rounds_to_fetch)
489491
),
490492
Err(e) => log::error!(
491493
"Drand: [{:?}] Failed to submit transaction: {:?}",
@@ -612,7 +614,8 @@ pub fn message(current_round: RoundNumber, prev_sig: &[u8]) -> Vec<u8> {
612614
impl<T: Config> Randomness<T::Hash, BlockNumberFor<T>> for Pallet<T> {
613615
// this function hashes together the subject with the latest known randomness from quicknet
614616
fn random(subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
615-
let block_number_minus_one = <frame_system::Pallet<T>>::block_number() - One::one();
617+
let block_number_minus_one =
618+
<frame_system::Pallet<T>>::block_number().saturating_sub(One::one());
616619

617620
let last_stored_round = LastStoredRound::<T>::get();
618621

0 commit comments

Comments
 (0)