Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit d2c0dc4

Browse files
remove unwrap, add tests with cairo2
1 parent fe0aed3 commit d2c0dc4

File tree

1 file changed

+112
-34
lines changed

1 file changed

+112
-34
lines changed

src/core/contract_address/casm_contract_address.rs

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ fn get_contract_entry_points_hashed(
2424
})?,
2525
);
2626
entry_points_flatted.push(FieldElement::from(entry_point.offset));
27-
let mut builtins_flatted = Vec::new();
28-
entry_point.builtins.iter().for_each(|builtin| {
29-
builtins_flatted.push(FieldElement::from_byte_slice_be(builtin.as_bytes()).unwrap());
30-
});
27+
let builtins_flatted = entry_point
28+
.builtins
29+
.iter()
30+
.map(|builtin| FieldElement::from_byte_slice_be(builtin.as_bytes()))
31+
.collect::<Result<Vec<FieldElement>, _>>()
32+
.map_err(|_err| {
33+
ContractAddressError::Cast("Felt252".to_string(), "FieldElement".to_string())
34+
})?;
3135
entry_points_flatted.push(poseidon_hash_many(&builtins_flatted));
3236
}
3337

@@ -101,90 +105,164 @@ mod tests {
101105
#[test]
102106
fn test_compute_casm_class_hash_contract_a() {
103107
// Open the file in read-only mode with buffer.
104-
let file = File::open("starknet_programs/cairo1/contract_a.casm").unwrap();
108+
let file;
109+
let expected_result;
110+
#[cfg(not(feature = "cairo_1_tests"))]
111+
{
112+
file = File::open("starknet_programs/cairo2/contract_a.casm").unwrap();
113+
expected_result = felt_str!(
114+
"321aadcf42b0a4ad905616598d16c42fa9b87c812dc398e49b57bf77930629f",
115+
16
116+
);
117+
}
118+
#[cfg(feature = "cairo_1_tests")]
119+
{
120+
file = File::open("starknet_programs/cairo1/contract_a.casm").unwrap();
121+
expected_result = felt_str!(
122+
"3a4f00bf75ba3b9230a94f104c7a4605a1901c4bd475beb59eeeeb7aceb9795",
123+
16
124+
);
125+
}
105126
let reader = BufReader::new(file);
106127

107-
// Read the JSON contents of the file as an instance of `User`.
128+
// Read the JSON contents of the file as an instance of `CasmContractClass`.
108129
let contract_class: CasmContractClass = serde_json::from_reader(reader).unwrap();
109130

110131
assert_eq!(
111132
compute_casm_class_hash(&contract_class).unwrap(),
112-
felt_str!(
113-
"3a4f00bf75ba3b9230a94f104c7a4605a1901c4bd475beb59eeeeb7aceb9795",
114-
16
115-
)
133+
expected_result
116134
);
117135
}
118136

119137
#[test]
120138
fn test_compute_casm_class_hash_deploy() {
121139
// Open the file in read-only mode with buffer.
122-
let file = File::open("starknet_programs/cairo1/deploy.casm").unwrap();
140+
let file;
141+
let expected_result;
142+
#[cfg(not(feature = "cairo_1_tests"))]
143+
{
144+
file = File::open("starknet_programs/cairo2/deploy.casm").unwrap();
145+
expected_result = felt_str!(
146+
"53ad3bfb13f39cf1a9940108be4f9c6a8d9cc48a59d5f9b3c73432f877f8cf0",
147+
16
148+
);
149+
}
150+
151+
#[cfg(feature = "cairo_1_tests")]
152+
{
153+
file = File::open("starknet_programs/cairo1/deploy.casm").unwrap();
154+
expected_result = felt_str!(
155+
"3bd56f1c3c1c595ac2ee6d07bdedc027d09df56235e20374649f0b3535c1f15",
156+
16
157+
);
158+
}
123159
let reader = BufReader::new(file);
124160

125-
// Read the JSON contents of the file as an instance of `User`.
161+
// Read the JSON contents of the file as an instance of `CasmContractClass`.
126162
let contract_class: CasmContractClass = serde_json::from_reader(reader).unwrap();
127163

128164
assert_eq!(
129165
compute_casm_class_hash(&contract_class).unwrap(),
130-
felt_str!(
131-
"3bd56f1c3c1c595ac2ee6d07bdedc027d09df56235e20374649f0b3535c1f15",
132-
16
133-
)
166+
expected_result
134167
);
135168
}
136169

137170
#[test]
138171
fn test_compute_casm_class_hash_fibonacci() {
139172
// Open the file in read-only mode with buffer.
140-
let file = File::open("starknet_programs/cairo1/fibonacci.casm").unwrap();
173+
let file;
174+
let expected_result;
175+
#[cfg(not(feature = "cairo_1_tests"))]
176+
{
177+
file = File::open("starknet_programs/cairo2/fibonacci.casm").unwrap();
178+
expected_result = felt_str!(
179+
"6638ce6c9bf336d1781a388668fa2206d928df5d1fa6b92e4cb41004c7e3f89",
180+
16
181+
);
182+
}
183+
184+
#[cfg(feature = "cairo_1_tests")]
185+
{
186+
file = File::open("starknet_programs/cairo1/fibonacci.casm").unwrap();
187+
expected_result = felt_str!(
188+
"44f12e6e59232e9909d7428b913b3cc8d9059458e5027740a3ccdbdc4b1ffd2",
189+
16
190+
);
191+
}
141192
let reader = BufReader::new(file);
142193

143-
// Read the JSON contents of the file as an instance of `User`.
194+
// Read the JSON contents of the file as an instance of `CasmContractClass`.
144195
let contract_class: CasmContractClass = serde_json::from_reader(reader).unwrap();
145196

146197
assert_eq!(
147198
compute_casm_class_hash(&contract_class).unwrap(),
148-
felt_str!(
149-
"44f12e6e59232e9909d7428b913b3cc8d9059458e5027740a3ccdbdc4b1ffd2",
150-
16
151-
)
199+
expected_result
152200
);
153201
}
154202

155203
#[test]
156204
fn test_compute_casm_class_hash_factorial() {
157205
// Open the file in read-only mode with buffer.
158-
let file = File::open("starknet_programs/cairo1/factorial.casm").unwrap();
206+
let file;
207+
let expected_result;
208+
#[cfg(not(feature = "cairo_1_tests"))]
209+
{
210+
file = File::open("starknet_programs/cairo2/factorial.casm").unwrap();
211+
expected_result = felt_str!(
212+
"7c48d040ceb3183837a0aff2adf33d879f790e202eb2c4b8622005c12252641",
213+
16
214+
);
215+
}
216+
217+
#[cfg(feature = "cairo_1_tests")]
218+
{
219+
file = File::open("starknet_programs/cairo1/factorial.casm").unwrap();
220+
expected_result = felt_str!(
221+
"189a9b8b852aedbb225aa28dce9cfc3133145dd623e2d2ca5e962b7d4e61e15",
222+
16
223+
);
224+
}
159225
let reader = BufReader::new(file);
160226

161-
// Read the JSON contents of the file as an instance of `User`.
227+
// Read the JSON contents of the file as an instance of `CasmContractClass`.
162228
let contract_class: CasmContractClass = serde_json::from_reader(reader).unwrap();
163229

164230
assert_eq!(
165231
compute_casm_class_hash(&contract_class).unwrap(),
166-
felt_str!(
167-
"189a9b8b852aedbb225aa28dce9cfc3133145dd623e2d2ca5e962b7d4e61e15",
168-
16
169-
)
232+
expected_result
170233
);
171234
}
172235

173236
#[test]
174237
fn test_compute_casm_class_hash_emit_event() {
175238
// Open the file in read-only mode with buffer.
176-
let file = File::open("starknet_programs/cairo1/emit_event.casm").unwrap();
239+
let file;
240+
let expected_result;
241+
#[cfg(not(feature = "cairo_1_tests"))]
242+
{
243+
file = File::open("starknet_programs/cairo2/emit_event.casm").unwrap();
244+
expected_result = felt_str!(
245+
"3010533bd60cb0e70ac1bf776e171713f0e5229a084989d3894c171c160ace2",
246+
16
247+
);
248+
}
249+
250+
#[cfg(feature = "cairo_1_tests")]
251+
{
252+
file = File::open("starknet_programs/cairo1/emit_event.casm").unwrap();
253+
expected_result = felt_str!(
254+
"3335fe731ceda1116eda8bbc2e282953ce54618309ad474189e627c59328fff",
255+
16
256+
);
257+
}
177258
let reader = BufReader::new(file);
178259

179-
// Read the JSON contents of the file as an instance of `User`.
260+
// Read the JSON contents of the file as an instance of `CasmContractClass`.
180261
let contract_class: CasmContractClass = serde_json::from_reader(reader).unwrap();
181262

182263
assert_eq!(
183264
compute_casm_class_hash(&contract_class).unwrap(),
184-
felt_str!(
185-
"3335fe731ceda1116eda8bbc2e282953ce54618309ad474189e627c59328fff",
186-
16
187-
)
265+
expected_result
188266
);
189267
}
190268
}

0 commit comments

Comments
 (0)