diff --git a/substrate/frame/message-queue/src/lib.rs b/substrate/frame/message-queue/src/lib.rs index ec85c785f79e..949b8c196879 100644 --- a/substrate/frame/message-queue/src/lib.rs +++ b/substrate/frame/message-queue/src/lib.rs @@ -646,7 +646,7 @@ pub mod pallet { impl Hooks> for Pallet { fn on_initialize(_n: BlockNumberFor) -> Weight { if let Some(weight_limit) = T::ServiceWeight::get() { - Self::service_queues(weight_limit) + Self::service_queues(weight_limit, ServiceQueuesContext::OnInitialize) } else { Weight::zero() } @@ -655,7 +655,7 @@ pub mod pallet { fn on_idle(_n: BlockNumberFor, remaining_weight: Weight) -> Weight { if let Some(weight_limit) = T::IdleMaxServiceWeight::get() { // Make use of the remaining weight to process enqueued messages. - Self::service_queues(weight_limit.min(remaining_weight)) + Self::service_queues(weight_limit.min(remaining_weight), ServiceQueuesContext::OnIdle) } else { Weight::zero() } @@ -673,6 +673,16 @@ pub mod pallet { } } + /// The context to pass to service_queues through on_idle and on_initialize hooks + /// We don't want to throw the defensive message if called from on_idle hook + #[derive(PartialEq)] + enum ServiceQueuesContext { + /// Context of on_idle hook + OnIdle, + /// Context of on_initialize hook + OnInitialize, + } + #[pallet::call] impl Pallet { /// Remove a page which has no more messages remaining to be processed or is stale. @@ -1542,12 +1552,14 @@ impl, O: Into> Get for IntoU32 { impl ServiceQueues for Pallet { type OverweightMessageAddress = (MessageOriginOf, PageIndex, T::Size); - fn service_queues(weight_limit: Weight) -> Weight { + fn service_queues(weight_limit: Weight, context: ServiceQueuesContext) -> Weight { let mut weight = WeightMeter::with_limit(weight_limit); // Get the maximum weight that processing a single message may take: let max_weight = Self::max_message_weight(weight_limit).unwrap_or_else(|| { - defensive!("Not enough weight to service a single message."); + if matches(context, ServiceQueuesContext::OnInitialize) { + defensive!("Not enough weight to service a single message."); + } Weight::zero() });