Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
FRAME: Allow message ID to be mutated in ProcessMessage (#14183)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork authored May 21, 2023
1 parent 2000147 commit 8fca6cf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
6 changes: 3 additions & 3 deletions frame/message-queue/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod benchmarks {
// Check that it was processed.
assert_last_event::<T>(
Event::Processed {
hash: T::Hashing::hash(&msg),
id: sp_io::hashing::blake2_256(&msg),
origin: 0.into(),
weight_used: 1.into_weight(),
success: true,
Expand Down Expand Up @@ -227,7 +227,7 @@ mod benchmarks {

assert_last_event::<T>(
Event::Processed {
hash: T::Hashing::hash(&((msgs - 1) as u32).encode()),
id: sp_io::hashing::blake2_256(&((msgs - 1) as u32).encode()),
origin: 0.into(),
weight_used: Weight::from_parts(1, 1),
success: true,
Expand Down Expand Up @@ -264,7 +264,7 @@ mod benchmarks {

assert_last_event::<T>(
Event::Processed {
hash: T::Hashing::hash(&((msgs - 1) as u32).encode()),
id: sp_io::hashing::blake2_256(&((msgs - 1) as u32).encode()),
origin: 0.into(),
weight_used: Weight::from_parts(1, 1),
success: true,
Expand Down
22 changes: 10 additions & 12 deletions frame/message-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub use pallet::*;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
use sp_runtime::{
traits::{Hash, One, Zero},
traits::{One, Zero},
SaturatedConversion, Saturating,
};
use sp_std::{fmt::Debug, ops::Deref, prelude::*, vec};
Expand Down Expand Up @@ -499,16 +499,13 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Message discarded due to an inability to decode the item. Usually caused by state
/// corruption.
Discarded { hash: T::Hash },
/// Message discarded due to an error in the `MessageProcessor` (usually a format error).
ProcessingFailed { hash: T::Hash, origin: MessageOriginOf<T>, error: ProcessMessageError },
ProcessingFailed { id: [u8; 32], origin: MessageOriginOf<T>, error: ProcessMessageError },
/// Message is processed.
Processed { hash: T::Hash, origin: MessageOriginOf<T>, weight_used: Weight, success: bool },
Processed { id: [u8; 32], origin: MessageOriginOf<T>, weight_used: Weight, success: bool },
/// Message placed in overweight queue.
OverweightEnqueued {
hash: T::Hash,
id: [u8; 32],
origin: MessageOriginOf<T>,
page_index: PageIndex,
message_index: T::Size,
Expand Down Expand Up @@ -1147,15 +1144,16 @@ impl<T: Config> Pallet<T> {
meter: &mut WeightMeter,
overweight_limit: Weight,
) -> MessageExecutionStatus {
let hash = T::Hashing::hash(message);
let hash = sp_io::hashing::blake2_256(message);
use ProcessMessageError::*;
let prev_consumed = meter.consumed;
let mut id = hash;

match T::MessageProcessor::process_message(message, origin.clone(), meter) {
match T::MessageProcessor::process_message(message, origin.clone(), meter, &mut id) {
Err(Overweight(w)) if w.any_gt(overweight_limit) => {
// Permanently overweight.
Self::deposit_event(Event::<T>::OverweightEnqueued {
hash,
id,
origin,
page_index,
message_index,
Expand All @@ -1173,13 +1171,13 @@ impl<T: Config> Pallet<T> {
},
Err(error @ BadFormat | error @ Corrupt | error @ Unsupported) => {
// Permanent error - drop
Self::deposit_event(Event::<T>::ProcessingFailed { hash, origin, error });
Self::deposit_event(Event::<T>::ProcessingFailed { id, origin, error });
MessageExecutionStatus::Unprocessable { permanent: true }
},
Ok(success) => {
// Success
let weight_used = meter.consumed.saturating_sub(prev_consumed);
Self::deposit_event(Event::<T>::Processed { hash, origin, weight_used, success });
Self::deposit_event(Event::<T>::Processed { id, origin, weight_used, success });
MessageExecutionStatus::Processed
},
}
Expand Down
2 changes: 2 additions & 0 deletions frame/message-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl ProcessMessage for RecordingMessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
_id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
processing_message(message, &origin)?;

Expand Down Expand Up @@ -239,6 +240,7 @@ impl ProcessMessage for CountingMessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
_id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
if let Err(e) = processing_message(message, &origin) {
NumMessagesErrored::set(NumMessagesErrored::get() + 1);
Expand Down
1 change: 1 addition & 0 deletions frame/message-queue/src/mock_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
_message: &[u8],
_origin: Self::Origin,
meter: &mut WeightMeter,
_id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let required = Weight::from_parts(REQUIRED_WEIGHT, REQUIRED_WEIGHT);

Expand Down
13 changes: 7 additions & 6 deletions frame/message-queue/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{mock::*, *};

use frame_support::{assert_noop, assert_ok, assert_storage_noop, StorageNoopGuard};
use rand::{rngs::StdRng, Rng, SeedableRng};
use sp_core::blake2_256;

#[test]
fn mocked_weight_works() {
Expand Down Expand Up @@ -178,7 +179,7 @@ fn service_queues_failing_messages_works() {
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"badformat"),
id: blake2_256(b"badformat"),
origin: MessageOrigin::Here,
error: ProcessMessageError::BadFormat,
}
Expand All @@ -187,7 +188,7 @@ fn service_queues_failing_messages_works() {
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"corrupt"),
id: blake2_256(b"corrupt"),
origin: MessageOrigin::Here,
error: ProcessMessageError::Corrupt,
}
Expand All @@ -196,7 +197,7 @@ fn service_queues_failing_messages_works() {
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"unsupported"),
id: blake2_256(b"unsupported"),
origin: MessageOrigin::Here,
error: ProcessMessageError::Unsupported,
}
Expand Down Expand Up @@ -677,7 +678,7 @@ fn service_page_item_skips_perm_overweight_message() {
assert_eq!(weight.consumed, 2.into_weight());
assert_last_event::<Test>(
Event::OverweightEnqueued {
hash: <Test as frame_system::Config>::Hashing::hash(b"TooMuch"),
id: blake2_256(b"TooMuch"),
origin: MessageOrigin::Here,
message_index: 0,
page_index: 0,
Expand Down Expand Up @@ -1050,7 +1051,7 @@ fn execute_overweight_works() {
assert_eq!(QueueChanges::take(), vec![(origin, 1, 8)]);
assert_last_event::<Test>(
Event::OverweightEnqueued {
hash: <Test as frame_system::Config>::Hashing::hash(b"weight=6"),
id: blake2_256(b"weight=6"),
origin: MessageOrigin::Here,
message_index: 0,
page_index: 0,
Expand Down Expand Up @@ -1105,7 +1106,7 @@ fn permanently_overweight_book_unknits() {
assert_eq!(MessageQueue::service_queues(8.into_weight()), 4.into_weight());
assert_last_event::<Test>(
Event::OverweightEnqueued {
hash: <Test as frame_system::Config>::Hashing::hash(b"weight=9"),
id: blake2_256(b"weight=9"),
origin: Here,
message_index: 0,
page_index: 0,
Expand Down
1 change: 1 addition & 0 deletions frame/support/src/traits/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait ProcessMessage {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError>;
}

Expand Down

0 comments on commit 8fca6cf

Please sign in to comment.