Skip to content

Commit

Permalink
Add dummy starknet_getStorageProof, remove unused Serialize (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabijanC authored Dec 10, 2024
1 parent dd2e612 commit 31b11ac
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
14 changes: 13 additions & 1 deletion crates/starknet-devnet-server/src/api/json_rpc/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use starknet_types::rpc::transactions::{
use starknet_types::starknet_api::block::BlockStatus;

use super::error::{ApiError, StrictRpcResult};
use super::models::{BlockHashAndNumberOutput, L1TransactionHashInput, SyncingOutput};
use super::models::{
BlockHashAndNumberOutput, GetStorageProofInput, L1TransactionHashInput, SyncingOutput,
};
use super::{DevnetResponse, JsonRpcHandler, JsonRpcResponse, StarknetResponse, RPC_SPEC_VERSION};
use crate::api::http::endpoints::accounts::{
get_account_balance_impl, get_predeployed_accounts_impl, BalanceQuery, PredeployedAccountsQuery,
Expand Down Expand Up @@ -136,6 +138,16 @@ impl JsonRpcHandler {
Ok(StarknetResponse::Felt(felt).into())
}

/// starknet_getStorageProof
pub async fn get_storage_proof(&self, data: GetStorageProofInput) -> StrictRpcResult {
match self.api.starknet.lock().await.get_block(data.block_id.as_ref()) {
// storage proofs not applicable to Devnet
Ok(_) => Err(ApiError::StorageProofNotSupported),
Err(Error::NoBlock) => Err(ApiError::BlockNotFound),
Err(unknown_error) => Err(ApiError::StarknetDevnetError(unknown_error)),
}
}

/// starknet_getTransactionByHash
pub async fn get_transaction_by_hash(
&self,
Expand Down
8 changes: 8 additions & 0 deletions crates/starknet-devnet-server/src/api/json_rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum ApiError {
CallOnPending,
#[error("Invalid subscription id")]
InvalidSubscriptionId,
#[error("Devnet doesn't support storage proofs")] // slightly modified spec message
StorageProofNotSupported,
}

impl ApiError {
Expand Down Expand Up @@ -226,6 +228,11 @@ impl ApiError {
message: error_message.into(),
data: None,
},
ApiError::StorageProofNotSupported => RpcError {
code: crate::rpc_core::error::ErrorCode::ServerError(42),
message: error_message.into(),
data: None,
},
}
}

Expand Down Expand Up @@ -260,6 +267,7 @@ impl ApiError {
| Self::TooManyBlocksBack
| Self::InvalidSubscriptionId
| Self::InsufficientResourcesForValidate
| Self::StorageProofNotSupported
| Self::CompiledClassHashMismatch => false,
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/starknet-devnet-server/src/api/json_rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use futures::stream::SplitSink;
use futures::{SinkExt, StreamExt};
use models::{
BlockAndClassHashInput, BlockAndContractAddressInput, BlockAndIndexInput, CallInput,
EstimateFeeInput, EventsInput, EventsSubscriptionInput, GetStorageInput,
EstimateFeeInput, EventsInput, EventsSubscriptionInput, GetStorageInput, GetStorageProofInput,
L1TransactionHashInput, PendingTransactionsSubscriptionInput, SubscriptionIdInput,
TransactionBlockInput, TransactionHashInput, TransactionHashOutput,
};
Expand Down Expand Up @@ -569,6 +569,7 @@ impl JsonRpcHandler {
JsonRpcRequest::Mint(data) => self.mint(data).await,
JsonRpcRequest::DevnetConfig => self.get_devnet_config().await,
JsonRpcRequest::MessagesStatusByL1Hash(data) => self.get_messages_status(data).await,
JsonRpcRequest::StorageProof(data) => self.get_storage_proof(data).await,
}
}

Expand Down Expand Up @@ -667,6 +668,8 @@ pub enum JsonRpcRequest {
StateUpdate(BlockIdInput),
#[serde(rename = "starknet_getStorageAt")]
StorageAt(GetStorageInput),
#[serde(rename = "starknet_getStorageProof")]
StorageProof(GetStorageProofInput),
#[serde(rename = "starknet_getTransactionByHash")]
TransactionByHash(TransactionHashInput),
#[serde(rename = "starknet_getTransactionByBlockIdAndIndex")]
Expand Down Expand Up @@ -813,6 +816,7 @@ impl JsonRpcRequest {
| Self::Restart(_)
| Self::PredeployedAccounts(_)
| Self::AccountBalance(_)
| Self::StorageProof(_)
| Self::DevnetConfig => false,
}
}
Expand Down Expand Up @@ -845,6 +849,7 @@ impl JsonRpcRequest {
| Self::TraceTransaction(_)
| Self::MessagesStatusByL1Hash(_)
| Self::CompiledCasmByClassHash(_)
| Self::StorageProof(_)
| Self::BlockTransactionTraces(_) => true,
Self::SpecVersion
| Self::ChainId
Expand Down Expand Up @@ -929,6 +934,7 @@ impl JsonRpcRequest {
| Self::AccountBalance(_)
| Self::MessagesStatusByL1Hash(_)
| Self::CompiledCasmByClassHash(_)
| Self::StorageProof(_)
| Self::DevnetConfig => false,
}
}
Expand Down Expand Up @@ -1031,6 +1037,7 @@ pub enum StarknetResponse {
TraceTransaction(TransactionTrace),
BlockTransactionTraces(Vec<BlockTransactionTrace>),
MessagesStatusByL1Hash(Vec<L1HandlerTransactionStatus>),
StorageProofs(serde_json::Value), // dummy, the corresponding RPC method always errors
}

#[derive(Serialize)]
Expand Down
33 changes: 24 additions & 9 deletions crates/starknet-devnet-server/src/api/json_rpc/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ use starknet_types::rpc::transactions::{
};
use starknet_types::starknet_api::block::BlockNumber;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct BlockIdInput {
pub block_id: BlockId,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct TransactionHashInput {
pub transaction_hash: TransactionHash,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct GetStorageInput {
Expand All @@ -34,34 +34,49 @@ pub struct GetStorageInput {
pub block_id: BlockId,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
pub struct ContractStorage {
pub contract_address: ContractAddress,
pub storage_keys: Vec<Felt>,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct GetStorageProofInput {
pub block_id: BlockId,
pub class_hashes: Option<Vec<Felt>>,
pub contract_addresses: Option<Vec<ContractAddress>>,
pub contract_storage_keys: Option<ContractStorage>,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct BlockAndIndexInput {
pub block_id: BlockId,
pub index: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct BlockAndClassHashInput {
pub block_id: BlockId,
pub class_hash: ClassHash,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct BlockAndContractAddressInput {
pub block_id: BlockId,
pub contract_address: ContractAddress,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct AccountAddressInput {
pub account_address: ContractAddress,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
#[serde(deny_unknown_fields)]
pub struct CallInput {
Expand Down Expand Up @@ -92,7 +107,7 @@ pub enum SyncingOutput {
False(bool),
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct EventsInput {
pub filter: EventFilter,
}
Expand Down
33 changes: 27 additions & 6 deletions crates/starknet-devnet/tests/general_rpc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod common;
mod general_rpc_tests {
use serde_json::json;
use server::api::json_rpc::RPC_SPEC_VERSION;
use server::rpc_core::error::RpcError;
use starknet_rs_core::types::BlockId;

use crate::common::background_devnet::BackgroundDevnet;
use crate::common::constants::RPC_PATH;
Expand Down Expand Up @@ -46,15 +48,34 @@ mod general_rpc_tests {
async fn rpc_returns_invalid_params() {
let devnet = BackgroundDevnet::spawn().await.unwrap();
let rpc_error = devnet
.send_custom_rpc(
"starknet_specVersion",
json!({
"invalid_param": "unneeded_value",
}),
)
.send_custom_rpc("starknet_specVersion", json!({ "invalid_param": "unneeded_value" }))
.await
.unwrap_err();

assert_eq!(rpc_error.code, server::rpc_core::error::ErrorCode::InvalidParams);
}

#[tokio::test]
async fn storage_proof_request_should_always_return_error() {
let devnet = BackgroundDevnet::spawn().await.unwrap();
devnet.create_block().await.unwrap();

for (req_params, expected_code, expected_msg) in [
(json!({}), -32602, "missing field `block_id`"),
(
json!({ "block_id": BlockId::Number(0) }),
42,
"Devnet doesn't support storage proofs",
),
(json!({ "block_id": "latest" }), 42, "Devnet doesn't support storage proofs"),
(json!({ "block_id": BlockId::Number(5) }), 24, "Block not found"),
] {
let error =
devnet.send_custom_rpc("starknet_getStorageProof", req_params).await.unwrap_err();
assert_eq!(
error,
RpcError { code: expected_code.into(), message: expected_msg.into(), data: None }
);
}
}
}
1 change: 1 addition & 0 deletions website/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sidebar_position: 1
:::danger Difference disclaimer

- Devnet should not be used as a replacement for official testnets. After testing on Devnet, be sure to test on a testnet (alpha-sepolia)!
- Devnet does not organize state data into Merkle-Patricia trees, so calling the `starknet_getStorageProof` RPC method shall always result in `STORAGE_PROOF_NOT_SUPPORTED`.
- The semantics of `REJECTED` and `REVERTED` status of a transaction is not the same as on the official testnet:

| Tx status | Official testnet | Devnet |
Expand Down

0 comments on commit 31b11ac

Please sign in to comment.