Skip to content

Commit 39445cf

Browse files
feat: mock da
1 parent 85d067f commit 39445cf

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

crates/da/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl From<EpochCommitments> for (Digest, Digest) {
6666

6767
/// `VerifiableStateTransition` is a trait wrapper around `FinalizedEpoch` that allows for mocking.
6868
/// The only concrete implementation of this trait is by `FinalizedEpoch`.
69+
#[automock]
6970
pub trait VerifiableStateTransition: Send {
7071
fn verify(
7172
&self,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
pub mod lightclient;
22
pub use lightclient::LightClient;
3+
4+
#[cfg(test)]
5+
mod tests;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)