-
Notifications
You must be signed in to change notification settings - Fork 40
testing(lightclient): Mocking DA layer #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+524
−192
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39445cf
feat: mock da
distractedm1nd 92df3a5
update
distractedm1nd d834c24
update
distractedm1nd a896e47
fix
distractedm1nd f727dd0
cleaner setup
distractedm1nd 5456356
another (failing) case
distractedm1nd af687eb
fixing bugs
distractedm1nd ed04f38
new test cases, some fixes
distractedm1nd e8705c5
ignoring too old headers
distractedm1nd fb791b8
simplifications
distractedm1nd de6e031
new test cases
distractedm1nd edf73f3
improvements
distractedm1nd d3f74f5
Merge branch 'main' into mock-da
distractedm1nd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod lightclient; | ||
pub use lightclient::LightClient; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
use std::sync::Arc; | ||
|
||
use prism_common::digest::Digest; | ||
use prism_da::{ | ||
MockLightDataAvailabilityLayer, MockVerifiableStateTransition, VerifiableStateTransition, | ||
events::{EventChannel, EventPublisher, EventSubscriber, PrismEvent}, | ||
}; | ||
use prism_keys::SigningKey; | ||
use tokio::spawn; | ||
|
||
use crate::LightClient; | ||
|
||
macro_rules! mock_da { | ||
($(($height:expr, $($spec:tt),+)),* $(,)?) => {{ | ||
let mut mock_da = MockLightDataAvailabilityLayer::new(); | ||
mock_da.expect_get_finalized_epoch().returning(move |height| { | ||
match height { | ||
$( | ||
$height => { | ||
let mut transitions = vec![]; | ||
$( | ||
let mut epoch = MockVerifiableStateTransition::new(); | ||
mock_da!(@make_epoch epoch, $spec, $height); | ||
transitions.push(Box::new(epoch) as Box<dyn VerifiableStateTransition>); | ||
)+ | ||
Ok(transitions) | ||
} | ||
)* | ||
_ => Ok(vec![]), | ||
} | ||
}); | ||
mock_da | ||
}}; | ||
|
||
// Success case - tuple | ||
(@make_epoch $epoch:ident, ($h1:expr, $h2:expr), $height:expr) => { | ||
let hash1 = $h1; | ||
let hash2 = $h2; | ||
$epoch.expect_height().returning(move || $height); | ||
$epoch.expect_verify().returning(move |_, _| { | ||
Ok((Digest::hash(hash1), Digest::hash(hash2)).into()) | ||
}); | ||
}; | ||
|
||
// Error case - Err(...) | ||
(@make_epoch $epoch:ident, Err($error:expr), $height:expr) => { | ||
let err = $error; | ||
$epoch.expect_height().returning(move || $height); | ||
$epoch.expect_verify().returning(move |_, _| Err(err)); | ||
}; | ||
|
||
// String error shorthand | ||
(@make_epoch $epoch:ident, $error:literal, $height:expr) => { | ||
let err_msg = $error; | ||
$epoch.expect_height().returning(move || $height); | ||
$epoch.expect_verify().returning(move |_, _| { | ||
Err(EpochVerificationError::ProofVerificationError(err_msg.to_string())) | ||
}); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
async fn wait_for_sync(sub: &mut EventSubscriber, target_height: u64) { | ||
wait_for_event(sub, |event| match event { | ||
PrismEvent::EpochVerified { height } => height >= target_height, | ||
PrismEvent::EpochVerificationFailed { height, .. } => height >= target_height, | ||
_ => false, | ||
}) | ||
.await; | ||
} | ||
|
||
macro_rules! assert_current_commitment { | ||
($lc:expr, $expected:expr) => { | ||
let actual = $lc.get_latest_commitment().await.unwrap(); | ||
assert_eq!(Digest::hash($expected), actual); | ||
}; | ||
} | ||
|
||
async fn wait_for_event<F>(sub: &mut EventSubscriber, mut handler: F) | ||
where | ||
F: FnMut(PrismEvent) -> bool, // return true to break the loop | ||
{ | ||
while let Ok(event_info) = sub.recv().await { | ||
if handler(event_info.event) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
async fn setup( | ||
mut mock_da: MockLightDataAvailabilityLayer, | ||
) -> (Arc<LightClient>, EventSubscriber, EventPublisher) { | ||
let chan = EventChannel::new(); | ||
let publisher = chan.publisher(); | ||
let arced_chan = Arc::new(chan); | ||
mock_da.expect_event_channel().return_const(arced_chan.clone()); | ||
|
||
let mock_da = Arc::new(mock_da); | ||
|
||
let prover_key = SigningKey::new_ed25519(); | ||
let lc = Arc::new(LightClient::new(mock_da, prover_key.verifying_key())); | ||
|
||
let runner = lc.clone(); | ||
spawn(async move { | ||
runner.run().await.unwrap(); | ||
}); | ||
let mut sub = arced_chan.clone().subscribe(); | ||
wait_for_event(&mut sub, |event| matches!(event, PrismEvent::Ready)).await; | ||
(lc, sub, publisher) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_realtime_sync() { | ||
let (lc, mut sub, publisher) = setup(mock_da![ | ||
(4, ("g", "a")), | ||
(5, ("a", "b")), | ||
(7, ("b", "c"), ("c", "d")), | ||
(8, ("d", "e")), | ||
]) | ||
.await; | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 3 }); | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 4 }); | ||
wait_for_sync(&mut sub, 4).await; | ||
assert_current_commitment!(lc, "a"); | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 5 }); | ||
wait_for_sync(&mut sub, 5).await; | ||
assert_current_commitment!(lc, "b"); | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 6 }); | ||
wait_for_sync(&mut sub, 6).await; | ||
assert_current_commitment!(lc, "b"); | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 7 }); | ||
wait_for_sync(&mut sub, 7).await; | ||
assert_current_commitment!(lc, "d"); | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 8 }); | ||
wait_for_sync(&mut sub, 8).await; | ||
assert_current_commitment!(lc, "e"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_backwards_sync() { | ||
let (lc, mut sub, publisher) = setup(mock_da![(8, ("a", "b"))]).await; | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 20 }); | ||
while let Ok(event_info) = sub.recv().await { | ||
if let PrismEvent::RecursiveVerificationCompleted { height } = event_info.event { | ||
assert_eq!(height, 8); | ||
assert_current_commitment!(lc, "b"); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_incoming_epoch_during_backwards_sync() { | ||
let (lc, mut sub, publisher) = setup(mock_da![(5000, ("a", "b")), (5101, ("c", "d"))]).await; | ||
|
||
publisher.send(PrismEvent::UpdateDAHeight { height: 5100 }); | ||
publisher.send(PrismEvent::UpdateDAHeight { height: 5101 }); | ||
while let Ok(event_info) = sub.recv().await { | ||
if let PrismEvent::RecursiveVerificationCompleted { height: _ } = event_info.event { | ||
assert_current_commitment!(lc, "d"); | ||
return; | ||
} | ||
} | ||
|
||
let sync_state = lc.get_sync_state().await; | ||
assert!(sync_state.initial_sync_completed); | ||
assert!(!sync_state.initial_sync_in_progress); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.