mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 08:27:55 +00:00
Contracts: Refactor API to use WeightMeter (#2943)
Update the Contracts API to use `WeightMeter`, as it simplifies the code and makes it easier to reason about, rather than taking a mutable weight or returning a tuple with the weight consumed --------- Co-authored-by: Alexander Theißen <alex.theissen@me.com>
This commit is contained in:
@@ -54,7 +54,7 @@ use frame_support::{
|
||||
tokens::Preservation,
|
||||
ConstU32, ConstU64, Contains, OnIdle, OnInitialize, StorageVersion,
|
||||
},
|
||||
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
|
||||
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight, WeightMeter},
|
||||
};
|
||||
use frame_system::{EventRecord, Phase};
|
||||
use pallet_contracts_fixtures::compile_module;
|
||||
@@ -1732,8 +1732,8 @@ fn lazy_removal_partial_remove_works() {
|
||||
|
||||
// We create a contract with some extra keys above the weight limit
|
||||
let extra_keys = 7u32;
|
||||
let weight_limit = Weight::from_parts(5_000_000_000, 0);
|
||||
let (_, max_keys) = ContractInfo::<Test>::deletion_budget(weight_limit);
|
||||
let mut meter = WeightMeter::with_limit(Weight::from_parts(5_000_000_000, 100 * 1024));
|
||||
let (weight_per_key, max_keys) = ContractInfo::<Test>::deletion_budget(&meter);
|
||||
let vals: Vec<_> = (0..max_keys + extra_keys)
|
||||
.map(|i| (blake2_256(&i.encode()), (i as u32), (i as u32).encode()))
|
||||
.collect();
|
||||
@@ -1778,10 +1778,10 @@ fn lazy_removal_partial_remove_works() {
|
||||
|
||||
ext.execute_with(|| {
|
||||
// Run the lazy removal
|
||||
let weight_used = ContractInfo::<Test>::process_deletion_queue_batch(weight_limit);
|
||||
ContractInfo::<Test>::process_deletion_queue_batch(&mut meter);
|
||||
|
||||
// Weight should be exhausted because we could not even delete all keys
|
||||
assert_eq!(weight_used, weight_limit);
|
||||
assert!(!meter.can_consume(weight_per_key));
|
||||
|
||||
let mut num_deleted = 0u32;
|
||||
let mut num_remaining = 0u32;
|
||||
@@ -1855,7 +1855,7 @@ fn lazy_removal_does_no_run_on_low_remaining_weight() {
|
||||
fn lazy_removal_does_not_use_all_weight() {
|
||||
let (code, _hash) = compile_module::<Test>("self_destruct").unwrap();
|
||||
|
||||
let weight_limit = Weight::from_parts(5_000_000_000, 100 * 1024);
|
||||
let mut meter = WeightMeter::with_limit(Weight::from_parts(5_000_000_000, 100 * 1024));
|
||||
let mut ext = ExtBuilder::default().existential_deposit(50).build();
|
||||
|
||||
let (trie, vals, weight_per_key) = ext.execute_with(|| {
|
||||
@@ -1867,7 +1867,8 @@ fn lazy_removal_does_not_use_all_weight() {
|
||||
.build_and_unwrap_account_id();
|
||||
|
||||
let info = get_contract(&addr);
|
||||
let (weight_per_key, max_keys) = ContractInfo::<Test>::deletion_budget(weight_limit);
|
||||
let (weight_per_key, max_keys) = ContractInfo::<Test>::deletion_budget(&meter);
|
||||
assert!(max_keys > 0);
|
||||
|
||||
// We create a contract with one less storage item than we can remove within the limit
|
||||
let vals: Vec<_> = (0..max_keys - 1)
|
||||
@@ -1902,10 +1903,10 @@ fn lazy_removal_does_not_use_all_weight() {
|
||||
|
||||
ext.execute_with(|| {
|
||||
// Run the lazy removal
|
||||
let weight_used = ContractInfo::<Test>::process_deletion_queue_batch(weight_limit);
|
||||
|
||||
// We have one less key in our trie than our weight limit suffices for
|
||||
assert_eq!(weight_used, weight_limit - weight_per_key);
|
||||
ContractInfo::<Test>::process_deletion_queue_batch(&mut meter);
|
||||
let base_weight =
|
||||
<<Test as Config>::WeightInfo as WeightInfo>::on_process_deletion_queue_batch();
|
||||
assert_eq!(meter.consumed(), weight_per_key.mul(vals.len() as _) + base_weight);
|
||||
|
||||
// All the keys are removed
|
||||
for val in vals {
|
||||
|
||||
Reference in New Issue
Block a user