Skip to content

Commit 46fd17c

Browse files
committed
apollo_committer: reduce tests boilerplate
1 parent 6559f5c commit 46fd17c

1 file changed

Lines changed: 46 additions & 62 deletions

File tree

crates/apollo_committer/src/request_paths_and_commit_block_tests.rs

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use apollo_committer_types::committer_types::{
55
AccessedKeys,
66
CommitBlockRequest,
77
ReadPathsAndCommitBlockRequest,
8+
ReadPathsAndCommitBlockResponse,
89
RevertBlockRequest,
910
};
1011
use indexmap::indexmap;
@@ -172,6 +173,36 @@ fn read_paths_and_commit_block_request(
172173
}
173174
}
174175

176+
/// Commits block 0 with `setup_state_diff` via [`crate::committer::Committer::commit_block`]
177+
/// (no witnesses), then calls [`crate::committer::Committer::read_paths_and_commit_block`] at
178+
/// block 1 with `state_diff` and `accessed_keys`, and returns its response.
179+
async fn setup_and_read_paths(
180+
committer: &mut ApolloTestCommitter,
181+
setup_state_diff: ThinStateDiff,
182+
state_diff: ThinStateDiff,
183+
accessed_keys: AccessedKeys,
184+
) -> ReadPathsAndCommitBlockResponse {
185+
committer
186+
.commit_block(CommitBlockRequest {
187+
state_diff: setup_state_diff.clone(),
188+
state_diff_commitment: Some(calculate_state_diff_hash(&setup_state_diff)),
189+
height: BlockNumber(0),
190+
})
191+
.await
192+
.unwrap();
193+
194+
let state_diff_commitment = Some(calculate_state_diff_hash(&state_diff));
195+
committer
196+
.read_paths_and_commit_block(read_paths_and_commit_block_request(
197+
state_diff,
198+
state_diff_commitment,
199+
1,
200+
accessed_keys,
201+
))
202+
.await
203+
.unwrap()
204+
}
205+
175206
fn leaf_hashes<Key, PatriciaLeaf>(
176207
leaves: &HashMap<Key, PatriciaLeaf>,
177208
key_into_node_index: impl Fn(&Key) -> NodeIndex,
@@ -325,30 +356,17 @@ async fn read_paths_and_commit_block_happy_flow() {
325356
#[tokio::test]
326357
async fn revert_removes_witnesses_and_digest() {
327358
let mut committer = new_test_committer().await;
328-
let height_0 = 0;
329359
let height_1 = 1;
330-
let block_0_state_diff = BLOCK_0_STATE_DIFF.clone();
331360
let block_1_state_diff = BLOCK_1_STATE_DIFF.clone();
332361
let accessed_keys = ACCESSED_KEYS.clone();
333362

334-
committer
335-
.commit_block(CommitBlockRequest {
336-
state_diff: block_0_state_diff.clone(),
337-
state_diff_commitment: Some(calculate_state_diff_hash(&block_0_state_diff)),
338-
height: BlockNumber(height_0),
339-
})
340-
.await
341-
.unwrap();
342-
343-
let block_1_response = committer
344-
.read_paths_and_commit_block(read_paths_and_commit_block_request(
345-
block_1_state_diff.clone(),
346-
Some(calculate_state_diff_hash(&block_1_state_diff)),
347-
height_1,
348-
accessed_keys.clone(),
349-
))
350-
.await
351-
.unwrap();
363+
let block_1_response = setup_and_read_paths(
364+
&mut committer,
365+
BLOCK_0_STATE_DIFF.clone(),
366+
block_1_state_diff,
367+
accessed_keys.clone(),
368+
)
369+
.await;
352370
assert_witnesses_and_digest_present(
353371
&mut committer,
354372
BlockNumber(height_1),
@@ -394,9 +412,7 @@ async fn test_bottom_of_new_edge_to_an_unmoidifed_subtree_is_present() {
394412
let compiled_class_hash_d_felt = 102_u64.into();
395413

396414
let mut committer = new_test_committer().await;
397-
let height_0 = 0;
398-
let height_1 = 1;
399-
let block_0_state_diff = ThinStateDiff {
415+
let setup_state_diff = ThinStateDiff {
400416
class_hash_to_compiled_class_hash: indexmap! {
401417
class_hash_a => CompiledClassHash(compiled_class_hash_a_felt),
402418
class_hash_b => CompiledClassHash(compiled_class_hash_b_felt),
@@ -415,24 +431,9 @@ async fn test_bottom_of_new_edge_to_an_unmoidifed_subtree_is_present() {
415431
..Default::default()
416432
};
417433

418-
committer
419-
.commit_block(CommitBlockRequest {
420-
state_diff: block_0_state_diff.clone(),
421-
state_diff_commitment: Some(calculate_state_diff_hash(&block_0_state_diff)),
422-
height: BlockNumber(height_0),
423-
})
424-
.await
425-
.unwrap();
426-
427-
let response = committer
428-
.read_paths_and_commit_block(read_paths_and_commit_block_request(
429-
block_1_state_diff.clone(),
430-
Some(calculate_state_diff_hash(&block_1_state_diff)),
431-
height_1,
432-
accessed_keys,
433-
))
434-
.await
435-
.unwrap();
434+
let response =
435+
setup_and_read_paths(&mut committer, setup_state_diff, block_1_state_diff, accessed_keys)
436+
.await;
436437

437438
let leaf_a_hash = TreeHashFunctionImpl::compute_leaf_hash(&CommitterCompiledClassHash(
438439
compiled_class_hash_a_felt,
@@ -488,9 +489,7 @@ async fn test_bottom_of_new_edge_which_was_not_bottom_of_an_old_edge_is_present(
488489
let compiled_class_hash_d_felt = 102_u64.into();
489490

490491
let mut committer = new_test_committer().await;
491-
let height_0 = 0;
492-
let height_1 = 1;
493-
let block_0_state_diff = ThinStateDiff {
492+
let setup_state_diff = ThinStateDiff {
494493
class_hash_to_compiled_class_hash: indexmap! {
495494
class_hash_a => CompiledClassHash(compiled_class_hash_a_felt),
496495
class_hash_b => CompiledClassHash(compiled_class_hash_b_felt),
@@ -509,24 +508,9 @@ async fn test_bottom_of_new_edge_which_was_not_bottom_of_an_old_edge_is_present(
509508
..Default::default()
510509
};
511510

512-
committer
513-
.commit_block(CommitBlockRequest {
514-
state_diff: block_0_state_diff.clone(),
515-
state_diff_commitment: Some(calculate_state_diff_hash(&block_0_state_diff)),
516-
height: BlockNumber(height_0),
517-
})
518-
.await
519-
.unwrap();
520-
521-
let response = committer
522-
.read_paths_and_commit_block(read_paths_and_commit_block_request(
523-
block_1_state_diff.clone(),
524-
Some(calculate_state_diff_hash(&block_1_state_diff)),
525-
height_1,
526-
accessed_keys,
527-
))
528-
.await
529-
.unwrap();
511+
let response =
512+
setup_and_read_paths(&mut committer, setup_state_diff, block_1_state_diff, accessed_keys)
513+
.await;
530514

531515
let leaf_a_hash = TreeHashFunctionImpl::compute_leaf_hash(&CommitterCompiledClassHash(
532516
compiled_class_hash_a_felt,

0 commit comments

Comments
 (0)