1use crate::{weights::WeightInfo, Config};
22
23use codec::{Decode, Encode};
24use frame_support::{weights::Weight, DefaultNoBound};
25use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug};
26use scale_info::TypeInfo;
27#[cfg(feature = "std")]
28use serde::{Deserialize, Serialize};
29use sp_runtime::RuntimeDebug;
30use sp_std::marker::PhantomData;
31
32#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
59#[cfg_attr(feature = "std", serde(bound(serialize = "", deserialize = "")))]
60#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, DefaultNoBound, TypeInfo)]
61#[scale_info(skip_type_params(T))]
62pub struct Schedule<T: Config> {
63 pub limits: Limits,
65
66 pub instruction_weights: InstructionWeights<T>,
68
69 pub host_fn_weights: HostFnWeights<T>,
71}
72
73#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
75#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo)]
76pub struct Limits {
77 pub event_topics: u32,
79
80 pub globals: u32,
84
85 pub locals: u32,
90
91 pub parameters: u32,
100
101 pub memory_pages: u32,
103
104 pub table_size: u32,
108
109 pub br_table_size: u32,
111
112 pub subject_len: u32,
114
115 pub payload_len: u32,
117
118 pub runtime_memory: u32,
121}
122
123impl Limits {
124 pub fn max_memory_size(&self) -> u32 {
126 self.memory_pages * 64 * 1024
127 }
128}
129
130#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
133#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, TypeInfo)]
134#[scale_info(skip_type_params(T))]
135pub struct InstructionWeights<T: Config> {
136 pub base: u32,
139 #[codec(skip)]
141 pub _phantom: PhantomData<T>,
142}
143
144#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
146#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
147#[scale_info(skip_type_params(T))]
148pub struct HostFnWeights<T: Config> {
149 pub caller: Weight,
151
152 pub is_contract: Weight,
154
155 pub code_hash: Weight,
157
158 pub own_code_hash: Weight,
160
161 pub caller_is_origin: Weight,
163
164 pub caller_is_root: Weight,
166
167 pub address: Weight,
169
170 pub gas_left: Weight,
172
173 pub balance: Weight,
175
176 pub value_transferred: Weight,
178
179 pub minimum_balance: Weight,
181
182 pub block_number: Weight,
184
185 pub now: Weight,
187
188 pub weight_to_fee: Weight,
190
191 pub input: Weight,
193
194 pub input_per_byte: Weight,
196
197 pub r#return: Weight,
199
200 pub return_per_byte: Weight,
202
203 pub terminate: Weight,
205
206 pub random: Weight,
208
209 pub deposit_event: Weight,
211
212 pub deposit_event_per_topic: Weight,
214
215 pub deposit_event_per_byte: Weight,
217
218 pub debug_message: Weight,
220
221 pub debug_message_per_byte: Weight,
223
224 pub set_storage: Weight,
226
227 pub set_storage_per_new_byte: Weight,
229
230 pub set_storage_per_old_byte: Weight,
232
233 pub set_code_hash: Weight,
235
236 pub clear_storage: Weight,
238
239 pub clear_storage_per_byte: Weight,
241
242 pub contains_storage: Weight,
244
245 pub contains_storage_per_byte: Weight,
247
248 pub get_storage: Weight,
250
251 pub get_storage_per_byte: Weight,
253
254 pub take_storage: Weight,
256
257 pub take_storage_per_byte: Weight,
259
260 pub transfer: Weight,
262
263 pub call: Weight,
265
266 pub delegate_call: Weight,
268
269 pub call_transfer_surcharge: Weight,
271
272 pub call_per_cloned_byte: Weight,
274
275 pub instantiate: Weight,
277
278 pub instantiate_transfer_surcharge: Weight,
280
281 pub instantiate_per_input_byte: Weight,
283
284 pub instantiate_per_salt_byte: Weight,
286
287 pub hash_sha2_256: Weight,
289
290 pub hash_sha2_256_per_byte: Weight,
292
293 pub hash_keccak_256: Weight,
295
296 pub hash_keccak_256_per_byte: Weight,
298
299 pub hash_blake2_256: Weight,
301
302 pub hash_blake2_256_per_byte: Weight,
304
305 pub hash_blake2_128: Weight,
307
308 pub hash_blake2_128_per_byte: Weight,
310
311 pub ecdsa_recover: Weight,
313
314 pub ecdsa_to_eth_address: Weight,
316
317 pub sr25519_verify: Weight,
319
320 pub sr25519_verify_per_byte: Weight,
322
323 pub reentrance_count: Weight,
325
326 pub account_reentrance_count: Weight,
328
329 pub instantiation_nonce: Weight,
331
332 pub add_delegate_dependency: Weight,
334
335 pub remove_delegate_dependency: Weight,
337
338 #[codec(skip)]
340 pub _phantom: PhantomData<T>,
341}
342
343macro_rules! replace_token {
344 ($_in:tt $replacement:tt) => {
345 $replacement
346 };
347}
348
349macro_rules! call_zero {
350 ($name:ident, $( $arg:expr ),*) => {
351 T::WeightInfo::$name($( replace_token!($arg 0) ),*)
352 };
353}
354
355macro_rules! cost_args {
356 ($name:ident, $( $arg: expr ),+) => {
357 (T::WeightInfo::$name($( $arg ),+).saturating_sub(call_zero!($name, $( $arg ),+)))
358 }
359}
360
361macro_rules! cost_instr_no_params {
362 ($name:ident) => {
363 cost_args!($name, 1).ref_time() as u32
364 };
365}
366
367macro_rules! cost {
368 ($name:ident) => {
369 cost_args!($name, 1)
370 };
371}
372
373macro_rules! cost_instr {
374 ($name:ident, $num_params:expr) => {
375 cost_instr_no_params!($name)
376 .saturating_sub((cost_instr_no_params!(instr_i64const) / 2).saturating_mul($num_params))
377 };
378}
379
380impl Default for Limits {
381 fn default() -> Self {
382 Self {
383 event_topics: 4,
384 globals: 256,
385 locals: 1024,
386 parameters: 128,
387 memory_pages: 16,
388 table_size: 4096,
390 br_table_size: 256,
391 subject_len: 32,
392 payload_len: 16 * 1024,
393 runtime_memory: 1024 * 1024 * 128,
394 }
395 }
396}
397
398impl<T: Config> Default for InstructionWeights<T> {
399 fn default() -> Self {
402 Self { base: cost_instr!(instr_i64const, 1), _phantom: PhantomData }
403 }
404}
405
406impl<T: Config> Default for HostFnWeights<T> {
407 fn default() -> Self {
408 Self {
409 caller: cost!(seal_caller),
410 is_contract: cost!(seal_is_contract),
411 code_hash: cost!(seal_code_hash),
412 own_code_hash: cost!(seal_own_code_hash),
413 caller_is_origin: cost!(seal_caller_is_origin),
414 caller_is_root: cost!(seal_caller_is_root),
415 address: cost!(seal_address),
416 gas_left: cost!(seal_gas_left),
417 balance: cost!(seal_balance),
418 value_transferred: cost!(seal_value_transferred),
419 minimum_balance: cost!(seal_minimum_balance),
420 block_number: cost!(seal_block_number),
421 now: cost!(seal_now),
422 weight_to_fee: cost!(seal_weight_to_fee),
423 input: cost!(seal_input),
424 input_per_byte: cost!(seal_input_per_byte),
425 r#return: cost!(seal_return),
426 return_per_byte: cost!(seal_return_per_byte),
427 terminate: cost!(seal_terminate),
428 random: cost!(seal_random),
429 deposit_event: cost!(seal_deposit_event),
430 deposit_event_per_topic: cost_args!(seal_deposit_event_per_topic_and_byte, 1, 0),
431 deposit_event_per_byte: cost_args!(seal_deposit_event_per_topic_and_byte, 0, 1),
432 debug_message: cost!(seal_debug_message),
433 debug_message_per_byte: cost!(seal_debug_message_per_byte),
434 set_storage: cost!(seal_set_storage),
435 set_code_hash: cost!(seal_set_code_hash),
436 set_storage_per_new_byte: cost!(seal_set_storage_per_new_byte),
437 set_storage_per_old_byte: cost!(seal_set_storage_per_old_byte),
438 clear_storage: cost!(seal_clear_storage),
439 clear_storage_per_byte: cost!(seal_clear_storage_per_byte),
440 contains_storage: cost!(seal_contains_storage),
441 contains_storage_per_byte: cost!(seal_contains_storage_per_byte),
442 get_storage: cost!(seal_get_storage),
443 get_storage_per_byte: cost!(seal_get_storage_per_byte),
444 take_storage: cost!(seal_take_storage),
445 take_storage_per_byte: cost!(seal_take_storage_per_byte),
446 transfer: cost!(seal_transfer),
447 call: cost!(seal_call),
448 delegate_call: cost!(seal_delegate_call),
449 call_transfer_surcharge: cost_args!(seal_call_per_transfer_clone_byte, 1, 0),
450 call_per_cloned_byte: cost_args!(seal_call_per_transfer_clone_byte, 0, 1),
451 instantiate: cost!(seal_instantiate),
452 instantiate_transfer_surcharge: cost_args!(
453 seal_instantiate_per_transfer_input_salt_byte,
454 1,
455 0,
456 0
457 ),
458 instantiate_per_input_byte: cost_args!(
459 seal_instantiate_per_transfer_input_salt_byte,
460 0,
461 1,
462 0
463 ),
464 instantiate_per_salt_byte: cost_args!(
465 seal_instantiate_per_transfer_input_salt_byte,
466 0,
467 0,
468 1
469 ),
470 hash_sha2_256: cost!(seal_hash_sha2_256),
471 hash_sha2_256_per_byte: cost!(seal_hash_sha2_256_per_byte),
472 hash_keccak_256: cost!(seal_hash_keccak_256),
473 hash_keccak_256_per_byte: cost!(seal_hash_keccak_256_per_byte),
474 hash_blake2_256: cost!(seal_hash_blake2_256),
475 hash_blake2_256_per_byte: cost!(seal_hash_blake2_256_per_byte),
476 hash_blake2_128: cost!(seal_hash_blake2_128),
477 hash_blake2_128_per_byte: cost!(seal_hash_blake2_128_per_byte),
478 ecdsa_recover: cost!(seal_ecdsa_recover),
479 sr25519_verify: cost!(seal_sr25519_verify),
480 sr25519_verify_per_byte: cost!(seal_sr25519_verify_per_byte),
481 ecdsa_to_eth_address: cost!(seal_ecdsa_to_eth_address),
482 reentrance_count: cost!(seal_reentrance_count),
483 account_reentrance_count: cost!(seal_account_reentrance_count),
484 instantiation_nonce: cost!(seal_instantiation_nonce),
485 add_delegate_dependency: cost!(add_delegate_dependency),
486 remove_delegate_dependency: cost!(remove_delegate_dependency),
487 _phantom: PhantomData,
488 }
489 }
490}
491
492#[cfg(test)]
493mod test {
494 use super::*;
495 use crate::tests::Test;
496
497 #[test]
498 fn print_test_schedule() {
499 let schedule = Schedule::<Test>::default();
500 println!("{:#?}", schedule);
501 }
502}