mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 22:31:06 +00:00
dmp-queue: Store messages if already processed more than the maximum (#2343)
* dmp-queue: Store messages if already processed more than the maximum * Put new event at the end
This commit is contained in:
committed by
EgorPopelyaev
parent
5e23bb7579
commit
71b6d4a0fe
@@ -189,6 +189,8 @@ pub mod pallet {
|
|||||||
},
|
},
|
||||||
/// Downward message from the overweight queue was executed.
|
/// Downward message from the overweight queue was executed.
|
||||||
OverweightServiced { overweight_index: OverweightIndex, weight_used: Weight },
|
OverweightServiced { overweight_index: OverweightIndex, weight_used: Weight },
|
||||||
|
/// The maximum number of downward messages was.
|
||||||
|
MaxMessagesExhausted { message_id: MessageId },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Config> Pallet<T> {
|
impl<T: Config> Pallet<T> {
|
||||||
@@ -306,46 +308,53 @@ pub mod pallet {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (i, (sent_at, data)) in iter.enumerate() {
|
for (i, (sent_at, data)) in iter.enumerate() {
|
||||||
if messages_processed >= MAX_MESSAGES_PER_BLOCK {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if maybe_enqueue_page.is_none() {
|
if maybe_enqueue_page.is_none() {
|
||||||
// We're not currently enqueuing - try to execute inline.
|
if messages_processed >= MAX_MESSAGES_PER_BLOCK {
|
||||||
let remaining_weight = limit.saturating_sub(used);
|
let item_count_left = item_count.saturating_sub(i);
|
||||||
messages_processed += 1;
|
maybe_enqueue_page = Some(Vec::with_capacity(item_count_left));
|
||||||
match Self::try_service_message(remaining_weight, sent_at, &data[..]) {
|
|
||||||
Ok(consumed) => used += consumed,
|
Self::deposit_event(Event::MaxMessagesExhausted {
|
||||||
Err((message_id, required_weight)) =>
|
message_id: sp_io::hashing::blake2_256(&data),
|
||||||
// Too much weight required right now.
|
});
|
||||||
{
|
} else {
|
||||||
let is_under_limit = Overweight::<T>::count() < MAX_OVERWEIGHT_MESSAGES;
|
// We're not currently enqueuing - try to execute inline.
|
||||||
used.saturating_accrue(T::DbWeight::get().reads(1));
|
let remaining_weight = limit.saturating_sub(used);
|
||||||
if required_weight.any_gt(config.max_individual) && is_under_limit {
|
messages_processed += 1;
|
||||||
// overweight - add to overweight queue and continue with
|
match Self::try_service_message(remaining_weight, sent_at, &data[..]) {
|
||||||
// message execution.
|
Ok(consumed) => used += consumed,
|
||||||
let overweight_index = page_index.overweight_count;
|
Err((message_id, required_weight)) =>
|
||||||
Overweight::<T>::insert(overweight_index, (sent_at, data));
|
// Too much weight required right now.
|
||||||
Self::deposit_event(Event::OverweightEnqueued {
|
{
|
||||||
message_id,
|
let is_under_limit =
|
||||||
overweight_index,
|
Overweight::<T>::count() < MAX_OVERWEIGHT_MESSAGES;
|
||||||
required_weight,
|
used.saturating_accrue(T::DbWeight::get().reads(1));
|
||||||
});
|
if required_weight.any_gt(config.max_individual) && is_under_limit {
|
||||||
page_index.overweight_count += 1;
|
// overweight - add to overweight queue and continue with
|
||||||
// Not needed for control flow, but only to ensure that the compiler
|
// message execution.
|
||||||
// understands that we won't attempt to re-use `data` later.
|
let overweight_index = page_index.overweight_count;
|
||||||
continue
|
Overweight::<T>::insert(overweight_index, (sent_at, data));
|
||||||
} else {
|
Self::deposit_event(Event::OverweightEnqueued {
|
||||||
// not overweight. stop executing inline and enqueue normally
|
message_id,
|
||||||
// from here on.
|
overweight_index,
|
||||||
let item_count_left = item_count.saturating_sub(i);
|
required_weight,
|
||||||
maybe_enqueue_page = Some(Vec::with_capacity(item_count_left));
|
});
|
||||||
Self::deposit_event(Event::WeightExhausted {
|
page_index.overweight_count += 1;
|
||||||
message_id,
|
// Not needed for control flow, but only to ensure that the compiler
|
||||||
remaining_weight,
|
// understands that we won't attempt to re-use `data` later.
|
||||||
required_weight,
|
continue
|
||||||
});
|
} else {
|
||||||
}
|
// not overweight. stop executing inline and enqueue normally
|
||||||
},
|
// from here on.
|
||||||
|
let item_count_left = item_count.saturating_sub(i);
|
||||||
|
maybe_enqueue_page = Some(Vec::with_capacity(item_count_left));
|
||||||
|
Self::deposit_event(Event::WeightExhausted {
|
||||||
|
message_id,
|
||||||
|
remaining_weight,
|
||||||
|
required_weight,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Cannot be an `else` here since the `maybe_enqueue_page` may have changed.
|
// Cannot be an `else` here since the `maybe_enqueue_page` may have changed.
|
||||||
@@ -888,4 +897,37 @@ mod tests {
|
|||||||
assert_eq!(pages_queued(), 1);
|
assert_eq!(pages_queued(), 1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handle_max_messages_per_block() {
|
||||||
|
new_test_ext().execute_with(|| {
|
||||||
|
enqueue(&vec![msg(1000), msg(1001)]);
|
||||||
|
enqueue(&vec![msg(1002), msg(1003)]);
|
||||||
|
enqueue(&vec![msg(1004), msg(1005)]);
|
||||||
|
|
||||||
|
let incoming = (0..MAX_MESSAGES_PER_BLOCK)
|
||||||
|
.into_iter()
|
||||||
|
.map(|i| msg(1006 + i as u64))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
handle_messages(&incoming, Weight::from_parts(25000, 25000));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
take_trace(),
|
||||||
|
(0..MAX_MESSAGES_PER_BLOCK)
|
||||||
|
.into_iter()
|
||||||
|
.map(|i| msg_complete(1000 + i as u64))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
|
assert_eq!(pages_queued(), 1);
|
||||||
|
|
||||||
|
handle_messages(&[], Weight::from_parts(25000, 25000));
|
||||||
|
assert_eq!(
|
||||||
|
take_trace(),
|
||||||
|
(MAX_MESSAGES_PER_BLOCK..MAX_MESSAGES_PER_BLOCK + 6)
|
||||||
|
.into_iter()
|
||||||
|
.map(|i| msg_complete(1000 + i as u64))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user