Skip to content

Commit

Permalink
fix(common): v3 transaction hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed Sep 28, 2023
1 parent 56f4eff commit cd3b1ce
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 229 deletions.
102 changes: 101 additions & 1 deletion crates/papyrus_common/resources/transaction_hash.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,105 @@
"transaction_hash": "0x23a8b9fb4e1648dad90077fcc862addb720ce9bdc3e028ea5a71fe3cbda8242",
"chain_id": "SN_MAIN",
"block_number": 100000
},
{
"transaction": {
"Declare": {
"V3": {
"signature": ["0x3", "0x4"],
"nonce": "0x0",
"sender_address": "0x12fd538",
"nonce_data_availability_mode": "L1",
"fee_data_availability_mode": "L1",
"resource_bounds": {
"L2Gas": {
"max_amount": 0,
"max_price_per_unit": 0
},
"L1Gas": {
"max_amount": 1993,
"max_price_per_unit": 1
}
},
"tip": "0x0",
"paymaster_data": [
"0x0"
],
"class_hash": "0x7d6b55b53dc0b621bb7e2b501340e4a88f7c448b513c9882d1be7ffac42ba3",
"compiled_class_hash": "0x7b",
"account_deployment_data": [
"0x0"
]
}
}
},
"transaction_hash": "0x6819909698b213a42e90751f85e6c6be877a679503e1a50921b1efc7ea997e",
"chain_id": "3",
"block_number": 0
},
{
"transaction": {
"DeployAccount": {
"V3": {
"signature": ["0x3", "0x4"],
"nonce": "0x0",
"sender_address": "0x219bea54dc352c0d6853de34019644758620fa6298c4608829228c3f5f8db33",
"constructor_calldata": ["0x21b", "0x151"],
"contract_address_salt": "0x12fd537",
"nonce_data_availability_mode": "L1",
"fee_data_availability_mode": "L1",
"resource_bounds": {
"L2Gas": {
"max_amount": 0,
"max_price_per_unit": 0
},
"L1Gas": {
"max_amount": 1993,
"max_price_per_unit": 1
}
},
"tip": "0x0",
"paymaster_data": [],
"class_hash": "0x65bcf29c898ff912fa2bdd4c6cd94b9142da0399127601ef35dfc9babc7a691"
}
}
},
"transaction_hash": "0x3877e0ffb3917187deb3321f6017f5339d22a3753d498df76203b6b8120dde5",
"chain_id": "2",
"block_number": 0
},
{
"transaction": {
"Invoke": {
"V3": {
"signature": ["0x3", "0x4"],
"nonce": "0x9",
"sender_address": "0x12fd538",
"constructor_calldata": ["0x21b", "0x151"],
"nonce_data_availability_mode": "L1",
"fee_data_availability_mode": "L1",
"resource_bounds": {
"L2Gas": {
"max_amount": 0,
"max_price_per_unit": 0
},
"L1Gas": {
"max_amount": 1993,
"max_price_per_unit": 1
}
},
"tip": "0x0",
"paymaster_data": [],
"account_deployment_data": [],
"calldata": [
"0x11",
"0x26"
]
}
}
},
"transaction_hash": "0x35591624e5ea7e612f7c65f7c5fcfa0d972365359cfb611aaf93a13a6026a13",
"chain_id": "1",
"block_number": 0
}
]
]
16 changes: 8 additions & 8 deletions crates/papyrus_common/src/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_api::transaction::{
use starknet_api::StarknetApiError;

use crate::patricia_hash_tree::calculate_root;
use crate::transaction_hash::{ascii_as_felt, PedersenHashChain, ZERO};
use crate::transaction_hash::{ascii_as_felt, HashChain, ZERO};

#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
enum BlockHashVersion {
Expand Down Expand Up @@ -57,7 +57,7 @@ fn calculate_block_hash_by_version(
block_header.sequencer.0.key().to_owned()
};

Ok(PedersenHashChain::new()
Ok(HashChain::new()
.chain(&block_header.block_number.0.into())
.chain(&block_header.state_root.0)
.chain(&sequencer)
Expand All @@ -70,7 +70,7 @@ fn calculate_block_hash_by_version(
.chain(&ZERO) // Not implemented element.
.chain_if(&ascii_as_felt(chain_id.0.as_str())?, version == BlockHashVersion::V0)
.chain(&block_header.parent_hash.0)
.get_hash())
.get_pedersen_hash())
}

/// Calculate the commitments according to the fittest block hash version. However, the result does
Expand Down Expand Up @@ -132,7 +132,7 @@ fn get_transaction_leaf(
} else {
get_signature_only_from_invoke(transaction)
};
let signature_hash = PedersenHashChain::new().chain_iter(signature.iter()).get_hash();
let signature_hash = HashChain::new().chain_iter(signature.iter()).get_pedersen_hash();
Ok(pedersen_hash(&transaction_hash.0, &signature_hash))
}

Expand Down Expand Up @@ -167,11 +167,11 @@ fn get_events_commitment(transaction_outputs: &[TransactionOutput]) -> StarkFelt
// Return a Patricia leaf value for an event.
fn get_event_leaf(event: &Event) -> StarkHash {
let event_keys: Vec<_> = event.content.keys.iter().map(|key| key.0).collect();
PedersenHashChain::new()
HashChain::new()
.chain(event.from_address.0.key())
.chain(&PedersenHashChain::new().chain_iter(event_keys.iter()).get_hash())
.chain(&PedersenHashChain::new().chain_iter(event.content.data.0.iter()).get_hash())
.get_hash()
.chain(&HashChain::new().chain_iter(event_keys.iter()).get_pedersen_hash())
.chain(&HashChain::new().chain_iter(event.content.data.0.iter()).get_pedersen_hash())
.get_pedersen_hash()
}

// The fixed sequencer addresses of the chains that have historic blocks with block hash version 2.
Expand Down
Loading

0 comments on commit cd3b1ce

Please sign in to comment.