Skip to content

Commit

Permalink
Define RangeInclusiveExt (paritytech#2037)
Browse files Browse the repository at this point in the history
* Define RangeInclusiveExt

* Use RangeInclusiveExt

* Add docs
  • Loading branch information
serban300 committed Apr 8, 2024
1 parent e6dc1e4 commit 8b9296d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
16 changes: 0 additions & 16 deletions bridges/bin/runtime-common/src/messages_call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,6 @@ pub enum CallInfo {
ReceiveMessagesDeliveryProof(ReceiveMessagesDeliveryProofInfo),
}

impl CallInfo {
/// Returns number of messages, bundled with this transaction.
pub fn bundled_messages(&self) -> MessageNonce {
let bundled_range = match *self {
Self::ReceiveMessagesProof(ref info) => &info.base.bundled_range,
Self::ReceiveMessagesDeliveryProof(ref info) => &info.0.bundled_range,
};

bundled_range
.end()
.checked_sub(*bundled_range.start())
.map(|d| d.saturating_add(1))
.unwrap_or(0)
}
}

/// Helper struct that provides methods for working with a call supported by `CallInfo`.
pub struct CallHelper<T: Config<I>, I: 'static> {
pub _phantom_data: sp_std::marker::PhantomData<(T, I)>,
Expand Down
6 changes: 3 additions & 3 deletions bridges/bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::messages_call_ext::{
};
use bp_messages::LaneId;
use bp_relayers::{RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::StaticStrProvider;
use bp_runtime::{RangeInclusiveExt, StaticStrProvider};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{CallableCallFor, DispatchInfo, Dispatchable, PostDispatchInfo},
Expand Down Expand Up @@ -319,9 +319,9 @@ where
if let Some(parsed_call) = parsed_call {
// we give delivery transactions some boost, that depends on number of messages inside
let messages_call_info = parsed_call.messages_call_info();
if let MessagesCallInfo::ReceiveMessagesProof(_) = messages_call_info {
if let MessagesCallInfo::ReceiveMessagesProof(info) = messages_call_info {
// compute total number of messages in transaction
let bundled_messages = messages_call_info.bundled_messages();
let bundled_messages = info.base.bundled_range.checked_len().unwrap_or(0);

// a quick check to avoid invalid high-priority transactions
if bundled_messages <= Runtime::MaxUnconfirmedMessagesAtInboundLane::get() {
Expand Down
7 changes: 2 additions & 5 deletions bridges/modules/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub const LOG_TARGET: &str = "runtime::bridge-messages";
pub mod pallet {
use super::*;
use bp_messages::{ReceivalResult, ReceivedMessages};
use bp_runtime::RangeInclusiveExt;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -514,11 +515,7 @@ pub mod pallet {
);
relayers_state.total_messages = sp_std::cmp::min(
relayers_state.total_messages,
received_range
.end()
.checked_sub(*received_range.start())
.and_then(|x| x.checked_add(1))
.unwrap_or(MessageNonce::MAX),
received_range.checked_len().unwrap_or(MessageNonce::MAX),
);
};

Expand Down
8 changes: 2 additions & 6 deletions bridges/primitives/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![allow(clippy::too_many_arguments)]

use bitvec::prelude::*;
use bp_runtime::{BasicOperatingMode, OperatingMode};
use bp_runtime::{BasicOperatingMode, OperatingMode, RangeInclusiveExt};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
Expand Down Expand Up @@ -347,11 +347,7 @@ impl DeliveredMessages {

/// Return total count of delivered messages.
pub fn total_messages(&self) -> MessageNonce {
if self.end >= self.begin {
self.end - self.begin + 1
} else {
0
}
(self.begin..=self.end).checked_len().unwrap_or(0)
}

/// Note new dispatched message.
Expand Down
21 changes: 19 additions & 2 deletions bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ use frame_system::RawOrigin;
use scale_info::TypeInfo;
use sp_core::storage::StorageKey;
use sp_runtime::traits::{BadOrigin, Header as HeaderT, UniqueSaturatedInto};
use sp_std::{convert::TryFrom, fmt::Debug, vec, vec::Vec};
use sp_std::{convert::TryFrom, fmt::Debug, ops::RangeInclusive, vec, vec::Vec};

pub use chain::{
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, EncodedOrDecodedCall, HashOf,
HasherOf, HeaderOf, IndexOf, Parachain, SignatureOf, TransactionEraOf, UnderlyingChainOf,
UnderlyingChainProvider,
};
pub use frame_support::storage::storage_prefix as storage_value_final_key;
use num_traits::{CheckedSub, One};
use num_traits::{CheckedAdd, CheckedSub, One};
pub use storage_proof::{
record_all_keys as record_all_trie_keys, Error as StorageProofError,
ProofSize as StorageProofSize, RawStorageProof, StorageProofChecker,
Expand Down Expand Up @@ -523,6 +523,23 @@ impl<T> Debug for StrippableError<T> {
}
}

/// A trait defining helper methods for `RangeInclusive` (start..=end)
pub trait RangeInclusiveExt<Idx> {
/// Computes the length of the `RangeInclusive`, checking for underflow and overflow.
fn checked_len(&self) -> Option<Idx>;
}

impl<Idx> RangeInclusiveExt<Idx> for RangeInclusive<Idx>
where
Idx: CheckedSub + CheckedAdd + One,
{
fn checked_len(&self) -> Option<Idx> {
self.end()
.checked_sub(self.start())
.and_then(|len| len.checked_add(&Idx::one()))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 8b9296d

Please sign in to comment.