Skip to content

Commit

Permalink
Abstract into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Oct 23, 2024
1 parent a3a1d7a commit 5032d63
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions rpc-state-reader/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,42 +237,10 @@ impl StateReader for RpcStateReader {
fn get_compiled_contract_class(&self, class_hash: ClassHash) -> StateResult<ContractClass> {
Ok(match self.get_contract_class(&class_hash)? {
SNContractClass::Legacy(compressed_legacy_cc) => {
let as_str = utils::decode_reader(compressed_legacy_cc.program).unwrap();
let program = Program::from_bytes(as_str.as_bytes(), None).unwrap();
let entry_points_by_type = utils::map_entry_points_by_type_legacy(
compressed_legacy_cc.entry_points_by_type,
);
let inner = Arc::new(ContractClassV0Inner {
program,
entry_points_by_type,
});
ContractClass::V0(ContractClassV0(inner))
compile_legacy_cc(compressed_legacy_cc)
}
SNContractClass::Sierra(flattened_sierra_cc) => {
let middle_sierra: utils::MiddleSierraContractClass = {
let v = serde_json::to_value(flattened_sierra_cc).unwrap();
serde_json::from_value(v).unwrap()
};
let sierra_cc = cairo_lang_starknet_classes::contract_class::ContractClass {
sierra_program: middle_sierra.sierra_program,
contract_class_version: middle_sierra.contract_class_version,
entry_points_by_type: middle_sierra.entry_points_by_type,
sierra_program_debug_info: None,
abi: None,
};

if cfg!(feature = "only_casm") {
let casm_cc =
cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(sierra_cc, false, usize::MAX).unwrap();
ContractClass::V1(casm_cc.try_into().unwrap())
} else {
let program = sierra_cc.extract_sierra_program().unwrap();
let executor = utils::get_native_executor(program, class_hash);

ContractClass::V1Native(
NativeContractClassV1::new(executor, sierra_cc).unwrap(),
)
}
compile_sierra_cc(flattened_sierra_cc, class_hash)
}
})
}
Expand All @@ -282,6 +250,48 @@ impl StateReader for RpcStateReader {
}
}

fn compile_sierra_cc(
flattened_sierra_cc: starknet::core::types::FlattenedSierraClass,
class_hash: ClassHash,
) -> ContractClass {
let middle_sierra: utils::MiddleSierraContractClass = {
let v = serde_json::to_value(flattened_sierra_cc).unwrap();
serde_json::from_value(v).unwrap()
};
let sierra_cc = cairo_lang_starknet_classes::contract_class::ContractClass {
sierra_program: middle_sierra.sierra_program,
contract_class_version: middle_sierra.contract_class_version,
entry_points_by_type: middle_sierra.entry_points_by_type,
sierra_program_debug_info: None,
abi: None,
};

if cfg!(feature = "only_casm") {
let casm_cc =
cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(sierra_cc, false, usize::MAX).unwrap();
ContractClass::V1(casm_cc.try_into().unwrap())
} else {
let program = sierra_cc.extract_sierra_program().unwrap();
let executor = utils::get_native_executor(program, class_hash);

ContractClass::V1Native(NativeContractClassV1::new(executor, sierra_cc).unwrap())
}
}

fn compile_legacy_cc(
compressed_legacy_cc: starknet::core::types::CompressedLegacyContractClass,
) -> ContractClass {
let as_str = utils::decode_reader(compressed_legacy_cc.program).unwrap();
let program = Program::from_bytes(as_str.as_bytes(), None).unwrap();
let entry_points_by_type =
utils::map_entry_points_by_type_legacy(compressed_legacy_cc.entry_points_by_type);
let inner = Arc::new(ContractClassV0Inner {
program,
entry_points_by_type,
});
ContractClass::V0(ContractClassV0(inner))
}

#[cfg(test)]
mod tests {
use std::num::NonZeroU128;
Expand Down

0 comments on commit 5032d63

Please sign in to comment.