Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renames WeightMeter constructors for better readability #1186

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cumulus/xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ macro_rules! decl_test_networks {
use $crate::{Bounded, ProcessMessage, WeightMeter};
use sp_core::Encode;
while let Some((from_para_id, msg)) = $crate::UPWARD_MESSAGES.with(|b| b.borrow_mut().get_mut(Self::name()).unwrap().pop_front()) {
let mut weight_meter = WeightMeter::max_limit();
let mut weight_meter = WeightMeter::new();
<$relay_chain>::ext_wrapper(|| {
let _ = <$relay_chain as RelayChain>::MessageProcessor::process_message(
&msg[..],
Expand Down
6 changes: 3 additions & 3 deletions polkadot/xcm/xcm-builder/src/process_xcm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {

// Errors if we stay below a weight limit of 1000.
for i in 0..10 {
let meter = &mut WeightMeter::from_limit((i * 10).into());
let meter = &mut WeightMeter::with_limit((i * 10).into());
let mut id = [0; 32];
assert_err!(
Processor::process_message(msg, ORIGIN, meter, &mut id),
Expand All @@ -120,7 +120,7 @@ mod tests {
}

// Works with a limit of 1000.
let meter = &mut WeightMeter::from_limit(1000.into());
let meter = &mut WeightMeter::with_limit(1000.into());
let mut id = [0; 32];
assert_ok!(Processor::process_message(msg, ORIGIN, meter, &mut id));
assert_eq!(meter.consumed(), 1000.into());
Expand Down Expand Up @@ -150,6 +150,6 @@ mod tests {
}

fn process_raw(raw: &[u8]) -> Result<bool, ProcessMessageError> {
Processor::process_message(raw, ORIGIN, &mut WeightMeter::max_limit(), &mut [0; 32])
Processor::process_message(raw, ORIGIN, &mut WeightMeter::new(), &mut [0; 32])
}
}
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-simulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ macro_rules! decl_test_network {
let mut _id = [0; 32];
let r = <$relay_chain>::process_message(
encoded.as_slice(), para_id,
&mut $crate::WeightMeter::max_limit(),
&mut $crate::WeightMeter::new(),
&mut _id,
);
match r {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/broker/src/tick_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T: Config> Pallet<T> {
_ => return Weight::zero(),
};

let mut meter = WeightMeter::max_limit();
let mut meter = WeightMeter::new();

if Self::process_core_count(&mut status) {
meter.consume(T::WeightInfo::process_core_count(status.core_count.into()));
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/glutton/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub mod pallet {
}

fn on_idle(_: BlockNumberFor<T>, remaining_weight: Weight) -> Weight {
let mut meter = WeightMeter::from_limit(remaining_weight);
let mut meter = WeightMeter::with_limit(remaining_weight);
if meter.try_consume(T::WeightInfo::empty_on_idle()).is_err() {
return T::WeightInfo::empty_on_idle()
}
Expand All @@ -197,7 +197,7 @@ pub mod pallet {
Storage::<T>::get().saturating_mul_int(meter.remaining().proof_size());
let computation_weight_limit =
Compute::<T>::get().saturating_mul_int(meter.remaining().ref_time());
let mut meter = WeightMeter::from_limit(Weight::from_parts(
let mut meter = WeightMeter::with_limit(Weight::from_parts(
computation_weight_limit,
proof_size_limit,
));
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/glutton/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn on_idle_weight_over_unity_is_close_enough_works() {
fn waste_at_most_ref_time_weight_close_enough() {
new_test_ext().execute_with(|| {
let mut meter =
WeightMeter::from_limit(Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX));
WeightMeter::with_limit(Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX));
// Over-spending fails defensively.
Glutton::waste_at_most_ref_time(&mut meter);

Expand All @@ -269,7 +269,7 @@ fn waste_at_most_ref_time_weight_close_enough() {
fn waste_at_most_proof_size_weight_close_enough() {
new_test_ext().execute_with(|| {
let mut meter =
WeightMeter::from_limit(Weight::from_parts(u64::MAX, WEIGHT_PROOF_SIZE_PER_MB * 5));
WeightMeter::with_limit(Weight::from_parts(u64::MAX, WEIGHT_PROOF_SIZE_PER_MB * 5));
// Over-spending fails defensively.
Glutton::waste_at_most_proof_size(&mut meter);

Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/message-queue/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod benchmarks {
fn service_queue_base() {
#[block]
{
MessageQueue::<T>::service_queue(0.into(), &mut WeightMeter::max_limit(), Weight::MAX);
MessageQueue::<T>::service_queue(0.into(), &mut WeightMeter::new(), Weight::MAX);
}
}

Expand All @@ -89,7 +89,7 @@ mod benchmarks {
let page = PageOf::<T>::default();
Pages::<T>::insert(&origin, 0, &page);
let mut book_state = single_page_book::<T>();
let mut meter = WeightMeter::max_limit();
let mut meter = WeightMeter::new();
let limit = Weight::MAX;

#[block]
Expand All @@ -108,7 +108,7 @@ mod benchmarks {
page.remaining = 1.into();
Pages::<T>::insert(&origin, 0, &page);
let mut book_state = single_page_book::<T>();
let mut meter = WeightMeter::max_limit();
let mut meter = WeightMeter::new();
let limit = Weight::MAX;

#[block]
Expand All @@ -124,7 +124,7 @@ mod benchmarks {
let mut page = page::<T>(&msg.clone());
let mut book = book_for::<T>(&page);
assert!(page.peek_first().is_some(), "There is one message");
let mut weight = WeightMeter::max_limit();
let mut weight = WeightMeter::new();

#[block]
{
Expand Down Expand Up @@ -158,7 +158,7 @@ mod benchmarks {
#[benchmark]
fn bump_service_head() {
setup_bump_service_head::<T>(0.into(), 10.into());
let mut weight = WeightMeter::max_limit();
let mut weight = WeightMeter::new();

#[block]
{
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/message-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ impl<T: Config> Pallet<T> {
);
ensure!(!is_processed, Error::<T>::AlreadyProcessed);
use MessageExecutionStatus::*;
let mut weight_counter = WeightMeter::from_limit(weight_limit);
let mut weight_counter = WeightMeter::with_limit(weight_limit);
match Self::process_message_payload(
origin.clone(),
page_index,
Expand Down Expand Up @@ -1150,7 +1150,7 @@ impl<T: Config> Pallet<T> {
//loop around this origin
let starting_origin = ServiceHead::<T>::get().unwrap();

while let Some(head) = Self::bump_service_head(&mut WeightMeter::max_limit()) {
while let Some(head) = Self::bump_service_head(&mut WeightMeter::new()) {
ensure!(
BookStateFor::<T>::contains_key(&head),
"Service head must point to an existing book"
Expand Down Expand Up @@ -1362,7 +1362,7 @@ impl<T: Config> ServiceQueues for Pallet<T> {
fn service_queues(weight_limit: Weight) -> Weight {
// The maximum weight that processing a single message may take.
let overweight_limit = weight_limit;
let mut weight = WeightMeter::from_limit(weight_limit);
let mut weight = WeightMeter::with_limit(weight_limit);

let mut next = match Self::bump_service_head(&mut weight) {
Some(h) => h,
Expand Down Expand Up @@ -1402,7 +1402,7 @@ impl<T: Config> ServiceQueues for Pallet<T> {
weight_limit: Weight,
(message_origin, page, index): Self::OverweightMessageAddress,
) -> Result<Weight, ExecuteOverweightError> {
let mut weight = WeightMeter::from_limit(weight_limit);
let mut weight = WeightMeter::with_limit(weight_limit);
if weight
.try_consume(
T::WeightInfo::execute_overweight_page_removed()
Expand Down
32 changes: 16 additions & 16 deletions substrate/frame/message-queue/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ fn service_queue_bails() {
// Not enough weight for `service_queue_base`.
build_and_execute::<Test>(|| {
set_weight("service_queue_base", 2.into_weight());
let mut meter = WeightMeter::from_limit(1.into_weight());
let mut meter = WeightMeter::with_limit(1.into_weight());

assert_storage_noop!(MessageQueue::service_queue(0u32.into(), &mut meter, Weight::MAX));
assert!(meter.consumed().is_zero());
});
// Not enough weight for `ready_ring_unknit`.
build_and_execute::<Test>(|| {
set_weight("ready_ring_unknit", 2.into_weight());
let mut meter = WeightMeter::from_limit(1.into_weight());
let mut meter = WeightMeter::with_limit(1.into_weight());

assert_storage_noop!(MessageQueue::service_queue(0u32.into(), &mut meter, Weight::MAX));
assert!(meter.consumed().is_zero());
Expand All @@ -399,7 +399,7 @@ fn service_queue_bails() {
set_weight("service_queue_base", 2.into_weight());
set_weight("ready_ring_unknit", 2.into_weight());

let mut meter = WeightMeter::from_limit(3.into_weight());
let mut meter = WeightMeter::with_limit(3.into_weight());
assert_storage_noop!(MessageQueue::service_queue(0.into(), &mut meter, Weight::MAX));
assert!(meter.consumed().is_zero());
});
Expand All @@ -426,7 +426,7 @@ fn service_page_works() {
msgs -= process;

// Enough weight to process `process` messages.
let mut meter = WeightMeter::from_limit(((2 + (3 + 1) * process) as u64).into_weight());
let mut meter = WeightMeter::with_limit(((2 + (3 + 1) * process) as u64).into_weight());
System::reset_events();
let (processed, status) =
crate::Pallet::<Test>::service_page(&Here, &mut book, &mut meter, Weight::MAX);
Expand All @@ -449,7 +449,7 @@ fn service_page_bails() {
// Not enough weight for `service_page_base_completion`.
build_and_execute::<Test>(|| {
set_weight("service_page_base_completion", 2.into_weight());
let mut meter = WeightMeter::from_limit(1.into_weight());
let mut meter = WeightMeter::with_limit(1.into_weight());

let (page, _) = full_page::<Test>();
let mut book = book_for::<Test>(&page);
Expand All @@ -466,7 +466,7 @@ fn service_page_bails() {
// Not enough weight for `service_page_base_no_completion`.
build_and_execute::<Test>(|| {
set_weight("service_page_base_no_completion", 2.into_weight());
let mut meter = WeightMeter::from_limit(1.into_weight());
let mut meter = WeightMeter::with_limit(1.into_weight());

let (page, _) = full_page::<Test>();
let mut book = book_for::<Test>(&page);
Expand All @@ -487,7 +487,7 @@ fn service_page_item_bails() {
build_and_execute::<Test>(|| {
let _guard = StorageNoopGuard::default();
let (mut page, _) = full_page::<Test>();
let mut weight = WeightMeter::from_limit(10.into_weight());
let mut weight = WeightMeter::with_limit(10.into_weight());
let overweight_limit = 10.into_weight();
set_weight("service_page_item", 11.into_weight());

Expand Down Expand Up @@ -518,7 +518,7 @@ fn service_page_suspension_works() {
Pages::<Test>::insert(Here, 0, page);

// First we process 5 messages from this page.
let mut meter = WeightMeter::from_limit(5.into_weight());
let mut meter = WeightMeter::with_limit(5.into_weight());
let (_, status) =
crate::Pallet::<Test>::service_page(&Here, &mut book, &mut meter, Weight::MAX);

Expand All @@ -534,7 +534,7 @@ fn service_page_suspension_works() {
let (_, status) = crate::Pallet::<Test>::service_page(
&Here,
&mut book,
&mut WeightMeter::max_limit(),
&mut WeightMeter::new(),
Weight::MAX,
);
assert_eq!(status, NoProgress);
Expand All @@ -546,7 +546,7 @@ fn service_page_suspension_works() {
let (_, status) = crate::Pallet::<Test>::service_page(
&Here,
&mut book,
&mut WeightMeter::max_limit(),
&mut WeightMeter::new(),
Weight::MAX,
);
assert_eq!(status, NoMore);
Expand All @@ -564,7 +564,7 @@ fn bump_service_head_works() {

// Bump 99 times.
for i in 0..99 {
let current = MessageQueue::bump_service_head(&mut WeightMeter::max_limit()).unwrap();
let current = MessageQueue::bump_service_head(&mut WeightMeter::new()).unwrap();
assert_eq!(current, [Here, There, Everywhere(0)][i % 3]);
}

Expand All @@ -581,7 +581,7 @@ fn bump_service_head_bails() {
setup_bump_service_head::<Test>(0.into(), 1.into());

let _guard = StorageNoopGuard::default();
let mut meter = WeightMeter::from_limit(1.into_weight());
let mut meter = WeightMeter::with_limit(1.into_weight());
assert!(MessageQueue::bump_service_head(&mut meter).is_none());
assert_eq!(meter.consumed(), 0.into_weight());
});
Expand All @@ -591,7 +591,7 @@ fn bump_service_head_bails() {
fn bump_service_head_trivial_works() {
build_and_execute::<Test>(|| {
set_weight("bump_service_head", 2.into_weight());
let mut meter = WeightMeter::max_limit();
let mut meter = WeightMeter::new();

assert_eq!(MessageQueue::bump_service_head(&mut meter), None, "Cannot bump");
assert_eq!(meter.consumed(), 2.into_weight());
Expand All @@ -616,15 +616,15 @@ fn bump_service_head_no_head_noops() {
ServiceHead::<Test>::kill();

// Nothing happens.
assert_storage_noop!(MessageQueue::bump_service_head(&mut WeightMeter::max_limit()));
assert_storage_noop!(MessageQueue::bump_service_head(&mut WeightMeter::new()));
});
}

#[test]
fn service_page_item_consumes_correct_weight() {
build_and_execute::<Test>(|| {
let mut page = page::<Test>(b"weight=3");
let mut weight = WeightMeter::from_limit(10.into_weight());
let mut weight = WeightMeter::with_limit(10.into_weight());
let overweight_limit = 0.into_weight();
set_weight("service_page_item", 2.into_weight());

Expand All @@ -648,7 +648,7 @@ fn service_page_item_consumes_correct_weight() {
fn service_page_item_skips_perm_overweight_message() {
build_and_execute::<Test>(|| {
let mut page = page::<Test>(b"TooMuch");
let mut weight = WeightMeter::from_limit(2.into_weight());
let mut weight = WeightMeter::with_limit(2.into_weight());
let overweight_limit = 0.into_weight();
set_weight("service_page_item", 2.into_weight());

Expand Down
16 changes: 8 additions & 8 deletions substrate/frame/scheduler/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ benchmarks! {
let now = BlockNumberFor::<T>::from(BLOCK_NUMBER);
IncompleteSince::<T>::put(now - One::one());
}: {
Scheduler::<T>::service_agendas(&mut WeightMeter::max_limit(), now, 0);
Scheduler::<T>::service_agendas(&mut WeightMeter::new(), now, 0);
} verify {
assert_eq!(IncompleteSince::<T>::get(), Some(now - One::one()));
}
Expand All @@ -143,7 +143,7 @@ benchmarks! {
fill_schedule::<T>(now, s)?;
let mut executed = 0;
}: {
Scheduler::<T>::service_agenda(&mut WeightMeter::max_limit(), &mut executed, now, now, 0);
Scheduler::<T>::service_agenda(&mut WeightMeter::new(), &mut executed, now, now, 0);
} verify {
assert_eq!(executed, 0);
}
Expand All @@ -154,7 +154,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightMeter::from_limit(Weight::zero());
let mut counter = WeightMeter::with_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
Expand All @@ -172,7 +172,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, Some(s), 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightMeter::from_limit(Weight::zero());
let mut counter = WeightMeter::with_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
Expand All @@ -184,7 +184,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, true, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightMeter::from_limit(Weight::zero());
let mut counter = WeightMeter::with_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
Expand All @@ -196,15 +196,15 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(true, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightMeter::from_limit(Weight::zero());
let mut counter = WeightMeter::with_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
}

// `execute_dispatch` when the origin is `Signed`, not counting the dispatable's weight.
execute_dispatch_signed {
let mut counter = WeightMeter::max_limit();
let mut counter = WeightMeter::new();
let origin = make_origin::<T>(true);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {
Expand All @@ -215,7 +215,7 @@ benchmarks! {

// `execute_dispatch` when the origin is not `Signed`, not counting the dispatable's weight.
execute_dispatch_unsigned {
let mut counter = WeightMeter::max_limit();
let mut counter = WeightMeter::new();
let origin = make_origin::<T>(false);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Execute the scheduled calls
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
let mut weight_counter = WeightMeter::from_limit(T::MaximumWeight::get());
let mut weight_counter = WeightMeter::with_limit(T::MaximumWeight::get());
Self::service_agendas(&mut weight_counter, now, u32::max_value());
weight_counter.consumed()
}
Expand Down
Loading