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 18, 2024
1 parent f3b59f6 commit 89b09d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 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,16 +16,21 @@ 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}")]
MessageSendError(String),
#[error(transparent)]
ProgramError(#[from] ProgramError),
#[error(transparent)]
StatefulTransactionValidatorError(#[from] StatefulTransactionValidatorError),
#[error(transparent)]
StatelessTransactionValidatorError(#[from] StatelessTransactionValidatorError),
#[error(transparent)]
ProgramError(#[from] ProgramError),
}

impl IntoResponse for GatewayError {
Expand Down
12 changes: 12 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::external_transaction::{ExternalDeclareTransaction, ExternalTransaction};
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::communication::SharedMempoolClient;
Expand Down Expand Up @@ -158,6 +160,16 @@ fn compile_contract_class(declare_tx: &ExternalDeclareTransaction) -> GatewayRes
}
};

// TODO: Handle unwrap.
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

0 comments on commit 89b09d2

Please sign in to comment.