Skip to content

Commit

Permalink
Merge pull request #58 from KasarLabs/fix/compute
Browse files Browse the repository at this point in the history
fix(compute): 🐛 Fixed prepare_data_availability_modes computation
  • Loading branch information
Tbelleng authored Apr 18, 2024
2 parents ef90311 + 83b8cd9 commit 73feeaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- fix(compute): Fixed prepare_data_availability_modes computation
- feat(rpc): add pending block to `get_block_with_receipts` rpc call
- chore: update bonsai-trie (benefit from perf boost)
- feat(rpc): add `get_block_with_receipts` rpc call
Expand Down
73 changes: 30 additions & 43 deletions crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::vec::Vec;

use mp_felt::Felt252Wrapper;
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,
Expand Down Expand Up @@ -59,12 +60,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()
}

Expand Down Expand Up @@ -365,11 +363,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 };
Expand Down Expand Up @@ -406,11 +408,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::<H>(
Expand All @@ -435,12 +441,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),
Expand Down Expand Up @@ -558,25 +567,3 @@ pub fn compute_hash_given_contract_address<H: HasherT>(
.into()
}
}

// 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
}

0 comments on commit 73feeaa

Please sign in to comment.