-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
304 lines (274 loc) · 11.1 KB
/
lib.rs
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
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::{AddAssign, Sub, SubAssign};
use std::str::FromStr;
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct ClassHash(pub String);
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct ContractAddress(pub String);
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct EntryPointSelector(pub String);
/// Tree structure representing trace of a call.
/// This struct should be serialized and used as an input to cairo-profiler.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallTrace {
pub entry_point: CallEntryPoint,
#[serde(rename = "used_execution_resources")]
pub cumulative_resources: ExecutionResources,
pub used_l1_resources: L1Resources,
pub nested_calls: Vec<CallTraceNode>,
pub cairo_execution_info: Option<CairoExecutionInfo>,
}
/// Struct needed for function level profiling.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CairoExecutionInfo {
/// Path to a file with serialized `ContractClass` or `VersionedProgram`.
pub source_sierra_path: Utf8PathBuf,
pub casm_level_info: CasmLevelInfo,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CasmLevelInfo {
pub run_with_call_header: bool,
pub vm_trace: Vec<TraceEntry>,
}
/// Enum representing node of a trace of a call.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CallTraceNode {
EntryPointCall(Box<CallTrace>),
DeployWithoutConstructor,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceEntry {
pub pc: usize,
pub ap: usize,
pub fp: usize,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct ExecutionResources {
pub vm_resources: VmExecutionResources,
pub syscall_counter: SyscallCounter,
}
impl AddAssign<&ExecutionResources> for ExecutionResources {
fn add_assign(&mut self, rhs: &ExecutionResources) {
self.vm_resources += &rhs.vm_resources;
for (syscall, count) in &rhs.syscall_counter {
*self.syscall_counter.entry(*syscall).or_insert(0) += count;
}
}
}
impl Sub<&ExecutionResources> for &ExecutionResources {
type Output = ExecutionResources;
fn sub(self, rhs: &ExecutionResources) -> Self::Output {
let mut result = self.clone();
result.vm_resources -= &rhs.vm_resources;
for (syscall, count) in &rhs.syscall_counter {
*result.syscall_counter.entry(*syscall).or_insert(0) -= count;
}
result
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct VmExecutionResources {
pub n_steps: usize,
pub n_memory_holes: usize,
pub builtin_instance_counter: HashMap<String, usize>,
}
impl AddAssign<&VmExecutionResources> for VmExecutionResources {
fn add_assign(&mut self, rhs: &VmExecutionResources) {
self.n_steps += rhs.n_steps;
self.n_memory_holes += rhs.n_memory_holes;
for (k, v) in &rhs.builtin_instance_counter {
*self.builtin_instance_counter.entry(k.clone()).or_insert(0) += v;
}
}
}
impl SubAssign<&VmExecutionResources> for VmExecutionResources {
fn sub_assign(&mut self, rhs: &VmExecutionResources) {
self.n_steps -= rhs.n_steps;
self.n_memory_holes -= rhs.n_memory_holes;
for (k, v) in &rhs.builtin_instance_counter {
let entry = self.builtin_instance_counter.entry(k.clone()).or_insert(0);
*entry = (*entry).saturating_sub(*v);
}
}
}
type SyscallCounter = HashMap<DeprecatedSyscallSelector, usize>;
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
pub enum DeprecatedSyscallSelector {
CallContract,
DelegateCall,
DelegateL1Handler,
Deploy,
EmitEvent,
GetBlockHash,
GetBlockNumber,
GetBlockTimestamp,
GetCallerAddress,
GetContractAddress,
GetExecutionInfo,
GetSequencerAddress,
GetTxInfo,
GetTxSignature,
Keccak,
LibraryCall,
LibraryCallL1Handler,
ReplaceClass,
Secp256k1Add,
Secp256k1GetPointFromX,
Secp256k1GetXy,
Secp256k1Mul,
Secp256k1New,
Secp256r1Add,
Secp256r1GetPointFromX,
Secp256r1GetXy,
Secp256r1Mul,
Secp256r1New,
SendMessageToL1,
StorageRead,
StorageWrite,
Sha256ProcessBlock,
}
impl DeprecatedSyscallSelector {
#[must_use]
pub fn all() -> &'static [Self] {
&[
DeprecatedSyscallSelector::CallContract,
DeprecatedSyscallSelector::DelegateCall,
DeprecatedSyscallSelector::DelegateL1Handler,
DeprecatedSyscallSelector::Deploy,
DeprecatedSyscallSelector::EmitEvent,
DeprecatedSyscallSelector::GetBlockHash,
DeprecatedSyscallSelector::GetBlockNumber,
DeprecatedSyscallSelector::GetBlockTimestamp,
DeprecatedSyscallSelector::GetCallerAddress,
DeprecatedSyscallSelector::GetContractAddress,
DeprecatedSyscallSelector::GetExecutionInfo,
DeprecatedSyscallSelector::GetSequencerAddress,
DeprecatedSyscallSelector::GetTxInfo,
DeprecatedSyscallSelector::GetTxSignature,
DeprecatedSyscallSelector::Keccak,
DeprecatedSyscallSelector::LibraryCall,
DeprecatedSyscallSelector::LibraryCallL1Handler,
DeprecatedSyscallSelector::ReplaceClass,
DeprecatedSyscallSelector::Secp256k1Add,
DeprecatedSyscallSelector::Secp256k1GetPointFromX,
DeprecatedSyscallSelector::Secp256k1GetXy,
DeprecatedSyscallSelector::Secp256k1Mul,
DeprecatedSyscallSelector::Secp256k1New,
DeprecatedSyscallSelector::Secp256r1Add,
DeprecatedSyscallSelector::Secp256r1GetPointFromX,
DeprecatedSyscallSelector::Secp256r1GetXy,
DeprecatedSyscallSelector::Secp256r1Mul,
DeprecatedSyscallSelector::Secp256r1New,
DeprecatedSyscallSelector::SendMessageToL1,
DeprecatedSyscallSelector::StorageRead,
DeprecatedSyscallSelector::StorageWrite,
DeprecatedSyscallSelector::Sha256ProcessBlock,
]
}
}
impl From<DeprecatedSyscallSelector> for String {
fn from(selector: DeprecatedSyscallSelector) -> Self {
format!("{selector:?}")
}
}
impl FromStr for DeprecatedSyscallSelector {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"CallContract" => Ok(DeprecatedSyscallSelector::CallContract),
"DelegateCall" => Ok(DeprecatedSyscallSelector::DelegateCall),
"DelegateL1Handler" => Ok(DeprecatedSyscallSelector::DelegateL1Handler),
"Deploy" => Ok(DeprecatedSyscallSelector::Deploy),
"EmitEvent" => Ok(DeprecatedSyscallSelector::EmitEvent),
"GetBlockHash" => Ok(DeprecatedSyscallSelector::GetBlockHash),
"GetBlockNumber" => Ok(DeprecatedSyscallSelector::GetBlockNumber),
"GetBlockTimestamp" => Ok(DeprecatedSyscallSelector::GetBlockTimestamp),
"GetCallerAddress" => Ok(DeprecatedSyscallSelector::GetCallerAddress),
"GetContractAddress" => Ok(DeprecatedSyscallSelector::GetContractAddress),
"GetExecutionInfo" => Ok(DeprecatedSyscallSelector::GetExecutionInfo),
"GetSequencerAddress" => Ok(DeprecatedSyscallSelector::GetSequencerAddress),
"GetTxInfo" => Ok(DeprecatedSyscallSelector::GetTxInfo),
"GetTxSignature" => Ok(DeprecatedSyscallSelector::GetTxSignature),
"Keccak" => Ok(DeprecatedSyscallSelector::Keccak),
"LibraryCall" => Ok(DeprecatedSyscallSelector::LibraryCall),
"LibraryCallL1Handler" => Ok(DeprecatedSyscallSelector::LibraryCallL1Handler),
"ReplaceClass" => Ok(DeprecatedSyscallSelector::ReplaceClass),
"Secp256k1Add" => Ok(DeprecatedSyscallSelector::Secp256k1Add),
"Secp256k1GetPointFromX" => Ok(DeprecatedSyscallSelector::Secp256k1GetPointFromX),
"Secp256k1GetXy" => Ok(DeprecatedSyscallSelector::Secp256k1GetXy),
"Secp256k1Mul" => Ok(DeprecatedSyscallSelector::Secp256k1Mul),
"Secp256k1New" => Ok(DeprecatedSyscallSelector::Secp256k1New),
"Secp256r1Add" => Ok(DeprecatedSyscallSelector::Secp256r1Add),
"Secp256r1GetPointFromX" => Ok(DeprecatedSyscallSelector::Secp256r1GetPointFromX),
"Secp256r1GetXy" => Ok(DeprecatedSyscallSelector::Secp256r1GetXy),
"Secp256r1Mul" => Ok(DeprecatedSyscallSelector::Secp256r1Mul),
"Secp256r1New" => Ok(DeprecatedSyscallSelector::Secp256r1New),
"SendMessageToL1" => Ok(DeprecatedSyscallSelector::SendMessageToL1),
"StorageRead" => Ok(DeprecatedSyscallSelector::StorageRead),
"StorageWrite" => Ok(DeprecatedSyscallSelector::StorageWrite),
"Sha256ProcessBlock" => Ok(DeprecatedSyscallSelector::Sha256ProcessBlock),
_ => Err(anyhow::anyhow!("Invalid DeprecatedSyscallSelector: {}", s)),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct CallEntryPoint {
pub class_hash: Option<ClassHash>,
pub entry_point_type: EntryPointType,
pub entry_point_selector: EntryPointSelector,
pub contract_address: ContractAddress,
pub call_type: CallType,
/// Contract name to display instead of contract address
pub contract_name: Option<String>,
/// Function name to display instead of entry point selector
pub function_name: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub enum CallType {
#[default]
Call = 0,
Delegate = 1,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub enum EntryPointType {
#[serde(rename = "CONSTRUCTOR")]
Constructor,
#[serde(rename = "EXTERNAL")]
#[default]
External,
#[serde(rename = "L1_HANDLER")]
L1Handler,
}
impl ExecutionResources {
#[must_use]
pub fn gt_eq_than(&self, other: &ExecutionResources) -> bool {
if self.vm_resources.n_steps < other.vm_resources.n_steps
|| self.vm_resources.n_memory_holes < other.vm_resources.n_memory_holes
{
return false;
}
let self_builtin_counter = &self.vm_resources.builtin_instance_counter;
let other_builtin_counter = &other.vm_resources.builtin_instance_counter;
for (builtin, other_count) in other_builtin_counter {
let self_count = self_builtin_counter.get(builtin).unwrap_or(&0);
if self_count < other_count {
return false;
}
}
let self_builtin_counter = &self.syscall_counter;
let other_builtin_counter = &other.syscall_counter;
for (syscall, other_count) in other_builtin_counter {
let self_count = self_builtin_counter.get(syscall).unwrap_or(&0);
if self_count < other_count {
return false;
}
}
true
}
}
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct L1Resources {
pub l2_l1_message_sizes: Vec<usize>,
}