From df92693bd33b5be9dc68243407587c56844528e4 Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Thu, 5 Dec 2024 16:30:21 +0200 Subject: [PATCH] refactor(papyrus_rpc): get compiled class --- crates/papyrus_rpc/src/v0_8/api/api_impl.rs | 19 +++++++++++++------ crates/papyrus_rpc/src/v0_8/api/mod.rs | 2 +- .../starknet_gateway/src/rpc_state_reader.rs | 3 ++- .../src/rpc_state_reader_test.rs | 8 ++++++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/papyrus_rpc/src/v0_8/api/api_impl.rs b/crates/papyrus_rpc/src/v0_8/api/api_impl.rs index ad9094da1ae..f3f5c811fec 100644 --- a/crates/papyrus_rpc/src/v0_8/api/api_impl.rs +++ b/crates/papyrus_rpc/src/v0_8/api/api_impl.rs @@ -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, @@ -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, @@ -1449,7 +1451,7 @@ impl JsonRpcServer for JsonRpcServerImpl { &self, block_id: BlockId, class_hash: ClassHash, - ) -> RpcResult { + ) -> 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)?; @@ -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. @@ -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, + )) } } diff --git a/crates/papyrus_rpc/src/v0_8/api/mod.rs b/crates/papyrus_rpc/src/v0_8/api/mod.rs index 5e45ff13866..5fb8f71948b 100644 --- a/crates/papyrus_rpc/src/v0_8/api/mod.rs +++ b/crates/papyrus_rpc/src/v0_8/api/mod.rs @@ -264,7 +264,7 @@ pub trait JsonRpc { &self, block_id: BlockId, class_hash: ClassHash, - ) -> RpcResult; + ) -> RpcResult<(CompiledContractClass, SierraVersion)>; } #[derive(Debug, Clone, Deserialize, Serialize)] diff --git a/crates/starknet_gateway/src/rpc_state_reader.rs b/crates/starknet_gateway/src/rpc_state_reader.rs index a01ccdc7036..62e637a2925 100644 --- a/crates/starknet_gateway/src/rpc_state_reader.rs +++ b/crates/starknet_gateway/src/rpc_state_reader.rs @@ -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; @@ -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( diff --git a/crates/starknet_gateway/src/rpc_state_reader_test.rs b/crates/starknet_gateway/src/rpc_state_reader_test.rs index 623c0494272..514518636ad 100644 --- a/crates/starknet_gateway/src/rpc_state_reader_test.rs +++ b/crates/starknet_gateway/src/rpc_state_reader_test.rs @@ -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; @@ -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() }), );