Skip to content

Commit a39bee1

Browse files
committed
Refactor test
1 parent 31e2542 commit a39bee1

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

mithril-common/src/messages/cardano_transactions_proof.rs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -325,55 +325,72 @@ mod tests {
325325
#[tokio::test]
326326
async fn verify_hashes_from_verified_cardano_transaction_and_from_signable_builder_are_equals()
327327
{
328-
let transaction_1 = CardanoTransaction::new("tx-hash-123", 1, 1);
329-
let transaction_2 = CardanoTransaction::new("tx-hash-456", 2, 1);
330-
let transaction_3 = CardanoTransaction::new("tx-hash-789", 3, 1);
331-
let transaction_4 = CardanoTransaction::new("tx-hash-abc", 4, 1);
328+
let transactions = vec![
329+
CardanoTransaction::new("tx-hash-123", 1, 1),
330+
CardanoTransaction::new("tx-hash-456", 2, 1),
331+
];
332332

333-
let transactions = vec![transaction_1, transaction_2, transaction_3, transaction_4];
333+
assert_eq!(
334+
from_verified_cardano_transaction(&transactions, 99999).compute_hash(),
335+
from_signable_builder(&transactions, 99999)
336+
.await
337+
.compute_hash()
338+
);
339+
340+
assert_ne!(
341+
from_verified_cardano_transaction(&transactions, 99999).compute_hash(),
342+
from_signable_builder(&transactions, 123456)
343+
.await
344+
.compute_hash()
345+
);
346+
}
347+
348+
fn from_verified_cardano_transaction(
349+
transactions: &[CardanoTransaction],
350+
immutable_file_number: u64,
351+
) -> ProtocolMessage {
334352
let transactions_hashes = transactions
335353
.iter()
336354
.map(|t| t.transaction_hash.clone())
337355
.collect::<Vec<_>>();
338-
let latest_immutable_file_number = 99999;
339-
340-
let message = {
341-
let proof = MKProof::from_leaves(&transactions).unwrap();
342-
let set_proof = CardanoTransactionsSetProof::new(transactions_hashes, proof);
343-
344-
let verified_transactions_fake = VerifiedCardanoTransactions {
345-
certificate_hash: "whatever".to_string(),
346-
merkle_root: set_proof.merkle_root(),
347-
certified_transactions: set_proof.transactions_hashes().to_vec(),
348-
latest_immutable_file_number,
349-
};
350-
351-
let mut message = ProtocolMessage::new();
352-
verified_transactions_fake.fill_protocol_message(&mut message);
353-
message
354-
};
355356

356-
let from_signable_builder = {
357-
let mut mock_transaction_store = MockTransactionStore::new();
358-
mock_transaction_store
359-
.expect_store_transactions()
360-
.returning(|_| Ok(()));
361-
362-
let cardano_transaction_signable_builder = CardanoTransactionsSignableBuilder::new(
363-
Arc::new(DumbTransactionParser::new(transactions.clone())),
364-
Arc::new(mock_transaction_store),
365-
Path::new("/tmp"),
366-
Logger::root(slog::Discard, slog::o!()),
367-
);
368-
cardano_transaction_signable_builder
369-
.compute_protocol_message(Beacon {
370-
immutable_file_number: latest_immutable_file_number,
371-
..Beacon::default()
372-
})
373-
.await
374-
.unwrap()
357+
let proof = MKProof::from_leaves(transactions).unwrap();
358+
let set_proof = CardanoTransactionsSetProof::new(transactions_hashes, proof);
359+
360+
let verified_transactions_fake = VerifiedCardanoTransactions {
361+
certificate_hash: "whatever".to_string(),
362+
merkle_root: set_proof.merkle_root(),
363+
certified_transactions: set_proof.transactions_hashes().to_vec(),
364+
latest_immutable_file_number: immutable_file_number,
375365
};
376366

377-
assert!(message.compute_hash() == from_signable_builder.compute_hash());
367+
let mut message = ProtocolMessage::new();
368+
verified_transactions_fake.fill_protocol_message(&mut message);
369+
370+
message
371+
}
372+
373+
async fn from_signable_builder(
374+
transactions: &[CardanoTransaction],
375+
immutable_file_number: u64,
376+
) -> ProtocolMessage {
377+
let mut mock_transaction_store = MockTransactionStore::new();
378+
mock_transaction_store
379+
.expect_store_transactions()
380+
.returning(|_| Ok(()));
381+
382+
let cardano_transaction_signable_builder = CardanoTransactionsSignableBuilder::new(
383+
Arc::new(DumbTransactionParser::new(transactions.to_vec())),
384+
Arc::new(mock_transaction_store),
385+
Path::new("/tmp"),
386+
Logger::root(slog::Discard, slog::o!()),
387+
);
388+
cardano_transaction_signable_builder
389+
.compute_protocol_message(Beacon {
390+
immutable_file_number,
391+
..Beacon::default()
392+
})
393+
.await
394+
.unwrap()
378395
}
379396
}

0 commit comments

Comments
 (0)