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
use datasize::DataSize;
use rand::{distributions::Standard, prelude::*, Rng};
use serde::{Deserialize, Serialize};
use casper_types::bytesrepr::{self, FromBytes, ToBytes};
use super::{
host_function_costs::HostFunctionCosts, opcode_costs::OpcodeCosts, storage_costs::StorageCosts,
};
pub const DEFAULT_WASM_MAX_MEMORY: u32 = 64;
pub const DEFAULT_MAX_STACK_HEIGHT: u32 = 188;
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Debug, DataSize)]
pub struct WasmConfig {
pub max_memory: u32,
pub max_stack_height: u32,
opcode_costs: OpcodeCosts,
storage_costs: StorageCosts,
host_function_costs: HostFunctionCosts,
}
impl WasmConfig {
pub const fn new(
max_memory: u32,
max_stack_height: u32,
opcode_costs: OpcodeCosts,
storage_costs: StorageCosts,
host_function_costs: HostFunctionCosts,
) -> Self {
Self {
max_memory,
max_stack_height,
opcode_costs,
storage_costs,
host_function_costs,
}
}
pub fn opcode_costs(&self) -> OpcodeCosts {
self.opcode_costs
}
pub fn storage_costs(&self) -> StorageCosts {
self.storage_costs
}
pub fn take_host_function_costs(self) -> HostFunctionCosts {
self.host_function_costs
}
}
impl Default for WasmConfig {
fn default() -> Self {
Self {
max_memory: DEFAULT_WASM_MAX_MEMORY,
max_stack_height: DEFAULT_MAX_STACK_HEIGHT,
opcode_costs: OpcodeCosts::default(),
storage_costs: StorageCosts::default(),
host_function_costs: HostFunctionCosts::default(),
}
}
}
impl ToBytes for WasmConfig {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let mut ret = bytesrepr::unchecked_allocate_buffer(self);
ret.append(&mut self.max_memory.to_bytes()?);
ret.append(&mut self.max_stack_height.to_bytes()?);
ret.append(&mut self.opcode_costs.to_bytes()?);
ret.append(&mut self.storage_costs.to_bytes()?);
ret.append(&mut self.host_function_costs.to_bytes()?);
Ok(ret)
}
fn serialized_length(&self) -> usize {
self.max_memory.serialized_length()
+ self.max_stack_height.serialized_length()
+ self.opcode_costs.serialized_length()
+ self.storage_costs.serialized_length()
+ self.host_function_costs.serialized_length()
}
}
impl FromBytes for WasmConfig {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (max_memory, rem) = FromBytes::from_bytes(bytes)?;
let (max_stack_height, rem) = FromBytes::from_bytes(rem)?;
let (opcode_costs, rem) = FromBytes::from_bytes(rem)?;
let (storage_costs, rem) = FromBytes::from_bytes(rem)?;
let (host_function_costs, rem) = FromBytes::from_bytes(rem)?;
Ok((
WasmConfig {
max_memory,
max_stack_height,
opcode_costs,
storage_costs,
host_function_costs,
},
rem,
))
}
}
impl Distribution<WasmConfig> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> WasmConfig {
WasmConfig {
max_memory: rng.gen(),
max_stack_height: rng.gen(),
opcode_costs: rng.gen(),
storage_costs: rng.gen(),
host_function_costs: rng.gen(),
}
}
}
#[doc(hidden)]
#[cfg(any(feature = "gens", test))]
pub mod gens {
use proptest::{num, prop_compose};
use super::WasmConfig;
use crate::shared::{
host_function_costs::gens::host_function_costs_arb, opcode_costs::gens::opcode_costs_arb,
storage_costs::gens::storage_costs_arb,
};
prop_compose! {
pub fn wasm_config_arb() (
max_memory in num::u32::ANY,
max_stack_height in num::u32::ANY,
opcode_costs in opcode_costs_arb(),
storage_costs in storage_costs_arb(),
host_function_costs in host_function_costs_arb(),
) -> WasmConfig {
WasmConfig {
max_memory,
max_stack_height,
opcode_costs,
storage_costs,
host_function_costs,
}
}
}
}