Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
second pass for apoorv review
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Apr 23, 2024
1 parent e71c3fa commit ebb9405
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
31 changes: 16 additions & 15 deletions crates/client/rpc-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,23 @@ fn entry_points_by_type_to_contract_entry_points(value: EntryPointsByType) -> Co

// Utils to convert Casm contract class to Compiled class
pub fn get_casm_cotract_class_hash(casm_contract_class: &CasmContractClass) -> FieldElement {
let compiled_class = casm_contract_class_to_compiled_class(casm_contract_class);
compiled_class.class_hash().unwrap()
}

/// Converts a [CasmContractClass] to a [CompiledClass]
pub fn casm_contract_class_to_compiled_class(casm_contract_class: &CasmContractClass) -> CompiledClass {
CompiledClass {
prime: casm_contract_class.prime.to_string(),
compiler_version: casm_contract_class.compiler_version.clone(),
bytecode: casm_contract_class.bytecode.iter().map(|x| biguint_to_field_element(&x.value)).collect(),
entry_points_by_type: casm_entry_points_to_compiled_entry_points(&casm_contract_class.entry_points_by_type),
// TODO: fill those
hints: vec![],
pythonic_hints: None,
bytecode_segment_lengths: vec![],
// Let's not expose it as it don't produce a full fleshed CompiledClass
// and is therefore only usefull in the context of computing the ClassHash
fn casm_contract_class_to_partial_compiled_class(casm_contract_class: &CasmContractClass) -> CompiledClass {
CompiledClass {
prime: casm_contract_class.prime.to_string(),
compiler_version: casm_contract_class.compiler_version.clone(),
bytecode: casm_contract_class.bytecode.iter().map(|x| biguint_to_field_element(&x.value)).collect(),
entry_points_by_type: casm_entry_points_to_compiled_entry_points(&casm_contract_class.entry_points_by_type),
// The following fields are not usefull to compute the class hash, so no need to fill those
hints: vec![],
pythonic_hints: None,
bytecode_segment_lengths: vec![],
}
}

let compiled_class = casm_contract_class_to_partial_compiled_class(casm_contract_class);
compiled_class.class_hash().unwrap()
}

/// Converts a [CasmContractEntryPoints] to a [CompiledClassEntrypointList]
Expand Down
56 changes: 28 additions & 28 deletions crates/primitives/transactions/src/from_broadcasted_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use starknet_api::deprecated_contract_class::{EntryPoint, EntryPointOffset, Entr
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{
AccountDeploymentData, Calldata, Fee, PaymasterData, Resource, ResourceBounds, ResourceBoundsMapping, Tip,
TransactionSignature,
TransactionHash, TransactionSignature,
};
use starknet_api::StarknetApiError;
use starknet_core::types::contract::legacy::{
Expand Down Expand Up @@ -89,6 +89,20 @@ pub fn try_declare_tx_from_broadcasted_declare_tx(
value: BroadcastedDeclareTransaction,
chain_id: Felt252Wrapper,
) -> Result<DeclareTransaction, BroadcastedTransactionConversionError> {
fn try_new_declare_transaction(
tx: starknet_api::transaction::DeclareTransaction,
tx_hash: TransactionHash,
class_info: ClassInfo,
is_query: bool,
) -> Result<DeclareTransaction, BroadcastedTransactionConversionError> {
if is_query {
DeclareTransaction::new_for_query(tx, tx_hash, class_info)
} else {
DeclareTransaction::new(tx, tx_hash, class_info)
}
.map_err(|_| BroadcastedTransactionConversionError::InvalidTransactionVersion)
}

let user_tx = match value {
BroadcastedDeclareTransaction::V1(BroadcastedDeclareTransactionV1 {
max_fee,
Expand Down Expand Up @@ -140,15 +154,7 @@ pub fn try_declare_tx_from_broadcasted_declare_tx(
let contract_class = instantiate_blockifier_contract_class(compresed_contract_class, decompressed_bytes)?;
let tx_hash = tx.compute_hash(chain_id, is_query);

{
let class_info = ClassInfo::new(&contract_class, 0, abi_length)?;
if is_query {
DeclareTransaction::new_for_query(tx, tx_hash, class_info)
} else {
DeclareTransaction::new(tx, tx_hash, class_info)
}
.map_err(|_| BroadcastedTransactionConversionError::InvalidTransactionVersion)?
}
try_new_declare_transaction(tx, tx_hash, ClassInfo::new(&contract_class, 0, abi_length)?, is_query)?
}
BroadcastedDeclareTransaction::V2(BroadcastedDeclareTransactionV2 {
max_fee,
Expand Down Expand Up @@ -189,15 +195,12 @@ pub fn try_declare_tx_from_broadcasted_declare_tx(
.map_err(|_| BroadcastedTransactionConversionError::CasmContractClassConversionFailed)?,
);

{
let class_info = ClassInfo::new(&contract_class, sierra_program_length, abi_length)?;
if is_query {
DeclareTransaction::new_for_query(tx, tx_hash, class_info)
} else {
DeclareTransaction::new(tx, tx_hash, class_info)
}
.map_err(|_| BroadcastedTransactionConversionError::InvalidTransactionVersion)?
}
try_new_declare_transaction(
tx,
tx_hash,
ClassInfo::new(&contract_class, sierra_program_length, abi_length)?,
is_query,
)?
}
BroadcastedDeclareTransaction::V3(BroadcastedDeclareTransactionV3 {
sender_address,
Expand Down Expand Up @@ -247,15 +250,12 @@ pub fn try_declare_tx_from_broadcasted_declare_tx(
.map_err(|_| BroadcastedTransactionConversionError::CasmContractClassConversionFailed)?,
);

{
let class_info = ClassInfo::new(&contract_class, sierra_program_length, abi_length)?;
if is_query {
DeclareTransaction::new_for_query(tx, tx_hash, class_info)
} else {
DeclareTransaction::new(tx, tx_hash, class_info)
}
.map_err(|_| BroadcastedTransactionConversionError::InvalidTransactionVersion)?
}
try_new_declare_transaction(
tx,
tx_hash,
ClassInfo::new(&contract_class, sierra_program_length, abi_length)?,
is_query,
)?
}
};

Expand Down

0 comments on commit ebb9405

Please sign in to comment.