1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_support::{storage, traits::Get, weights::DispatchClass};
use frame_system::{Call, Pallet as System, RawOrigin};
use sp_core::storage::well_known_keys;
use sp_runtime::traits::Hash;
use sp_std::{prelude::*, vec};
mod mock;
pub struct Pallet<T: Config>(System<T>);
pub trait Config: frame_system::Config {}
benchmarks! {
remark {
let b in 0 .. *T::BlockLength::get().max.get(DispatchClass::Normal) as u32;
let remark_message = vec![1; b as usize];
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), remark_message)
remark_with_event {
let b in 0 .. *T::BlockLength::get().max.get(DispatchClass::Normal) as u32;
let remark_message = vec![1; b as usize];
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), remark_message)
set_heap_pages {
}: _(RawOrigin::Root, Default::default())
#[extra]
set_code_without_checks {
let code = vec![1; 4_000_000 as usize];
}: _(RawOrigin::Root, code)
verify {
let current_code = storage::unhashed::get_raw(well_known_keys::CODE).ok_or("Code not stored.")?;
assert_eq!(current_code.len(), 4_000_000 as usize);
}
#[skip_meta]
set_storage {
let i in 1 .. 1000;
let mut items = Vec::new();
for j in 0 .. i {
let hash = (i, j).using_encoded(T::Hashing::hash).as_ref().to_vec();
items.push((hash.clone(), hash.clone()));
}
}: _(RawOrigin::Root, items)
verify {
let last_hash = (i, i - 1).using_encoded(T::Hashing::hash);
let value = storage::unhashed::get_raw(last_hash.as_ref()).ok_or("No value stored")?;
assert_eq!(value, last_hash.as_ref().to_vec());
}
#[skip_meta]
kill_storage {
let i in 1 .. 1000;
let mut items = Vec::new();
for j in 0 .. i {
let hash = (i, j).using_encoded(T::Hashing::hash).as_ref().to_vec();
storage::unhashed::put_raw(&hash, &hash);
items.push(hash);
}
let last_hash = (i, i - 1).using_encoded(T::Hashing::hash);
let value = storage::unhashed::get_raw(last_hash.as_ref()).ok_or("No value stored")?;
assert_eq!(value, last_hash.as_ref().to_vec());
}: _(RawOrigin::Root, items)
verify {
assert_eq!(storage::unhashed::get_raw(last_hash.as_ref()), None);
}
#[skip_meta]
kill_prefix {
let p in 1 .. 1000;
let prefix = p.using_encoded(T::Hashing::hash).as_ref().to_vec();
for i in 0 .. p {
let hash = (p, i).using_encoded(T::Hashing::hash).as_ref().to_vec();
let key = [&prefix[..], &hash[..]].concat();
storage::unhashed::put_raw(&key, &key);
}
let last_hash = (p, p - 1).using_encoded(T::Hashing::hash).as_ref().to_vec();
let last_key = [&prefix[..], &last_hash[..]].concat();
let value = storage::unhashed::get_raw(&last_key).ok_or("No value stored")?;
assert_eq!(value, last_key);
}: _(RawOrigin::Root, prefix, p)
verify {
assert_eq!(storage::unhashed::get_raw(&last_key), None);
}
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}