starknet_in_rust 0.4.0

A Rust implementation of Starknet execution logic
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::core::contract_address::compute_hinted_class_hash;
use crate::services::api::contract_class_errors::ContractClassError;
use cairo_vm::felt::{Felt252, PRIME_STR};
use cairo_vm::serde::deserialize_program::{
    deserialize_array_of_bigint_hex, Attribute, BuiltinName, HintParams, Identifier,
    ReferenceManager,
};
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::types::{errors::program_errors::ProgramError, program::Program};
use core::str::FromStr;
use getset::{CopyGetters, Getters};
use serde_json::Value;
use starknet_api::deprecated_contract_class::{ContractClassAbiEntry, EntryPoint};
use std::collections::HashMap;
use std::path::Path;

pub type AbiType = Vec<ContractClassAbiEntry>;

// -------------------------------
//       Entry Point types
// -------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EntryPointType {
    External,
    L1Handler,
    Constructor,
}

#[derive(Clone, CopyGetters, Debug, Default, Eq, Getters, Hash, PartialEq)]
pub struct ContractEntryPoint {
    #[getset(get = "pub")]
    selector: Felt252,
    #[getset(get_copy = "pub")]
    offset: usize,
}

impl ContractEntryPoint {
    pub const fn new(selector: Felt252, offset: usize) -> ContractEntryPoint {
        ContractEntryPoint { selector, offset }
    }
}

// -------------------------------
//         From traits
// -------------------------------

impl From<&ContractEntryPoint> for Vec<MaybeRelocatable> {
    fn from(entry_point: &ContractEntryPoint) -> Self {
        vec![
            MaybeRelocatable::from(entry_point.selector.clone()),
            MaybeRelocatable::from(entry_point.offset),
        ]
    }
}

impl From<starknet_api::deprecated_contract_class::EntryPointType> for EntryPointType {
    fn from(entry_type: starknet_api::deprecated_contract_class::EntryPointType) -> Self {
        type ApiEPT = starknet_api::deprecated_contract_class::EntryPointType;
        type StarknetEPT = EntryPointType;

        match entry_type {
            ApiEPT::Constructor => StarknetEPT::Constructor,
            ApiEPT::External => StarknetEPT::External,
            ApiEPT::L1Handler => StarknetEPT::L1Handler,
        }
    }
}

// -------------------------------
//         Contract Class
// -------------------------------

#[derive(Clone, Debug, Eq, Getters, PartialEq)]
pub struct ContractClass {
    #[getset(get = "pub")]
    pub(crate) program: Program,
    #[getset(get = "pub")]
    pub(crate) hinted_class_hash: Felt252,
    #[getset(get = "pub")]
    pub(crate) entry_points_by_type: HashMap<EntryPointType, Vec<ContractEntryPoint>>,
    #[getset(get = "pub")]
    pub(crate) abi: Option<AbiType>,
}

impl ContractClass {
    pub fn new(
        program_json: Value,
        program: Program,
        entry_points_by_type: HashMap<EntryPointType, Vec<ContractEntryPoint>>,
        abi: Option<AbiType>,
    ) -> Result<Self, ContractClassError> {
        for entry_points in entry_points_by_type.values() {
            for i in 1..entry_points.len() {
                if entry_points[i - 1].selector() > entry_points[i].selector() {
                    return Err(ContractClassError::EntrypointError(entry_points.clone()));
                }
            }
        }
        let hinted_class_hash = compute_hinted_class_hash(&program_json).unwrap();
        Ok(ContractClass {
            hinted_class_hash,
            program,
            entry_points_by_type,
            abi,
        })
    }

    pub fn new_with_hinted_class_hash(
        hinted_class_hash: Felt252,
        program: Program,
        entry_points_by_type: HashMap<EntryPointType, Vec<ContractEntryPoint>>,
        abi: Option<AbiType>,
    ) -> Result<Self, ContractClassError> {
        for entry_points in entry_points_by_type.values() {
            for i in 1..entry_points.len() {
                if entry_points[i - 1].selector() > entry_points[i].selector() {
                    return Err(ContractClassError::EntrypointError(entry_points.clone()));
                }
            }
        }

        Ok(ContractClass {
            hinted_class_hash,
            program,
            entry_points_by_type,
            abi,
        })
    }

    /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON and a class hash.
    ///
    /// This constructor avoids the need to recompute the class hash from the program JSON,
    /// but it does not verify that the given class hash is correct.
    ///
    /// This could return a wrong [`ContractClass`] if the given class hash is wrong.
    ///
    /// # Parameters
    /// - `program_json`: The JSON of the compiled Cairo 0 program.
    /// - `hinted_class_hash`: The class hash of the program.
    ///
    /// # Returns
    /// A [`ContractClass`] parsed from the given JSON and class hash.
    pub fn from_program_json_and_class_hash(
        program_json: &str,
        hinted_class_hash: Felt252,
    ) -> Result<Self, ContractClassError> {
        let contract_class: starknet_api::deprecated_contract_class::ContractClass =
            serde_json::from_str(program_json).map_err(|_| ContractClassError::ParseError)?;
        let program = to_cairo_runner_program(contract_class.program)
            .map_err(|e| ContractClassError::ProgramError(e.to_string()))?;
        let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
        Ok(ContractClass {
            hinted_class_hash,
            program,
            entry_points_by_type,
            abi: contract_class.abi,
        })
    }

    /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON
    /// at the given file path.
    pub fn from_path<F>(path: F) -> Result<Self, ProgramError>
    where
        F: AsRef<Path>,
    {
        Self::from_str(std::fs::read_to_string(path)?.as_str())
    }

    /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON
    /// given by a reader.
    pub fn from_reader<R>(mut reader: R) -> Result<Self, ProgramError>
    where
        R: std::io::Read,
    {
        let mut s = String::new();
        reader.read_to_string(&mut s)?;
        Self::from_str(s.as_str())
    }
}

// -------------------------------
//         From traits
// -------------------------------

impl FromStr for ContractClass {
    type Err = ProgramError;

    /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON.
    fn from_str(program_json: &str) -> Result<Self, ProgramError> {
        let contract_class: starknet_api::deprecated_contract_class::ContractClass =
            serde_json::from_str(program_json)?;
        let program = to_cairo_runner_program(contract_class.program)?;
        let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
        let hinted_class_hash =
            compute_hinted_class_hash(&serde_json::from_str(program_json)?).unwrap();
        Ok(ContractClass {
            hinted_class_hash,
            program,
            entry_points_by_type,
            abi: contract_class.abi,
        })
    }
}

// -------------------
//  Helper Functions
// -------------------

pub(crate) fn convert_entry_points(
    entry_points: HashMap<starknet_api::deprecated_contract_class::EntryPointType, Vec<EntryPoint>>,
) -> HashMap<EntryPointType, Vec<ContractEntryPoint>> {
    let mut converted_entries: HashMap<EntryPointType, Vec<ContractEntryPoint>> = HashMap::new();
    for (entry_type, entry_points) in entry_points {
        let en_type = entry_type.into();

        let contracts_entry_points = entry_points
            .into_iter()
            .map(|e| {
                let selector = Felt252::from_bytes_be(e.selector.0.bytes());
                let offset = e.offset.0;
                ContractEntryPoint::new(selector, offset)
            })
            .collect::<Vec<ContractEntryPoint>>();

        converted_entries.insert(en_type, contracts_entry_points);
    }

    converted_entries
}

pub(crate) fn to_cairo_runner_program(
    program: starknet_api::deprecated_contract_class::Program,
) -> Result<Program, ProgramError> {
    let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?;

    if program.prime != *PRIME_STR {
        return Err(ProgramError::PrimeDiffers(program.prime.to_string()));
    };

    let mut error_message_attributes =
        serde_json::from_value::<Vec<Attribute>>(program.attributes).unwrap_or_default();
    error_message_attributes.retain(|attr| attr.name == "error_message");

    let program = Program::new(
        serde_json::from_value::<Vec<BuiltinName>>(program.builtins)?,
        deserialize_array_of_bigint_hex(program.data)?,
        None,
        serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?,
        serde_json::from_value::<ReferenceManager>(program.reference_manager)?,
        identifiers,
        error_message_attributes,
        None,
    )?;

    Ok(program)
}

#[cfg(test)]
mod tests {
    use crate::core::contract_address::compute_deprecated_class_hash;

    use super::*;
    use cairo_vm::{
        felt::{felt_str, PRIME_STR},
        serde::deserialize_program::BuiltinName,
    };
    use starknet_api::deprecated_contract_class::{FunctionAbiEntry, TypedParameter};

    #[test]
    fn deserialize_contract_class() {
        // This specific contract compiles with --no_debug_info
        let contract_class = ContractClass::from_path("starknet_programs/AccountPreset.json")
            .expect("should be able to read file");

        // We check only some of the attributes. Ideally we would serialize
        // and compare with original
        // TODO: add abi.
        // assert_eq!(contract_class.abi(), &None);
        assert_eq!(
            contract_class
                .program()
                .iter_builtins()
                .cloned()
                .collect::<Vec<BuiltinName>>(),
            [
                BuiltinName::pedersen,
                BuiltinName::range_check,
                BuiltinName::ecdsa,
                BuiltinName::bitwise
            ]
        );
        assert_eq!(contract_class.program().prime(), PRIME_STR);
        assert_eq!(
            contract_class
                .entry_points_by_type()
                .get(&EntryPointType::L1Handler)
                .unwrap(),
            &vec![]
        );
        assert_eq!(
            contract_class
                .entry_points_by_type()
                .get(&EntryPointType::Constructor)
                .unwrap(),
            &vec![ContractEntryPoint::new(
                felt_str!(
                    "1159040026212278395030414237414753050475174923702621880048416706425641521556"
                ),
                366
            )]
        );
    }

    #[test]
    fn test_compute_class_hash_0x4479c3b883b34f1eafa5065418225d78a11ee7957c371e1b285e4b77afc6dad_try_from(
    ) {
        let contract_class = ContractClass::from_path("starknet_programs/raw_contract_classes/0x4479c3b883b34f1eafa5065418225d78a11ee7957c371e1b285e4b77afc6dad.json").expect("should be able to read file");

        assert_eq!(
            compute_deprecated_class_hash(&contract_class).unwrap(),
            felt_str!(
                "4479c3b883b34f1eafa5065418225d78a11ee7957c371e1b285e4b77afc6dad",
                16
            )
        );
    }

    #[test]
    fn parse_abi_is_correct() {
        // This specific contract compiles with --no_debug_info
        let res = ContractClass::from_path("starknet_programs/fibonacci.json");
        let contract_class = res.expect("should be able to read file");
        let expected_abi = Some(vec![ContractClassAbiEntry::Function(FunctionAbiEntry {
            name: "fib".to_string(),
            inputs: vec![
                TypedParameter {
                    name: "first_element".to_string(),
                    r#type: "felt".to_string(),
                },
                TypedParameter {
                    name: "second_element".to_string(),
                    r#type: "felt".to_string(),
                },
                TypedParameter {
                    name: "n".to_string(),
                    r#type: "felt".to_string(),
                },
            ],
            outputs: vec![TypedParameter {
                name: "res".to_string(),
                r#type: "felt".to_string(),
            }],
            state_mutability: None,
        })]);
        assert_eq!(contract_class.abi, expected_abi);
    }

    #[test]
    fn parse_without_debug_info() {
        // This specific contract compiles with --no_debug_info
        let res = ContractClass::from_path("starknet_programs/AccountPreset.json");

        let contract_class = res.expect("should be able to read file");

        let program_builtins: Vec<BuiltinName> =
            contract_class.program.iter_builtins().cloned().collect();
        assert_eq!(
            program_builtins,
            vec![
                BuiltinName::pedersen,
                BuiltinName::range_check,
                BuiltinName::ecdsa,
                BuiltinName::bitwise
            ]
        );
        assert_eq!(
            contract_class
                .entry_points_by_type
                .get(&EntryPointType::L1Handler)
                .unwrap(),
            &vec![]
        );
        assert_eq!(
            contract_class
                .entry_points_by_type
                .get(&EntryPointType::Constructor)
                .unwrap(),
            &vec![ContractEntryPoint {
                selector: felt_str!(
                    "1159040026212278395030414237414753050475174923702621880048416706425641521556"
                ),
                offset: 366
            }]
        );
    }

    #[test]
    fn parse_without_program_attributes() {
        // This specific contract was extracted from: https://testnet.starkscan.co/class/0x068dd0dd8a54ebdaa10563fbe193e6be1e0f7c423c0c3ce1e91c0b682a86b5f9
        let res = ContractClass::from_path(
            "starknet_programs/raw_contract_classes/program_without_attributes.json",
        );

        res.expect("should be able to read file");
    }

    #[test]
    fn parse_without_program_attributes_2() {
        // This specific contract was extracted from: https://testnet.starkscan.co/class/0x071b7f73b5e2b4f81f7cf01d4d1569ccba2921b3fa3170cf11cff3720dfe918e
        let res = ContractClass::from_path(
            "starknet_programs/raw_contract_classes/program_without_attributes_2.json",
        );

        res.expect("should be able to read file");
    }

    #[test]
    fn test_from_program_json_and_class_hash_should_equal_from_path() {
        let program_json = include_str!("../../../../starknet_programs/fibonacci.json");
        let contract_class_from_path = ContractClass::from_path("starknet_programs/fibonacci.json")
            .expect("should be able to read file");

        let contract_class_from_program_json_and_class_hash =
            ContractClass::from_program_json_and_class_hash(
                program_json,
                contract_class_from_path.hinted_class_hash.clone(),
            )
            .expect("should be able to read file");
        assert_eq!(
            contract_class_from_path,
            contract_class_from_program_json_and_class_hash
        );
    }
}