Skip to content

Commit d03c72b

Browse files
author
kku9706
committed
Remove WeightRouter
1 parent de74015 commit d03c72b

File tree

2 files changed

+38
-85
lines changed

2 files changed

+38
-85
lines changed

modules/ismp/pallets/pallet/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub mod pallet {
199199
child_trie::{RequestCommitments, ResponseCommitments, CHILD_TRIE_PREFIX},
200200
errors::HandlingError,
201201
fee::HandleFee,
202-
weights::{get_weight, WeightProvider},
202+
weights::{get_weight, IsmpModuleWeight},
203203
};
204204
use codec::{Codec, Encode};
205205
use core::fmt::Debug;
@@ -295,10 +295,6 @@ pub mod pallet {
295295
/// subsystems.
296296
type ConsensusClients: ConsensusClientProvider;
297297

298-
/// This implementation should provide the weight consumed by `IsmpModule` callbacks from
299-
/// their benchmarks.
300-
type WeightProvider: WeightProvider;
301-
302298
/// Offchain database implementation. Outgoing requests and responses are
303299
/// inserted in this database, while their commitments are stored onchain.
304300
///
@@ -308,6 +304,10 @@ pub mod pallet {
308304

309305
/// FeeHandler with custom fee logic
310306
type FeeHandler: HandleFee<Self>;
307+
308+
/// This implementation should provide the weight consumed by `IsmpModuleWeight` from
309+
/// their benchmarks.
310+
type WeightProvider: IsmpModuleWeight;
311311
}
312312

313313
// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and

modules/ismp/pallets/pallet/src/weights.rs

+33-80
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,67 @@
1515

1616
//! Utilities for providing the static weights for module callbacks
1717
18-
use crate::{utils::ModuleId, Config};
19-
use alloc::boxed::Box;
20-
use frame_support::weights::Weight;
18+
use crate::Config;
19+
use frame_support::weights::{constants::RocksDbWeight, Weight};
2120
use ismp::{
2221
messaging::{Message, TimeoutMessage},
23-
router::{GetResponse, PostRequest, Request, RequestResponse, Response, Timeout},
22+
router::{GetResponse, PostRequest, RequestResponse, Response, Timeout},
2423
};
2524

2625
/// Interface for providing the weight information about [`IsmpModule`](ismp::module::IsmpModule)
2726
/// callbacks
2827
pub trait IsmpModuleWeight {
2928
/// Should return the weight used in processing this request
30-
fn on_accept(&self, request: &PostRequest) -> Weight;
29+
fn on_accept(request: &PostRequest) -> Weight;
3130
/// Should return the weight used in processing this timeout
32-
fn on_timeout(&self, request: &Timeout) -> Weight;
31+
fn on_timeout(request: &Timeout) -> Weight;
3332
/// Should return the weight used in processing this response
34-
fn on_response(&self, response: &Response) -> Weight;
33+
fn on_response(response: &Response) -> Weight;
3534
}
3635

36+
/// Just by estimation, require benchmark to generate weight for production in runtimes
3737
impl IsmpModuleWeight for () {
38-
fn on_accept(&self, _request: &PostRequest) -> Weight {
39-
Weight::zero()
38+
fn on_accept(_request: &PostRequest) -> Weight {
39+
Weight::from_parts(63_891_000, 0)
40+
.saturating_add(Weight::from_parts(0, 52674))
41+
.saturating_add(RocksDbWeight::get().reads(6))
42+
.saturating_add(RocksDbWeight::get().writes(1))
4043
}
41-
fn on_timeout(&self, _request: &Timeout) -> Weight {
42-
Weight::zero()
44+
fn on_timeout(_request: &Timeout) -> Weight {
45+
Weight::from_parts(63_891_000, 0)
46+
.saturating_add(Weight::from_parts(0, 52674))
47+
.saturating_add(RocksDbWeight::get().reads(6))
48+
.saturating_add(RocksDbWeight::get().writes(1))
4349
}
44-
fn on_response(&self, _response: &Response) -> Weight {
45-
Weight::zero()
46-
}
47-
}
48-
49-
/// An interface for querying the [`IsmpModuleWeight`] for a given
50-
/// [`IsmpModule`](ismp::module::IsmpModule)
51-
pub trait WeightProvider {
52-
/// Returns a reference to the weight provider for a module
53-
fn module_callback(dest_module: ModuleId) -> Option<Box<dyn IsmpModuleWeight>>;
54-
}
55-
56-
impl WeightProvider for () {
57-
fn module_callback(_dest_module: ModuleId) -> Option<Box<dyn IsmpModuleWeight>> {
58-
None
50+
fn on_response(_response: &Response) -> Weight {
51+
Weight::from_parts(63_891_000, 0)
52+
.saturating_add(Weight::from_parts(0, 52674))
53+
.saturating_add(RocksDbWeight::get().reads(6))
54+
.saturating_add(RocksDbWeight::get().writes(1))
5955
}
6056
}
6157

6258
/// Returns the weight that would be consumed when executing a batch of messages
6359
pub(crate) fn get_weight<T: Config>(messages: &[Message]) -> Weight {
6460
messages.into_iter().fold(Weight::zero(), |acc, msg| match msg {
6561
Message::Request(msg) => {
66-
let cb_weight = msg.requests.iter().fold(Weight::zero(), |acc, req| {
67-
let dest_module = ModuleId::from_bytes(req.to.as_slice()).ok();
68-
let handle = dest_module
69-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
70-
.flatten()
71-
.unwrap_or(Box::new(()));
72-
acc + handle.on_accept(&req)
73-
});
62+
let cb_weight = msg
63+
.requests
64+
.iter()
65+
.fold(Weight::zero(), |acc, req| acc + T::WeightProvider::on_accept(&req));
7466
acc + cb_weight
7567
},
7668
Message::Response(msg) => match &msg.datagram {
7769
RequestResponse::Response(responses) => {
78-
let cb_weight = responses.iter().fold(Weight::zero(), |acc, res| {
79-
let dest_module = match res {
80-
Response::Post(ref post) =>
81-
ModuleId::from_bytes(post.post.from.as_slice()).ok(),
82-
_ => return acc,
83-
};
84-
85-
let handle = dest_module
86-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
87-
.flatten()
88-
.unwrap_or(Box::new(()));
89-
acc + handle.on_response(&res)
90-
});
70+
let cb_weight = responses
71+
.iter()
72+
.fold(Weight::zero(), |acc, res| acc + T::WeightProvider::on_response(&res));
9173

9274
acc + cb_weight
9375
},
9476
RequestResponse::Request(requests) => {
9577
let cb_weight = requests.iter().fold(Weight::zero(), |acc, req| {
96-
let dest_module = match req {
97-
Request::Get(ref get) => ModuleId::from_bytes(get.from.as_slice()).ok(),
98-
_ => return acc,
99-
};
100-
let handle = dest_module
101-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
102-
.flatten()
103-
.unwrap_or(Box::new(()));
104-
acc + handle.on_response(&Response::Get(GetResponse {
78+
acc + T::WeightProvider::on_response(&Response::Get(GetResponse {
10579
get: req.get_request().expect("Infallible"),
10680
values: Default::default(),
10781
}))
@@ -113,42 +87,21 @@ pub(crate) fn get_weight<T: Config>(messages: &[Message]) -> Weight {
11387
Message::Timeout(msg) => match msg {
11488
TimeoutMessage::Post { requests, .. } => {
11589
let cb_weight = requests.iter().fold(Weight::zero(), |acc, req| {
116-
let dest_module = match req {
117-
Request::Post(ref post) => ModuleId::from_bytes(post.from.as_slice()).ok(),
118-
_ => return acc,
119-
};
120-
let handle = dest_module
121-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
122-
.flatten()
123-
.unwrap_or(Box::new(()));
124-
acc + handle.on_timeout(&Timeout::Request(req.clone()))
90+
acc + T::WeightProvider::on_timeout(&Timeout::Request(req.clone()))
12591
});
12692

12793
acc + cb_weight
12894
},
12995
TimeoutMessage::PostResponse { responses, .. } => {
13096
let cb_weight = responses.iter().fold(Weight::zero(), |acc, res| {
131-
let dest_module = ModuleId::from_bytes(&res.post.to).ok();
132-
let handle = dest_module
133-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
134-
.flatten()
135-
.unwrap_or(Box::new(()));
136-
acc + handle.on_timeout(&Timeout::Response(res.clone()))
97+
acc + T::WeightProvider::on_timeout(&Timeout::Response(res.clone()))
13798
});
13899

139100
acc + cb_weight
140101
},
141102
TimeoutMessage::Get { requests } => {
142103
let cb_weight = requests.iter().fold(Weight::zero(), |acc, req| {
143-
let dest_module = match req {
144-
Request::Get(ref get) => ModuleId::from_bytes(get.from.as_slice()).ok(),
145-
_ => return acc,
146-
};
147-
let handle = dest_module
148-
.map(|id| <T as Config>::WeightProvider::module_callback(id))
149-
.flatten()
150-
.unwrap_or(Box::new(()));
151-
acc + handle.on_timeout(&Timeout::Request(req.clone()))
104+
acc + T::WeightProvider::on_timeout(&Timeout::Request(req.clone()))
152105
});
153106
acc + cb_weight
154107
},

0 commit comments

Comments
 (0)