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 24, 2024
1 parent 09b9c05 commit d42570e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -16,6 +17,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(transparent)]
DeclaredContractClassError(#[from] ContractClassError),
#[error(transparent)]
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 @@ -7,6 +7,8 @@ use axum::extract::State;
use axum::routing::{get, post};
use axum::{Json, Router};
use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1};
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 @@ -157,6 +159,15 @@ pub fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResu
}
};

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 =
ContractClass::V1(ContractClassV1::try_from(casm_contract_class)?);
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 @@ -96,6 +96,9 @@ pub fn declare_tx() -> RPCTransaction {
env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Couldn't set working dir.");
let json_file_path = Path::new(CONTRACT_CLASS_FILE);
let contract_class = serde_json::from_reader(File::open(json_file_path).unwrap()).unwrap();
let compiled_class_hash = CompiledClassHash(stark_felt!(
"0x01e4f1248860f32c336f93f2595099aaa4959be515e40b75472709ef5243ae17"
));

let cairo_version = CairoVersion::Cairo1;
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
Expand All @@ -108,7 +111,8 @@ pub fn declare_tx() -> RPCTransaction {
sender_address: account_address,
resource_bounds: executable_resource_bounds_mapping(),
nonce,
contract_class
class_hash: compiled_class_hash,
contract_class,
))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::stateful_transaction_validator::StatefulTransactionValidator;
declare_tx(),
local_test_state_reader_factory(CairoVersion::Cairo1, false),
Ok(TransactionHash(StarkFelt::try_from(
"0x0278ed2700d5a30254a6b895d4e1140438d7d1a3b2b2ce0c096a9d5ee1c61f39"
"0x02da54b89e00d2e201f8e3ed2bcc715a69e89aefdce88aff2d2facb8dec55c0a"
).unwrap()))
)]
#[case::invalid_tx(
Expand Down

0 comments on commit d42570e

Please sign in to comment.