From ab8b6ac4587a860dbe0321ba3a63eb8e25bc0a1f Mon Sep 17 00:00:00 2001 From: Alon Haramati Date: Wed, 27 Nov 2024 14:33:21 +0200 Subject: [PATCH] chore(batcher): extract propose/validate block inputs into function --- crates/starknet_batcher/src/batcher_test.rs | 77 ++++++++++----------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/crates/starknet_batcher/src/batcher_test.rs b/crates/starknet_batcher/src/batcher_test.rs index e6ca53d1e1..9387d31bfb 100644 --- a/crates/starknet_batcher/src/batcher_test.rs +++ b/crates/starknet_batcher/src/batcher_test.rs @@ -77,6 +77,24 @@ fn deadline() -> chrono::DateTime { chrono::Utc::now() + BLOCK_GENERATION_TIMEOUT } +fn propose_block_input() -> ProposeBlockInput { + ProposeBlockInput { + proposal_id: PROPOSAL_ID, + deadline: deadline(), + retrospective_block_hash: None, + block_info: Default::default(), + } +} + +fn validate_block_input() -> ValidateBlockInput { + ValidateBlockInput { + proposal_id: PROPOSAL_ID, + deadline: deadline(), + retrospective_block_hash: None, + block_info: Default::default(), + } +} + struct MockDependencies { storage_reader: MockBatcherStorageReaderTrait, storage_writer: MockBatcherStorageWriterTrait, @@ -230,24 +248,10 @@ async fn no_active_height() { // Calling `propose_block` and `validate_block` without starting a height should fail. - let result = batcher - .propose_block(ProposeBlockInput { - proposal_id: ProposalId(0), - retrospective_block_hash: None, - deadline: chrono::Utc::now() + chrono::Duration::seconds(1), - block_info: Default::default(), - }) - .await; + let result = batcher.propose_block(propose_block_input()).await; assert_eq!(result, Err(BatcherError::NoActiveHeight)); - let result = batcher - .validate_block(ValidateBlockInput { - proposal_id: ProposalId(0), - retrospective_block_hash: None, - deadline: chrono::Utc::now() + chrono::Duration::seconds(1), - block_info: Default::default(), - }) - .await; + let result = batcher.validate_block(validate_block_input()).await; assert_eq!(result, Err(BatcherError::NoActiveHeight)); } @@ -264,13 +268,13 @@ async fn validate_block_full_flow() { batcher.start_height(StartHeightInput { height: INITIAL_HEIGHT }).await.unwrap(); - let validate_block_input = ValidateBlockInput { - proposal_id: PROPOSAL_ID, - deadline: deadline(), - retrospective_block_hash: None, - block_info: initial_block_info(), - }; - batcher.validate_block(validate_block_input).await.unwrap(); + batcher + .validate_block(ValidateBlockInput { + block_info: initial_block_info(), + ..validate_block_input() + }) + .await + .unwrap(); let send_proposal_input_txs = SendProposalContentInput { proposal_id: PROPOSAL_ID, @@ -388,13 +392,13 @@ async fn send_finish_to_an_invalid_proposal() { }); batcher.start_height(StartHeightInput { height: INITIAL_HEIGHT }).await.unwrap(); - let validate_block_input = ValidateBlockInput { - proposal_id: PROPOSAL_ID, - deadline: deadline(), - retrospective_block_hash: None, - block_info: initial_block_info(), - }; - batcher.validate_block(validate_block_input).await.unwrap(); + batcher + .validate_block(ValidateBlockInput { + block_info: initial_block_info(), + ..validate_block_input() + }) + .await + .unwrap(); let send_proposal_input_txs = SendProposalContentInput { proposal_id: PROPOSAL_ID, content: SendProposalContent::Finish }; @@ -426,10 +430,8 @@ async fn propose_block_full_flow() { batcher.start_height(StartHeightInput { height: INITIAL_HEIGHT }).await.unwrap(); batcher .propose_block(ProposeBlockInput { - proposal_id: PROPOSAL_ID, - retrospective_block_hash: None, - deadline: chrono::Utc::now() + chrono::Duration::seconds(1), block_info: initial_block_info(), + ..propose_block_input() }) .await .unwrap(); @@ -480,14 +482,7 @@ async fn propose_block_without_retrospective_block_hash() { .start_height(StartHeightInput { height: BlockNumber(constants::STORED_BLOCK_HASH_BUFFER) }) .await .unwrap(); - let result = batcher - .propose_block(ProposeBlockInput { - proposal_id: PROPOSAL_ID, - retrospective_block_hash: None, - deadline: deadline(), - block_info: Default::default(), - }) - .await; + let result = batcher.propose_block(propose_block_input()).await; assert_matches!(result, Err(BatcherError::MissingRetrospectiveBlockHash)); }