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 5, 2024
1 parent d1ec273 commit eb75d10
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
19 changes: 16 additions & 3 deletions crates/papyrus_rpc/src/v0_8/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use papyrus_execution::{
};
use papyrus_storage::body::events::{EventIndex, EventsReader};
use papyrus_storage::body::{BodyStorageReader, TransactionIndex};
use papyrus_storage::class::ClassStorageReader;
use papyrus_storage::compiled_class::CasmStorageReader;
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 @@ -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 @@ -1466,7 +1468,15 @@ impl JsonRpcServer for JsonRpcServerImpl {
.get_casm(&class_hash)
.map_err(internal_server_error)?
.ok_or_else(|| ErrorObjectOwned::from(CLASS_HASH_NOT_FOUND))?;
return Ok(CompiledContractClass::V1(casm));
let sierra_version = SierraVersion::extract_from_program(
&storage_txn
.get_class(&class_hash)
.map_err(internal_server_error)?
.ok_or_else(|| ErrorObjectOwned::from(CLASS_HASH_NOT_FOUND))?
.sierra_program,
)
.unwrap();
return Ok((CompiledContractClass::V1(casm), sierra_version));
}

// Check if this class exists in the Cairo0 classes table.
Expand All @@ -1476,7 +1486,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 eb75d10

Please sign in to comment.