|
| 1 | +use mithril_aggregator::Configuration; |
| 2 | +use mithril_common::{ |
| 3 | + entities::{ |
| 4 | + CardanoDbBeacon, CardanoTransactionsSigningConfig, ChainPoint, Epoch, |
| 5 | + ProtocolMessagePartKey, ProtocolParameters, SignedEntityType, |
| 6 | + SignedEntityTypeDiscriminants, TimePoint, |
| 7 | + }, |
| 8 | + test_utils::MithrilFixtureBuilder, |
| 9 | +}; |
| 10 | +use test_extensions::{utilities::get_test_dir, ExpectedCertificate, RuntimeTester}; |
| 11 | + |
| 12 | +use crate::test_extensions::utilities::tx_hash; |
| 13 | + |
| 14 | +mod test_extensions; |
| 15 | + |
| 16 | +#[tokio::test(flavor = "multi_thread")] |
| 17 | +async fn prove_transactions() { |
| 18 | + let protocol_parameters = ProtocolParameters { |
| 19 | + k: 5, |
| 20 | + m: 150, |
| 21 | + phi_f: 0.95, |
| 22 | + }; |
| 23 | + let configuration = Configuration { |
| 24 | + protocol_parameters: protocol_parameters.clone(), |
| 25 | + signed_entity_types: Some(SignedEntityTypeDiscriminants::CardanoTransactions.to_string()), |
| 26 | + data_stores_directory: get_test_dir("prove_transactions"), |
| 27 | + cardano_transactions_signing_config: CardanoTransactionsSigningConfig { |
| 28 | + security_parameter: 0, |
| 29 | + step: 30, |
| 30 | + }, |
| 31 | + ..Configuration::new_sample() |
| 32 | + }; |
| 33 | + let mut tester = RuntimeTester::build( |
| 34 | + TimePoint { |
| 35 | + epoch: Epoch(1), |
| 36 | + immutable_file_number: 1, |
| 37 | + chain_point: ChainPoint { |
| 38 | + slot_number: 10, |
| 39 | + block_number: 100, |
| 40 | + block_hash: "block_hash-100".to_string(), |
| 41 | + }, |
| 42 | + }, |
| 43 | + configuration, |
| 44 | + ) |
| 45 | + .await; |
| 46 | + let observer = tester.observer.clone(); |
| 47 | + let prover = tester.dependencies.prover_service.clone(); |
| 48 | + |
| 49 | + comment!("create signers & declare stake distribution"); |
| 50 | + let fixture = MithrilFixtureBuilder::default() |
| 51 | + .with_signers(10) |
| 52 | + .with_protocol_parameters(protocol_parameters.clone()) |
| 53 | + .build(); |
| 54 | + let signers = &fixture.signers_fixture(); |
| 55 | + |
| 56 | + tester.init_state_from_fixture(&fixture).await.unwrap(); |
| 57 | + |
| 58 | + comment!("Boostrap the genesis certificate"); |
| 59 | + tester.register_genesis_certificate(&fixture).await.unwrap(); |
| 60 | + |
| 61 | + assert_last_certificate_eq!( |
| 62 | + tester, |
| 63 | + ExpectedCertificate::new_genesis( |
| 64 | + CardanoDbBeacon::new("devnet", 1, 1), |
| 65 | + fixture.compute_and_encode_avk() |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + // Lock all signed entity types except CardanoTransactions to limit the scope of the test |
| 70 | + for entity in SignedEntityTypeDiscriminants::all() |
| 71 | + .into_iter() |
| 72 | + .filter(|e| e != &SignedEntityTypeDiscriminants::CardanoTransactions) |
| 73 | + { |
| 74 | + tester |
| 75 | + .dependencies |
| 76 | + .signed_entity_type_lock |
| 77 | + .lock(entity) |
| 78 | + .await; |
| 79 | + } |
| 80 | + |
| 81 | + comment!("register signers"); |
| 82 | + cycle!(tester, "ready"); |
| 83 | + tester |
| 84 | + .register_signers(&fixture.signers_fixture()) |
| 85 | + .await |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + comment!( |
| 89 | + "Increase cardano chain block number to 185, |
| 90 | + the state machine should be signing CardanoTransactions up to block 179 included" |
| 91 | + ); |
| 92 | + tester.increase_block_number(85, 185).await.unwrap(); |
| 93 | + cycle!(tester, "signing"); |
| 94 | + tester |
| 95 | + .send_single_signatures(SignedEntityTypeDiscriminants::CardanoTransactions, signers) |
| 96 | + .await |
| 97 | + .unwrap(); |
| 98 | + |
| 99 | + comment!("The state machine should issue a certificate for the CardanoTransactions"); |
| 100 | + cycle!(tester, "ready"); |
| 101 | + assert_last_certificate_eq!( |
| 102 | + tester, |
| 103 | + ExpectedCertificate::new( |
| 104 | + CardanoDbBeacon::new("devnet", 1, 1), |
| 105 | + &signers |
| 106 | + .iter() |
| 107 | + .map(|s| s.signer_with_stake.clone().into()) |
| 108 | + .collect::<Vec<_>>(), |
| 109 | + fixture.compute_and_encode_avk(), |
| 110 | + SignedEntityType::CardanoTransactions(Epoch(1), 179), |
| 111 | + ExpectedCertificate::genesis_identifier(&CardanoDbBeacon::new("devnet", 1, 1)), |
| 112 | + ) |
| 113 | + ); |
| 114 | + |
| 115 | + cycle!(tester, "ready"); |
| 116 | + |
| 117 | + comment!("Get the proof for the last transaction, BlockNumber(179), and verify it"); |
| 118 | + let last_transaction_hash = tx_hash(179, 1); |
| 119 | + let last_tx_snapshot = observer |
| 120 | + .get_last_cardano_transactions_snapshot() |
| 121 | + .await |
| 122 | + .unwrap(); |
| 123 | + let proof_for_last_transaction = prover |
| 124 | + .compute_transactions_proofs( |
| 125 | + last_tx_snapshot.artifact.block_number, |
| 126 | + &[last_transaction_hash.clone()], |
| 127 | + ) |
| 128 | + .await |
| 129 | + .unwrap() |
| 130 | + .pop() |
| 131 | + .unwrap(); |
| 132 | + assert!(proof_for_last_transaction |
| 133 | + .transactions_hashes() |
| 134 | + .contains(&last_transaction_hash)); |
| 135 | + |
| 136 | + proof_for_last_transaction.verify().unwrap(); |
| 137 | + |
| 138 | + comment!("Get the certificate associated with the last transaction and check that it matches the proof"); |
| 139 | + let proof_merkle_root = proof_for_last_transaction.merkle_root(); |
| 140 | + let proof_certificate = observer.get_last_certificate().await.unwrap(); |
| 141 | + assert_eq!(&last_tx_snapshot.certificate_id, &proof_certificate.hash); |
| 142 | + assert_eq!( |
| 143 | + proof_certificate |
| 144 | + .protocol_message |
| 145 | + .get_message_part(&ProtocolMessagePartKey::CardanoTransactionsMerkleRoot), |
| 146 | + Some(&proof_merkle_root), |
| 147 | + "The proof merkle root should match the one in the certificate" |
| 148 | + ); |
| 149 | +} |
0 commit comments