|
| 1 | +//! Marshal storage conformance tests. |
| 2 | +
|
| 3 | +use super::mocks::{ |
| 4 | + application::Application, |
| 5 | + harness::{ |
| 6 | + self, CodingHarness, StandardHarness, TestHarness, ValidatorHandle, ValidatorSetup, |
| 7 | + BLOCKS_PER_EPOCH, NAMESPACE, NUM_VALIDATORS, QUORUM, V, |
| 8 | + }, |
| 9 | +}; |
| 10 | +use crate::{ |
| 11 | + simplex::{scheme::bls12381_threshold::vrf as bls12381_threshold_vrf, types::Proposal}, |
| 12 | + types::{Epoch, Height, Round, View}, |
| 13 | +}; |
| 14 | +use commonware_conformance::{conformance_tests, Conformance}; |
| 15 | +use commonware_cryptography::certificate::{mocks::Fixture, ConstantProvider}; |
| 16 | +use commonware_runtime::{deterministic, Clock, Runner, Supervisor as _}; |
| 17 | +use commonware_utils::NZUsize; |
| 18 | +use rand::Rng; |
| 19 | +use std::time::Duration; |
| 20 | + |
| 21 | +const CASES: usize = 32; |
| 22 | + |
| 23 | +struct StandardStorageConformance; |
| 24 | +struct CodingStorageConformance; |
| 25 | + |
| 26 | +impl Conformance for StandardStorageConformance { |
| 27 | + async fn commit(seed: u64) -> Vec<u8> { |
| 28 | + marshal_commit::<StandardHarness>(seed) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Conformance for CodingStorageConformance { |
| 33 | + async fn commit(seed: u64) -> Vec<u8> { |
| 34 | + marshal_commit::<CodingHarness>(seed) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn marshal_commit<H: TestHarness>(seed: u64) -> Vec<u8> { |
| 39 | + let runner = deterministic::Runner::new( |
| 40 | + deterministic::Config::default() |
| 41 | + .with_seed(seed) |
| 42 | + .with_timeout(Some(Duration::from_secs(30))), |
| 43 | + ); |
| 44 | + runner.start(|mut context| async move { |
| 45 | + let Fixture { |
| 46 | + participants, |
| 47 | + schemes, |
| 48 | + .. |
| 49 | + } = bls12381_threshold_vrf::fixture::<V, _>(&mut context, NAMESPACE, NUM_VALIDATORS); |
| 50 | + let mut oracle = harness::setup_network_with_participants( |
| 51 | + context.child("network"), |
| 52 | + NZUsize!(1), |
| 53 | + participants.clone(), |
| 54 | + ) |
| 55 | + .await; |
| 56 | + |
| 57 | + let validator = participants[0].clone(); |
| 58 | + let provider = ConstantProvider::new(schemes[0].clone()); |
| 59 | + let application = Application::<H::ApplicationBlock>::manual_ack(); |
| 60 | + let setup = H::setup_validator_with( |
| 61 | + context.child("validator"), |
| 62 | + &mut oracle, |
| 63 | + validator, |
| 64 | + provider, |
| 65 | + NZUsize!(1), |
| 66 | + application, |
| 67 | + ) |
| 68 | + .await; |
| 69 | + |
| 70 | + assert_eq!(setup.application.acknowledged().await, Height::zero()); |
| 71 | + wait_processed(&mut context, &setup, Height::zero()).await; |
| 72 | + |
| 73 | + let mut handle = ValidatorHandle::<H> { |
| 74 | + mailbox: setup.mailbox.clone(), |
| 75 | + extra: setup.extra.clone(), |
| 76 | + }; |
| 77 | + let mut peers = Vec::<ValidatorHandle<H>>::new(); |
| 78 | + let mut parent = H::genesis_block(NUM_VALIDATORS as u16); |
| 79 | + let count = context.gen_range(1..=BLOCKS_PER_EPOCH.get().min(4)); |
| 80 | + for height in 1..=count { |
| 81 | + let height = Height::new(height); |
| 82 | + let round = Round::new(Epoch::zero(), View::new(height.get())); |
| 83 | + let parent_view = height |
| 84 | + .previous() |
| 85 | + .map_or(View::zero(), |h| View::new(h.get())); |
| 86 | + let block = H::make_test_block( |
| 87 | + H::digest(&parent), |
| 88 | + H::commitment(&parent), |
| 89 | + height, |
| 90 | + context.gen(), |
| 91 | + NUM_VALIDATORS as u16, |
| 92 | + ); |
| 93 | + H::verify(&mut handle, round, &block, &mut peers).await; |
| 94 | + |
| 95 | + let proposal = Proposal::new(round, parent_view, H::commitment(&block)); |
| 96 | + let finalization = H::make_finalization(proposal, &schemes, QUORUM); |
| 97 | + let mut mailbox = setup.mailbox.clone(); |
| 98 | + H::report_finalization(&mut mailbox, finalization).await; |
| 99 | + |
| 100 | + assert_eq!(setup.application.acknowledged().await, height); |
| 101 | + wait_processed(&mut context, &setup, height).await; |
| 102 | + parent = block; |
| 103 | + } |
| 104 | + |
| 105 | + setup.actor_handle.abort(); |
| 106 | + let _ = setup.actor_handle.await; |
| 107 | + context.storage_audit().to_vec() |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +async fn wait_processed<H: TestHarness>( |
| 112 | + context: &mut deterministic::Context, |
| 113 | + setup: &ValidatorSetup<H>, |
| 114 | + height: Height, |
| 115 | +) { |
| 116 | + loop { |
| 117 | + if setup.mailbox.get_processed_height().await == Some(height) { |
| 118 | + break; |
| 119 | + } |
| 120 | + context.sleep(Duration::from_millis(1)).await; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +conformance_tests! { |
| 125 | + StandardStorageConformance => CASES, |
| 126 | + CodingStorageConformance => CASES, |
| 127 | +} |
0 commit comments