Skip to content

Commit

Permalink
refactor(starknet_api): add contract class version to sierra contract
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Nov 26, 2024
1 parent fcf5cce commit 2295fe7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/papyrus_protobuf/src/converters/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ impl TryFrom<protobuf::Cairo1Class> for state::SierraContractClass {
let sierra_program =
value.program.into_iter().map(Felt::try_from).collect::<Result<Vec<_>, _>>()?;

let contract_class_version = value.contract_class_version;

let mut entry_points_by_type = HashMap::new();
let entry_points =
value.entry_points.clone().ok_or(ProtobufConversionError::MissingField {
Expand Down Expand Up @@ -243,7 +245,12 @@ impl TryFrom<protobuf::Cairo1Class> for state::SierraContractClass {
);
}

Ok(state::SierraContractClass { sierra_program, entry_points_by_type, abi })
Ok(state::SierraContractClass {
sierra_program,
entry_points_by_type,
abi,
contract_class_version,
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ impl StorageSerde for TransactionOffsetInBlock {
impl StorageSerde for SierraContractClass {
fn serialize_into(&self, res: &mut impl std::io::Write) -> Result<(), StorageSerdeError> {
serialize_and_compress(&self.sierra_program)?.serialize_into(res)?;
self.contract_class_version.serialize_into(res)?;
self.entry_points_by_type.serialize_into(res)?;
serialize_and_compress(&self.abi)?.serialize_into(res)?;
Ok(())
Expand All @@ -979,6 +980,7 @@ impl StorageSerde for SierraContractClass {
sierra_program: Vec::<Felt>::deserialize_from(
&mut decompress_from_reader(bytes)?.as_slice(),
)?,
contract_class_version: String::deserialize_from(bytes)?,
entry_points_by_type: HashMap::<EntryPointType, Vec<EntryPoint>>::deserialize_from(
bytes,
)?,
Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_storage/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{open_storage, StorageConfig, StorageError, StorageReader, StorageRes
#[derive(Serialize)]
struct DumpDeclaredClass {
class_hash: ClassHash,
contract_class_version: String,
compiled_class_hash: CompiledClassHash,
sierra_program: Vec<Felt>,
entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
Expand Down Expand Up @@ -75,6 +76,7 @@ fn dump_declared_classes_table_by_block_range_internal(
&mut writer,
&DumpDeclaredClass {
class_hash: *class_hash,
contract_class_version: contract_class.contract_class_version.clone(),
compiled_class_hash: *compiled_class_hash,
sierra_program: contract_class.sierra_program.clone(),
entry_points_by_type: contract_class.entry_points_by_type.clone(),
Expand Down
3 changes: 3 additions & 0 deletions crates/papyrus_storage/src/utils_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn test_dump_declared_classes() {
declared_classes.push((
ClassHash(i_felt),
SierraContractClass {
contract_class_version: "0.1.0".to_string(),
sierra_program: vec![i_felt, i_felt],
entry_points_by_type: HashMap::new(),
abi: "".to_string(),
Expand Down Expand Up @@ -65,12 +66,14 @@ fn test_dump_declared_classes() {
DumpDeclaredClass {
class_hash: declared_classes[2].0,
compiled_class_hash,
contract_class_version: declared_classes[2].1.contract_class_version.clone(),
sierra_program: declared_classes[2].1.sierra_program.clone(),
entry_points_by_type: declared_classes[2].1.entry_points_by_type.clone(),
},
DumpDeclaredClass {
class_hash: declared_classes[3].0,
compiled_class_hash,
contract_class_version: declared_classes[3].1.contract_class_version.clone(),
sierra_program: declared_classes[3].1.sierra_program.clone(),
entry_points_by_type: declared_classes[3].1.entry_points_by_type.clone(),
},
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ auto_impl_get_test_instance! {
pub sierra_program: Vec<Felt>,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub abi: String,
pub contract_class_version: String,
}
pub struct DeprecatedContractClass {
pub abi: Option<Vec<ContractClassAbiEntry>>,
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl_from_through_intermediate!(u128, StorageKey, u8, u16, u32, u64);
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct SierraContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub abi: String,
}
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_client/src/reader/objects/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct ContractClass {
impl From<ContractClass> for starknet_api::state::SierraContractClass {
fn from(class: ContractClass) -> Self {
Self {
contract_class_version: class.contract_class_version,
sierra_program: class.sierra_program,
entry_points_by_type: class.entry_points_by_type,
abi: class.abi,
Expand Down

0 comments on commit 2295fe7

Please sign in to comment.