ceres_sandbox/
schedule.rs

1//! Schedule
2use crate::Sandbox;
3use ceres_std::Vec;
4use parity_scale_codec::{Decode, Encode};
5
6/// Describes the upper limits on various metrics.
7#[derive(Default, Clone, Encode, Decode, PartialEq, Eq)]
8pub struct Limits {
9    pub event_topics: u32,
10    pub stack_height: u32,
11    pub globals: u32,
12    pub parameters: u32,
13    pub memory_pages: u32,
14    pub table_size: u32,
15    pub br_table_size: u32,
16    pub subject_len: u32,
17    pub code_size: u32,
18}
19
20/// Describes the weight for all categories of supported wasm instructions.
21///
22/// There there is one field for each wasm instruction that describes the weight to
23/// execute one instruction of that name. There are a few execptions:
24///
25/// 1. If there is a i64 and a i32 variant of an instruction we use the weight
26///    of the former for both.
27/// 2. The following instructions are free of charge because they merely structure the
28///    wasm module and cannot be spammed without making the module invalid (and rejected):
29///    End, Unreachable, Return, Else
30/// 3. The following instructions cannot be benchmarked because they are removed by any
31///    real world execution engine as a preprocessing step and therefore don't yield a
32///    meaningful benchmark result. However, in contrast to the instructions mentioned
33///    in 2. they can be spammed. We price them with the same weight as the "default"
34///    instruction (i64.const): Block, Loop, Nop
35/// 4. We price both i64.const and drop as InstructionWeights.i64const / 2. The reason
36///    for that is that we cannot benchmark either of them on its own but we need their
37///    individual values to derive (by subtraction) the weight of all other instructions
38///    that use them as supporting instructions. Supporting means mainly pushing arguments
39///    and dropping return values in order to maintain a valid module.
40#[derive(Default, Clone, Encode, Decode, PartialEq, Eq)]
41pub struct InstructionWeights {
42    pub i64const: u32,
43    pub i64load: u32,
44    pub i64store: u32,
45    pub select: u32,
46    pub r#if: u32,
47    pub br: u32,
48    pub br_if: u32,
49    pub br_table: u32,
50    pub br_table_per_entry: u32,
51    pub call: u32,
52    pub call_indirect: u32,
53    pub call_indirect_per_param: u32,
54    pub local_get: u32,
55    pub local_set: u32,
56    pub local_tee: u32,
57    pub global_get: u32,
58    pub global_set: u32,
59    pub memory_current: u32,
60    pub memory_grow: u32,
61    pub i64clz: u32,
62    pub i64ctz: u32,
63    pub i64popcnt: u32,
64    pub i64eqz: u32,
65    pub i64extendsi32: u32,
66    pub i64extendui32: u32,
67    pub i32wrapi64: u32,
68    pub i64eq: u32,
69    pub i64ne: u32,
70    pub i64lts: u32,
71    pub i64ltu: u32,
72    pub i64gts: u32,
73    pub i64gtu: u32,
74    pub i64les: u32,
75    pub i64leu: u32,
76    pub i64ges: u32,
77    pub i64geu: u32,
78    pub i64add: u32,
79    pub i64sub: u32,
80    pub i64mul: u32,
81    pub i64divs: u32,
82    pub i64divu: u32,
83    pub i64rems: u32,
84    pub i64remu: u32,
85    pub i64and: u32,
86    pub i64or: u32,
87    pub i64xor: u32,
88    pub i64shl: u32,
89    pub i64shrs: u32,
90    pub i64shru: u32,
91    pub i64rotl: u32,
92    pub i64rotr: u32,
93}
94
95/// Definition of the cost schedule and other parameterizations for wasm vm.
96#[derive(Default, Clone, Encode, Decode, PartialEq, Eq)]
97pub struct Schedule {
98    pub version: u32,
99    pub enable_println: bool,
100    pub limits: Limits,
101}
102
103impl Sandbox {
104    pub fn schedule(&self) -> Vec<u8> {
105        self.ext.schedule.encode()
106    }
107}