Skip to content

Commit

Permalink
feat: check that the compiled class hash matches the supplied class
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 19, 2024
1 parent 61e94d8 commit 0fdbdbb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use cairo_vm::types::errors::program_errors::ProgramError;
use starknet_api::block::BlockNumber;
use starknet_api::core::CompiledClassHash;
use starknet_api::transaction::{Resource, ResourceBounds};
use starknet_api::StarknetApiError;
use thiserror::Error;
Expand All @@ -15,6 +16,11 @@ use tokio::task::JoinError;
pub enum GatewayError {
#[error(transparent)]
CompilationError(#[from] starknet_sierra_compile::compile::CompilationUtilError),
#[error(
"The supplied compiled class hash {supplied:?} does not match the hash of the Casm class \
compiled from the supplied Sierra {hash_result:?}."
)]
CompiledClassHashMismatch { supplied: CompiledClassHash, hash_result: CompiledClassHash },
#[error("Internal server error: {0}")]
InternalServerError(#[from] JoinError),
#[error("Error sending message: {0}")]
Expand Down
11 changes: 11 additions & 0 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use axum::routing::{get, post};
use axum::{Json, Router};
use blockifier::execution::contract_class::{ClassInfo, ContractClassV1};
use blockifier::execution::errors::ContractClassError;
use blockifier::execution::execution_utils::felt_to_stark_felt;
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::{RPCDeclareTransaction, RPCTransaction};
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::communication::SharedMempoolClient;
Expand Down Expand Up @@ -158,6 +160,15 @@ fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResult<C
}
};

let hash_result =
CompiledClassHash(felt_to_stark_felt(&casm_contract_class.compiled_class_hash()));
if hash_result != tx.compiled_class_hash {
return Err(GatewayError::CompiledClassHashMismatch {
supplied: tx.compiled_class_hash,
hash_result,
});
}

// Convert Casm contract class to Starknet contract class directly.
let blockifier_contract_class = ContractClassV1::try_from(casm_contract_class)?.into();
let class_info = match ClassInfo::new(
Expand Down
6 changes: 5 additions & 1 deletion crates/gateway/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub fn declare_tx() -> RPCTransaction {
let json_file_path = Path::new(CONTRACT_CLASS_FILE);
let json_file = File::open(json_file_path).unwrap();
let contract_class = serde_json::from_reader(json_file).unwrap();
let compiled_class_hash = CompiledClassHash(stark_felt!(
"0x01e4f1248860f32c336f93f2595099aaa4959be515e40b75472709ef5243ae17"
));

let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
Expand All @@ -106,8 +109,9 @@ pub fn declare_tx() -> RPCTransaction {
signature: TransactionSignature(vec![StarkFelt::ZERO]),
sender_address: account_address,
resource_bounds: executable_resource_bounds_mapping(),
nonce,
class_hash: compiled_class_hash,
contract_class,
nonce
))
}

Expand Down

0 comments on commit 0fdbdbb

Please sign in to comment.