Skip to content

Commit f114f03

Browse files
committed
refactor: create mock validator for test
1 parent d6e966e commit f114f03

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

Cargo.lock

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "nightly-2025-08-04"
3+
components = ["clippy", "rustfmt", "rust-analyzer"]

zkvm_execution_layer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ tracing = { workspace = true }
2727

2828

2929
[dev-dependencies]
30+
mockall = "0.12"

zkvm_execution_layer/src/dummy_proof_verifier.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::ethproofs_demo::validate_proof;
1+
use crate::ethproofs_demo::{EthproofsValidator, ProofValidator};
22
use crate::proof_verification::{ProofVerificationResult, ProofVerifier, VerificationError};
3+
use std::sync::Arc;
34
use std::time::Duration;
45
use tracing::debug;
56
use types::{ExecutionProof, ExecutionProofId};
@@ -12,6 +13,7 @@ use types::{ExecutionProof, ExecutionProofId};
1213
pub struct DummyVerifier {
1314
proof_id: ExecutionProofId,
1415
verification_delay: Duration,
16+
validator: Arc<dyn ProofValidator>,
1517
}
1618

1719
impl DummyVerifier {
@@ -20,6 +22,7 @@ impl DummyVerifier {
2022
Self {
2123
proof_id,
2224
verification_delay: Duration::from_millis(0),
25+
validator: Arc::new(EthproofsValidator),
2326
}
2427
}
2528

@@ -28,6 +31,20 @@ impl DummyVerifier {
2831
Self {
2932
proof_id,
3033
verification_delay: delay,
34+
validator: Arc::new(EthproofsValidator),
35+
}
36+
}
37+
38+
/// Create a new dummy verifier with a custom validator (for testing)
39+
#[cfg(test)]
40+
fn with_validator(
41+
proof_id: ExecutionProofId,
42+
validator: Arc<dyn ProofValidator>,
43+
) -> Self {
44+
Self {
45+
proof_id,
46+
verification_delay: Duration::from_millis(0),
47+
validator,
3148
}
3249
}
3350
}
@@ -50,8 +67,8 @@ impl ProofVerifier for DummyVerifier {
5067
"[Ethproofs] Verifying proof"
5168
);
5269

53-
// Perform cryptographic verification using Ethproofs verifiers
54-
Ok(validate_proof(proof))
70+
// Perform cryptographic verification using the injected validator
71+
Ok(self.validator.validate(proof))
5572
}
5673

5774
fn proof_id(&self) -> ExecutionProofId {
@@ -62,13 +79,20 @@ impl ProofVerifier for DummyVerifier {
6279
#[cfg(test)]
6380
mod tests {
6481
use super::*;
65-
use types::{ExecutionBlockHash, FixedBytesExtended};
82+
use mockall::mock;
83+
use types::{ExecutionBlockHash, FixedBytesExtended, Hash256, Slot};
84+
85+
mock! {
86+
TestValidator {}
87+
impl ProofValidator for TestValidator {
88+
fn validate(&self, proof: &ExecutionProof) -> bool;
89+
}
90+
}
6691

6792
fn create_test_proof(
6893
subnet_id: ExecutionProofId,
6994
block_hash: types::ExecutionBlockHash,
7095
) -> ExecutionProof {
71-
use types::{Hash256, Slot};
7296
ExecutionProof::new(
7397
subnet_id,
7498
Slot::new(100),
@@ -82,10 +106,17 @@ mod tests {
82106
#[tokio::test]
83107
async fn test_dummy_verifier_success() {
84108
let subnet = ExecutionProofId::new(0).unwrap();
85-
let verifier = DummyVerifier::new(subnet);
86109
let block_hash = ExecutionBlockHash::zero();
87110
let proof = create_test_proof(subnet, block_hash);
88111

112+
let mut mock_validator = MockTestValidator::new();
113+
mock_validator
114+
.expect_validate()
115+
.withf(move |p| p.proof_id == subnet)
116+
.returning(|_| true);
117+
118+
let verifier = DummyVerifier::with_validator(subnet, Arc::new(mock_validator));
119+
89120
let result = verifier.verify(&proof);
90121
assert!(result.is_ok());
91122
assert!(result.unwrap());
@@ -95,10 +126,12 @@ mod tests {
95126
async fn test_dummy_verifier_wrong_subnet() {
96127
let subnet_0 = ExecutionProofId::new(0).unwrap();
97128
let subnet_1 = ExecutionProofId::new(1).unwrap();
98-
let verifier = DummyVerifier::new(subnet_0);
99129
let block_hash = ExecutionBlockHash::zero();
100130
let proof = create_test_proof(subnet_1, block_hash);
101131

132+
let mock_validator = MockTestValidator::new();
133+
let verifier = DummyVerifier::with_validator(subnet_0, Arc::new(mock_validator));
134+
102135
let result = verifier.verify(&proof);
103136
assert!(result.is_err());
104137
assert!(matches!(

zkvm_execution_layer/src/ethproofs_demo.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ use std::time::{Duration, Instant};
77
use tracing::{debug, warn};
88
use types::ExecutionProof;
99

10+
/// Trait for validating proofs
11+
pub trait ProofValidator: Send + Sync {
12+
/// Validate a proof using the verifier store
13+
fn validate(&self, proof: &ExecutionProof) -> bool;
14+
}
15+
16+
/// Default implementation using the Ethproofs verifier store
17+
pub struct EthproofsValidator;
18+
19+
impl ProofValidator for EthproofsValidator {
20+
fn validate(&self, proof: &ExecutionProof) -> bool {
21+
validate_proof(proof)
22+
}
23+
}
24+
1025
/// Global verification key store, loaded once on first access
1126
pub static VERIFICATION_KEY_STORE: Lazy<Option<VerificationKeyStore>> =
1227
Lazy::new(|| match VerificationKeyStore::load_embedded() {

0 commit comments

Comments
 (0)