Skip to content

Commit 25f9cf5

Browse files
authored
Another use of RangeInclusiveExt::checked_len() (#2060)
1 parent 4942c12 commit 25f9cf5

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

bin/runtime-common/src/messages.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! pallet is used to dispatch incoming messages. Message identified by a tuple
2121
//! of to elements - message lane id and message nonce.
2222
23-
pub use bp_runtime::{UnderlyingChainOf, UnderlyingChainProvider};
23+
pub use bp_runtime::{RangeInclusiveExt, UnderlyingChainOf, UnderlyingChainProvider};
2424

2525
use bp_header_chain::{HeaderChain, HeaderChainError};
2626
use bp_messages::{
@@ -365,6 +365,7 @@ pub mod target {
365365
nonces_start,
366366
nonces_end,
367367
} = proof;
368+
let nonces_range = nonces_start..=nonces_end;
368369

369370
B::BridgedHeaderChain::parse_finalized_storage_proof(
370371
bridged_header_hash,
@@ -374,26 +375,17 @@ pub mod target {
374375
StorageProofCheckerAdapter::<_, B> { storage, _dummy: Default::default() };
375376

376377
// receiving proofs where end < begin is ok (if proof includes outbound lane state)
377-
let messages_in_the_proof =
378-
if let Some(nonces_difference) = nonces_end.checked_sub(nonces_start) {
379-
// let's check that the user (relayer) has passed correct `messages_count`
380-
// (this bounds maximal capacity of messages vec below)
381-
let messages_in_the_proof = nonces_difference.saturating_add(1);
382-
if messages_in_the_proof != MessageNonce::from(messages_count) {
383-
return Err(Error::MessagesCountMismatch)
384-
}
385-
386-
messages_in_the_proof
387-
} else {
388-
0
389-
};
378+
let messages_in_the_proof = nonces_range.checked_len().unwrap_or(0);
379+
if messages_in_the_proof != MessageNonce::from(messages_count) {
380+
return Err(Error::MessagesCountMismatch)
381+
}
390382

391383
// Read messages first. All messages that are claimed to be in the proof must
392384
// be in the proof. So any error in `read_value`, or even missing value is fatal.
393385
//
394386
// Mind that we allow proofs with no messages if outbound lane state is proved.
395387
let mut messages = Vec::with_capacity(messages_in_the_proof as _);
396-
for nonce in nonces_start..=nonces_end {
388+
for nonce in nonces_range {
397389
let message_key = MessageKey { lane_id: lane, nonce };
398390
let message_payload = parser.read_and_decode_message_payload(&message_key)?;
399391
messages.push(Message { key: message_key, payload: message_payload });

0 commit comments

Comments
 (0)