Newer
Older
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#[frame_support::pallet]
pub mod test_module {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*, weights::Weight};
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
#[pallet::storage]
#[pallet::getter(fn something)]
pub type Something<T> = StorageValue<_, u32>;
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn expect_100(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
Self::put_100();
Ok(Some(orml_weight_meter::used_weight()).into())
}
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn expect_500(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
Self::put_100();
Self::put_100();
Self::put_100();
Self::put_100();
Self::put_100();
Ok(Some(orml_weight_meter::used_weight()).into())
}
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn expect_max_weight(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
Self::max_weight();
Self::put_100();
Ok(Some(orml_weight_meter::used_weight()).into())
}
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn expect_100_or_200(origin: OriginFor<T>, branch: bool) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
if branch {
Self::put_200();
} else {
Self::put_100();
}
Ok(Some(orml_weight_meter::used_weight()).into())
}
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn nested_inner_methods(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
Self::put_300_nested();
Ok(Some(orml_weight_meter::used_weight()).into())
}
#[pallet::weight(50_000)]
#[orml_weight_meter::start]
pub fn nested_extrinsic(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
ensure_signed(origin.clone())?;
// some module call
Self::put_300_nested();
// call extrinsic method
Self::expect_100(origin)?;
// some module call
Self::put_300_nested();
Ok(Some(orml_weight_meter::used_weight()).into())
}
}
impl<T: Config> Pallet<T> {
#[orml_weight_meter::weight(100)]
fn put_100() {
let something = Self::something();
if let Some(v) = something {
Something::<T>::put(v.checked_add(100).unwrap());
} else {
Something::<T>::put(100);
}
}
#[orml_weight_meter::weight(200)]
fn put_200() {
let something = Self::something();
if let Some(v) = something {
Something::<T>::put(v.checked_add(200).unwrap());
} else {
Something::<T>::put(100);
}
}
#[orml_weight_meter::weight(200)]
fn put_300_nested() {
Self::put_100();
}
#[orml_weight_meter::weight(Weight::MAX)]
fn max_weight() {
return;
}
}
}
use frame_support::sp_runtime::traits::IdentityLookup;
use sp_runtime::testing::{Header, H256};
pub type BlockNumber = u64;
frame_support::parameter_types! {
pub const BlockHashCount: u64 = 250;
}
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
type Block = frame_system::mocking::MockBlock<Runtime>;
type Balance = u128;
impl frame_system::Config for Runtime {
type Origin = Origin;
type Index = u64;
type BlockNumber = BlockNumber;
type Call = Call;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
frame_support::parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Config for Runtime {
type Balance = Balance;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Pallet<Runtime>;
type MaxLocks = ();
type WeightInfo = ();
}
impl test_module::Config for Runtime {}
frame_support::construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Storage, Config, Event<T>},
TestModule: test_module::{Pallet, Call, Storage},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
}
);
pub struct ExtBuilder();
impl Default for ExtBuilder {
fn default() -> Self {
Self()
}
}
impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(100, 100_000)],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}
pub fn new_test_ext() -> sp_io::TestExternalities {
ExtBuilder::default().build()
}