-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
215 lines (189 loc) · 6.41 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
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::{AddAssign, Sub, SubAssign};
#[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(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,
}
#[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>,
}