Skip to content

Commit

Permalink
chore(JSON-RPC): merge v0_4 into v0_5 (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahakShama authored Nov 19, 2023
1 parent 92f19bb commit ed71a31
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
27 changes: 3 additions & 24 deletions crates/papyrus_rpc/src/v0_5/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,34 +572,13 @@ impl JsonRpcServer for JsonRpcServerV0_5Impl {
}

#[instrument(skip(self), level = "debug", err, ret)]
fn get_class_at(
async fn get_class_at(
&self,
block_id: BlockId,
contract_address: ContractAddress,
) -> RpcResult<GatewayContractClass> {
let txn = self.storage_reader.begin_ro_txn().map_err(internal_server_error)?;

let block_number = get_block_number(&txn, block_id)?;
let state_number = StateNumber::right_after_block(block_number);
let state_reader = txn.get_state_reader().map_err(internal_server_error)?;

let class_hash = state_reader
.get_class_hash_at(state_number, &contract_address)
.map_err(internal_server_error)?
.ok_or_else(|| ErrorObjectOwned::from(CONTRACT_NOT_FOUND))?;

if let Some(class) = state_reader
.get_class_definition_at(state_number, &class_hash)
.map_err(internal_server_error)?
{
Ok(GatewayContractClass::Sierra(class.try_into().map_err(internal_server_error)?))
} else {
let class = state_reader
.get_deprecated_class_definition_at(state_number, &class_hash)
.map_err(internal_server_error)?
.ok_or_else(|| ErrorObjectOwned::from(CONTRACT_NOT_FOUND))?;
Ok(GatewayContractClass::Cairo0(class.try_into().map_err(internal_server_error)?))
}
let class_hash = self.get_class_hash_at(block_id, contract_address).await?;
self.get_class(block_id, class_hash).await
}

#[instrument(skip(self), level = "debug", err, ret)]
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_5/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub trait JsonRpc {

/// Gets the contract class definition in the given block at the given address.
#[method(name = "getClassAt")]
fn get_class_at(
async fn get_class_at(
&self,
block_id: BlockId,
contract_address: ContractAddress,
Expand Down
43 changes: 42 additions & 1 deletion crates/papyrus_rpc/src/v0_5/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,16 @@ async fn get_transaction_receipt() {
#[tokio::test]
async fn get_class_at() {
let method_name = "starknet_V0_5_getClassAt";
let pending_data = get_test_pending_data();
let pending_classes = get_test_pending_classes();
let (module, mut storage_writer) =
get_test_rpc_server_and_storage_writer::<JsonRpcServerImpl>();
get_test_rpc_server_and_storage_writer_from_params::<JsonRpcServerImpl>(
None,
None,
Some(pending_data.clone()),
Some(pending_classes.clone()),
None,
);
let parent_header = BlockHeader::default();
let header = BlockHeader {
block_hash: BlockHash(stark_felt!("0x1")),
Expand Down Expand Up @@ -1156,6 +1164,21 @@ async fn get_class_at() {
.commit()
.unwrap();

let pending_address: ContractAddress = random::<u64>().into();
let pending_class_hash = ClassHash(random::<u64>().into());
let pending_class = ApiContractClass::ContractClass(
StarknetApiContractClass::get_test_instance(&mut get_rng()),
);
pending_data
.write()
.await
.state_update
.state_diff
.deployed_contracts
.push(ClientDeployedContract { address: pending_address, class_hash: pending_class_hash });
pending_data.write().await.block.parent_block_hash = header.block_hash;
pending_classes.write().await.add_class(pending_class_hash, pending_class.clone());

// Deprecated Class
let (class_hash, contract_class) = diff.deprecated_declared_classes.get_index(0).unwrap();
let expected_contract_class = contract_class.clone().try_into().unwrap();
Expand Down Expand Up @@ -1213,6 +1236,24 @@ async fn get_class_at() {
.unwrap();
assert_eq!(res, expected_contract_class);

// Get class hash of pending block.
let res = module
.call::<_, GatewayContractClass>(method_name, (BlockId::Tag(Tag::Pending), pending_address))
.await
.unwrap();
assert_eq!(res, pending_class.try_into().unwrap());

// Get class hash of pending block when it's not up to date.
pending_data.write().await.block.parent_block_hash = BlockHash(random::<u64>().into());
call_api_then_assert_and_validate_schema_for_err::<_, (BlockId, ContractAddress), ContractClass>(
&module,
method_name,
&Some((BlockId::Tag(Tag::Pending), pending_address)),
&VERSION,
&CONTRACT_NOT_FOUND.into(),
)
.await;

// Invalid Call
// Ask for an invalid contract.
call_api_then_assert_and_validate_schema_for_err::<
Expand Down

0 comments on commit ed71a31

Please sign in to comment.