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
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
// 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.

use crate::{weights::WeightInfo, Config, CostsPerBlockOf, DbWeightOf};
use common::scheduler::SchedulingCostsPerBlock;
use frame_support::{traits::Get, weights::Weight};
use gear_core::{
    code::MAX_WASM_PAGES_AMOUNT,
    costs::{ExtCosts, InstantiationCosts, LazyPagesCosts, ProcessCosts, RentCosts, SyscallCosts},
    message,
    pages::{GearPage, WasmPage},
};
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;

/// Maximum number of data segments in a wasm module.
/// It has been determined that the maximum number of data segments in a wasm module
/// does not exceed 1024 by a large margin.
pub const DATA_SEGMENTS_AMOUNT_LIMIT: u32 = 1024;

/// Maximum number of wasm tables in a wasm module.
/// The same limit also imposed by `wasmparser`,
/// see <https://github.com/bytecodealliance/wasm-tools/blob/main/crates/wasmparser/src/limits.rs>
pub const TABLE_NUMBER_LIMIT: u32 = 100;

/// 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>,

    /// The weights for renting.
    pub rent_weights: RentWeights<T>,

    /// The weights for database access.
    pub db_weights: DbWeights<T>,

    /// The weights for executing tasks.
    pub task_weights: TaskWeights<T>,

    /// The weights for instantiation of the module.
    pub instantiation_weights: InstantiationWeights<T>,

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

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

    /// Load allocations weight.
    pub load_allocations_weight: 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 tables allowed for a program.
    /// The same limit also imposed by `wasmparser`,
    /// see <https://github.com/bytecodealliance/wasm-tools/blob/main/crates/wasmparser/src/limits.rs>
    pub table_number: 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,

    /// The maximum number of wasm data segments allowed for a program.
    pub data_segments_amount: u32,
}

/// 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 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] for memory growing.
    pub mem_grow: Weight,

    /// Cost per one [WasmPage] for memory growing.
    pub mem_grow_per_page: 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>,
}

/// Describes the weight for instantiation of the module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, ScheduleDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct InstantiationWeights<T: Config> {
    /// WASM module code section instantiation per byte cost.
    pub code_section_per_byte: Weight,

    /// WASM module data section instantiation per byte cost.
    pub data_section_per_byte: Weight,

    /// WASM module global section instantiation per byte cost.
    pub global_section_per_byte: Weight,

    /// WASM module table section instantiation per byte cost.
    pub table_section_per_byte: Weight,

    /// WASM module element section instantiation per byte cost.
    pub element_section_per_byte: Weight,

    /// WASM module type section instantiation per byte cost.
    pub type_section_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 renting.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct RentWeights<T: Config> {
    /// Holding message in waitlist weight.
    pub waitlist: Weight,
    /// Holding message in dispatch stash weight.
    pub dispatch_stash: Weight,
    /// Holding reservation weight.
    pub reservation: Weight,
    /// Holding message in mailbox weight.
    pub mailbox: Weight,
    /// The minimal gas amount for message to be inserted in mailbox.
    pub mailbox_threshold: Weight,
    /// The type parameter is used in the default implementation.
    #[codec(skip)]
    #[cfg_attr(feature = "std", serde(skip))]
    pub _phantom: PhantomData<T>,
}

/// Describes DB access weights.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct DbWeights<T: Config> {
    pub read: Weight,
    pub read_per_byte: Weight,
    pub write: Weight,
    pub write_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 weights for running tasks.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, WeightDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct TaskWeights<T: Config> {
    pub remove_gas_reservation: Weight,
    pub send_user_message_to_mailbox: Weight,
    pub send_user_message: Weight,
    pub send_dispatch: Weight,
    pub wake_message: Weight,
    pub wake_message_no_wake: Weight,
    pub remove_from_waitlist: Weight,
    pub remove_from_mailbox: 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> Default for TaskWeights<T> {
    fn default() -> Self {
        type W<T> = <T as Config>::WeightInfo;

        Self {
            remove_gas_reservation: W::<T>::tasks_remove_gas_reservation(),
            send_user_message_to_mailbox: W::<T>::tasks_send_user_message_to_mailbox(),
            send_user_message: W::<T>::tasks_send_user_message(),
            send_dispatch: W::<T>::tasks_send_dispatch(),
            wake_message: W::<T>::tasks_wake_message(),
            wake_message_no_wake: W::<T>::tasks_wake_message_no_wake(),
            remove_from_waitlist: W::<T>::tasks_remove_from_waitlist(),
            remove_from_mailbox: W::<T>::tasks_remove_from_mailbox(),
            _phantom: PhantomData,
        }
    }
}

#[inline]
fn cost(w: fn(u32) -> Weight) -> Weight {
    Weight::from_parts(w(1).saturating_sub(w(0)).ref_time(), 0)
}

#[inline]
fn cost_byte(w: fn(u32) -> Weight) -> Weight {
    Weight::from_parts(cost(w).ref_time() / 1024, 0)
}

#[inline]
fn cost_batched(w: fn(u32) -> Weight) -> Weight {
    Weight::from_parts(cost(w).ref_time() / u64::from(API_BENCHMARK_BATCH_SIZE), 0)
}

#[inline]
fn cost_byte_batched(w: fn(u32) -> Weight) -> Weight {
    Weight::from_parts(cost_batched(w).ref_time() / 1024, 0)
}

#[inline]
fn cost_byte_batched_args(w: fn(u32, u32) -> Weight, arg1: u32, arg2: u32) -> Weight {
    Weight::from_parts(
        w(arg1, arg2).saturating_sub(w(0, 0)).ref_time()
            / u64::from(API_BENCHMARK_BATCH_SIZE)
            / 1024,
        0,
    )
}

#[inline]
fn cost_zero(w: fn(u32) -> Weight) -> Weight {
    let ref_time = w(0).ref_time();
    Weight::from_parts(ref_time, w(0).proof_size())
}

#[inline]
fn cost_instr_no_params_with_batch_size(w: fn(u32) -> Weight) -> u32 {
    ((w(1).saturating_sub(w(0))).ref_time() / u64::from(INSTR_BENCHMARK_BATCH_SIZE)) as u32
}

#[inline]
fn cost_instr<T: Config>(w: fn(u32) -> Weight, num_params: u32) -> u32 {
    cost_instr_no_params_with_batch_size(w)
        .saturating_sub(cost_i64const::<T>().saturating_mul(num_params))
}

#[inline]
fn cost_i64const<T: Config>() -> u32 {
    type W<T> = <T as Config>::WeightInfo;
    // Since we cannot directly benchmark the weight of `i64.const` (or `i32.const`; we consider their weights to be the same for our purposes),
    // we estimate it as the difference between the benchmarks `instr_call_const` and `instr_call`.
    // The difference between these two benchmarks will give us the weight of `i64.const`, which for x86-64
    // can be represented by a single `mov` instruction with the embedded const parameter
    // (for e.x: `mov reg,0xDEADBEAFDEABEAF`).
    //
    // This approach may work, but the estimation is not very accurate.
    // To reduce the impact of this inaccuracy on the assessment of other instructions
    // (as we subtract the weight of `i64.const` from other instructions),
    // we introduce a weight division coefficient called `I64CONST_WEIGHT_DIVIDER`.
    // This helps bring the weight of `i64.const` closer to that of the x86-64 `mov` instruction's estimate.
    const I64CONST_WEIGHT_DIVIDER: u32 = 2;

    cost_instr_no_params_with_batch_size(W::<T>::instr_i64const) / I64CONST_WEIGHT_DIVIDER
}

impl<T: Config> Default for Schedule<T> {
    fn default() -> Self {
        type W<T> = <T as Config>::WeightInfo;
        Self {
            limits: Default::default(),
            instruction_weights: Default::default(),
            syscall_weights: Default::default(),
            memory_weights: Default::default(),
            rent_weights: Default::default(),
            db_weights: Default::default(),
            task_weights: Default::default(),
            instantiation_weights: Default::default(),
            code_instrumentation_cost: cost_zero(W::<T>::reinstrument_per_kb),
            code_instrumentation_byte_cost: cost_byte(W::<T>::reinstrument_per_kb),
            load_allocations_weight: cost(W::<T>::load_allocations_per_interval),
        }
    }
}

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),
            data_segments_amount: DATA_SEGMENTS_AMOUNT_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,
            table_number: TABLE_NUMBER_LIMIT,
            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 {
        // # Wasmer's compiler optimization (relevant for version 4.3.5 single-pass compiler for x86-64 target)
        //
        // Wasmer's single-pass compiler implements an optimization for certain wasm i32 instructions where
        // `i64const`/`i32const` parameters can be embedded into native x86-64 instructions.
        //
        // This optimization works for the following types of instructions:
        //
        // - Single-parameter i32 instructions that compile to a single x86-64 `mov` instruction,
        //   e.g., `i64.extend_u/i32`, `i32.wrap_i64`.
        // - Binary operation i32 instructions that compile to an x86-64 `cmp` instruction,
        //   e.g., `i32.eq`, `i32.ne`, `i32.lt_s`, `i32.lt_u`, etc.
        // - `i32.add`, `i32.sub` instructions where one parameter is embedded into the instruction.
        // - Several logical i32 instructions: `i32.and`, `i32.or`, `i32.xor`.
        //
        // See below for the assembly listings of the mentioned instructions.
        type W<T> = <T as Config>::WeightInfo;
        Self {
            version: 1620,
            i64const: cost_i64const::<T>(),
            i64load: cost_instr::<T>(W::<T>::instr_i64load, 0),
            i32load: cost_instr::<T>(W::<T>::instr_i32load, 0),
            i64store: cost_instr::<T>(W::<T>::instr_i64store, 1),
            i32store: cost_instr::<T>(W::<T>::instr_i32store, 0),
            select: cost_instr::<T>(W::<T>::instr_select, 2),
            r#if: cost_instr::<T>(W::<T>::instr_if, 0),
            br: cost_instr::<T>(W::<T>::instr_br, 0),
            br_if: cost_instr::<T>(W::<T>::instr_br_if, 1),
            br_table: cost_instr::<T>(W::<T>::instr_br_table, 0),
            br_table_per_entry: cost_instr::<T>(W::<T>::instr_br_table_per_entry, 0),
            call: cost_instr::<T>(W::<T>::instr_call, 2),
            call_indirect: cost_instr::<T>(W::<T>::instr_call_indirect, 1),
            call_indirect_per_param: cost_instr::<T>(W::<T>::instr_call_indirect_per_param, 1),
            call_per_local: cost_instr::<T>(W::<T>::instr_call_per_local, 1),
            local_get: cost_instr::<T>(W::<T>::instr_local_get, 0),
            local_set: cost_instr::<T>(W::<T>::instr_local_set, 1),
            local_tee: cost_instr::<T>(W::<T>::instr_local_tee, 1),
            global_get: cost_instr::<T>(W::<T>::instr_global_get, 0),
            global_set: cost_instr::<T>(W::<T>::instr_global_set, 1),
            memory_current: cost_instr::<T>(W::<T>::instr_memory_current, 1),
            i64clz: cost_instr::<T>(W::<T>::instr_i64clz, 1),
            i32clz: cost_instr::<T>(W::<T>::instr_i32clz, 1),
            i64ctz: cost_instr::<T>(W::<T>::instr_i64ctz, 1),
            i32ctz: cost_instr::<T>(W::<T>::instr_i32ctz, 1),
            i64popcnt: cost_instr::<T>(W::<T>::instr_i64popcnt, 1),
            i32popcnt: cost_instr::<T>(W::<T>::instr_i32popcnt, 1),
            i64eqz: cost_instr::<T>(W::<T>::instr_i64eqz, 1),
            i32eqz: cost_instr::<T>(W::<T>::instr_i32eqz, 1),
            // `i32extend8s` compiles to:
            // ```assembly
            //     mov rax,0x3b578dc7  <- i64const
            //     movsx esi,al
            // ```
            i32extend8s: cost_instr::<T>(W::<T>::instr_i32extend8s, 1),
            // `i32extend16s` compiles to:
            // ```assembly
            //     mov rax,0xffffffffdd0b1b34  <- i64const
            //     movsx esi,ax
            // ```
            i32extend16s: cost_instr::<T>(W::<T>::instr_i32extend16s, 1),
            i64extend8s: cost_instr::<T>(W::<T>::instr_i64extend8s, 1),
            i64extend16s: cost_instr::<T>(W::<T>::instr_i64extend16s, 1),
            i64extend32s: cost_instr::<T>(W::<T>::instr_i64extend32s, 1),
            // `i64extendsi32` compiles to:
            // ```assembly
            //     mov rax,0xffffffffdd0b1b34  <- i64const
            //     movsxd rsi,eax
            // ```
            i64extendsi32: cost_instr::<T>(W::<T>::instr_i64extendsi32, 1),
            // `i64extendui32` compiles to:
            // ```assembly
            //     mov esi,0x3b578dc7  <- i64const embedded in the instruction
            // ```
            i64extendui32: cost_instr::<T>(W::<T>::instr_i64extendui32, 0),
            // `i32wrapi64` compiles to:
            // ```assembly
            //     mov esi,0x3b578dc7 <- i64const embedded in the instruction
            // ```
            i32wrapi64: cost_instr::<T>(W::<T>::instr_i32wrapi64, 0),
            i64eq: cost_instr::<T>(W::<T>::instr_i64eq, 2),
            // `i32eq` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     cmp eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     setz sil
            //     and esi,0xff
            // ```
            i32eq: cost_instr::<T>(W::<T>::instr_i32eq, 1),
            i64ne: cost_instr::<T>(W::<T>::instr_i64ne, 2),
            // `i32ne` compiles to:
            // ```assembly
            //     mov eax,0xa9601ba6 <- i64const
            //     cmp eax,0x4b51bf3  <- i64const embedded in the instruction
            //     setnz sil
            //     and esi,0xff
            // ```
            i32ne: cost_instr::<T>(W::<T>::instr_i32ne, 1),
            i64lts: cost_instr::<T>(W::<T>::instr_i64lts, 2),
            // `i32lts` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     cmp eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     setl sil
            //     and esi,0xff
            // ```
            i32lts: cost_instr::<T>(W::<T>::instr_i32lts, 1),
            i64ltu: cost_instr::<T>(W::<T>::instr_i64ltu, 2),
            // `i32ltu` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32ltu: cost_instr::<T>(W::<T>::instr_i32ltu, 1),
            i64gts: cost_instr::<T>(W::<T>::instr_i64gts, 2),
            // `i32gts` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32gts: cost_instr::<T>(W::<T>::instr_i32gts, 1),
            i64gtu: cost_instr::<T>(W::<T>::instr_i64gtu, 2),
            // `i32gtu` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32gtu: cost_instr::<T>(W::<T>::instr_i32gtu, 1),
            i64les: cost_instr::<T>(W::<T>::instr_i64les, 2),
            // `i32les` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32les: cost_instr::<T>(W::<T>::instr_i32les, 1),
            i64leu: cost_instr::<T>(W::<T>::instr_i64leu, 2),
            // `i32leu` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32leu: cost_instr::<T>(W::<T>::instr_i32leu, 1),
            i64ges: cost_instr::<T>(W::<T>::instr_i64ges, 2),
            // `i32ges` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32ges: cost_instr::<T>(W::<T>::instr_i32ges, 1),
            i64geu: cost_instr::<T>(W::<T>::instr_i64geu, 2),
            // `i32geu` compiles similarly to other i32 comparisons instructions,
            // so we subtract `i64const` (`num_params`) only 1 time.
            i32geu: cost_instr::<T>(W::<T>::instr_i32geu, 1),
            i64add: cost_instr::<T>(W::<T>::instr_i64add, 2),
            // `i32add` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     add eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     mov esi,eax
            // ```
            i32add: cost_instr::<T>(W::<T>::instr_i32add, 1),
            i64sub: cost_instr::<T>(W::<T>::instr_i64sub, 2),
            // `i32sub` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     sub eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     mov esi,eax
            // ```
            i32sub: cost_instr::<T>(W::<T>::instr_i32sub, 1),
            i64mul: cost_instr::<T>(W::<T>::instr_i64mul, 2),
            i32mul: cost_instr::<T>(W::<T>::instr_i32mul, 2),
            i64divs: cost_instr::<T>(W::<T>::instr_i64divs, 2),
            i32divs: cost_instr::<T>(W::<T>::instr_i32divs, 2),
            i64divu: cost_instr::<T>(W::<T>::instr_i64divu, 2),
            i32divu: cost_instr::<T>(W::<T>::instr_i32divu, 2),
            i64rems: cost_instr::<T>(W::<T>::instr_i64rems, 2),
            i32rems: cost_instr::<T>(W::<T>::instr_i32rems, 2),
            i64remu: cost_instr::<T>(W::<T>::instr_i64remu, 2),
            i32remu: cost_instr::<T>(W::<T>::instr_i32remu, 2),
            i64and: cost_instr::<T>(W::<T>::instr_i64and, 2),
            // `i32and` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     and eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     mov esi,eax
            // ```
            i32and: cost_instr::<T>(W::<T>::instr_i32and, 1),
            i64or: cost_instr::<T>(W::<T>::instr_i64or, 2),
            // `i32or` compiles to:
            // ```assembly
            //     mov eax,0xf9e1253c <- i64const
            //     or eax,0xeaf224f  <- i64const embedded in the instruction
            //     mov esi,eax
            // ```
            i32or: cost_instr::<T>(W::<T>::instr_i32or, 1),
            i64xor: cost_instr::<T>(W::<T>::instr_i64xor, 2),
            // `i32xor` compiles to:
            // ```assembly
            //     mov eax,0x3b578dc7 <- i64const
            //     xor eax,0xdd0b1b34 <- i64const embedded in the instruction
            //     mov esi,eax
            // ```
            i32xor: cost_instr::<T>(W::<T>::instr_i32xor, 1),
            i64shl: cost_instr::<T>(W::<T>::instr_i64shl, 2),
            i32shl: cost_instr::<T>(W::<T>::instr_i32shl, 2),
            i64shrs: cost_instr::<T>(W::<T>::instr_i64shrs, 2),
            i32shrs: cost_instr::<T>(W::<T>::instr_i32shrs, 2),
            i64shru: cost_instr::<T>(W::<T>::instr_i64shru, 2),
            i32shru: cost_instr::<T>(W::<T>::instr_i32shru, 2),
            i64rotl: cost_instr::<T>(W::<T>::instr_i64rotl, 2),
            i32rotl: cost_instr::<T>(W::<T>::instr_i32rotl, 2),
            i64rotr: cost_instr::<T>(W::<T>::instr_i64rotr, 2),
            i32rotr: cost_instr::<T>(W::<T>::instr_i32rotr, 2),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> Default for SyscallWeights<T> {
    fn default() -> Self {
        type W<T> = <T as Config>::WeightInfo;
        Self {
            gr_reply_deposit: cost_batched(W::<T>::gr_reply_deposit)
                .saturating_sub(cost_batched(W::<T>::gr_send)),

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

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

            alloc: cost_batched(W::<T>::alloc),
            free: cost_batched(W::<T>::free),
            free_range: cost_batched(W::<T>::free_range),
            free_range_per_page: cost_batched(W::<T>::free_range_per_page),

            gr_reserve_gas: cost(W::<T>::gr_reserve_gas),
            gr_system_reserve_gas: cost_batched(W::<T>::gr_system_reserve_gas),
            gr_unreserve_gas: cost(W::<T>::gr_unreserve_gas),
            gr_gas_available: cost_batched(W::<T>::gr_gas_available),
            gr_message_id: cost_batched(W::<T>::gr_message_id),
            gr_program_id: cost_batched(W::<T>::gr_program_id),
            gr_source: cost_batched(W::<T>::gr_source),
            gr_value: cost_batched(W::<T>::gr_value),
            gr_value_available: cost_batched(W::<T>::gr_value_available),
            gr_size: cost_batched(W::<T>::gr_size),
            gr_read: cost_batched(W::<T>::gr_read),
            gr_read_per_byte: cost_byte_batched(W::<T>::gr_read_per_kb),
            gr_env_vars: cost_batched(W::<T>::gr_env_vars),
            gr_block_height: cost_batched(W::<T>::gr_block_height),
            gr_block_timestamp: cost_batched(W::<T>::gr_block_timestamp),
            gr_random: cost_batched(W::<T>::gr_random),
            gr_debug: cost_batched(W::<T>::gr_debug),
            gr_debug_per_byte: cost_byte_batched(W::<T>::gr_debug_per_kb),
            gr_reply_to: cost_batched(W::<T>::gr_reply_to),
            gr_signal_code: cost_batched(W::<T>::gr_signal_code),
            gr_signal_from: cost_batched(W::<T>::gr_signal_from),
            gr_reply_code: cost_batched(W::<T>::gr_reply_code),
            gr_exit: cost(W::<T>::gr_exit),
            gr_leave: cost(W::<T>::gr_leave),
            gr_wait: cost(W::<T>::gr_wait),
            gr_wait_for: cost(W::<T>::gr_wait_for),
            gr_wait_up_to: cost(W::<T>::gr_wait_up_to),
            gr_wake: cost_batched(W::<T>::gr_wake),

            gr_create_program: cost_batched(W::<T>::gr_create_program),
            gr_create_program_payload_per_byte: cost_byte_batched_args(
                W::<T>::gr_create_program_per_kb,
                1,
                0,
            ),
            gr_create_program_salt_per_byte: cost_byte_batched_args(
                W::<T>::gr_create_program_per_kb,
                0,
                1,
            ),
            gr_create_program_wgas: cost_batched(W::<T>::gr_create_program_wgas),
            gr_create_program_wgas_payload_per_byte: cost_byte_batched_args(
                W::<T>::gr_create_program_wgas_per_kb,
                1,
                0,
            ),
            gr_create_program_wgas_salt_per_byte: cost_byte_batched_args(
                W::<T>::gr_create_program_wgas_per_kb,
                0,
                1,
            ),
            _phantom: PhantomData,
        }
    }
}

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

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.
        fn to_cost_per_gear_page(w: fn(u32) -> Weight) -> Weight {
            Weight::from_parts(
                cost(w).ref_time() / (WasmPage::SIZE / GearPage::SIZE) as u64,
                0,
            )
        }

        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.
        fn host_func_access(w: fn(u32) -> Weight, syscall: fn(u32) -> Weight) -> Weight {
            let syscall_per_kb_weight = cost_batched(syscall).ref_time();
            let syscall_per_gear_page_weight =
                (syscall_per_kb_weight / KB_SIZE) * GearPage::SIZE as u64;

            let ref_time = to_cost_per_gear_page(w)
                .ref_time()
                .saturating_sub(syscall_per_gear_page_weight);

            Weight::from_parts(ref_time, 0)
        }

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

        type W<T> = <T as Config>::WeightInfo;

        Self {
            lazy_pages_signal_read: to_cost_per_gear_page(W::<T>::lazy_pages_signal_read),
            lazy_pages_signal_write: to_cost_per_gear_page(W::<T>::lazy_pages_signal_write),
            lazy_pages_signal_write_after_read: to_cost_per_gear_page(
                W::<T>::lazy_pages_signal_write_after_read,
            ),
            lazy_pages_host_func_read: host_func_access(
                W::<T>::lazy_pages_host_func_read,
                W::<T>::gr_debug_per_kb,
            ),
            lazy_pages_host_func_write: host_func_access(
                W::<T>::lazy_pages_host_func_write,
                W::<T>::gr_read_per_kb,
            ),
            lazy_pages_host_func_write_after_read: host_func_access(
                W::<T>::lazy_pages_host_func_write_after_read,
                W::<T>::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_cost_per_gear_page(W::<T>::lazy_pages_load_page_storage_data)
                .saturating_sub(to_cost_per_gear_page(W::<T>::lazy_pages_signal_read)),
            upload_page_data: cost(W::<T>::db_write_per_kb)
                .saturating_mul(KB_AMOUNT_IN_ONE_GEAR_PAGE)
                .saturating_add(T::DbWeight::get().writes(1)),
            mem_grow: cost_batched(W::<T>::mem_grow),
            mem_grow_per_page: cost_batched(W::<T>::mem_grow_per_page),
            // TODO: make it non-zero for para-chains (issue #2225)
            parachain_read_heuristic: Weight::zero(),
            _phantom: PhantomData,
        }
    }
}

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(),
        }
    }
}

impl<T: Config> Default for RentWeights<T> {
    fn default() -> Self {
        Self {
            waitlist: Weight::from_parts(CostsPerBlockOf::<T>::waitlist(), 0),
            dispatch_stash: Weight::from_parts(CostsPerBlockOf::<T>::dispatch_stash(), 0),
            reservation: Weight::from_parts(CostsPerBlockOf::<T>::reservation(), 0),
            mailbox: Weight::from_parts(CostsPerBlockOf::<T>::mailbox(), 0),
            mailbox_threshold: Weight::from_parts(T::MailboxThreshold::get(), 0),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> From<RentWeights<T>> for RentCosts {
    fn from(val: RentWeights<T>) -> Self {
        Self {
            waitlist: val.waitlist.ref_time().into(),
            dispatch_stash: val.dispatch_stash.ref_time().into(),
            reservation: val.reservation.ref_time().into(),
        }
    }
}

impl<T: Config> Default for DbWeights<T> {
    fn default() -> Self {
        type W<T> = <T as Config>::WeightInfo;
        Self {
            write: DbWeightOf::<T>::get().writes(1),
            read: DbWeightOf::<T>::get().reads(1),
            write_per_byte: cost_byte(W::<T>::db_write_per_kb),
            read_per_byte: cost_byte(W::<T>::db_read_per_kb),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> Default for InstantiationWeights<T> {
    fn default() -> Self {
        type W<T> = <T as Config>::WeightInfo;
        Self {
            code_section_per_byte: cost_byte(W::<T>::instantiate_module_code_section_per_kb),
            data_section_per_byte: cost_byte(W::<T>::instantiate_module_data_section_per_kb),
            global_section_per_byte: cost_byte(W::<T>::instantiate_module_global_section_per_kb),
            table_section_per_byte: cost_byte(W::<T>::instantiate_module_table_section_per_kb),
            element_section_per_byte: cost_byte(W::<T>::instantiate_module_element_section_per_kb),
            type_section_per_byte: cost_byte(W::<T>::instantiate_module_type_section_per_kb),
            _phantom: PhantomData,
        }
    }
}

impl<T: Config> From<InstantiationWeights<T>> for InstantiationCosts {
    fn from(val: InstantiationWeights<T>) -> Self {
        Self {
            code_section_per_byte: val.code_section_per_byte.ref_time().into(),
            data_section_per_byte: val.data_section_per_byte.ref_time().into(),
            global_section_per_byte: val.global_section_per_byte.ref_time().into(),
            table_section_per_byte: val.table_section_per_byte.ref_time().into(),
            element_section_per_byte: val.element_section_per_byte.ref_time().into(),
            type_section_per_byte: val.type_section_per_byte.ref_time().into(),
        }
    }
}

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

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
    }
}

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: self.rent_weights.clone().into(),
                mem_grow: self.memory_weights.mem_grow.ref_time().into(),
                mem_grow_per_page: self.memory_weights.mem_grow_per_page.ref_time().into(),
            },
            lazy_pages: self.memory_weights.clone().into(),
            read: self.db_weights.read.ref_time().into(),
            read_per_byte: self.db_weights.read_per_byte.ref_time().into(),
            write: self.db_weights.write.ref_time().into(),
            instrumentation: self.code_instrumentation_cost.ref_time().into(),
            instrumentation_per_byte: self.code_instrumentation_byte_cost.ref_time().into(),
            instantiation_costs: self.instantiation_weights.clone().into(),
            load_allocations_per_interval: self.load_allocations_weight.ref_time().into(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    /// 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);
    }
}