|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use prism_common::digest::Digest; |
| 5 | +use prism_da::{ |
| 6 | + EpochVerificationError, MockLightDataAvailabilityLayer, MockVerifiableStateTransition, |
| 7 | + VerifiableEpoch, VerifiableStateTransition, |
| 8 | +}; |
| 9 | + |
| 10 | +macro_rules! mock_da { |
| 11 | + ($(($height:expr, $($spec:tt),+)),* $(,)?) => {{ |
| 12 | + let mut mock_da = MockLightDataAvailabilityLayer::new(); |
| 13 | + mock_da.expect_get_finalized_epoch().returning(move |height| { |
| 14 | + match height { |
| 15 | + $( |
| 16 | + $height => { |
| 17 | + let mut transitions = vec![]; |
| 18 | + $( |
| 19 | + let mut epoch = MockVerifiableStateTransition::new(); |
| 20 | + mock_da!(@make_epoch epoch, $spec); |
| 21 | + transitions.push(Box::new(epoch) as Box<dyn VerifiableStateTransition>); |
| 22 | + )+ |
| 23 | + Ok(transitions) |
| 24 | + } |
| 25 | + )* |
| 26 | + _ => Ok(vec![]), |
| 27 | + } |
| 28 | + }); |
| 29 | + mock_da |
| 30 | + }}; |
| 31 | + |
| 32 | + // Success case - tuple |
| 33 | + (@make_epoch $epoch:ident, ($h1:expr, $h2:expr)) => { |
| 34 | + let hash1 = $h1; |
| 35 | + let hash2 = $h2; |
| 36 | + $epoch.expect_verify().returning(move |_, _| { |
| 37 | + Ok((Digest::hash(hash1), Digest::hash(hash2))) |
| 38 | + }); |
| 39 | + }; |
| 40 | + |
| 41 | + // Error case - Err(...) |
| 42 | + (@make_epoch $epoch:ident, Err($error:expr)) => { |
| 43 | + let err = $error; |
| 44 | + $epoch.expect_verify().returning(move |_, _| Err(err)); |
| 45 | + }; |
| 46 | + |
| 47 | + // String error shorthand |
| 48 | + (@make_epoch $epoch:ident, $error:literal) => { |
| 49 | + let err_msg = $error; |
| 50 | + $epoch.expect_verify().returning(move |_, _| { |
| 51 | + Err(EpochVerificationError::ProofVerificationError(err_msg.to_string())) |
| 52 | + }); |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +fn setup() { |
| 57 | + let mut mock_da = MockLightDataAvailabilityLayer::new(); |
| 58 | + mock_da![ |
| 59 | + (5, ("abc", "def")), |
| 60 | + (6, ("ghi", "jkl"), ("xyz", "yuu")), |
| 61 | + (7, ("mno", "pqr")), |
| 62 | + (8, "Expected Error") |
| 63 | + ]; |
| 64 | +} |
0 commit comments