Skip to content

Commit

Permalink
fix: support txs without signature
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed May 20, 2024
1 parent 64dd7ca commit 0065cd8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
20 changes: 14 additions & 6 deletions src/block_hash/transaction_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ mod transaction_commitment_test;

/// The elements used to calculate a leaf in the transactions Patricia tree.
#[derive(Clone)]
pub struct TransactionLeafElements {
transaction_hash: TransactionHash,
transaction_signature: TransactionSignature,
pub struct TransactionLeafElement {
pub transaction_hash: TransactionHash,
pub transaction_signature: Option<TransactionSignature>,
}

/// Returns the root of a Patricia tree where each leaf is
/// Poseidon(transaction_hash, transaction_signature).
/// The leaf of a transaction types without a signature field is: Poseidon(transaction_hash, 0).
pub fn calculate_transactions_commitment<H: HashFunction>(
transaction_leaf_elements: &[TransactionLeafElements],
transaction_leaf_elements: &[TransactionLeafElement],
) -> TransactionCommitment {
let transaction_leaves =
transaction_leaf_elements.iter().map(calculate_transaction_leaf).collect();
TransactionCommitment(calculate_root::<H>(transaction_leaves))
}

fn calculate_transaction_leaf(transaction_leaf_elements: &TransactionLeafElements) -> StarkFelt {
fn calculate_transaction_leaf(transaction_leaf_elements: &TransactionLeafElement) -> StarkFelt {
HashChain::new()
.chain(&transaction_leaf_elements.transaction_hash.0)
.chain_iter(transaction_leaf_elements.transaction_signature.0.iter())
.chain_iter(
transaction_leaf_elements
.transaction_signature
.as_ref()
.unwrap_or(&TransactionSignature(vec![StarkFelt::ZERO]))
.0
.iter(),
)
.get_poseidon_hash()
}
34 changes: 23 additions & 11 deletions src/block_hash/transaction_commitment_test.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use super::TransactionLeafElement;
use crate::block_hash::transaction_commitment::{
calculate_transaction_leaf, calculate_transactions_commitment, TransactionLeafElements,
calculate_transaction_leaf, calculate_transactions_commitment,
};
use crate::core::TransactionCommitment;
use crate::hash::{PoseidonHashCalculator, StarkFelt};
use crate::transaction::{TransactionHash, TransactionSignature};

#[test]
fn test_transaction_leaf_regression() {
let transaction_hash = TransactionHash(StarkFelt::ONE);
let transaction_signature = TransactionSignature(vec![StarkFelt::TWO, StarkFelt::THREE]);
let transaction_leaf_elements =
TransactionLeafElements { transaction_hash, transaction_signature };

let transaction_leaf_elements = get_transaction_leaf_element();
let expected_leaf =
StarkFelt::try_from("0x2f0d8840bcf3bc629598d8a6cc80cb7c0d9e52d93dab244bbf9cd0dca0ad082")
.unwrap();
Expand All @@ -20,12 +17,21 @@ fn test_transaction_leaf_regression() {
}

#[test]
fn test_transactions_commitment_regression() {
let transaction_hash = TransactionHash(StarkFelt::ONE);
let transaction_signature = TransactionSignature(vec![StarkFelt::TWO, StarkFelt::THREE]);
let transaction_leaf_elements =
TransactionLeafElements { transaction_hash, transaction_signature };
fn test_transaction_leaf_without_signature_regression() {
let transaction_leaf_elements = TransactionLeafElement {
transaction_hash: TransactionHash(StarkFelt::ONE),
transaction_signature: None,
};
let expected_leaf =
StarkFelt::try_from("0x00a93bf5e58b9378d093aa86ddc2f61a3295a1d1e665bd0ef3384dd07b30e033")
.unwrap();

assert_eq!(expected_leaf, calculate_transaction_leaf(&transaction_leaf_elements));
}

#[test]
fn test_transactions_commitment_regression() {
let transaction_leaf_elements = get_transaction_leaf_element();
let expected_root =
StarkFelt::try_from("0x0282b635972328bd1cfa86496fe920d20bd9440cd78ee8dc90ae2b383d664dcf")
.unwrap();
Expand All @@ -38,3 +44,9 @@ fn test_transactions_commitment_regression() {
],)
);
}

fn get_transaction_leaf_element() -> TransactionLeafElement {
let transaction_hash = TransactionHash(StarkFelt::ONE);
let transaction_signature = TransactionSignature(vec![StarkFelt::TWO, StarkFelt::THREE]);
TransactionLeafElement { transaction_hash, transaction_signature: Some(transaction_signature) }
}

0 comments on commit 0065cd8

Please sign in to comment.