15
15
16
16
//! Utilities for providing the static weights for module callbacks
17
17
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 } ;
21
20
use ismp:: {
22
21
messaging:: { Message , TimeoutMessage } ,
23
- router:: { GetResponse , PostRequest , Request , RequestResponse , Response , Timeout } ,
22
+ router:: { GetResponse , PostRequest , RequestResponse , Response , Timeout } ,
24
23
} ;
25
24
26
25
/// Interface for providing the weight information about [`IsmpModule`](ismp::module::IsmpModule)
27
26
/// callbacks
28
27
pub trait IsmpModuleWeight {
29
28
/// Should return the weight used in processing this request
30
- fn on_accept ( & self , request : & PostRequest ) -> Weight ;
29
+ fn on_accept ( request : & PostRequest ) -> Weight ;
31
30
/// Should return the weight used in processing this timeout
32
- fn on_timeout ( & self , request : & Timeout ) -> Weight ;
31
+ fn on_timeout ( request : & Timeout ) -> Weight ;
33
32
/// Should return the weight used in processing this response
34
- fn on_response ( & self , response : & Response ) -> Weight ;
33
+ fn on_response ( response : & Response ) -> Weight ;
35
34
}
36
35
36
+ /// Just by estimation, require benchmark to generate weight for production in runtimes
37
37
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 ) )
40
43
}
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 ) )
43
49
}
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 ) )
59
55
}
60
56
}
61
57
62
58
/// Returns the weight that would be consumed when executing a batch of messages
63
59
pub ( crate ) fn get_weight < T : Config > ( messages : & [ Message ] ) -> Weight {
64
60
messages. into_iter ( ) . fold ( Weight :: zero ( ) , |acc, msg| match msg {
65
61
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) ) ;
74
66
acc + cb_weight
75
67
} ,
76
68
Message :: Response ( msg) => match & msg. datagram {
77
69
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) ) ;
91
73
92
74
acc + cb_weight
93
75
} ,
94
76
RequestResponse :: Request ( requests) => {
95
77
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 {
105
79
get : req. get_request ( ) . expect ( "Infallible" ) ,
106
80
values : Default :: default ( ) ,
107
81
} ) )
@@ -113,42 +87,21 @@ pub(crate) fn get_weight<T: Config>(messages: &[Message]) -> Weight {
113
87
Message :: Timeout ( msg) => match msg {
114
88
TimeoutMessage :: Post { requests, .. } => {
115
89
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 ( ) ) )
125
91
} ) ;
126
92
127
93
acc + cb_weight
128
94
} ,
129
95
TimeoutMessage :: PostResponse { responses, .. } => {
130
96
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 ( ) ) )
137
98
} ) ;
138
99
139
100
acc + cb_weight
140
101
} ,
141
102
TimeoutMessage :: Get { requests } => {
142
103
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 ( ) ) )
152
105
} ) ;
153
106
acc + cb_weight
154
107
} ,
0 commit comments