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 8, 2024
1 parent 3f30869 commit df92693
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
19 changes: 13 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,13 @@ 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 (casm, sierra) = storage_txn
.get_casm_and_sierra(&class_hash)
.map_err(internal_server_error_with_msg)?
.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 +1480,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
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 df92693

Please sign in to comment.