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 27, 2024
1 parent 13693b0 commit 15764ce
Showing 7 changed files with 19 additions and 10 deletions.
19 changes: 9 additions & 10 deletions crates/papyrus_protobuf/src/converters/class.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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,
})
}
}

@@ -282,15 +289,7 @@ impl From<state::SierraContractClass> for protobuf::Cairo1Class {
.collect(),
});

let contract_class_version = format!(
"sierra-v{}.{}.{} cairo-v{}.{}.{}",
value.sierra_program[0],
value.sierra_program[1],
value.sierra_program[2],
value.sierra_program[3],
value.sierra_program[4],
value.sierra_program[5]
);
let contract_class_version = value.contract_class_version;

protobuf::Cairo1Class { abi, program, entry_points, contract_class_version }
}
2 changes: 2 additions & 0 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
@@ -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(())
@@ -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,
)?,
2 changes: 2 additions & 0 deletions crates/papyrus_storage/src/utils.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ struct DumpDeclaredClass {
class_hash: ClassHash,
compiled_class_hash: CompiledClassHash,
sierra_program: Vec<Felt>,
contract_class_version: String,
entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
}

@@ -77,6 +78,7 @@ fn dump_declared_classes_table_by_block_range_internal(
class_hash: *class_hash,
compiled_class_hash: *compiled_class_hash,
sierra_program: contract_class.sierra_program.clone(),
contract_class_version: contract_class.contract_class_version.clone(),
entry_points_by_type: contract_class.entry_points_by_type.clone(),
},
)?;
3 changes: 3 additions & 0 deletions crates/papyrus_storage/src/utils_test.rs
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ fn test_dump_declared_classes() {
ClassHash(i_felt),
SierraContractClass {
sierra_program: vec![i_felt, i_felt],
contract_class_version: "0.1.0".to_string(),
entry_points_by_type: HashMap::new(),
abi: "".to_string(),
},
@@ -66,12 +67,14 @@ fn test_dump_declared_classes() {
class_hash: declared_classes[2].0,
compiled_class_hash,
sierra_program: declared_classes[2].1.sierra_program.clone(),
contract_class_version: declared_classes[2].1.contract_class_version.clone(),
entry_points_by_type: declared_classes[2].1.entry_points_by_type.clone(),
},
DumpDeclaredClass {
class_hash: declared_classes[3].0,
compiled_class_hash,
sierra_program: declared_classes[3].1.sierra_program.clone(),
contract_class_version: declared_classes[3].1.contract_class_version.clone(),
entry_points_by_type: declared_classes[3].1.entry_points_by_type.clone(),
},
];
1 change: 1 addition & 0 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -498,6 +498,7 @@ auto_impl_get_test_instance! {
pub struct ContractAddressSalt(pub StarkHash);
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,
}
1 change: 1 addition & 0 deletions crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
@@ -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,
}
1 change: 1 addition & 0 deletions crates/starknet_client/src/reader/objects/state.rs
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ impl From<ContractClass> for starknet_api::state::SierraContractClass {
fn from(class: ContractClass) -> Self {
Self {
sierra_program: class.sierra_program,
contract_class_version: class.contract_class_version,
entry_points_by_type: class.entry_points_by_type,
abi: class.abi,
}

0 comments on commit 15764ce

Please sign in to comment.