Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 0c9978a

Browse files
authored
DeclareV2 class hash fix (#788)
* Store Sierra class hash in Declare v2 transaction We'll need this value when we're adding the declared class to storage so this change moves the computation from the transaction hash calculation function into DeclareV2::new(). * Fix Declare V2 execution The code used the _transaction hash_ instead of the Sierra class hash to add the declared class to storage. This change fixes that to use the class hash instead.
1 parent 4b1d443 commit 0c9978a

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/core/transaction_hash/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use crate::{
2-
core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash},
3-
definitions::constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR,
4-
hash_utils::compute_hash_on_elements,
2+
core::contract_address::compute_deprecated_class_hash,
3+
definitions::constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR, hash_utils::compute_hash_on_elements,
54
services::api::contract_classes::deprecated_contract_class::ContractClass,
6-
syscalls::syscall_handler_errors::SyscallHandlerError,
7-
utils::Address,
5+
syscalls::syscall_handler_errors::SyscallHandlerError, utils::Address,
86
};
9-
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
107
use cairo_vm::felt::{felt_str, Felt252};
118
use num_traits::Zero;
129

@@ -152,18 +149,15 @@ pub fn calculate_declare_transaction_hash(
152149
// ----------------------------
153150

154151
pub fn calculate_declare_v2_transaction_hash(
155-
contract_class: &SierraContractClass,
152+
sierra_class_hash: Felt252,
156153
compiled_class_hash: Felt252,
157154
chain_id: Felt252,
158155
sender_address: &Address,
159156
max_fee: u128,
160157
version: Felt252,
161158
nonce: Felt252,
162159
) -> Result<Felt252, SyscallHandlerError> {
163-
let class_hash = compute_sierra_class_hash(contract_class)
164-
.map_err(|_| SyscallHandlerError::FailToComputeHash)?;
165-
166-
let calldata = [class_hash].to_vec();
160+
let calldata = [sierra_class_hash].to_vec();
167161
let additional_data = [nonce, compiled_class_hash].to_vec();
168162

169163
calculate_transaction_hash_common(

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ mod test {
193193
use std::collections::HashMap;
194194
use std::path::PathBuf;
195195

196-
use crate::core::contract_address::compute_deprecated_class_hash;
196+
use crate::core::contract_address::{compute_deprecated_class_hash, compute_sierra_class_hash};
197197
use crate::definitions::{
198198
block_context::StarknetChainId,
199199
constants::{
@@ -836,6 +836,8 @@ mod test {
836836
let sierra_contract_class: SierraContractClass =
837837
serde_json::from_slice(program_data).unwrap();
838838

839+
let sierra_class_hash = compute_sierra_class_hash(&sierra_contract_class).unwrap();
840+
839841
DeclareV2 {
840842
sender_address: TEST_ACCOUNT_CONTRACT_ADDRESS.clone(),
841843
tx_type: TransactionType::Declare,
@@ -847,6 +849,7 @@ mod test {
847849
hash_value: 0.into(),
848850
compiled_class_hash: TEST_FIB_COMPILED_CONTRACT_CLASS_HASH.clone(),
849851
sierra_contract_class,
852+
sierra_class_hash,
850853
casm_class: Default::default(),
851854
skip_execute: false,
852855
skip_fee_transfer: false,

src/transaction/declare_v2.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{verify_version, Transaction};
2+
use crate::core::contract_address::compute_sierra_class_hash;
23
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
34

45
use crate::{
@@ -41,6 +42,7 @@ pub struct DeclareV2 {
4142
pub nonce: Felt252,
4243
pub compiled_class_hash: Felt252,
4344
pub sierra_contract_class: SierraContractClass,
45+
pub sierra_class_hash: Felt252,
4446
pub hash_value: Felt252,
4547
pub casm_class: once_cell::unsync::OnceCell<CasmContractClass>,
4648
pub skip_validate: bool,
@@ -63,6 +65,7 @@ impl DeclareV2 {
6365
#[allow(clippy::too_many_arguments)]
6466
pub fn new(
6567
sierra_contract_class: &SierraContractClass,
68+
sierra_class_hash: Option<Felt252>,
6669
compiled_class_hash: Felt252,
6770
chain_id: Felt252,
6871
sender_address: Address,
@@ -74,10 +77,15 @@ impl DeclareV2 {
7477
) -> Result<Self, TransactionError> {
7578
let validate_entry_point_selector = VALIDATE_DECLARE_ENTRY_POINT_SELECTOR.clone();
7679

80+
let sierra_class_hash = match sierra_class_hash {
81+
Some(h) => h,
82+
None => compute_sierra_class_hash(sierra_contract_class)?,
83+
};
84+
7785
let hash_value = match hash_value {
7886
Some(hash) => hash,
7987
None => calculate_declare_v2_transaction_hash(
80-
sierra_contract_class,
88+
sierra_class_hash.clone(),
8189
compiled_class_hash.clone(),
8290
chain_id,
8391
&sender_address,
@@ -89,6 +97,7 @@ impl DeclareV2 {
8997

9098
let internal_declare = DeclareV2 {
9199
sierra_contract_class: sierra_contract_class.to_owned(),
100+
sierra_class_hash,
92101
sender_address,
93102
tx_type: TransactionType::Declare,
94103
validate_entry_point_selector,
@@ -259,7 +268,7 @@ impl DeclareV2 {
259268
})
260269
.map_err(|e| TransactionError::SierraCompileError(e.to_string()))?;
261270

262-
state.set_compiled_class_hash(&self.hash_value, &self.compiled_class_hash)?;
271+
state.set_compiled_class_hash(&self.sierra_class_hash, &self.compiled_class_hash)?;
263272
state.set_compiled_class(&self.compiled_class_hash, casm_class.clone())?;
264273

265274
Ok(())
@@ -331,6 +340,7 @@ mod tests {
331340
use std::{collections::HashMap, fs::File, io::BufReader, path::PathBuf};
332341

333342
use super::DeclareV2;
343+
use crate::core::contract_address::compute_sierra_class_hash;
334344
use crate::services::api::contract_classes::compiled_class::CompiledClass;
335345
use crate::state::state_api::StateReader;
336346
use crate::{
@@ -362,6 +372,7 @@ mod tests {
362372
let reader = BufReader::new(file);
363373
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass =
364374
serde_json::from_reader(reader).unwrap();
375+
let sierra_class_hash = compute_sierra_class_hash(&sierra_contract_class).unwrap();
365376
let chain_id = StarknetChainId::TestNet.to_felt();
366377

367378
let sender_address = Address(1.into());
@@ -370,6 +381,7 @@ mod tests {
370381

371382
let internal_declare = DeclareV2::new(
372383
&sierra_contract_class,
384+
Some(sierra_class_hash),
373385
Felt252::one(),
374386
chain_id,
375387
sender_address,

tests/internals.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cairo_vm::vm::{
1212
use lazy_static::lazy_static;
1313
use num_bigint::BigUint;
1414
use num_traits::{FromPrimitive, Num, One, Zero};
15+
use starknet_in_rust::core::contract_address::compute_sierra_class_hash;
1516
use starknet_in_rust::core::errors::state_errors::StateError;
1617
use starknet_in_rust::definitions::constants::{
1718
DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS, VALIDATE_ENTRY_POINT_SELECTOR,
@@ -698,6 +699,7 @@ fn declarev2_tx() -> DeclareV2 {
698699
#[cfg(feature = "cairo_1_tests")]
699700
let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.sierra");
700701
let sierra_contract_class: SierraContractClass = serde_json::from_slice(program_data).unwrap();
702+
let sierra_class_hash = compute_sierra_class_hash(&sierra_contract_class).unwrap();
701703

702704
DeclareV2 {
703705
sender_address: TEST_ACCOUNT_CONTRACT_ADDRESS.clone(),
@@ -710,6 +712,7 @@ fn declarev2_tx() -> DeclareV2 {
710712
hash_value: 0.into(),
711713
compiled_class_hash: TEST_FIB_COMPILED_CONTRACT_CLASS_HASH.clone(),
712714
sierra_contract_class,
715+
sierra_class_hash,
713716
casm_class: Default::default(),
714717
skip_execute: false,
715718
skip_fee_transfer: false,

0 commit comments

Comments
 (0)