Skip to content

Commit

Permalink
tests: Also cover unhappy case when we store duplicate transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed Feb 5, 2025
1 parent d3e9c9e commit 5dc7e60
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion crates/fraud_proofs/global_merkle_root/storage/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ where
.storage_as_mut::<ProcessedTransactions>()
.replace(&tx_id, &())?;

assert!(previous_tx.is_none());
if previous_tx.is_some() {
anyhow::bail!("duplicate transaction detected")
};

Ok(())
}
Expand Down Expand Up @@ -543,6 +545,8 @@ mod tests {
}

#[test]
/// After processing a transaction,
/// it should be stored in the `ProcessedTransactions` table.
fn process_transaction__should_store_processed_transaction() {
// Given
let mut storage: InMemoryStorage<Column> = InMemoryStorage::default();
Expand Down Expand Up @@ -571,6 +575,32 @@ mod tests {
.is_some());
}

#[test]
/// We get an error if we encounter the same transaction
/// twice in `process_transaction`.
fn process_transaction__should_error_on_duplicate_transaction() {
// Given
let mut storage: InMemoryStorage<Column> = InMemoryStorage::default();
let mut storage_tx = storage.write_transaction();
let mut storage_update_tx =
storage_tx.construct_update_merkleized_tables_transaction();

let block_height = BlockHeight::new(0);
let tx_idx = 0;
let tx = Transaction::default_test_tx();

// When
storage_update_tx
.process_transaction(block_height, tx_idx, &tx)
.unwrap();

let result_after_second_call =
storage_update_tx.process_transaction(block_height, tx_idx, &tx);

// Then
assert!(result_after_second_call.is_err());
}

fn random_utxo_id(rng: &mut impl rand::RngCore) -> UtxoId {
let mut txid = TxId::default();
rng.fill_bytes(txid.as_mut());
Expand Down

0 comments on commit 5dc7e60

Please sign in to comment.