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

Commit 7c9d906

Browse files
fguthmannfannyguthmann
andauthored
Added comments to core/contract_address module (#900)
Co-authored-by: fannyguthmann <[email protected]>
1 parent 9947695 commit 7c9d906

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/core/contract_address/casm_contract_address.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use starknet_crypto::{poseidon_hash_many, FieldElement};
66

77
const CONTRACT_CLASS_VERSION: &[u8] = b"COMPILED_CLASS_V1";
88

9+
/// Return hashed entry points for a given contract class and entry point type.
910
fn get_contract_entry_points_hashed(
1011
contract_class: &CasmContractClass,
1112
entry_point_type: &EntryPointType,
@@ -38,6 +39,7 @@ fn get_contract_entry_points_hashed(
3839
Ok(poseidon_hash_many(&entry_points_flatted))
3940
}
4041

42+
/// Compute hash for the entire CASM contract class.
4143
pub fn compute_casm_class_hash(
4244
contract_class: &CasmContractClass,
4345
) -> Result<Felt252, ContractAddressError> {
@@ -80,6 +82,7 @@ pub fn compute_casm_class_hash(
8082
))
8183
}
8284

85+
/// Helper function to fetch entry points based on their type.
8386
fn get_contract_entry_points(
8487
contract_class: &CasmContractClass,
8588
entry_point_type: &EntryPointType,

src/core/contract_address/deprecated_contract_address.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{borrow::Cow, collections::BTreeMap, io};
2121
/// Instead of doing a Mask with 250 bits, we are only masking the most significant byte.
2222
pub const MASK_3: u8 = 0x03;
2323

24+
/// Returns the contract entry points.
2425
fn get_contract_entry_points(
2526
contract_class: &ContractClass,
2627
entry_point_type: &EntryPointType,
@@ -40,6 +41,7 @@ fn get_contract_entry_points(
4041
Ok(entry_points.to_owned())
4142
}
4243

44+
/// Recursively add extra spaces to Cairo named tuple representations in a JSON structure.
4345
fn add_extra_space_to_cairo_named_tuples(value: &mut serde_json::Value) {
4446
match value {
4547
serde_json::Value::Array(v) => walk_array(v),
@@ -48,12 +50,14 @@ fn add_extra_space_to_cairo_named_tuples(value: &mut serde_json::Value) {
4850
}
4951
}
5052

53+
/// Helper function to walk through a JSON array and apply extra space to cairo named tuples.
5154
fn walk_array(array: &mut [serde_json::Value]) {
5255
for v in array.iter_mut() {
5356
add_extra_space_to_cairo_named_tuples(v);
5457
}
5558
}
5659

60+
/// Helper function to walk through a JSON map and apply extra space to cairo named tuples.
5761
fn walk_map(object: &mut serde_json::Map<String, serde_json::Value>) {
5862
for (k, v) in object.iter_mut() {
5963
match v {
@@ -68,6 +72,7 @@ fn walk_map(object: &mut serde_json::Map<String, serde_json::Value>) {
6872
}
6973
}
7074

75+
/// Add extra space to named tuple type definition.
7176
fn add_extra_space_to_named_tuple_type_definition<'a>(
7277
key: &str,
7378
value: &'a str,
@@ -79,6 +84,7 @@ fn add_extra_space_to_named_tuple_type_definition<'a>(
7984
}
8085
}
8186

87+
/// Replaces ": " with " : " and " :" with " :" for Cairo-specific formatting.
8288
fn add_extra_space_before_colon(v: &str) -> String {
8389
// This is required because if we receive an already correct ` : `, we will still
8490
// "repair" it to ` : ` which we then fix at the end.
@@ -88,11 +94,13 @@ fn add_extra_space_before_colon(v: &str) -> String {
8894
struct KeccakWriter(sha3::Keccak256);
8995

9096
impl std::io::Write for KeccakWriter {
97+
/// Write data into the Keccak256 hasher.
9198
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
9299
self.0.update(buf);
93100
Ok(buf.len())
94101
}
95102

103+
/// No operation is required for flushing, as we finalize after writing.
96104
fn flush(&mut self) -> std::io::Result<()> {
97105
// noop is fine, we'll finalize after the write phase
98106
Ok(())
@@ -104,6 +112,7 @@ impl std::io::Write for KeccakWriter {
104112
struct PythonDefaultFormatter;
105113

106114
impl serde_json::ser::Formatter for PythonDefaultFormatter {
115+
/// Handles formatting for array values.
107116
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> std::io::Result<()>
108117
where
109118
W: ?Sized + std::io::Write,
@@ -115,6 +124,7 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
115124
}
116125
}
117126

127+
/// Handles formatting for object keys.
118128
fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> std::io::Result<()>
119129
where
120130
W: ?Sized + std::io::Write,
@@ -126,13 +136,15 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
126136
}
127137
}
128138

139+
/// Handles formatting for object values.
129140
fn begin_object_value<W>(&mut self, writer: &mut W) -> std::io::Result<()>
130141
where
131142
W: ?Sized + std::io::Write,
132143
{
133144
writer.write_all(b": ")
134145
}
135146

147+
/// Custom logic for writing string fragments, handling non-ASCII characters.
136148
#[inline]
137149
fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
138150
where
@@ -157,6 +169,7 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
157169

158170
#[derive(serde::Deserialize, serde::Serialize)]
159171
#[serde(deny_unknown_fields)]
172+
160173
pub struct CairoContractDefinition<'a> {
161174
/// Contract ABI, which has no schema definition.
162175
pub abi: serde_json::Value,
@@ -268,13 +281,15 @@ pub(crate) fn compute_hinted_class_hash(
268281
Ok(truncated_keccak(<[u8; 32]>::from(hash.finalize())))
269282
}
270283

284+
/// Truncate the given Keccak hash to fit within Felt252's constraints.
271285
pub(crate) fn truncated_keccak(mut plain: [u8; 32]) -> Felt252 {
272286
// python code masks with (2**250 - 1) which starts 0x03 and is followed by 31 0xff in be
273287
// truncation is needed not to overflow the field element.
274288
plain[0] &= MASK_3;
275289
Felt252::from_bytes_be(&plain)
276290
}
277291

292+
/// Returns the hashed entry points of a contract class.
278293
fn get_contract_entry_points_hashed(
279294
contract_class: &ContractClass,
280295
entry_point_type: &EntryPointType,
@@ -292,6 +307,7 @@ fn get_contract_entry_points_hashed(
292307
)?)
293308
}
294309

310+
/// Compute the hash for a deprecated contract class.
295311
pub fn compute_deprecated_class_hash(
296312
contract_class: &ContractClass,
297313
) -> Result<Felt252, ContractAddressError> {

src/core/contract_address/sierra_contract_address.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const CONTRACT_CLASS_VERSION: &[u8] = b"CONTRACT_CLASS_V0.1.0";
1212
// Version 2 functions and structs
1313
// ---------------------------------
1414

15+
/// Computes the hash of contract entry points.
1516
fn get_contract_entry_points_hashed(
1617
contract_class: &SierraContractClass,
1718
entry_point_type: &EntryPointType,
@@ -33,6 +34,7 @@ fn get_contract_entry_points_hashed(
3334
Ok(hasher.finalize())
3435
}
3536

37+
/// Computes the hash of a given Sierra contract class.
3638
pub fn compute_sierra_class_hash(
3739
contract_class: &SierraContractClass,
3840
) -> Result<Felt252, ContractAddressError> {
@@ -90,6 +92,7 @@ pub fn compute_sierra_class_hash(
9092
Ok(Felt252::from_bytes_be(&hash.to_bytes_be()))
9193
}
9294

95+
/// Returns the contract entry points.
9396
fn get_contract_entry_points(
9497
contract_class: &SierraContractClass,
9598
entry_point_type: &EntryPointType,
@@ -120,6 +123,7 @@ mod tests {
120123
use cairo_vm::felt::felt_str;
121124
use std::{fs::File, io::BufReader};
122125

126+
/// Test the correctness of the compute_sierra_class_hash function for a specific testnet contract.
123127
#[test]
124128
fn test_declare_tx_from_testnet() {
125129
let file = File::open("starknet_programs/cairo2/events.sierra").unwrap();

0 commit comments

Comments
 (0)