FRAME: Allow message ID to be mutated in ProcessMessage (#14183)

This commit is contained in:
Gavin Wood
2023-05-21 20:20:14 +01:00
committed by GitHub
parent 4e892439ab
commit 7f0597a1e6
6 changed files with 24 additions and 21 deletions
@@ -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,
@@ -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,
@@ -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,
+10 -12
View File
@@ -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};
@@ -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,
@@ -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,
@@ -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
},
}
@@ -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)?;
@@ -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);
@@ -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);
+7 -6
View File
@@ -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() {
@@ -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,
}
@@ -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,
}
@@ -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,
}
@@ -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,
@@ -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,
@@ -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,