Skip to content

Commit

Permalink
refactor: use snapi class info in process declare tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 19, 2024
1 parent b6955bb commit 9a5d8bd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
17 changes: 5 additions & 12 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;

use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1};
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoLangContractClass;
use starknet_api::contract_class::{ClassInfo, ContractClass, ContractClassV1};
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::RpcDeclareTransaction;
use starknet_sierra_compile::cairo_lang_compiler::CairoLangSierraToCasmCompiler;
Expand Down Expand Up @@ -43,17 +43,10 @@ impl GatewayCompiler {

validate_compiled_class_hash(&casm_contract_class, &tx.compiled_class_hash)?;

ClassInfo::new(
&ContractClass::V1(ContractClassV1::try_from(casm_contract_class).map_err(|e| {
error!("Failed to convert CasmContractClass to Blockifier ContractClass: {:?}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
})?),
rpc_contract_class.sierra_program.len(),
rpc_contract_class.abi.len(),
)
.map_err(|e| {
error!("Failed to convert Blockifier ContractClass to Blockifier ClassInfo: {:?}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
Ok(ClassInfo {
contract_class: ContractClass::V1(ContractClassV1::Casm(casm_contract_class)),
sierra_program_length: rpc_contract_class.sierra_program.len(),
abi_length: rpc_contract_class.abi.len(),
})
}

Expand Down
8 changes: 4 additions & 4 deletions crates/gateway/src/compilation_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use assert_matches::assert_matches;
use blockifier::execution::contract_class::ContractClass;
use cairo_lang_sierra_to_casm::compiler::CompilationError;
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;
use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError;
use mempool_test_utils::starknet_api_test_utils::declare_tx as rpc_declare_tx;
use rstest::{fixture, rstest};
use starknet_api::contract_class::ContractClass;
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::{
RpcDeclareTransaction,
Expand Down Expand Up @@ -103,7 +103,7 @@ fn test_process_declare_tx_success(
let declare_tx = RpcDeclareTransaction::V3(declare_tx_v3);

let class_info = gateway_compiler.process_declare_tx(&declare_tx).unwrap();
assert_matches!(class_info.contract_class(), ContractClass::V1(_));
assert_eq!(class_info.sierra_program_length(), sierra_program_length);
assert_eq!(class_info.abi_length(), abi_length);
assert_matches!(class_info.contract_class, ContractClass::V1(_));
assert_eq!(class_info.sierra_program_length, sierra_program_length);
assert_eq!(class_info.abi_length, abi_length);
}
10 changes: 7 additions & 3 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_trait::async_trait;
use axum::extract::State;
use axum::routing::{get, post};
use axum::{Json, Router};
use blockifier::execution::contract_class::ClassInfo;
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter};
Expand Down Expand Up @@ -130,9 +131,12 @@ fn process_tx(

// Compile Sierra to Casm.
let optional_class_info = match &tx {
RpcTransaction::Declare(declare_tx) => {
Some(gateway_compiler.process_declare_tx(declare_tx)?)
}
RpcTransaction::Declare(declare_tx) => Some(
ClassInfo::try_from(gateway_compiler.process_declare_tx(declare_tx)?).map_err(|e| {
error!("Failed to convert Starknet API ClassInfo to Blockifier ClassInfo: {:?}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
})?,
),
_ => None,
};

Expand Down
10 changes: 7 additions & 3 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use blockifier::blockifier::stateful_validator::{
StatefulValidatorResult as BlockifierStatefulValidatorResult,
};
use blockifier::context::BlockContext;
use blockifier::execution::contract_class::ClassInfo;
use blockifier::test_utils::CairoVersion;
use blockifier::transaction::errors::{TransactionFeeError, TransactionPreValidationError};
use mempool_test_utils::invoke_tx_args;
Expand Down Expand Up @@ -83,9 +84,12 @@ fn test_stateful_tx_validator(
) {
let optional_class_info = match &external_tx {
RpcTransaction::Declare(declare_tx) => Some(
GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default())
.process_declare_tx(declare_tx)
.unwrap(),
ClassInfo::try_from(
GatewayCompiler::new_cairo_lang_compiler(SierraToCasmCompilationConfig::default())
.process_declare_tx(declare_tx)
.unwrap(),
)
.unwrap(),
),
_ => None,
};
Expand Down

0 comments on commit 9a5d8bd

Please sign in to comment.