Skip to content

Commit

Permalink
refactor(papyrus_rpc): get compiled class
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 10, 2024
1 parent a4c372e commit 2f6028a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
23 changes: 17 additions & 6 deletions crates/papyrus_rpc/src/v0_8/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use papyrus_storage::db::{TransactionKind, RO};
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::{StorageError, StorageReader, StorageTxn};
use starknet_api::block::{BlockHash, BlockHeaderWithoutHash, BlockNumber, BlockStatus};
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{
ChainId,
ClassHash,
Expand Down Expand Up @@ -142,6 +143,7 @@ use crate::{
get_block_status,
get_latest_block_number,
internal_server_error,
internal_server_error_with_msg,
verify_storage_scope,
ContinuationTokenAsStruct,
GENESIS_HASH,
Expand Down Expand Up @@ -1449,7 +1451,7 @@ impl JsonRpcServer for JsonRpcServerImpl {
&self,
block_id: BlockId,
class_hash: ClassHash,
) -> RpcResult<CompiledContractClass> {
) -> RpcResult<(CompiledContractClass, SierraVersion)> {
let storage_txn = self.storage_reader.begin_ro_txn().map_err(internal_server_error)?;
let state_reader = storage_txn.get_state_reader().map_err(internal_server_error)?;
let block_number = get_accepted_block_number(&storage_txn, block_id)?;
Expand All @@ -1462,11 +1464,17 @@ impl JsonRpcServer for JsonRpcServerImpl {
if class_definition_block_number > block_number {
return Err(ErrorObjectOwned::from(CLASS_HASH_NOT_FOUND));
}
let casm = storage_txn
.get_casm(&class_hash)
.map_err(internal_server_error)?
let (option_casm, option_sierra) = storage_txn
.get_casm_and_sierra(&class_hash)
.map_err(internal_server_error_with_msg)?;

// Check if both options are `Some`.
let (casm, sierra) = option_casm
.zip(option_sierra)
.ok_or_else(|| ErrorObjectOwned::from(CLASS_HASH_NOT_FOUND))?;
return Ok(CompiledContractClass::V1(casm));
let sierra_version = SierraVersion::extract_from_program(&sierra.sierra_program)
.map_err(internal_server_error_with_msg)?;
return Ok((CompiledContractClass::V1(casm), sierra_version));
}

// Check if this class exists in the Cairo0 classes table.
Expand All @@ -1476,7 +1484,10 @@ impl JsonRpcServer for JsonRpcServerImpl {
.get_deprecated_class_definition_at(state_number, &class_hash)
.map_err(internal_server_error)?
.ok_or_else(|| ErrorObjectOwned::from(CLASS_HASH_NOT_FOUND))?;
Ok(CompiledContractClass::V0(deprecated_compiled_contract_class))
Ok((
CompiledContractClass::V0(deprecated_compiled_contract_class),
SierraVersion::DEPRECATED,
))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_8/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub trait JsonRpc {
&self,
block_id: BlockId,
class_hash: ClassHash,
) -> RpcResult<CompiledContractClass>;
) -> RpcResult<(CompiledContractClass, SierraVersion)>;
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
20 changes: 15 additions & 5 deletions crates/papyrus_rpc/src/v0_8/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use starknet_api::block::{
GasPricePerToken,
StarknetVersion,
};
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{
ClassHash,
CompiledClassHash,
Expand Down Expand Up @@ -3688,6 +3689,9 @@ async fn get_compiled_class() {
JsonRpcServerImpl,
>(None, None, None, None, None);
let cairo1_contract_class = CasmContractClass::get_test_instance(&mut get_rng());
// We need to save the Sierra component of the Cairo 1 contract in storage to maintain
// consistency.
let sierra_contract_class = StarknetApiContractClass::default();
let cairo0_contract_class =
StarknetApiDeprecatedContractClass::get_test_instance(&mut get_rng());
storage_writer
Expand All @@ -3709,28 +3713,34 @@ async fn get_compiled_class() {
.unwrap()
// Note: there is no need to write the cairo1 contract class here because the
// declared_classes_table is not used in the rpc method.
.append_classes(BlockNumber(0), &[], &[(cairo0_class_hash, &cairo0_contract_class)])
.append_classes(BlockNumber(0), &[(cairo1_class_hash, &sierra_contract_class)], &[(cairo0_class_hash, &cairo0_contract_class)])
.unwrap()
.commit()
.unwrap();

let res = module
.call::<_, CompiledContractClass>(
.call::<_, (CompiledContractClass, SierraVersion)>(
method_name,
(BlockId::Tag(Tag::Latest), cairo1_class_hash),
)
.await
.unwrap();
assert_eq!(res, CompiledContractClass::V1(cairo1_contract_class));
assert_eq!(
res,
(
CompiledContractClass::V1(cairo1_contract_class),
SierraVersion::extract_from_program(&sierra_contract_class.sierra_program).unwrap()
)
);

let res = module
.call::<_, CompiledContractClass>(
.call::<_, (CompiledContractClass, SierraVersion)>(
method_name,
(BlockId::Tag(Tag::Latest), cairo0_class_hash),
)
.await
.unwrap();
assert_eq!(res, CompiledContractClass::V0(cairo0_contract_class));
assert_eq!(res, (CompiledContractClass::V0(cairo0_contract_class), SierraVersion::DEPRECATED));

// Ask for an invalid class hash, which does no exist in the table.
let err = module
Expand Down
3 changes: 2 additions & 1 deletion crates/starknet_gateway/src/rpc_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use reqwest::blocking::Client as BlockingClient;
use serde::Serialize;
use serde_json::{json, Value};
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::contract_class::SierraVersion;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -144,7 +145,7 @@ impl BlockifierStateReader for RpcStateReader {

let result =
self.send_rpc_request("starknet_getCompiledContractClass", get_compiled_class_params)?;
let contract_class: CompiledContractClass =
let (contract_class, _): (CompiledContractClass, SierraVersion) =
serde_json::from_value(result).map_err(serde_err_to_state_err)?;
match contract_class {
CompiledContractClass::V1(contract_class_v1) => Ok(RunnableCompiledClass::V1(
Expand Down
8 changes: 6 additions & 2 deletions crates/starknet_gateway/src/rpc_state_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use papyrus_rpc::CompiledContractClass;
use serde::Serialize;
use serde_json::json;
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::contract_class::SierraVersion;
use starknet_api::{class_hash, contract_address, felt, nonce};

use crate::config::RpcStateReaderConfig;
Expand Down Expand Up @@ -178,8 +179,11 @@ async fn test_get_compiled_class() {
"starknet_getCompiledContractClass",
GetCompiledClassParams { block_id: BlockId::Latest, class_hash: class_hash!("0x1") },
&RpcResponse::Success(RpcSuccessResponse {
result: serde_json::to_value(CompiledContractClass::V1(expected_result.clone()))
.unwrap(),
result: serde_json::to_value((
CompiledContractClass::V1(expected_result.clone()),
SierraVersion::default(),
))
.unwrap(),
..Default::default()
}),
);
Expand Down

0 comments on commit 2f6028a

Please sign in to comment.