From 19f8844875c6bd793e0d16651209e8370c6ada63 Mon Sep 17 00:00:00 2001 From: antiyro Date: Thu, 18 Apr 2024 11:00:36 +0200 Subject: [PATCH 1/3] fix: :bug: Fixed prepare_data_availability_modes computation --- CHANGELOG.md | 1 + crates/client/sync/src/lib.rs | 2 +- crates/primitives/convert/src/contract.rs | 18 +-- .../transactions/src/compute_hash.rs | 104 ++++++++++-------- 4 files changed, 68 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5145e398e..83ecef04d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ git # Deoxys Changelog ## Next release +- fix(compute): Fixed prepare_data_availability_modes computation - chore: update bonsai-trie (benefit from perf boost) - feat(rpc): add `get_block_with_receipts` rpc call - refactor: remove crate mp-state, mp-fee, mp-messages diff --git a/crates/client/sync/src/lib.rs b/crates/client/sync/src/lib.rs index 117836c71..94acb2459 100644 --- a/crates/client/sync/src/lib.rs +++ b/crates/client/sync/src/lib.rs @@ -37,7 +37,7 @@ pub mod starknet_sync_worker { ) where C: HeaderBackend + 'static, { - let starting_block = starting_block + 1; + let starting_block = starting_block + 524435; let _ = tokio::join!( l1::sync(l1_url.clone()), diff --git a/crates/primitives/convert/src/contract.rs b/crates/primitives/convert/src/contract.rs index 568d6e201..0982e40b4 100644 --- a/crates/primitives/convert/src/contract.rs +++ b/crates/primitives/convert/src/contract.rs @@ -3,6 +3,7 @@ use std::io::{Read, Write}; use std::sync::Arc; use anyhow::anyhow; +use blockifier::blockifier::block; use blockifier::execution::contract_class::{ ContractClass as ContractClassBlockifier, ContractClassV0, ContractClassV0Inner, ContractClassV1, }; @@ -93,14 +94,15 @@ pub fn from_contract_class_sierra( contract_class: &FlattenedSierraClass, starknet_version: Option, ) -> anyhow::Result { - let casm_contract = flattened_sierra_to_casm_contract_class(contract_class, starknet_version)?; - let raw_casm_contract = match casm_contract { - CasmContractClassVersion::V1(inner) => serde_json::to_string(&inner).unwrap(), - CasmContractClassVersion::V2(inner) => serde_json::to_string(&inner).unwrap(), - CasmContractClassVersion::V3(inner) => serde_json::to_string(&inner).unwrap(), - CasmContractClassVersion::Base(inner) => serde_json::to_string(&inner).unwrap(), - }; - let blockifier_contract = ContractClassV1::try_from_json_string(&raw_casm_contract).unwrap(); + // let casm_contract = flattened_sierra_to_casm_contract_class(contract_class, starknet_version)?; + // let raw_casm_contract = match casm_contract { + // CasmContractClassVersion::V1(inner) => serde_json::to_string(&inner).unwrap(), + // CasmContractClassVersion::V2(inner) => serde_json::to_string(&inner).unwrap(), + // CasmContractClassVersion::V3(inner) => serde_json::to_string(&inner).unwrap(), + // CasmContractClassVersion::Base(inner) => serde_json::to_string(&inner).unwrap(), + // }; + // let blockifier_contract = ContractClassV1::try_from_json_string(&raw_casm_contract).unwrap(); + let blockifier_contract = ContractClassV1::default(); anyhow::Ok(ContractClassBlockifier::V1(blockifier_contract)) } diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index 2f1587af2..979ea76d5 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -1,7 +1,9 @@ use alloc::vec::Vec; use mp_felt::Felt252Wrapper; +use mp_hashers::poseidon::PoseidonHasher; use mp_hashers::HasherT; +use starknet_api::core::calculate_contract_address; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::transaction::{ Calldata, DeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, DeclareTransactionV3, @@ -57,12 +59,9 @@ fn prepare_data_availability_modes( fee_data_availability_mode: DataAvailabilityMode, ) -> FieldElement { let mut buffer = [0u8; 32]; - buffer[16..24].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); - buffer[24..].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); - // buffer[64..96].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); - // buffer[96..].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); + buffer[8..12].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); + buffer[12..16].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); - // Safe to unwrap because we left most significant bit of the buffer empty FieldElement::from_bytes_be(&buffer).unwrap() } @@ -152,7 +151,7 @@ impl ComputeTransactionHash for InvokeTransactionV1 { } impl ComputeTransactionHash for InvokeTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -183,7 +182,7 @@ impl ComputeTransactionHash for InvokeTransactionV3 { compute_hash_on_elements(&[account_deployment_data_hash, calldata_hash]) }; - Felt252Wrapper(H::compute_hash_on_elements(&[ + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ prefix, version, sender_address, @@ -282,7 +281,7 @@ impl ComputeTransactionHash for DeclareTransactionV2 { } impl ComputeTransactionHash for DeclareTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -307,7 +306,7 @@ impl ComputeTransactionHash for DeclareTransactionV3 { &self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), ); - Felt252Wrapper(H::compute_hash_on_elements(&[ + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ prefix, version, sender_address, @@ -363,11 +362,15 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { ) -> TransactionHash { let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); let prefix = FieldElement::from_byte_slice_be(DEPLOY_ACCOUNT_PREFIX).unwrap(); let version = if offset_version { SIMULATE_TX_VERSION_OFFSET + FieldElement::ONE } else { FieldElement::ONE }; @@ -404,11 +407,15 @@ impl ComputeTransactionHash for DeployTransaction { let chain_id = chain_id.into(); let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); compute_hash_given_contract_address::( @@ -423,7 +430,7 @@ impl ComputeTransactionHash for DeployTransaction { } impl ComputeTransactionHash for DeployAccountTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -433,12 +440,15 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { let version = if offset_version { SIMULATE_TX_VERSION_OFFSET + FieldElement::THREE } else { FieldElement::THREE }; let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); let gas_hash = compute_hash_on_elements(&[ FieldElement::from(self.tip.0), @@ -453,7 +463,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { prepare_data_availability_modes(self.nonce_data_availability_mode, self.fee_data_availability_mode); let constructor_calldata_hash = compute_hash_on_elements(&constructor_calldata); - Felt252Wrapper(H::compute_hash_on_elements(&[ + Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ prefix, version, contract_address, @@ -557,24 +567,22 @@ pub fn compute_hash_given_contract_address( } } -// Deploy Transaction custom function to compute the contract address -pub fn calculate_contract_address( - contract_address_salt: FieldElement, - class_hash: FieldElement, - constructor_calldata: &[FieldElement], -) -> FieldElement { - /// Cairo string for "STARKNET_CONTRACT_ADDRESS" - const PREFIX_CONTRACT_ADDRESS: FieldElement = - FieldElement::from_mont([3829237882463328880, 17289941567720117366, 8635008616843941496, 533439743893157637]); - // 2 ** 251 - 256 - const ADDR_BOUND: FieldElement = - FieldElement::from_mont([18446743986131443745, 160989183, 18446744073709255680, 576459263475590224]); - - starknet_core::crypto::compute_hash_on_elements(&[ - PREFIX_CONTRACT_ADDRESS, - FieldElement::ZERO, - contract_address_salt, - class_hash, - starknet_core::crypto::compute_hash_on_elements(constructor_calldata), - ]) % ADDR_BOUND -} +// // Deploy Transaction custom function to compute the contract address +// pub fn deploy_calculate_contract_address( +// contract_address_salt: FieldElement, +// class_hash: FieldElement, +// constructor_calldata: &[FieldElement], +// ) -> FieldElement { /// Cairo string for "STARKNET_CONTRACT_ADDRESS" const +// PREFIX_CONTRACT_ADDRESS: FieldElement = FieldElement::from_mont([3829237882463328880, +// 17289941567720117366, 8635008616843941496, 533439743893157637]); // 2 ** 251 - 256 const +// ADDR_BOUND: FieldElement = FieldElement::from_mont([18446743986131443745, 160989183, +// 18446744073709255680, 576459263475590224]); + +// starknet_core::crypto::compute_hash_on_elements(&[ +// PREFIX_CONTRACT_ADDRESS, +// FieldElement::ZERO, +// contract_address_salt, +// class_hash, +// starknet_core::crypto::compute_hash_on_elements(constructor_calldata), +// ]) % ADDR_BOUND +// } From c7640bd5171a24a7511caf62e6b23a01dec5b5a4 Mon Sep 17 00:00:00 2001 From: antiyro Date: Thu, 18 Apr 2024 11:11:19 +0200 Subject: [PATCH 2/3] cleaned for clippy --- crates/primitives/convert/src/contract.rs | 18 +++++----- .../transactions/src/compute_hash.rs | 33 ++++--------------- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/crates/primitives/convert/src/contract.rs b/crates/primitives/convert/src/contract.rs index 0982e40b4..568d6e201 100644 --- a/crates/primitives/convert/src/contract.rs +++ b/crates/primitives/convert/src/contract.rs @@ -3,7 +3,6 @@ use std::io::{Read, Write}; use std::sync::Arc; use anyhow::anyhow; -use blockifier::blockifier::block; use blockifier::execution::contract_class::{ ContractClass as ContractClassBlockifier, ContractClassV0, ContractClassV0Inner, ContractClassV1, }; @@ -94,15 +93,14 @@ pub fn from_contract_class_sierra( contract_class: &FlattenedSierraClass, starknet_version: Option, ) -> anyhow::Result { - // let casm_contract = flattened_sierra_to_casm_contract_class(contract_class, starknet_version)?; - // let raw_casm_contract = match casm_contract { - // CasmContractClassVersion::V1(inner) => serde_json::to_string(&inner).unwrap(), - // CasmContractClassVersion::V2(inner) => serde_json::to_string(&inner).unwrap(), - // CasmContractClassVersion::V3(inner) => serde_json::to_string(&inner).unwrap(), - // CasmContractClassVersion::Base(inner) => serde_json::to_string(&inner).unwrap(), - // }; - // let blockifier_contract = ContractClassV1::try_from_json_string(&raw_casm_contract).unwrap(); - let blockifier_contract = ContractClassV1::default(); + let casm_contract = flattened_sierra_to_casm_contract_class(contract_class, starknet_version)?; + let raw_casm_contract = match casm_contract { + CasmContractClassVersion::V1(inner) => serde_json::to_string(&inner).unwrap(), + CasmContractClassVersion::V2(inner) => serde_json::to_string(&inner).unwrap(), + CasmContractClassVersion::V3(inner) => serde_json::to_string(&inner).unwrap(), + CasmContractClassVersion::Base(inner) => serde_json::to_string(&inner).unwrap(), + }; + let blockifier_contract = ContractClassV1::try_from_json_string(&raw_casm_contract).unwrap(); anyhow::Ok(ContractClassBlockifier::V1(blockifier_contract)) } diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index 979ea76d5..0dbc9177d 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -1,7 +1,6 @@ use alloc::vec::Vec; use mp_felt::Felt252Wrapper; -use mp_hashers::poseidon::PoseidonHasher; use mp_hashers::HasherT; use starknet_api::core::calculate_contract_address; use starknet_api::data_availability::DataAvailabilityMode; @@ -151,7 +150,7 @@ impl ComputeTransactionHash for InvokeTransactionV1 { } impl ComputeTransactionHash for InvokeTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -182,7 +181,7 @@ impl ComputeTransactionHash for InvokeTransactionV3 { compute_hash_on_elements(&[account_deployment_data_hash, calldata_hash]) }; - Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ + Felt252Wrapper(H::compute_hash_on_elements(&[ prefix, version, sender_address, @@ -281,7 +280,7 @@ impl ComputeTransactionHash for DeclareTransactionV2 { } impl ComputeTransactionHash for DeclareTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -306,7 +305,7 @@ impl ComputeTransactionHash for DeclareTransactionV3 { &self.account_deployment_data.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect::>(), ); - Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ + Felt252Wrapper(H::compute_hash_on_elements(&[ prefix, version, sender_address, @@ -430,7 +429,7 @@ impl ComputeTransactionHash for DeployTransaction { } impl ComputeTransactionHash for DeployAccountTransactionV3 { - fn compute_hash( + fn compute_hash( &self, chain_id: Felt252Wrapper, offset_version: bool, @@ -463,7 +462,7 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { prepare_data_availability_modes(self.nonce_data_availability_mode, self.fee_data_availability_mode); let constructor_calldata_hash = compute_hash_on_elements(&constructor_calldata); - Felt252Wrapper(PoseidonHasher::compute_hash_on_elements(&[ + Felt252Wrapper(H::compute_hash_on_elements(&[ prefix, version, contract_address, @@ -566,23 +565,3 @@ pub fn compute_hash_given_contract_address( .into() } } - -// // Deploy Transaction custom function to compute the contract address -// pub fn deploy_calculate_contract_address( -// contract_address_salt: FieldElement, -// class_hash: FieldElement, -// constructor_calldata: &[FieldElement], -// ) -> FieldElement { /// Cairo string for "STARKNET_CONTRACT_ADDRESS" const -// PREFIX_CONTRACT_ADDRESS: FieldElement = FieldElement::from_mont([3829237882463328880, -// 17289941567720117366, 8635008616843941496, 533439743893157637]); // 2 ** 251 - 256 const -// ADDR_BOUND: FieldElement = FieldElement::from_mont([18446743986131443745, 160989183, -// 18446744073709255680, 576459263475590224]); - -// starknet_core::crypto::compute_hash_on_elements(&[ -// PREFIX_CONTRACT_ADDRESS, -// FieldElement::ZERO, -// contract_address_salt, -// class_hash, -// starknet_core::crypto::compute_hash_on_elements(constructor_calldata), -// ]) % ADDR_BOUND -// } From 83b8cd9d0c0e2014bc58c88b406556902a8a6480 Mon Sep 17 00:00:00 2001 From: antiyro Date: Thu, 18 Apr 2024 11:23:36 +0200 Subject: [PATCH 3/3] changed first block --- crates/client/sync/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/client/sync/src/lib.rs b/crates/client/sync/src/lib.rs index 94acb2459..117836c71 100644 --- a/crates/client/sync/src/lib.rs +++ b/crates/client/sync/src/lib.rs @@ -37,7 +37,7 @@ pub mod starknet_sync_worker { ) where C: HeaderBackend + 'static, { - let starting_block = starting_block + 524435; + let starting_block = starting_block + 1; let _ = tokio::join!( l1::sync(l1_url.clone()),