remove hash attribute from Transaction and use a method instead

This commit is contained in:
Sergio Chouhy 2025-07-10 11:35:46 -03:00
parent 5172a95f53
commit 789dec673e
5 changed files with 26 additions and 33 deletions

View File

@ -8,7 +8,7 @@ pub trait TreeLeavItem {
impl TreeLeavItem for Transaction {
fn hash(&self) -> TreeHashType {
self.hash
self.hash()
}
}

View File

@ -26,7 +26,6 @@ pub enum TxKind {
#[derive(Debug, Serialize, Deserialize, Clone)]
///General transaction object
pub struct Transaction {
pub hash: TreeHashType,
pub tx_kind: TxKind,
///Tx input data (public part)
pub execution_input: Vec<u8>,
@ -58,6 +57,15 @@ pub struct Transaction {
pub state_changes: (serde_json::Value, usize),
}
impl Transaction {
pub fn hash(&self) -> TreeHashType {
let raw_data = serde_json::to_vec(&self).unwrap();
let mut hasher = sha2::Sha256::new();
hasher.update(&raw_data);
TreeHashType::from(hasher.finalize_fixed())
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
///General transaction object
pub struct TransactionPayload {
@ -94,16 +102,7 @@ pub struct TransactionPayload {
impl From<TransactionPayload> for Transaction {
fn from(value: TransactionPayload) -> Self {
let raw_data = serde_json::to_vec(&value).unwrap();
let mut hasher = sha2::Sha256::new();
hasher.update(&raw_data);
let hash = <TreeHashType>::from(hasher.finalize_fixed());
Self {
hash,
tx_kind: value.tx_kind,
execution_input: value.execution_input,
execution_output: value.execution_output,
@ -217,7 +216,7 @@ impl ActionData {
impl Transaction {
pub fn log(&self) {
info!("Transaction hash is {:?}", hex::encode(self.hash));
info!("Transaction hash is {:?}", hex::encode(self.hash()));
info!("Transaction tx_kind is {:?}", self.tx_kind);
info!("Transaction execution_input is {:?}", {
if let Ok(action) = serde_json::from_slice::<ActionData>(&self.execution_input) {

View File

@ -304,7 +304,6 @@ mod tests {
let mut rng = rand::thread_rng();
Transaction {
hash,
tx_kind: TxKind::Private,
execution_input: vec![],
execution_output: vec![],

View File

@ -75,7 +75,6 @@ impl SequencerCore {
tx_roots: [[u8; 32]; 2],
) -> Result<(), TransactionMalformationErrorKind> {
let Transaction {
hash,
tx_kind,
ref execution_input,
ref execution_output,
@ -87,7 +86,7 @@ impl SequencerCore {
let mempool_size = self.mempool.len();
if mempool_size >= self.sequencer_config.max_num_tx_in_block {
return Err(TransactionMalformationErrorKind::MempoolFullForRound { tx: *hash });
return Err(TransactionMalformationErrorKind::MempoolFullForRound { tx: tx.hash() });
}
let curr_sequencer_roots = self.get_tree_roots();
@ -95,7 +94,7 @@ impl SequencerCore {
if tx_roots != curr_sequencer_roots {
return Err(
TransactionMalformationErrorKind::ChainStateFurtherThanTransactionState {
tx: *hash,
tx: tx.hash(),
},
);
}
@ -109,7 +108,7 @@ impl SequencerCore {
//Public transactions can not make private operations.
return Err(
TransactionMalformationErrorKind::PublicTransactionChangedPrivateData {
tx: *hash,
tx: tx.hash(),
},
);
}
@ -121,7 +120,7 @@ impl SequencerCore {
//between public and private state.
return Err(
TransactionMalformationErrorKind::PrivateTransactionChangedPublicData {
tx: *hash,
tx: tx.hash(),
},
);
}
@ -130,7 +129,7 @@ impl SequencerCore {
};
//Tree checks
let tx_tree_check = self.store.pub_tx_store.get_tx(*hash).is_some();
let tx_tree_check = self.store.pub_tx_store.get_tx(tx.hash()).is_some();
let nullifier_tree_check = nullifier_created_hashes
.iter()
.map(|nullifier_hash| {
@ -150,18 +149,18 @@ impl SequencerCore {
.any(|check| check);
if tx_tree_check {
return Err(TransactionMalformationErrorKind::TxHashAlreadyPresentInTree { tx: *hash });
return Err(TransactionMalformationErrorKind::TxHashAlreadyPresentInTree { tx: tx.hash() });
}
if nullifier_tree_check {
return Err(
TransactionMalformationErrorKind::NullifierAlreadyPresentInTree { tx: *hash },
TransactionMalformationErrorKind::NullifierAlreadyPresentInTree { tx: tx.hash() },
);
}
if utxo_commitments_check {
return Err(
TransactionMalformationErrorKind::UTXOCommitmentAlreadyPresentInTree { tx: *hash },
TransactionMalformationErrorKind::UTXOCommitmentAlreadyPresentInTree { tx: tx.hash() },
);
}
@ -185,8 +184,6 @@ impl SequencerCore {
tx: TransactionMempool,
) -> Result<(), TransactionMalformationErrorKind> {
let Transaction {
// ToDo: remove hashing of transactions on node side [Issue #66]
hash: _,
ref utxo_commitments_created_hashes,
ref nullifier_created_hashes,
..
@ -280,7 +277,6 @@ mod tests {
}
fn create_dummy_transaction(
hash: TreeHashType,
nullifier_created_hashes: Vec<[u8; 32]>,
utxo_commitments_spent_hashes: Vec<[u8; 32]>,
utxo_commitments_created_hashes: Vec<[u8; 32]>,
@ -288,7 +284,6 @@ mod tests {
let mut rng = rand::thread_rng();
Transaction {
hash,
tx_kind: TxKind::Private,
execution_input: vec![],
execution_output: vec![],
@ -306,8 +301,8 @@ mod tests {
}
}
fn common_setup(mut sequencer: &mut SequencerCore) {
let tx = create_dummy_transaction([12; 32], vec![[9; 32]], vec![[7; 32]], vec![[8; 32]]);
fn common_setup(sequencer: &mut SequencerCore) {
let tx = create_dummy_transaction(vec![[9; 32]], vec![[7; 32]], vec![[8; 32]]);
let tx_mempool = TransactionMempool { tx };
sequencer.mempool.push_item(tx_mempool);
@ -342,7 +337,7 @@ mod tests {
common_setup(&mut sequencer);
let tx = create_dummy_transaction([1; 32], vec![[91; 32]], vec![[71; 32]], vec![[81; 32]]);
let tx = create_dummy_transaction(vec![[91; 32]], vec![[71; 32]], vec![[81; 32]]);
let tx_roots = sequencer.get_tree_roots();
let result = sequencer.transaction_pre_check(&tx, tx_roots);
@ -359,7 +354,7 @@ mod tests {
common_setup(&mut sequencer);
let tx = create_dummy_transaction([2; 32], vec![[92; 32]], vec![[72; 32]], vec![[82; 32]]);
let tx = create_dummy_transaction(vec![[92; 32]], vec![[72; 32]], vec![[82; 32]]);
let tx_roots = sequencer.get_tree_roots();
// Fill the mempool
@ -381,7 +376,7 @@ mod tests {
common_setup(&mut sequencer);
let tx = create_dummy_transaction([3; 32], vec![[93; 32]], vec![[73; 32]], vec![[83; 32]]);
let tx = create_dummy_transaction(vec![[93; 32]], vec![[73; 32]], vec![[83; 32]]);
let tx_roots = sequencer.get_tree_roots();
let tx_mempool = TransactionMempool { tx };
@ -395,7 +390,7 @@ mod tests {
let config = setup_sequencer_config();
let mut sequencer = SequencerCore::start_from_config(config);
let tx = create_dummy_transaction([4; 32], vec![[94; 32]], vec![[7; 32]], vec![[8; 32]]);
let tx = create_dummy_transaction(vec![[94; 32]], vec![[7; 32]], vec![[8; 32]]);
let tx_mempool = TransactionMempool { tx };
sequencer.mempool.push_item(tx_mempool);

View File

@ -38,6 +38,6 @@ impl MemPoolItem for TransactionMempool {
type Identifier = TreeHashType;
fn identifier(&self) -> Self::Identifier {
self.tx.hash
self.tx.hash()
}
}