Skip to content

Commit 1bac052

Browse files
committed
Refactor
- Extract common code - Rename tests - Inline methods
1 parent b3070eb commit 1bac052

1 file changed

Lines changed: 34 additions & 42 deletions

File tree

crates/charon/src/eth2wrap/valcache.rs

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ mod tests {
264264
};
265265
use wiremock::{
266266
Mock, MockServer, ResponseTemplate,
267-
matchers::{method, path_regex},
267+
matchers::{method, path},
268268
};
269269

270270
#[tokio::test]
271-
async fn validator_cache() {
271+
async fn get_by_head_successful_fetch() {
272272
// Create a set of validators with different statuses (some active, some not)
273273
let pubkeys = (0..10)
274274
.map(|i| test_pubkey(i as u8))
@@ -305,11 +305,7 @@ mod tests {
305305

306306
// Create a mock server that tracks request count
307307
let mock_server = MockServer::start().await;
308-
Mock::given(method("POST"))
309-
.and(path_regex(r"/eth/v1/beacon/states/head/validators"))
310-
.respond_with(
311-
ResponseTemplate::new(200).set_body_json(success_response_body(datums.to_vec())),
312-
)
308+
post_state_validators_success("head", datums.to_vec())
313309
.expect(2) // Should be called exactly twice (once before trim, once after)
314310
.mount(&mock_server)
315311
.await;
@@ -349,12 +345,11 @@ mod tests {
349345
}
350346

351347
#[tokio::test]
352-
async fn get_by_head_returns_error_when_request_fails() {
348+
async fn get_by_head_fail_fetch() {
353349
// Create a mock server that returns a 404 error
354350
let mock_server = MockServer::start().await;
355-
Mock::given(method("POST"))
356-
.and(path_regex(r"/eth/v1/beacon/states/head/validators"))
357-
.respond_with(ResponseTemplate::new(404).set_body_json(not_found_response_body()))
351+
352+
post_state_validators_not_found("head")
358353
.expect(1)
359354
.mount(&mock_server)
360355
.await;
@@ -388,7 +383,7 @@ mod tests {
388383
// Set up mock server with different responses based on slot
389384
let mock_server = MockServer::start().await;
390385

391-
endpoint_success(
386+
post_state_validators_success(
392387
"1",
393388
vec![
394389
test_validator_datum(0, &pubkeys[0], ValidatorStatus::PendingQueued),
@@ -398,7 +393,7 @@ mod tests {
398393
.mount(&mock_server)
399394
.await;
400395

401-
endpoint_success(
396+
post_state_validators_success(
402397
"2",
403398
vec![
404399
test_validator_datum(0, &pubkeys[0], ValidatorStatus::ActiveOngoing),
@@ -408,7 +403,7 @@ mod tests {
408403
.mount(&mock_server)
409404
.await;
410405

411-
endpoint_success(
406+
post_state_validators_success(
412407
"11",
413408
vec![
414409
test_validator_datum(0, &pubkeys[0], ValidatorStatus::PendingQueued),
@@ -418,12 +413,17 @@ mod tests {
418413
.mount(&mock_server)
419414
.await;
420415

421-
endpoint_not_found("3").mount(&mock_server).await;
422-
endpoint_not_found("head").mount(&mock_server).await;
416+
post_state_validators_not_found("3")
417+
.mount(&mock_server)
418+
.await;
419+
post_state_validators_not_found("head")
420+
.mount(&mock_server)
421+
.await;
423422

424423
let eth2_cl = EthBeaconNodeApiClient::with_base_url(mock_server.uri())
425424
.expect("Failed to create client");
426425

426+
// Create a cache.
427427
let cache = ValidatorCache::new(eth2_cl, pubkeys.clone());
428428

429429
// Test slot 1: 1 active validator (index 1), 2 complete, refreshed_by_slot=true
@@ -490,44 +490,36 @@ mod tests {
490490
}
491491
}
492492

493-
fn endpoint_success(
493+
fn post_state_validators_success(
494494
state_id: impl AsRef<str>,
495495
validators: Vec<GetStateValidatorsResponseResponseDatum>,
496496
) -> Mock {
497497
Mock::given(method("POST"))
498-
.and(path_regex(format!(
498+
.and(path(format!(
499499
"/eth/v1/beacon/states/{}/validators",
500500
state_id.as_ref()
501501
)))
502-
.respond_with(
503-
ResponseTemplate::new(200).set_body_json(success_response_body(validators)),
504-
)
502+
.respond_with(ResponseTemplate::new(200).set_body_json(
503+
GetStateValidatorsResponseResponse {
504+
execution_optimistic: false,
505+
finalized: true,
506+
data: validators,
507+
},
508+
))
505509
}
506510

507-
fn endpoint_not_found(state_id: impl AsRef<str>) -> Mock {
511+
fn post_state_validators_not_found(state_id: impl AsRef<str>) -> Mock {
508512
Mock::given(method("POST"))
509-
.and(path_regex(format!(
513+
.and(path(format!(
510514
"/eth/v1/beacon/states/{}/validators",
511515
state_id.as_ref()
512516
)))
513-
.respond_with(ResponseTemplate::new(404).set_body_json(not_found_response_body()))
514-
}
515-
516-
fn success_response_body(
517-
validators: Vec<GetStateValidatorsResponseResponseDatum>,
518-
) -> GetStateValidatorsResponseResponse {
519-
GetStateValidatorsResponseResponse {
520-
execution_optimistic: false,
521-
finalized: true,
522-
data: validators,
523-
}
524-
}
525-
526-
fn not_found_response_body() -> BlindedBlock400Response {
527-
BlindedBlock400Response {
528-
code: 404.0,
529-
message: "State not found".to_string(),
530-
stacktraces: None,
531-
}
517+
.respond_with(
518+
ResponseTemplate::new(404).set_body_json(BlindedBlock400Response {
519+
code: 404.0,
520+
message: "State not found".to_string(),
521+
stacktraces: None,
522+
}),
523+
)
532524
}
533525
}

0 commit comments

Comments
 (0)