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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
// This file is part of Gear.

// Copyright (C) 2022-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! This module contains the cost schedule and supporting code that constructs a
//! sane default schedule from a `WeightInfo` implementation.

#![allow(unused_parens)]

use crate::{weights::WeightInfo, Config, CostsPerBlockOf, DbWeightOf};
use common::scheduler::SchedulingCostsPerBlock;
use core_processor::configs::{ExtCosts, ProcessCosts, RentCosts};
use frame_support::{traits::Get, weights::Weight};
use gear_core::{
    code::MAX_WASM_PAGES_AMOUNT,
    costs::SyscallCosts,
    message,
    pages::{GearPage, WasmPage},
};
use gear_lazy_pages_common::LazyPagesCosts;
use gear_wasm_instrument::{
    gas_metering::{MemoryGrowCost, Rules},
    parity_wasm::elements::{Instruction, Module, SignExtInstruction, Type},
};
use pallet_gear_proc_macro::{ScheduleDebug, WeightDebug};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{
    codec::{Decode, Encode},
    RuntimeDebug,
};
use sp_std::{marker::PhantomData, vec::Vec};

/// How many API calls are executed in a single batch. The reason for increasing the amount
/// of API calls in batches (per benchmark component increase) is so that the linear regression
/// has an easier time determining the contribution of that component.
pub const API_BENCHMARK_BATCH_SIZE: u32 = 80;

/// How many instructions are executed in a single batch. The reasoning is the same
/// as for `API_BENCHMARK_BATCH_SIZE`.
pub const INSTR_BENCHMARK_BATCH_SIZE: u32 = 500;

/// Constant for `stack_height` is calculated via `calc-stack-height` utility to be small enough
/// to avoid stack overflow in wasmer and wasmi executors.
/// To avoid potential stack overflow problems we have a panic in sandbox in case,
/// execution is ended with stack overflow error. So, process queue execution will be
/// stopped and we will be able to investigate the problem and decrease this constant if needed.
#[cfg(not(feature = "fuzz"))]
pub const STACK_HEIGHT_LIMIT: u32 = 36_743;

/// For the fuzzer, we take the maximum possible stack limit calculated by the `calc-stack-height`
/// utility, which would be suitable for Linux machines. This has a positive effect on code coverage.
#[cfg(feature = "fuzz")]
pub const FUZZER_STACK_HEIGHT_LIMIT: u32 = 65_000;

/// Definition of the cost schedule and other parameterization for the wasm vm.
///
/// Its [`Default`] implementation is the designated way to initialize this type. It uses
/// the benchmarked information supplied by [`Config::WeightInfo`]. All of its fields are
/// public and can therefore be modified. For example in order to change some of the limits
/// and set a custom instruction weight version the following code could be used:
/// ```rust
/// use pallet_gear::{Schedule, Limits, InstructionWeights, Config};
///
/// fn create_schedule<T: Config>() -> Schedule<T> {
///     Schedule {
///         limits: Limits {
///                 globals: 3,
///                 locals: 3,
///                 parameters: 3,
///                 memory_pages: 16,
///                 table_size: 3,
///                 br_table_size: 3,
///                 .. Default::default()
///             },
///         instruction_weights: InstructionWeights {
///                 version: 5,
///             .. Default::default()
///         },
///             .. Default::default()
///     }
/// }
/// ```
///
/// # Note
///
/// Please make sure to bump the [`InstructionWeights::version`] whenever substantial
/// changes are made to its values.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(bound(serialize = "", deserialize = "")))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct Schedule<T: Config> {
    /// Describes the upper limits on various metrics.
    pub limits: Limits,

    /// The weights for individual wasm instructions.
    pub instruction_weights: InstructionWeights<T>,

    /// The weights for each imported function a program is allowed to call.
    pub syscall_weights: SyscallWeights<T>,

    /// The weights for memory interaction.
    pub memory_weights: MemoryWeights<T>,

    /// WASM module instantiation per byte cost.
    pub module_instantiation_per_byte: Weight,

    /// Single db write per byte cost.
    pub db_write_per_byte: Weight,

    /// Single db read per byte cost.
    pub db_read_per_byte: Weight,

    /// WASM code instrumentation base cost.
    pub code_instrumentation_cost: Weight,

    /// WASM code instrumentation per-byte cost.
    pub code_instrumentation_byte_cost: Weight,
}

/// Describes the upper limits on various metrics.
///
/// # Note
///
/// The values in this struct should never be decreased. The reason is that decreasing those
/// values will break existing programs which are above the new limits when a
/// re-instrumentation is triggered.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct Limits {
    /// Maximum allowed stack height in number of elements.
    ///
    /// See <https://wiki.parity.io/WebAssembly-StackHeight> to find out
    /// how the stack frame cost is calculated. Each element can be of one of the
    /// wasm value types. This means the maximum size per element is 64bit.
    ///
    /// # Note
    ///
    /// It is safe to disable (pass `None`) the `stack_height` when the execution engine
    /// is part of the runtime and hence there can be no indeterminism between different
    /// client resident execution engines.
    pub stack_height: Option<u32>,

    /// Maximum number of globals a module is allowed to declare.
    ///
    /// Globals are not limited through the linear memory limit `memory_pages`.
    pub globals: u32,

    /// Maximum number of locals a function can have.
    ///
    /// As wasm engine initializes each of the local, we need to limit their number to confine
    /// execution costs.
    pub locals: u32,

    /// Maximum numbers of parameters a function can have.
    ///
    /// Those need to be limited to prevent a potentially exploitable interaction with
    /// the stack height instrumentation: The costs of executing the stack height
    /// instrumentation for an indirectly called function scales linearly with the amount
    /// of parameters of this function. Because the stack height instrumentation itself is
    /// is not weight metered its costs must be static (via this limit) and included in
    /// the costs of the instructions that cause them (call, call_indirect).
    pub parameters: u32,

    /// Maximum number of memory pages allowed for a program.
    pub memory_pages: u16,

    /// Maximum number of elements allowed in a table.
    ///
    /// Currently, the only type of element that is allowed in a table is funcref.
    pub table_size: u32,

    /// Maximum number of elements that can appear as immediate value to the br_table instruction.
    pub br_table_size: u32,

    /// The maximum length of a subject in bytes used for PRNG generation.
    pub subject_len: u32,

    /// The maximum nesting level of the call stack.
    pub call_depth: u32,

    /// The maximum size of a message payload in bytes.
    pub payload_len: u32,

    /// The maximum length of a program code in bytes. This limit applies to the instrumented
    /// version of the code. Therefore `instantiate_with_code` can fail even when supplying
    /// a wasm binary below this maximum size.
    pub code_len: u32,
}

impl Limits {
    /// The maximum memory size in bytes that a program can occupy.
    pub fn max_memory_size(&self) -> u32 {
        self.memory_pages as u32 * 64 * 1024
    }
}

/// Describes the weight for all categories of supported wasm instructions.
///
/// There there is one field for each wasm instruction that describes the weight to
/// execute one instruction of that name. There are a few exceptions:
///
/// 1. If there is a i64 and a i32 variant of an instruction we use the weight
///    of the former for both.
/// 2. The following instructions are free of charge because they merely structure the
///    wasm module and cannot be spammed without making the module invalid (and rejected):
///    End, Unreachable, Return, Else
/// 3. The following instructions cannot be benchmarked because they are removed by any
///    real world execution engine as a preprocessing step and therefore don't yield a
///    meaningful benchmark result. However, in contrast to the instructions mentioned
///    in 2. they can be spammed. We price them with the same weight as the "default"
///    instruction (i64.const): Block, Loop, Nop
/// 4. We price both i64.const and drop as InstructionWeights.i64const / 2. The reason
///    for that is that we cannot benchmark either of them on its own but we need their
///    individual values to derive (by subtraction) the weight of all other instructions
///    that use them as supporting instructions. Supporting means mainly pushing arguments
///    and dropping return values in order to maintain a valid module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct InstructionWeights<T: Config> {
    /// Version of the instruction weights.
    ///
    /// # Note
    ///
    /// Should be incremented whenever any instruction weight is changed. The
    /// reason is that changes to instruction weights require a re-instrumentation
    /// in order to apply the changes to an already deployed code. The re-instrumentation
    /// is triggered by comparing the version of the current schedule with the version the code was
    /// instrumented with. Changes usually happen when pallet_gear is re-benchmarked.
    ///
    /// Changes to other parts of the schedule should not increment the version in
    /// order to avoid unnecessary re-instrumentations.
    pub version: u32,
    pub i64const: u32,
    pub i64load: u32,
    pub i32load: u32,
    pub i64store: u32,
    pub i32store: u32,
    pub select: u32,
    pub r#if: u32,
    pub br: u32,
    pub br_if: u32,
    pub br_table: u32,
    pub br_table_per_entry: u32,
    pub call: u32,
    pub call_indirect: u32,
    pub call_indirect_per_param: u32,
    pub call_per_local: u32,
    pub local_get: u32,
    pub local_set: u32,
    pub local_tee: u32,
    pub global_get: u32,
    pub global_set: u32,
    pub memory_current: u32,
    pub i64clz: u32,
    pub i32clz: u32,
    pub i64ctz: u32,
    pub i32ctz: u32,
    pub i64popcnt: u32,
    pub i32popcnt: u32,
    pub i64eqz: u32,
    pub i32eqz: u32,
    pub i32extend8s: u32,
    pub i32extend16s: u32,
    pub i64extend8s: u32,
    pub i64extend16s: u32,
    pub i64extend32s: u32,
    pub i64extendsi32: u32,
    pub i64extendui32: u32,
    pub i32wrapi64: u32,
    pub i64eq: u32,
    pub i32eq: u32,
    pub i64ne: u32,
    pub i32ne: u32,
    pub i64lts: u32,
    pub i32lts: u32,
    pub i64ltu: u32,
    pub i32ltu: u32,
    pub i64gts: u32,
    pub i32gts: u32,
    pub i64gtu: u32,
    pub i32gtu: u32,
    pub i64les: u32,
    pub i32les: u32,
    pub i64leu: u32,
    pub i32leu: u32,
    pub i64ges: u32,
    pub i32ges: u32,
    pub i64geu: u32,
    pub i32geu: u32,
    pub i64add: u32,
    pub i32add: u32,
    pub i64sub: u32,
    pub i32sub: u32,
    pub i64mul: u32,
    pub i32mul: u32,
    pub i64divs: u32,
    pub i32divs: u32,
    pub i64divu: u32,
    pub i32divu: u32,
    pub i64rems: u32,
    pub i32rems: u32,
    pub i64remu: u32,
    pub i32remu: u32,
    pub i64and: u32,
    pub i32and: u32,
    pub i64or: u32,
    pub i32or: u32,
    pub i64xor: u32,
    pub i32xor: u32,
    pub i64shl: u32,
    pub i32shl: u32,
    pub i64shrs: u32,
    pub i32shrs: u32,
    pub i64shru: u32,
    pub i32shru: u32,
    pub i64rotl: u32,
    pub i32rotl: u32,
    pub i64rotr: u32,
    pub i32rotr: u32,
    /// The type parameter is used in the default implementation.
    #[codec(skip)]
    #[cfg_attr(feature = "std", serde(skip))]
    pub _phantom: PhantomData<T>,
}

/// Describes the weight for each imported function that a program is allowed to call.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct SyscallWeights<T: Config> {
    /// Weight of calling `alloc`.
    pub alloc: Weight,

    /// Weight per page in `alloc`.
    pub alloc_per_page: Weight,

    /// Weight of calling `free`.
    pub free: Weight,

    /// Weight of calling `free_range`.
    pub free_range: Weight,

    /// Weight of calling `free_range` per page.
    pub free_range_per_page: Weight,

    /// Weight of calling `gr_reserve_gas`.
    pub gr_reserve_gas: Weight,

    /// Weight of calling `gr_unreserve_gas`
    pub gr_unreserve_gas: Weight,

    /// Weight of calling `gr_system_reserve_gas`
    pub gr_system_reserve_gas: Weight,

    /// Weight of calling `gr_gas_available`.
    pub gr_gas_available: Weight,

    /// Weight of calling `gr_message_id`.
    pub gr_message_id: Weight,

    /// Weight of calling `gr_program_id`.
    pub gr_program_id: Weight,

    /// Weight of calling `gr_source`.
    pub gr_source: Weight,

    /// Weight of calling `gr_value`.
    pub gr_value: Weight,

    /// Weight of calling `gr_value_available`.
    pub gr_value_available: Weight,

    /// Weight of calling `gr_size`.
    pub gr_size: Weight,

    /// Weight of calling `gr_read`.
    pub gr_read: Weight,

    /// Weight per payload byte by `gr_read`.
    pub gr_read_per_byte: Weight,

    /// Weight of calling `gr_env_vars`.
    pub gr_env_vars: Weight,

    /// Weight of calling `gr_block_height`.
    pub gr_block_height: Weight,

    /// Weight of calling `gr_block_timestamp`.
    pub gr_block_timestamp: Weight,

    /// Weight of calling `gr_random`.
    pub gr_random: Weight,

    /// Weight of calling `gr_reply_deposit`.
    pub gr_reply_deposit: Weight,

    /// Weight of calling `gr_send`.
    pub gr_send: Weight,

    /// Weight per payload byte in `gr_send`.
    pub gr_send_per_byte: Weight,

    /// Weight of calling `gr_send_wgas`.
    pub gr_send_wgas: Weight,

    /// Weight per payload byte in `gr_send_wgas`.
    pub gr_send_wgas_per_byte: Weight,

    /// Weight of calling `gr_value_available`.
    pub gr_send_init: Weight,

    /// Weight of calling `gr_send_push`.
    pub gr_send_push: Weight,

    /// Weight per payload byte by `gr_send_push`.
    pub gr_send_push_per_byte: Weight,

    /// Weight of calling `gr_send_commit`.
    pub gr_send_commit: Weight,

    /// Weight of calling `gr_send_commit_wgas`.
    pub gr_send_commit_wgas: Weight,

    /// Weight of calling `gr_reservation_send`.
    pub gr_reservation_send: Weight,

    /// Weight per payload byte in `gr_reservation_send`.
    pub gr_reservation_send_per_byte: Weight,

    /// Weight of calling `gr_reservation_send_commit`.
    pub gr_reservation_send_commit: Weight,

    /// Weight of calling `gr_reply_commit`.
    pub gr_reply_commit: Weight,

    /// Weight of calling `gr_reply_commit_wgas`.
    pub gr_reply_commit_wgas: Weight,

    /// Weight of calling `gr_reservation_reply`.
    pub gr_reservation_reply: Weight,

    /// Weight of calling `gr_reservation_reply` per one payload byte.
    pub gr_reservation_reply_per_byte: Weight,

    /// Weight of calling `gr_reservation_reply_commit`.
    pub gr_reservation_reply_commit: Weight,

    /// Weight of calling `gr_reply_push`.
    pub gr_reply_push: Weight,

    /// Weight of calling `gr_reply`.
    pub gr_reply: Weight,

    /// Weight of calling `gr_reply` per one payload byte.
    pub gr_reply_per_byte: Weight,

    /// Weight of calling `gr_reply_wgas`.
    pub gr_reply_wgas: Weight,

    /// Weight of calling `gr_reply_wgas` per one payload byte.
    pub gr_reply_wgas_per_byte: Weight,

    /// Weight per payload byte by `gr_reply_push`.
    pub gr_reply_push_per_byte: Weight,

    /// Weight of calling `gr_reply_to`.
    pub gr_reply_to: Weight,

    /// Weight of calling `gr_signal_code`.
    pub gr_signal_code: Weight,

    /// Weight of calling `gr_signal_from`.
    pub gr_signal_from: Weight,

    /// Weight of calling `gr_reply_input`.
    pub gr_reply_input: Weight,

    /// Weight of calling `gr_reply_input_wgas`.
    pub gr_reply_input_wgas: Weight,

    /// Weight of calling `gr_reply_push_input`.
    pub gr_reply_push_input: Weight,

    /// Weight per payload byte by `gr_reply_push_input`.
    pub gr_reply_push_input_per_byte: Weight,

    /// Weight of calling `gr_send_input`.
    pub gr_send_input: Weight,

    /// Weight of calling `gr_send_input_wgas`.
    pub gr_send_input_wgas: Weight,

    /// Weight of calling `gr_send_push_input`.
    pub gr_send_push_input: Weight,

    /// Weight per payload byte by `gr_send_push_input`.
    pub gr_send_push_input_per_byte: Weight,

    /// Weight of calling `gr_debug`.
    pub gr_debug: Weight,

    /// Weight per payload byte by `gr_debug_per_byte`.
    pub gr_debug_per_byte: Weight,

    /// Weight of calling `gr_reply_code`.
    pub gr_reply_code: Weight,

    /// Weight of calling `gr_exit`.
    pub gr_exit: Weight,

    /// Weight of calling `gr_leave`.
    pub gr_leave: Weight,

    /// Weight of calling `gr_wait`.
    pub gr_wait: Weight,

    /// Weight of calling `gr_wait_for`.
    pub gr_wait_for: Weight,

    /// Weight of calling `gr_wait_up_to`.
    pub gr_wait_up_to: Weight,

    /// Weight of calling `gr_wake`.
    pub gr_wake: Weight,

    /// Weight of calling `gr_create_program`.
    pub gr_create_program: Weight,

    /// Weight per payload byte in `gr_create_program`.
    pub gr_create_program_payload_per_byte: Weight,

    /// Weight per salt byte in `gr_create_program`
    pub gr_create_program_salt_per_byte: Weight,

    /// Weight of calling `create_program_wgas`.
    pub gr_create_program_wgas: Weight,

    /// Weight per payload byte by `create_program_wgas`.
    pub gr_create_program_wgas_payload_per_byte: Weight,

    /// Weight per salt byte by `create_program_wgas`.
    pub gr_create_program_wgas_salt_per_byte: Weight,

    /// The type parameter is used in the default implementation.
    #[codec(skip)]
    #[cfg_attr(feature = "std", serde(skip))]
    pub _phantom: PhantomData<T>,
}

/// Describes the weight for memory interaction.
///
/// Each weight with `lazy_pages_` prefix includes weight for storage read,
/// because for each first page access we need to at least check whether page exists in storage.
/// But they do not include cost for loading page data from storage into program memory.
/// This weight is taken in account separately, when loading occurs.
///
/// Lazy-pages write accesses does not include cost for uploading page data to storage,
/// because uploading happens after execution, so benchmarks do not include this cost.
/// But they include cost for processing changed page data in runtime.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct MemoryWeights<T: Config> {
    /// Cost per one [GearPage] signal `read` processing in lazy-pages,
    pub lazy_pages_signal_read: Weight,

    /// Cost per one [GearPage] signal `write` processing in lazy-pages,
    pub lazy_pages_signal_write: Weight,

    /// Cost per one [GearPage] signal `write after read` processing in lazy-pages,
    pub lazy_pages_signal_write_after_read: Weight,

    /// Cost per one [GearPage] host func `read` access processing in lazy-pages,
    pub lazy_pages_host_func_read: Weight,

    /// Cost per one [GearPage] host func `write` access processing in lazy-pages,
    pub lazy_pages_host_func_write: Weight,

    /// Cost per one [GearPage] host func `write after read` access processing in lazy-pages,
    pub lazy_pages_host_func_write_after_read: Weight,

    /// Cost per one [GearPage] data loading from storage and moving it in program memory.
    /// Does not include cost for storage read, because it is taken in account separately.
    pub load_page_data: Weight,

    /// Cost per one [GearPage] uploading data to storage.
    /// Does not include cost for processing changed page data in runtime,
    /// cause it is taken in account separately.
    pub upload_page_data: Weight,

    /// Cost per one [WasmPage] static page. Static pages can have static data,
    /// and executor must to move this data to static pages before execution.
    pub static_page: Weight,

    /// Cost per one [WasmPage] for memory growing.
    pub mem_grow: Weight,

    /// Cost per one [GearPage].
    /// When we read page data from storage in para-chain, then it should be sent to relay-chain,
    /// in order to use it for process queue execution. So, reading from storage cause
    /// additional resources consumption after block(s) production on para-chain.
    pub parachain_read_heuristic: Weight,

    /// The type parameter is used in the default implementation.
    #[codec(skip)]
    #[cfg_attr(feature = "std", serde(skip))]
    pub _phantom: PhantomData<T>,
}

impl<T: Config> From<MemoryWeights<T>> for LazyPagesCosts {
    fn from(val: MemoryWeights<T>) -> Self {
        Self {
            signal_read: val.lazy_pages_signal_read.ref_time().into(),
            signal_write: val
                .lazy_pages_signal_write
                .saturating_add(val.upload_page_data)
                .ref_time()
                .into(),
            signal_write_after_read: val
                .lazy_pages_signal_write_after_read
                .saturating_add(val.upload_page_data)
                .ref_time()
                .into(),
            host_func_read: val.lazy_pages_host_func_read.ref_time().into(),
            host_func_write: val
                .lazy_pages_host_func_write
                .saturating_add(val.upload_page_data)
                .ref_time()
                .into(),
            host_func_write_after_read: val
                .lazy_pages_host_func_write_after_read
                .saturating_add(val.upload_page_data)
                .ref_time()
                .into(),
            load_page_storage_data: val
                .load_page_data
                .saturating_add(val.parachain_read_heuristic)
                .ref_time()
                .into(),
        }
    }
}

macro_rules! replace_token {
    ($_in:tt $replacement:tt) => {
        $replacement
    };
}

macro_rules! call_zero {
    ($name:ident, $( $arg:expr ),*) => {
        <T as Config>::WeightInfo::$name($( replace_token!($arg 0) ),*)
    };
}

macro_rules! cost_args {
    ($name:ident, $( $arg: expr ),+) => {
        (<T as Config>::WeightInfo::$name($( $arg ),+).saturating_sub(call_zero!($name, $( $arg ),+))).ref_time()
    }
}

macro_rules! cost_batched_args {
    ($name:ident, $( $arg: expr ),+) => {
        cost_args!($name, $( $arg ),+) / u64::from(API_BENCHMARK_BATCH_SIZE)
    }
}

macro_rules! cost_instr_no_params_with_batch_size {
    ($name:ident, $batch_size:expr) => {
        (cost_args!($name, 1) / u64::from($batch_size)) as u32
    };
}

macro_rules! cost_instr_with_batch_size {
    ($name:ident, $num_params:expr, $batch_size:expr) => {
        cost_instr_no_params_with_batch_size!($name, $batch_size).saturating_sub(
            (cost_instr_no_params_with_batch_size!(instr_i64const, $batch_size) / 2)
                .saturating_mul($num_params),
        )
    };
}

macro_rules! cost_instr {
    ($name:ident, $num_params:expr) => {
        cost_instr_with_batch_size!($name, $num_params, INSTR_BENCHMARK_BATCH_SIZE)
    };
}

macro_rules! cost_byte_args {
    ($name:ident, $( $arg: expr ),+) => {
        cost_args!($name, $( $arg ),+) / 1024
    }
}

macro_rules! cost_byte_batched_args {
    ($name:ident, $( $arg: expr ),+) => {
        cost_batched_args!($name, $( $arg ),+) / 1024
    }
}

macro_rules! cost {
    ($name:ident) => {
        cost_args!($name, 1)
    };
}

macro_rules! cost_batched {
    ($name:ident) => {
        cost_batched_args!($name, 1)
    };
}

macro_rules! cost_byte {
    ($name:ident) => {
        cost_byte_args!($name, 1)
    };
}

macro_rules! cost_byte_batched {
    ($name:ident) => {
        cost_byte_batched_args!($name, 1)
    };
}

macro_rules! to_weight {
    ($ref_time:expr $(, $proof_size:expr )?) => {
        Weight::from_parts($ref_time, 0)$(.set_proof_size($proof_size))?
    };
}

impl<T: Config> Default for Schedule<T> {
    fn default() -> Self {
        Self {
            limits: Default::default(),
            instruction_weights: Default::default(),
            syscall_weights: Default::default(),
            memory_weights: Default::default(),
            db_write_per_byte: to_weight!(cost_byte!(db_write_per_kb)),
            db_read_per_byte: to_weight!(cost_byte!(db_read_per_kb)),
            module_instantiation_per_byte: to_weight!(cost_byte!(instantiate_module_per_kb)),
            code_instrumentation_cost: call_zero!(reinstrument_per_kb, 0),
            code_instrumentation_byte_cost: to_weight!(cost_byte!(reinstrument_per_kb)),
        }
    }
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            #[cfg(not(feature = "fuzz"))]
            stack_height: Some(STACK_HEIGHT_LIMIT),
            #[cfg(feature = "fuzz")]
            stack_height: Some(FUZZER_STACK_HEIGHT_LIMIT),
            globals: 256,
            locals: 1024,
            parameters: 128,
            memory_pages: MAX_WASM_PAGES_AMOUNT,
            // 4k function pointers (This is in count not bytes).
            table_size: 4096,
            br_table_size: 256,
            subject_len: 32,
            call_depth: 32,
            payload_len: message::MAX_PAYLOAD_SIZE as u32,
            code_len: 512 * 1024,
        }
    }
}

impl<T: Config> Default for InstructionWeights<T> {
    fn default() -> Self {
        Self {
            version: 1300,
            i64const: cost_instr!(instr_i64const, 1),
            i64load: cost_instr!(instr_i64load, 0),
            i32load: cost_instr!(instr_i32load, 0),
            i64store: cost_instr!(instr_i64store, 1),
            i32store: cost_instr!(instr_i32store, 0),
            select: cost_instr!(instr_select, 2),
            r#if: cost_instr!(instr_if, 0),
            br: cost_instr!(instr_br, 0),
            br_if: cost_instr!(instr_br_if, 1),
            br_table: cost_instr!(instr_br_table, 0),
            br_table_per_entry: cost_instr!(instr_br_table_per_entry, 0),
            call: cost_instr!(instr_call, 2),
            call_indirect: cost_instr!(instr_call_indirect, 1),
            call_indirect_per_param: cost_instr!(instr_call_indirect_per_param, 1),
            call_per_local: cost_instr!(instr_call_per_local, 1),
            local_get: cost_instr!(instr_local_get, 0),
            local_set: cost_instr!(instr_local_set, 1),
            local_tee: cost_instr!(instr_local_tee, 1),
            global_get: cost_instr!(instr_global_get, 0),
            global_set: cost_instr!(instr_global_set, 1),
            memory_current: cost_instr!(instr_memory_current, 1),
            i64clz: cost_instr!(instr_i64clz, 1),
            i32clz: cost_instr!(instr_i32clz, 1),
            i64ctz: cost_instr!(instr_i64ctz, 1),
            i32ctz: cost_instr!(instr_i32ctz, 1),
            i64popcnt: cost_instr!(instr_i64popcnt, 1),
            i32popcnt: cost_instr!(instr_i32popcnt, 1),
            i64eqz: cost_instr!(instr_i64eqz, 1),
            i32eqz: cost_instr!(instr_i32eqz, 1),
            i32extend8s: cost_instr!(instr_i32extend8s, 0),
            i32extend16s: cost_instr!(instr_i32extend16s, 0),
            i64extend8s: cost_instr!(instr_i64extend8s, 1),
            i64extend16s: cost_instr!(instr_i64extend16s, 1),
            i64extend32s: cost_instr!(instr_i64extend32s, 1),
            i64extendsi32: cost_instr!(instr_i64extendsi32, 0),
            i64extendui32: cost_instr!(instr_i64extendui32, 0),
            i32wrapi64: cost_instr!(instr_i32wrapi64, 1),
            i64eq: cost_instr!(instr_i64eq, 2),
            i32eq: cost_instr!(instr_i32eq, 2),
            i64ne: cost_instr!(instr_i64ne, 2),
            i32ne: cost_instr!(instr_i32ne, 2),
            i64lts: cost_instr!(instr_i64lts, 2),
            i32lts: cost_instr!(instr_i32lts, 2),
            i64ltu: cost_instr!(instr_i64ltu, 2),
            i32ltu: cost_instr!(instr_i32ltu, 2),
            i64gts: cost_instr!(instr_i64gts, 2),
            i32gts: cost_instr!(instr_i32gts, 2),
            i64gtu: cost_instr!(instr_i64gtu, 2),
            i32gtu: cost_instr!(instr_i32gtu, 2),
            i64les: cost_instr!(instr_i64les, 2),
            i32les: cost_instr!(instr_i32les, 2),
            i64leu: cost_instr!(instr_i64leu, 2),
            i32leu: cost_instr!(instr_i32leu, 2),
            i64ges: cost_instr!(instr_i64ges, 2),
            i32ges: cost_instr!(instr_i32ges, 2),
            i64geu: cost_instr!(instr_i64geu, 2),
            i32geu: cost_instr!(instr_i32geu, 2),
            i64add: cost_instr!(instr_i64add, 2),
            i32add: cost_instr!(instr_i32add, 2),
            i64sub: cost_instr!(instr_i64sub, 2),
            i32sub: cost_instr!(instr_i32sub, 2),
            i64mul: cost_instr!(instr_i64mul, 2),
            i32mul: cost_instr!(instr_i32mul, 2),
            i64divs: cost_instr!(instr_i64divs, 2),
            i32divs: cost_instr!(instr_i32divs, 2),
            i64divu: cost_instr!(instr_i64divu, 2),
            i32divu: cost_instr!(instr_i32divu, 2),
            i64rems: cost_instr!(instr_i64rems, 2),
            i32rems: cost_instr!(instr_i32rems, 2),
            i64remu: cost_instr!(instr_i64remu, 2),
            i32remu: cost_instr!(instr_i32remu, 2),
            i64and: cost_instr!(instr_i64and, 2),
            i32and: cost_instr!(instr_i32and, 2),
            i64or: cost_instr!(instr_i64or, 2),
            i32or: cost_instr!(instr_i32or, 2),
            i64xor: cost_instr!(instr_i64xor, 2),
            i32xor: cost_instr!(instr_i32xor, 2),
            i64shl: cost_instr!(instr_i64shl, 2),
            i32shl: cost_instr!(instr_i32shl, 2),
            i64shrs: cost_instr!(instr_i64shrs, 2),
            i32shrs: cost_instr!(instr_i32shrs, 2),
            i64shru: cost_instr!(instr_i64shru, 2),
            i32shru: cost_instr!(instr_i32shru, 2),
            i64rotl: cost_instr!(instr_i64rotl, 2),
            i32rotl: cost_instr!(instr_i32rotl, 2),
            i64rotr: cost_instr!(instr_i64rotr, 2),
            i32rotr: cost_instr!(instr_i32rotr, 2),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> From<SyscallWeights<T>> for SyscallCosts {
    fn from(weights: SyscallWeights<T>) -> SyscallCosts {
        SyscallCosts {
            alloc: weights.alloc.ref_time().into(),
            alloc_per_page: weights.alloc_per_page.ref_time().into(),
            free: weights.free.ref_time().into(),
            free_range: weights.free_range.ref_time().into(),
            free_range_per_page: weights.free_range_per_page.ref_time().into(),
            gr_reserve_gas: weights.gr_reserve_gas.ref_time().into(),
            gr_unreserve_gas: weights.gr_unreserve_gas.ref_time().into(),
            gr_system_reserve_gas: weights.gr_system_reserve_gas.ref_time().into(),
            gr_gas_available: weights.gr_gas_available.ref_time().into(),
            gr_message_id: weights.gr_message_id.ref_time().into(),
            gr_program_id: weights.gr_program_id.ref_time().into(),
            gr_source: weights.gr_source.ref_time().into(),
            gr_value: weights.gr_value.ref_time().into(),
            gr_value_available: weights.gr_value_available.ref_time().into(),
            gr_size: weights.gr_size.ref_time().into(),
            gr_read: weights.gr_read.ref_time().into(),
            gr_read_per_byte: weights.gr_read_per_byte.ref_time().into(),
            gr_env_vars: weights.gr_env_vars.ref_time().into(),
            gr_block_height: weights.gr_block_height.ref_time().into(),
            gr_block_timestamp: weights.gr_block_timestamp.ref_time().into(),
            gr_random: weights.gr_random.ref_time().into(),
            gr_reply_deposit: weights.gr_reply_deposit.ref_time().into(),
            gr_send: weights.gr_send.ref_time().into(),
            gr_send_per_byte: weights.gr_send_per_byte.ref_time().into(),
            gr_send_wgas: weights.gr_send_wgas.ref_time().into(),
            gr_send_wgas_per_byte: weights.gr_send_wgas_per_byte.ref_time().into(),
            gr_send_init: weights.gr_send_init.ref_time().into(),
            gr_send_push: weights.gr_send_push.ref_time().into(),
            gr_send_push_per_byte: weights.gr_send_push_per_byte.ref_time().into(),
            gr_send_commit: weights.gr_send_commit.ref_time().into(),
            gr_send_commit_wgas: weights.gr_send_commit_wgas.ref_time().into(),
            gr_reservation_send: weights.gr_reservation_send.ref_time().into(),
            gr_reservation_send_per_byte: weights.gr_reservation_send_per_byte.ref_time().into(),
            gr_reservation_send_commit: weights.gr_reservation_send_commit.ref_time().into(),
            gr_send_input: weights.gr_send_input.ref_time().into(),
            gr_send_input_wgas: weights.gr_send_input_wgas.ref_time().into(),
            gr_send_push_input: weights.gr_send_push_input.ref_time().into(),
            gr_send_push_input_per_byte: weights.gr_send_push_input_per_byte.ref_time().into(),
            gr_reply: weights.gr_reply.ref_time().into(),
            gr_reply_per_byte: weights.gr_reply_per_byte.ref_time().into(),
            gr_reply_wgas: weights.gr_reply_wgas.ref_time().into(),
            gr_reply_wgas_per_byte: weights.gr_reply_wgas_per_byte.ref_time().into(),
            gr_reply_push: weights.gr_reply_push.ref_time().into(),
            gr_reply_push_per_byte: weights.gr_reply_push_per_byte.ref_time().into(),
            gr_reply_commit: weights.gr_reply_commit.ref_time().into(),
            gr_reply_commit_wgas: weights.gr_reply_commit_wgas.ref_time().into(),
            gr_reservation_reply: weights.gr_reservation_reply.ref_time().into(),
            gr_reservation_reply_per_byte: weights.gr_reservation_reply_per_byte.ref_time().into(),
            gr_reservation_reply_commit: weights.gr_reservation_reply_commit.ref_time().into(),
            gr_reply_input: weights.gr_reply_input.ref_time().into(),
            gr_reply_input_wgas: weights.gr_reply_input_wgas.ref_time().into(),
            gr_reply_push_input: weights.gr_reply_push_input.ref_time().into(),
            gr_reply_push_input_per_byte: weights.gr_reply_push_input_per_byte.ref_time().into(),
            gr_debug: weights.gr_debug.ref_time().into(),
            gr_debug_per_byte: weights.gr_debug_per_byte.ref_time().into(),
            gr_reply_to: weights.gr_reply_to.ref_time().into(),
            gr_signal_code: weights.gr_signal_code.ref_time().into(),
            gr_signal_from: weights.gr_signal_from.ref_time().into(),
            gr_reply_code: weights.gr_reply_code.ref_time().into(),
            gr_exit: weights.gr_exit.ref_time().into(),
            gr_leave: weights.gr_leave.ref_time().into(),
            gr_wait: weights.gr_wait.ref_time().into(),
            gr_wait_for: weights.gr_wait_for.ref_time().into(),
            gr_wait_up_to: weights.gr_wait_up_to.ref_time().into(),
            gr_wake: weights.gr_wake.ref_time().into(),
            gr_create_program: weights.gr_create_program.ref_time().into(),
            gr_create_program_payload_per_byte: weights
                .gr_create_program_payload_per_byte
                .ref_time()
                .into(),
            gr_create_program_salt_per_byte: weights
                .gr_create_program_salt_per_byte
                .ref_time()
                .into(),
            gr_create_program_wgas: weights.gr_create_program_wgas.ref_time().into(),
            gr_create_program_wgas_payload_per_byte: weights
                .gr_create_program_wgas_payload_per_byte
                .ref_time()
                .into(),
            gr_create_program_wgas_salt_per_byte: weights
                .gr_create_program_wgas_salt_per_byte
                .ref_time()
                .into(),
        }
    }
}

impl<T: Config> Default for SyscallWeights<T> {
    fn default() -> Self {
        Self {
            gr_reply_deposit: to_weight!(cost_batched!(gr_reply_deposit))
                .saturating_sub(to_weight!(cost_batched!(gr_send))),

            gr_send: to_weight!(cost_batched!(gr_send)),
            gr_send_per_byte: to_weight!(cost_byte_batched!(gr_send_per_kb)),
            gr_send_wgas: to_weight!(cost_batched!(gr_send_wgas)),
            gr_send_wgas_per_byte: to_weight!(cost_byte_batched!(gr_send_wgas_per_kb)),
            gr_send_init: to_weight!(cost_batched!(gr_send_init)),
            gr_send_push: to_weight!(cost_batched!(gr_send_push)),
            gr_send_push_per_byte: to_weight!(cost_byte_batched!(gr_send_push_per_kb)),
            gr_send_commit: to_weight!(cost_batched!(gr_send_commit)),
            gr_send_commit_wgas: to_weight!(cost_batched!(gr_send_commit_wgas)),
            gr_reservation_send: to_weight!(cost_batched!(gr_reservation_send)),
            gr_reservation_send_per_byte: to_weight!(cost_byte_batched!(
                gr_reservation_send_per_kb
            )),
            gr_reservation_send_commit: to_weight!(cost_batched!(gr_reservation_send_commit)),
            gr_send_input: to_weight!(cost_batched!(gr_send_input)),
            gr_send_input_wgas: to_weight!(cost_batched!(gr_send_input_wgas)),
            gr_send_push_input: to_weight!(cost_batched!(gr_send_push_input)),
            gr_send_push_input_per_byte: to_weight!(cost_byte_batched!(gr_send_push_input_per_kb)),

            gr_reply: to_weight!(cost!(gr_reply)),
            gr_reply_per_byte: to_weight!(cost_byte!(gr_reply_per_kb)),
            gr_reply_wgas: to_weight!(cost!(gr_reply_wgas)),
            gr_reply_wgas_per_byte: to_weight!(cost_byte!(gr_reply_wgas_per_kb)),
            gr_reply_push: to_weight!(cost_batched!(gr_reply_push)),
            gr_reply_push_per_byte: to_weight!(cost_byte!(gr_reply_push_per_kb)),
            gr_reply_commit: to_weight!(cost!(gr_reply_commit)),
            gr_reply_commit_wgas: to_weight!(cost!(gr_reply_commit_wgas)),
            gr_reservation_reply: to_weight!(cost!(gr_reservation_reply)),
            gr_reservation_reply_per_byte: to_weight!(cost!(gr_reservation_reply_per_kb)),
            gr_reservation_reply_commit: to_weight!(cost!(gr_reservation_reply_commit)),
            gr_reply_input: to_weight!(cost!(gr_reply_input)),
            gr_reply_input_wgas: to_weight!(cost!(gr_reply_input_wgas)),
            gr_reply_push_input: to_weight!(cost_batched!(gr_reply_push_input)),
            gr_reply_push_input_per_byte: to_weight!(cost_byte!(gr_reply_push_input_per_kb)),

            // Alloc benchmark causes grow memory calls so we subtract it here as grow is charged separately.
            alloc: to_weight!(cost_batched!(alloc))
                .saturating_sub(to_weight!(cost_batched!(alloc_per_page)))
                .saturating_sub(to_weight!(cost_batched!(mem_grow))),
            alloc_per_page: to_weight!(cost_batched!(alloc_per_page)),
            free: to_weight!(cost_batched!(free)),
            free_range: to_weight!(cost_batched!(free_range)),
            free_range_per_page: to_weight!(cost_batched!(free_range_per_page)),
            gr_reserve_gas: to_weight!(cost!(gr_reserve_gas)),
            gr_system_reserve_gas: to_weight!(cost_batched!(gr_system_reserve_gas)),
            gr_unreserve_gas: to_weight!(cost!(gr_unreserve_gas)),
            gr_gas_available: to_weight!(cost_batched!(gr_gas_available)),
            gr_message_id: to_weight!(cost_batched!(gr_message_id)),
            gr_program_id: to_weight!(cost_batched!(gr_program_id)),
            gr_source: to_weight!(cost_batched!(gr_source)),
            gr_value: to_weight!(cost_batched!(gr_value)),
            gr_value_available: to_weight!(cost_batched!(gr_value_available)),
            gr_size: to_weight!(cost_batched!(gr_size)),
            gr_read: to_weight!(cost_batched!(gr_read)),
            gr_read_per_byte: to_weight!(cost_byte_batched!(gr_read_per_kb)),
            gr_env_vars: to_weight!(cost_batched!(gr_env_vars)),
            gr_block_height: to_weight!(cost_batched!(gr_block_height)),
            gr_block_timestamp: to_weight!(cost_batched!(gr_block_timestamp)),
            gr_random: to_weight!(cost_batched!(gr_random)),
            gr_debug: to_weight!(cost_batched!(gr_debug)),
            gr_debug_per_byte: to_weight!(cost_byte_batched!(gr_debug_per_kb)),
            gr_reply_to: to_weight!(cost_batched!(gr_reply_to)),
            gr_signal_code: to_weight!(cost_batched!(gr_signal_code)),
            gr_signal_from: to_weight!(cost_batched!(gr_signal_from)),
            gr_reply_code: to_weight!(cost_batched!(gr_reply_code)),
            gr_exit: to_weight!(cost!(gr_exit)),
            gr_leave: to_weight!(cost!(gr_leave)),
            gr_wait: to_weight!(cost!(gr_wait)),
            gr_wait_for: to_weight!(cost!(gr_wait_for)),
            gr_wait_up_to: to_weight!(cost!(gr_wait_up_to)),
            gr_wake: to_weight!(cost_batched!(gr_wake)),

            gr_create_program: to_weight!(cost_batched!(gr_create_program)),
            gr_create_program_payload_per_byte: to_weight!(cost_byte_batched_args!(
                gr_create_program_per_kb,
                1,
                0
            )),
            gr_create_program_salt_per_byte: to_weight!(cost_byte_batched_args!(
                gr_create_program_per_kb,
                0,
                1
            )),
            gr_create_program_wgas: to_weight!(cost_batched!(gr_create_program_wgas)),
            gr_create_program_wgas_payload_per_byte: to_weight!(cost_byte_batched_args!(
                gr_create_program_wgas_per_kb,
                1,
                0
            )),
            gr_create_program_wgas_salt_per_byte: to_weight!(cost_byte_batched_args!(
                gr_create_program_wgas_per_kb,
                0,
                1
            )),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> Default for MemoryWeights<T> {
    fn default() -> Self {
        // In benchmarks we calculate cost per wasm page,
        // so here we must convert it to cost per gear page.
        macro_rules! to_cost_per_gear_page {
            ($name:ident) => {
                cost!($name) / (WasmPage::SIZE / GearPage::SIZE) as u64
            };
        }

        const KB_SIZE: u64 = 1024;

        // Memory access thru host function benchmark uses a syscall,
        // which accesses memory. So, we have to subtract corresponding syscall weight.
        macro_rules! host_func_access {
            ($name:ident, $syscall:ident) => {{
                let syscall_per_kb_weight = cost_batched!($syscall);
                let syscall_per_gear_page_weight =
                    (syscall_per_kb_weight / KB_SIZE) * GearPage::SIZE as u64;
                to_cost_per_gear_page!($name).saturating_sub(syscall_per_gear_page_weight)
            }};
        }

        const KB_AMOUNT_IN_ONE_GEAR_PAGE: u64 = GearPage::SIZE as u64 / KB_SIZE;
        const _: () = assert!(KB_AMOUNT_IN_ONE_GEAR_PAGE > 0);
        const _: () = assert!(GearPage::SIZE as u64 % KB_SIZE == 0);

        Self {
            lazy_pages_signal_read: to_weight!(to_cost_per_gear_page!(lazy_pages_signal_read)),
            lazy_pages_signal_write: to_weight!(to_cost_per_gear_page!(lazy_pages_signal_write)),
            lazy_pages_signal_write_after_read: to_weight!(to_cost_per_gear_page!(
                lazy_pages_signal_write_after_read
            )),
            lazy_pages_host_func_read: to_weight!(host_func_access!(
                lazy_pages_host_func_read,
                gr_debug_per_kb
            )),
            lazy_pages_host_func_write: to_weight!(host_func_access!(
                lazy_pages_host_func_write,
                gr_read_per_kb
            )),
            lazy_pages_host_func_write_after_read: to_weight!(host_func_access!(
                lazy_pages_host_func_write_after_read,
                gr_read_per_kb
            )),
            // As you can see from calculation: `load_page_data` doesn't include weight for db read.
            // This is correct situation, because this weight is already included in above
            // lazy-pages weights.
            load_page_data: to_weight!(to_cost_per_gear_page!(lazy_pages_load_page_storage_data)
                .saturating_sub(to_cost_per_gear_page!(lazy_pages_signal_read))),
            upload_page_data: to_weight!(cost!(db_write_per_kb)
                .saturating_mul(KB_AMOUNT_IN_ONE_GEAR_PAGE)
                .saturating_add(T::DbWeight::get().writes(1).ref_time())),
            // TODO: make benches to calculate static page cost and mem grow cost (issue #2226)
            static_page: Weight::from_parts(100, 0),
            mem_grow: to_weight!(cost_batched!(mem_grow)),
            // TODO: make it non-zero for para-chains (issue #2225)
            parachain_read_heuristic: Weight::zero(),
            _phantom: PhantomData,
        }
    }
}

struct ScheduleRules<'a, T: Config> {
    schedule: &'a Schedule<T>,
    params: Vec<u32>,
}

impl<T: Config> Schedule<T> {
    pub fn rules(&self, module: &Module) -> impl Rules + '_ {
        ScheduleRules {
            schedule: self,
            params: module
                .type_section()
                .iter()
                .flat_map(|section| section.types())
                .map(|func| {
                    let Type::Function(func) = func;
                    func.params().len() as u32
                })
                .collect(),
        }
    }

    pub fn process_costs(&self) -> ProcessCosts {
        ProcessCosts {
            ext: ExtCosts {
                syscalls: self.syscall_weights.clone().into(),
                rent: RentCosts {
                    waitlist: CostsPerBlockOf::<T>::waitlist().into(),
                    dispatch_stash: CostsPerBlockOf::<T>::dispatch_stash().into(),
                    reservation: CostsPerBlockOf::<T>::reservation().into(),
                },
                mem_grow: self.memory_weights.mem_grow.ref_time().into(),
            },
            lazy_pages: self.memory_weights.clone().into(),
            read: DbWeightOf::<T>::get().reads(1).ref_time().into(),
            read_per_byte: self.db_read_per_byte.ref_time().into(),
            write: DbWeightOf::<T>::get().writes(1).ref_time().into(),
            static_page: self.memory_weights.static_page.ref_time().into(),
            instrumentation: self.code_instrumentation_cost.ref_time().into(),
            instrumentation_per_byte: self.code_instrumentation_byte_cost.ref_time().into(),
            module_instantiation_per_byte: self.module_instantiation_per_byte.ref_time().into(),
        }
    }
}

impl<'a, T: Config> Rules for ScheduleRules<'a, T> {
    fn instruction_cost(&self, instruction: &Instruction) -> Option<u32> {
        use Instruction::*;
        use SignExtInstruction::*;

        let w = &self.schedule.instruction_weights;
        let max_params = self.schedule.limits.parameters;

        let weight = match *instruction {
            End | Unreachable | Return | Else | Block(_) | Loop(_) | Nop | Drop => 0,
            I32Const(_) | I64Const(_) => w.i64const,
            I32Load(_, _)
            | I32Load8S(_, _)
            | I32Load8U(_, _)
            | I32Load16S(_, _)
            | I32Load16U(_, _) => w.i32load,
            I64Load(_, _)
            | I64Load8S(_, _)
            | I64Load8U(_, _)
            | I64Load16S(_, _)
            | I64Load16U(_, _)
            | I64Load32S(_, _)
            | I64Load32U(_, _) => w.i64load,
            I32Store(_, _) | I32Store8(_, _) | I32Store16(_, _) => w.i32store,
            I64Store(_, _) | I64Store8(_, _) | I64Store16(_, _) | I64Store32(_, _) => w.i64store,
            Select => w.select,
            If(_) => w.r#if,
            Br(_) => w.br,
            BrIf(_) => w.br_if,
            Call(_) => w.call,
            GetLocal(_) => w.local_get,
            SetLocal(_) => w.local_set,
            TeeLocal(_) => w.local_tee,
            GetGlobal(_) => w.global_get,
            SetGlobal(_) => w.global_set,
            CurrentMemory(_) => w.memory_current,
            CallIndirect(idx, _) => *self.params.get(idx as usize).unwrap_or(&max_params),
            BrTable(ref data) => w
                .br_table
                .saturating_add(w.br_table_per_entry.saturating_mul(data.table.len() as u32)),
            I32Clz => w.i32clz,
            I64Clz => w.i64clz,
            I32Ctz => w.i32ctz,
            I64Ctz => w.i64ctz,
            I32Popcnt => w.i32popcnt,
            I64Popcnt => w.i64popcnt,
            I32Eqz => w.i32eqz,
            I64Eqz => w.i64eqz,
            I64ExtendSI32 => w.i64extendsi32,
            I64ExtendUI32 => w.i64extendui32,
            I32WrapI64 => w.i32wrapi64,
            I32Eq => w.i32eq,
            I64Eq => w.i64eq,
            I32Ne => w.i32ne,
            I64Ne => w.i64ne,
            I32LtS => w.i32lts,
            I64LtS => w.i64lts,
            I32LtU => w.i32ltu,
            I64LtU => w.i64ltu,
            I32GtS => w.i32gts,
            I64GtS => w.i64gts,
            I32GtU => w.i32gtu,
            I64GtU => w.i64gtu,
            I32LeS => w.i32les,
            I64LeS => w.i64les,
            I32LeU => w.i32leu,
            I64LeU => w.i64leu,
            I32GeS => w.i32ges,
            I64GeS => w.i64ges,
            I32GeU => w.i32geu,
            I64GeU => w.i64geu,
            I32Add => w.i32add,
            I64Add => w.i64add,
            I32Sub => w.i32sub,
            I64Sub => w.i64sub,
            I32Mul => w.i32mul,
            I64Mul => w.i64mul,
            I32DivS => w.i32divs,
            I64DivS => w.i64divs,
            I32DivU => w.i32divu,
            I64DivU => w.i64divu,
            I32RemS => w.i32rems,
            I64RemS => w.i64rems,
            I32RemU => w.i32remu,
            I64RemU => w.i64remu,
            I32And => w.i32and,
            I64And => w.i64and,
            I32Or => w.i32or,
            I64Or => w.i64or,
            I32Xor => w.i32xor,
            I64Xor => w.i64xor,
            I32Shl => w.i32shl,
            I64Shl => w.i64shl,
            I32ShrS => w.i32shrs,
            I64ShrS => w.i64shrs,
            I32ShrU => w.i32shru,
            I64ShrU => w.i64shru,
            I32Rotl => w.i32rotl,
            I64Rotl => w.i64rotl,
            I32Rotr => w.i32rotr,
            I64Rotr => w.i64rotr,
            SignExt(ref s) => match s {
                I32Extend8S => w.i32extend8s,
                I32Extend16S => w.i32extend16s,
                I64Extend8S => w.i64extend8s,
                I64Extend16S => w.i64extend16s,
                I64Extend32S => w.i64extend32s,
            },
            // Returning None makes the gas instrumentation fail which we intend for
            // unsupported or unknown instructions.
            _ => return None,
        };
        Some(weight)
    }

    fn memory_grow_cost(&self) -> MemoryGrowCost {
        MemoryGrowCost::Free
    }

    fn call_per_local_cost(&self) -> u32 {
        self.schedule.instruction_weights.call_per_local
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::mock::Test;
    use gear_wasm_instrument::{
        gas_metering::{CustomConstantCostRules, Rules, Schedule as WasmInstrumentSchedule},
        parity_wasm::elements,
    };

    fn all_measured_instructions() -> Vec<Instruction> {
        use elements::{BlockType, BrTableData, Instruction::*};

        let default_table_data = BrTableData {
            table: Default::default(),
            default: 0,
        };

        // A set of instructions weights for which the Gear provides.
        // Instruction must not be removed (!), but can be added.
        vec![
            End,
            Unreachable,
            Return,
            Else,
            I32Const(0),
            I64Const(0),
            Block(BlockType::NoResult),
            Loop(BlockType::NoResult),
            Nop,
            Drop,
            I32Load(0, 0),
            I32Load8S(0, 0),
            I32Load8U(0, 0),
            I32Load16S(0, 0),
            I32Load16U(0, 0),
            I64Load(0, 0),
            I64Load8S(0, 0),
            I64Load8U(0, 0),
            I64Load16S(0, 0),
            I64Load16U(0, 0),
            I64Load32S(0, 0),
            I64Load32U(0, 0),
            I32Store(0, 0),
            I32Store8(0, 0),
            I32Store16(0, 0),
            I64Store(0, 0),
            I64Store8(0, 0),
            I64Store16(0, 0),
            I64Store32(0, 0),
            Select,
            If(BlockType::NoResult),
            Br(0),
            BrIf(0),
            Call(0),
            GetLocal(0),
            SetLocal(0),
            TeeLocal(0),
            GetGlobal(0),
            SetGlobal(0),
            CurrentMemory(0),
            CallIndirect(0, 0),
            BrTable(default_table_data.into()),
            I32Clz,
            I64Clz,
            I32Ctz,
            I64Ctz,
            I32Popcnt,
            I64Popcnt,
            I32Eqz,
            I64Eqz,
            I64ExtendSI32,
            I64ExtendUI32,
            I32WrapI64,
            I32Eq,
            I64Eq,
            I32Ne,
            I64Ne,
            I32LtS,
            I64LtS,
            I32LtU,
            I64LtU,
            I32GtS,
            I64GtS,
            I32GtU,
            I64GtU,
            I32LeS,
            I64LeS,
            I32LeU,
            I64LeU,
            I32GeS,
            I64GeS,
            I32GeU,
            I64GeU,
            I32Add,
            I64Add,
            I32Sub,
            I64Sub,
            I32Mul,
            I64Mul,
            I32DivS,
            I64DivS,
            I32DivU,
            I64DivU,
            I32RemS,
            I64RemS,
            I32RemU,
            I64RemU,
            I32And,
            I64And,
            I32Or,
            I64Or,
            I32Xor,
            I64Xor,
            I32Shl,
            I64Shl,
            I32ShrS,
            I64ShrS,
            I32ShrU,
            I64ShrU,
            I32Rotl,
            I64Rotl,
            I32Rotr,
            I64Rotr,
        ]
    }

    fn default_wasm_module() -> Module {
        let simple_wat = r#"
        (module
            (import "env" "memory" (memory 1))
            (export "handle" (func $handle))
            (export "init" (func $init))
            (func $handle)
            (func $init)
        )"#;
        Module::from_bytes(
            wabt::Wat2Wasm::new()
                .validate(false)
                .convert(simple_wat)
                .expect("failed to parse module"),
        )
        .expect("module instantiation failed")
    }

    // This test must never fail during local development/release.
    //
    // The instruction set in the test mustn't be changed. Test checks
    // whether no instruction weight was removed from Rules, so backward
    // compatibility is reached.
    #[test]
    fn instructions_backward_compatibility() {
        let schedule = Schedule::<Test>::default();
        let wasm_instrument_schedule = WasmInstrumentSchedule::default();

        // used in `pallet-gear` to estimate the gas used by the program
        let schedule_rules = schedule.rules(&default_wasm_module());

        // used to simulate real gas from `pallet-gear` in crates like gtest
        let wasm_instrument_schedule_rules = wasm_instrument_schedule.rules(&default_wasm_module());

        // used to simulate gas and reject unsupported instructions in unit tests
        let custom_cost_rules = CustomConstantCostRules::default();

        all_measured_instructions().iter().for_each(|i| {
            assert!(schedule_rules.instruction_cost(i).is_some());
            assert_eq!(
                schedule_rules.instruction_cost(i),
                wasm_instrument_schedule_rules.instruction_cost(i)
            );
            assert!(custom_cost_rules.instruction_cost(i).is_some());
        })
    }

    /// This function creates a program with full of empty
    /// functions, and returns the size of the wasm code.
    fn module_with_full_idx(count: usize) -> usize {
        let funcs = "(func)".repeat(count);
        let wat = format!("(module {funcs})");
        wabt::wat2wasm(wat).expect("Failed to serialize wasm").len()
    }

    #[test]
    fn deserialize_max_fn_idx_with_code_limit() {
        use gear_wasm_instrument::parity_wasm::elements::{IndexMap, Serialize, VarUint32};
        use std::io;

        // Calculates the max limit of the function indexes
        // with our code length limit.
        let code_limit = Limits::default().code_len as usize;
        let empty_program_len = module_with_full_idx(1);
        let empty_fn_len = module_with_full_idx(2) - empty_program_len;

        // NOTE:
        //
        // For triggering the bug, assigning `u32::MAX` to `max_idx`.
        let max_idx = ((code_limit - empty_program_len) / empty_fn_len + 1) as u32;

        // NOTE:
        //
        // We are not generating the wasm module in memory because it
        // takes too much.
        //
        // Mock the max idx in the index map of parity-wasm, cherry-pick
        // https://github.com/casper-network/casper-wasm/pull/1 to our
        // parity-wasm fork when this test getting failed which could be
        // happened on **raising the code len limit of our programs**.
        //
        // For the current testing machine, Apple M1 chip, 16GB memory,
        // deserializing a program with full of indexes in code size 4GB
        // will lead to the problem.
        let mut buffer = vec![];
        VarUint32::from(1u32).serialize(&mut buffer).unwrap();
        VarUint32::from(max_idx - 1).serialize(&mut buffer).unwrap();
        "foobar".to_string().serialize(&mut buffer).unwrap();

        let indexmap =
            IndexMap::<String>::deserialize(max_idx as usize, &mut io::Cursor::new(buffer))
                .unwrap();

        assert_eq!(indexmap.get(max_idx - 1), Some(&"foobar".to_string()));
        assert_eq!(indexmap.len(), 1);
    }
}