Skip to content

Commit d7d5192

Browse files
authored
Add weightInfo (#289)
* add weightInfo * fix clippy
1 parent 0ff8829 commit d7d5192

File tree

15 files changed

+169
-24
lines changed

15 files changed

+169
-24
lines changed

authority/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Authority module
2+
3+
### Overview
4+
5+
Authority module to provide features for governance including dispatch method on behalf other accounts and schdule dispatchables.
6+
7+
- `dispatch_as` can dispatch a dispatchable on behalf of other origin.
8+
- `schedule_dispatch` can schdule a dispatchable to be dispatched at later block.
9+
- `fast_track_scheduled_dispatch` can fast track a scheduled dispatchable.
10+
- `delay_scheduled_dispatch` can delay a scheduled dispatchable.
11+
- `cancel_scheduled_dispatch` can cancel a scheduled dispatchable.

authority/src/default_weight.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Weights for the authority Module
2+
3+
use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
4+
5+
impl crate::WeightInfo for () {
6+
fn dispatch_as() -> Weight {
7+
50750000 as Weight
8+
}
9+
fn schedule_dispatch() -> Weight {
10+
(147800000 as Weight)
11+
.saturating_add(DbWeight::get().reads(3 as Weight))
12+
.saturating_add(DbWeight::get().writes(3 as Weight))
13+
}
14+
fn fast_track_scheduled_dispatch() -> Weight {
15+
// TODO
16+
0 as Weight
17+
}
18+
fn delay_scheduled_dispatch() -> Weight {
19+
// TODO
20+
0 as Weight
21+
}
22+
fn cancel_scheduled_dispatch() -> Weight {
23+
(127400000 as Weight)
24+
.saturating_add(DbWeight::get().reads(2 as Weight))
25+
.saturating_add(DbWeight::get().writes(2 as Weight))
26+
}
27+
}

authority/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![allow(clippy::borrowed_box)]
2020

2121
use codec::{Decode, Encode};
22+
use frame_support::weights::Weight;
2223
use frame_support::{
2324
decl_error, decl_event, decl_module, decl_storage,
2425
dispatch::PostDispatchInfo,
@@ -35,9 +36,18 @@ use sp_runtime::{
3536
};
3637
use sp_std::prelude::*;
3738

39+
mod default_weight;
3840
mod mock;
3941
mod tests;
4042

43+
pub trait WeightInfo {
44+
fn dispatch_as() -> Weight;
45+
fn schedule_dispatch() -> Weight;
46+
fn fast_track_scheduled_dispatch() -> Weight;
47+
fn delay_scheduled_dispatch() -> Weight;
48+
fn cancel_scheduled_dispatch() -> Weight;
49+
}
50+
4151
/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay.
4252
#[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode)]
4353
pub struct DelayedOrigin<BlockNumber, PalletsOrigin> {
@@ -143,6 +153,9 @@ pub trait Trait: frame_system::Trait {
143153

144154
/// Additional permission config.
145155
type AuthorityConfig: AuthorityConfig<<Self as frame_system::Trait>::Origin, Self::PalletsOrigin, Self::BlockNumber>;
156+
157+
/// Weight information for extrinsics in this module.
158+
type WeightInfo: WeightInfo;
146159
}
147160

148161
decl_error! {
@@ -188,7 +201,7 @@ decl_module! {
188201
fn deposit_event() = default;
189202

190203
/// Dispatch a dispatchable on behalf of other origin
191-
#[weight = (call.get_dispatch_info().weight + 10_000, call.get_dispatch_info().class)]
204+
#[weight = (T::WeightInfo::dispatch_as().saturating_add(call.get_dispatch_info().weight), call.get_dispatch_info().class)]
192205
pub fn dispatch_as(
193206
origin,
194207
as_origin: T::AsOriginId,
@@ -203,7 +216,7 @@ decl_module! {
203216

204217
/// Schdule a dispatchable to be dispatched at later block.
205218
/// This is the only way to dispatch a call with `DelayedOrigin`.
206-
#[weight = 10_000]
219+
#[weight = T::WeightInfo::schedule_dispatch()]
207220
pub fn schedule_dispatch(
208221
origin,
209222
when: DispatchTime<T::BlockNumber>,
@@ -252,7 +265,7 @@ decl_module! {
252265
}
253266

254267
/// Fast track a scheduled dispatchable.
255-
#[weight = 10_000]
268+
#[weight = T::WeightInfo::fast_track_scheduled_dispatch()]
256269
pub fn fast_track_scheduled_dispatch(
257270
origin,
258271
initial_origin: T::PalletsOrigin,
@@ -281,7 +294,7 @@ decl_module! {
281294
}
282295

283296
/// Delay a scheduled dispatchable.
284-
#[weight = 10_000]
297+
#[weight = T::WeightInfo::delay_scheduled_dispatch()]
285298
pub fn delay_scheduled_dispatch(
286299
origin,
287300
initial_origin: T::PalletsOrigin,
@@ -296,7 +309,7 @@ decl_module! {
296309
}
297310

298311
/// Cancel a scheduled dispatchable.
299-
#[weight = 10_000]
312+
#[weight = T::WeightInfo::cancel_scheduled_dispatch()]
300313
pub fn cancel_scheduled_dispatch(
301314
origin,
302315
initial_origin: T::PalletsOrigin,

authority/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl Trait for Runtime {
151151
type Call = Call;
152152
type AsOriginId = MockAsOriginId;
153153
type AuthorityConfig = AuthorityConfigImpl;
154+
type WeightInfo = ();
154155
}
155156

156157
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Weights for the Gradually-update Module
2+
3+
use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
4+
5+
impl crate::WeightInfo for () {
6+
fn gradually_update() -> Weight {
7+
(82320000 as Weight)
8+
.saturating_add(DbWeight::get().reads(2 as Weight))
9+
.saturating_add(DbWeight::get().writes(1 as Weight))
10+
}
11+
fn cancel_gradually_update() -> Weight {
12+
(72950000 as Weight)
13+
.saturating_add(DbWeight::get().reads(1 as Weight))
14+
.saturating_add(DbWeight::get().writes(1 as Weight))
15+
}
16+
fn on_initialize(need_update: bool, update_len: usize) -> Weight {
17+
if !need_update {
18+
return 0;
19+
}
20+
21+
if update_len == 0 {
22+
(30430000 as Weight)
23+
.saturating_add(DbWeight::get().reads(2 as Weight))
24+
.saturating_add(DbWeight::get().writes(1 as Weight))
25+
} else {
26+
(91390000 + (30000000 * update_len) as Weight)
27+
.saturating_add(DbWeight::get().reads(3 as Weight))
28+
.saturating_add(DbWeight::get().writes(3 as Weight))
29+
}
30+
}
31+
}

gradually-update/src/lib.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ use frame_system::ensure_root;
3131
use sp_runtime::{traits::SaturatedConversion, DispatchResult, RuntimeDebug};
3232
use sp_std::prelude::Vec;
3333

34+
mod default_weight;
3435
mod mock;
3536
mod tests;
3637

38+
pub trait WeightInfo {
39+
fn gradually_update() -> Weight;
40+
fn cancel_gradually_update() -> Weight;
41+
fn on_initialize(need_update: bool, update_len: usize) -> Weight;
42+
}
43+
3744
type StorageKey = Vec<u8>;
3845
type StorageValue = Vec<u8>;
3946

@@ -42,11 +49,11 @@ type StorageValue = Vec<u8>;
4249
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
4350
pub struct GraduallyUpdate {
4451
/// The storage key of the value to update
45-
key: StorageKey,
52+
pub key: StorageKey,
4653
/// The target value
47-
target_value: StorageValue,
54+
pub target_value: StorageValue,
4855
/// The amount of the value to update per one block
49-
per_block: StorageValue,
56+
pub per_block: StorageValue,
5057
}
5158

5259
pub trait Trait: frame_system::Trait {
@@ -55,6 +62,8 @@ pub trait Trait: frame_system::Trait {
5562
type UpdateFrequency: Get<Self::BlockNumber>;
5663
/// The origin that can schedule an update
5764
type DispatchOrigin: EnsureOrigin<Self::Origin>;
65+
/// Weight information for extrinsics in this module.
66+
type WeightInfo: WeightInfo;
5867
}
5968

6069
decl_storage! {
@@ -103,7 +112,7 @@ decl_module! {
103112
const UpdateFrequency: T::BlockNumber = T::UpdateFrequency::get();
104113

105114
/// Add gradually_update to adjust numeric parameter.
106-
#[weight = 10_000]
115+
#[weight = T::WeightInfo::gradually_update()]
107116
pub fn gradually_update(origin, update: GraduallyUpdate) {
108117
T::DispatchOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
109118

@@ -127,7 +136,7 @@ decl_module! {
127136
}
128137

129138
/// Cancel gradually_update to adjust numeric parameter.
130-
#[weight = 10_000]
139+
#[weight = T::WeightInfo::cancel_gradually_update()]
131140
pub fn cancel_gradually_update(origin, key: StorageKey) {
132141
T::DispatchOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
133142

@@ -143,10 +152,10 @@ decl_module! {
143152
Self::deposit_event(RawEvent::GraduallyUpdateCancelled(key));
144153
}
145154

146-
/// dummy `on_initialize` to return the weight used in `on_finalize`.
155+
/// `on_initialize` to return the weight used in `on_finalize`.
147156
fn on_initialize() -> Weight {
148-
// weight of `on_finalize`
149-
0
157+
let now = <frame_system::Module<T>>::block_number();
158+
T::WeightInfo::on_initialize(Self::_need_update(now), GraduallyUpdates::get().len())
150159
}
151160

152161
/// Update gradually_update to adjust numeric parameter.
@@ -157,8 +166,12 @@ decl_module! {
157166
}
158167

159168
impl<T: Trait> Module<T> {
169+
fn _need_update(now: T::BlockNumber) -> bool {
170+
now >= Self::last_updated_at() + T::UpdateFrequency::get()
171+
}
172+
160173
fn _on_finalize(now: T::BlockNumber) {
161-
if now < Self::last_updated_at() + T::UpdateFrequency::get() {
174+
if !Self::_need_update(now) {
162175
return;
163176
}
164177

gradually-update/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl Trait for Runtime {
7474
type Event = TestEvent;
7575
type UpdateFrequency = UpdateFrequency;
7676
type DispatchOrigin = system::EnsureRoot<AccountId>;
77+
type WeightInfo = ();
7778
}
7879
pub type GraduallyUpdateModule = Module<Runtime>;
7980

oracle/rpc/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ where
6262
at: Option<<Block as BlockT>::Hash>,
6363
) -> Result<Option<Value>> {
6464
let api = self.client.runtime_api();
65-
let at = BlockId::hash(at.unwrap_or_else(||
65+
let at = BlockId::hash(at.unwrap_or(
6666
// If the block hash is not supplied assume the best block.
67-
self.client.info().best_hash));
67+
self.client.info().best_hash,
68+
));
6869
api.get_value(&at, provider_id, key).map_err(|e| RpcError {
6970
code: ErrorCode::ServerError(Error::RuntimeError.into()),
7071
message: "Unable to get value.".into(),
@@ -78,9 +79,10 @@ where
7879
at: Option<<Block as BlockT>::Hash>,
7980
) -> Result<Vec<(Key, Option<Value>)>> {
8081
let api = self.client.runtime_api();
81-
let at = BlockId::hash(at.unwrap_or_else(||
82+
let at = BlockId::hash(at.unwrap_or(
8283
// If the block hash is not supplied assume the best block.
83-
self.client.info().best_hash));
84+
self.client.info().best_hash,
85+
));
8486
api.get_all_values(&at, provider_id).map_err(|e| RpcError {
8587
code: ErrorCode::ServerError(Error::RuntimeError.into()),
8688
message: "Unable to get all values.".into(),

oracle/src/default_weight.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Weights for the Auction Module
2+
3+
use frame_support::weights::{constants::RocksDbWeight as DbWeight, Weight};
4+
5+
impl crate::WeightInfo for () {
6+
fn feed_values(values_len: usize) -> Weight {
7+
(101600000 as Weight)
8+
.saturating_add(DbWeight::get().reads(3 as Weight))
9+
.saturating_add(DbWeight::get().writes((1 + values_len * 2) as Weight))
10+
}
11+
12+
fn on_initialize() -> Weight {
13+
(18400000 as Weight).saturating_add(DbWeight::get().writes(1) as Weight)
14+
}
15+
}

oracle/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
#![allow(clippy::string_lit_as_bytes)]
2121

2222
mod default_combine_data;
23+
mod default_weight;
2324
mod mock;
2425
mod tests;
2526

27+
pub trait WeightInfo {
28+
fn feed_values(n: usize) -> Weight;
29+
fn on_initialize() -> Weight;
30+
}
31+
2632
use codec::{Decode, Encode};
2733
pub use default_combine_data::DefaultCombineData;
2834
use frame_support::{
@@ -72,6 +78,9 @@ pub trait Trait<I: Instance = DefaultInstance>: frame_system::Trait {
7278

7379
/// The root operator account id, recorad all sudo feeds on this account.
7480
type RootOperatorAccountId: Get<Self::AccountId>;
81+
82+
/// Weight information for extrinsics in this module.
83+
type WeightInfo: WeightInfo;
7584
}
7685

7786
decl_error! {
@@ -130,7 +139,7 @@ decl_module! {
130139
/// Feed the external value.
131140
///
132141
/// Require unsigned. However a valid signature signed by session key is required along with payload.
133-
#[weight = (10_000, DispatchClass::Operational)]
142+
#[weight = (T::WeightInfo::feed_values(values.len()), DispatchClass::Operational)]
134143
pub fn feed_values(
135144
origin,
136145
values: Vec<(T::OracleKey, T::OracleValue)>,
@@ -140,10 +149,9 @@ decl_module! {
140149
Ok(Pays::No.into())
141150
}
142151

143-
/// dummy `on_initialize` to return the weight used in `on_finalize`.
152+
/// `on_initialize` to return the weight used in `on_finalize`.
144153
fn on_initialize() -> Weight {
145-
// weight of `on_finalize`
146-
0
154+
T::WeightInfo::on_initialize()
147155
}
148156

149157
fn on_finalize(_n: T::BlockNumber) {

0 commit comments

Comments
 (0)