Skip to content

Commit 97d8aec

Browse files
committed
apollo_committer: reduce tests boilerplate
1 parent 965263d commit 97d8aec

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;
@@ -179,6 +180,36 @@ fn read_paths_and_commit_block_request(
179180
}
180181
}
181182

183+
/// Commits block 0 with `setup_state_diff` via [`crate::committer::Committer::commit_block`]
184+
/// (no witnesses), then calls [`crate::committer::Committer::read_paths_and_commit_block`] at
185+
/// block 1 with `state_diff` and `accessed_keys`, and returns its response.
186+
async fn setup_and_read_paths(
187+
committer: &mut ApolloTestCommitter,
188+
setup_state_diff: ThinStateDiff,
189+
state_diff: ThinStateDiff,
190+
accessed_keys: AccessedKeys,
191+
) -> ReadPathsAndCommitBlockResponse {
192+
committer
193+
.commit_block(CommitBlockRequest {
194+
state_diff: setup_state_diff.clone(),
195+
state_diff_commitment: Some(calculate_state_diff_hash(&setup_state_diff)),
196+
height: BlockNumber(0),
197+
})
198+
.await
199+
.unwrap();
200+
201+
let state_diff_commitment = Some(calculate_state_diff_hash(&state_diff));
202+
committer
203+
.read_paths_and_commit_block(read_paths_and_commit_block_request(
204+
state_diff,
205+
state_diff_commitment,
206+
1,
207+
accessed_keys,
208+
))
209+
.await
210+
.unwrap()
211+
}
212+
182213
fn leaf_hashes<Key, PatriciaLeaf>(
183214
leaves: &HashMap<Key, PatriciaLeaf>,
184215
key_into_node_index: impl Fn(&Key) -> NodeIndex,
@@ -332,30 +363,17 @@ async fn read_paths_and_commit_block_happy_flow() {
332363
#[tokio::test]
333364
async fn revert_removes_witnesses_and_digest() {
334365
let mut committer = new_test_committer().await;
335-
let height_0 = 0;
336366
let height_1 = 1;
337-
let block_0_state_diff = BLOCK_0_STATE_DIFF.clone();
338367
let block_1_state_diff = BLOCK_1_STATE_DIFF.clone();
339368
let accessed_keys = ACCESSED_KEYS.clone();
340369

341-
committer
342-
.commit_block(CommitBlockRequest {
343-
state_diff: block_0_state_diff.clone(),
344-
state_diff_commitment: Some(calculate_state_diff_hash(&block_0_state_diff)),
345-
height: BlockNumber(height_0),
346-
})
347-
.await
348-
.unwrap();
349-
350-
let block_1_response = committer
351-
.read_paths_and_commit_block(read_paths_and_commit_block_request(
352-
block_1_state_diff.clone(),
353-
Some(calculate_state_diff_hash(&block_1_state_diff)),
354-
height_1,
355-
accessed_keys.clone(),
356-
))
357-
.await
358-
.unwrap();
370+
let block_1_response = setup_and_read_paths(
371+
&mut committer,
372+
BLOCK_0_STATE_DIFF.clone(),
373+
block_1_state_diff,
374+
accessed_keys.clone(),
375+
)
376+
.await;
359377
assert_witnesses_and_digest_present(
360378
&mut committer,
361379
BlockNumber(height_1),
@@ -401,9 +419,7 @@ async fn test_bottom_of_new_edge_to_an_unmoidifed_subtree_is_present() {
401419
let compiled_class_hash_d_felt = 102_u64.into();
402420

403421
let mut committer = new_test_committer().await;
404-
let height_0 = 0;
405-
let height_1 = 1;
406-
let block_0_state_diff = ThinStateDiff {
422+
let setup_state_diff = ThinStateDiff {
407423
class_hash_to_compiled_class_hash: indexmap! {
408424
class_hash_a => CompiledClassHash(compiled_class_hash_a_felt),
409425
class_hash_b => CompiledClassHash(compiled_class_hash_b_felt),
@@ -422,24 +438,9 @@ async fn test_bottom_of_new_edge_to_an_unmoidifed_subtree_is_present() {
422438
..Default::default()
423439
};
424440

425-
committer
426-
.commit_block(CommitBlockRequest {
427-
state_diff: block_0_state_diff.clone(),
428-
state_diff_commitment: Some(calculate_state_diff_hash(&block_0_state_diff)),
429-
height: BlockNumber(height_0),
430-
})
431-
.await
432-
.unwrap();
433-
434-
let response = committer
435-
.read_paths_and_commit_block(read_paths_and_commit_block_request(
436-
block_1_state_diff.clone(),
437-
Some(calculate_state_diff_hash(&block_1_state_diff)),
438-
height_1,
439-
accessed_keys,
440-
))
441-
.await
442-
.unwrap();
441+
let response =
442+
setup_and_read_paths(&mut committer, setup_state_diff, block_1_state_diff, accessed_keys)
443+
.await;
443444

444445
let leaf_a_hash = TreeHashFunctionImpl::compute_leaf_hash(&CommitterCompiledClassHash(
445446
compiled_class_hash_a_felt,
@@ -490,9 +491,7 @@ async fn test_bottom_of_new_edge_to_a_binary_unmodified_subtree_is_present() {
490491
let compiled_class_hash_d_felt = 102_u64.into();
491492

492493
let mut committer = new_test_committer().await;
493-
let height_0 = 0;
494-
let height_1 = 1;
495-
let block_0_state_diff = ThinStateDiff {
494+
let setup_state_diff = ThinStateDiff {
496495
class_hash_to_compiled_class_hash: indexmap! {
497496
class_hash_a => CompiledClassHash(compiled_class_hash_a_felt),
498497
class_hash_b => CompiledClassHash(compiled_class_hash_b_felt),
@@ -511,24 +510,9 @@ async fn test_bottom_of_new_edge_to_a_binary_unmodified_subtree_is_present() {
511510
..Default::default()
512511
};
513512

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

533517
let leaf_a_hash = TreeHashFunctionImpl::compute_leaf_hash(&CommitterCompiledClassHash(
534518
compiled_class_hash_a_felt,

0 commit comments

Comments
 (0)