Skip to content

Commit

Permalink
fix(batcher): check that retrospective_block_hash value is not none
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Nov 21, 2024
1 parent d97d79c commit d1c7d4c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::HashMap;
use std::sync::Arc;

use blockifier::abi::constants;
use blockifier::state::global_cache::GlobalContractCache;
use chrono::Utc;
#[cfg(test)]
use mockall::automock;
use papyrus_storage::state::{StateStorageReader, StateStorageWriter};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::executable_transaction::Transaction;
use starknet_api::state::ThinStateDiff;
use starknet_batcher_types::batcher_types::{
Expand Down Expand Up @@ -119,6 +120,7 @@ impl Batcher {
propose_block_input: ProposeBlockInput,
) -> BatcherResult<()> {
let active_height = self.active_height.ok_or(BatcherError::NoActiveHeight)?;
verify_block_input(active_height, propose_block_input.retrospective_block_hash)?;

let proposal_id = propose_block_input.proposal_id;
let deadline = deadline_as_instant(propose_block_input.deadline)?;
Expand Down Expand Up @@ -152,6 +154,7 @@ impl Batcher {
validate_block_input: ValidateBlockInput,
) -> BatcherResult<()> {
let active_height = self.active_height.ok_or(BatcherError::NoActiveHeight)?;
verify_block_input(active_height, validate_block_input.retrospective_block_hash)?;

let proposal_id = validate_block_input.proposal_id;
let deadline = deadline_as_instant(validate_block_input.deadline)?;
Expand Down Expand Up @@ -408,3 +411,15 @@ pub fn deadline_as_instant(deadline: chrono::DateTime<Utc>) -> BatcherResult<tok
time_to_deadline.to_std().map_err(|_| BatcherError::TimeToDeadlineError { deadline })?;
Ok((std::time::Instant::now() + as_duration).into())
}

fn verify_block_input(
height: BlockNumber,
retrospective_block_hash: Option<BlockHashAndNumber>,
) -> BatcherResult<()> {
if height >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER)
&& retrospective_block_hash.is_none()
{
return Err(BatcherError::MissingRetrospectiveBlockHash);
}
Ok(())
}
34 changes: 34 additions & 0 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;

use assert_matches::assert_matches;
use async_trait::async_trait;
use blockifier::abi::constants;
use chrono::Utc;
use futures::future::BoxFuture;
use futures::FutureExt;
Expand Down Expand Up @@ -410,6 +411,39 @@ async fn propose_block_full_flow() {
assert_matches!(exhausted, Err(BatcherError::ProposalNotFound { .. }));
}

#[tokio::test]
async fn propose_block_without_retrospective_block_hash() {
let mut proposal_manager = MockProposalManagerTraitWrapper::new();
proposal_manager.expect_wrap_reset().times(1).return_once(|| async {}.boxed());

let mut storage_reader = MockBatcherStorageReaderTrait::new();
storage_reader
.expect_height()
.returning(|| Ok(BlockNumber(constants::STORED_BLOCK_HASH_BUFFER)));

let mut batcher = Batcher::new(
batcher_config(),
Arc::new(storage_reader),
Box::new(storage_writer()),
Arc::new(mempool_client()),
Box::new(proposal_manager),
);

batcher
.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(),
})
.await;

assert_matches!(result, Err(BatcherError::MissingRetrospectiveBlockHash));
}

#[rstest]
#[tokio::test]
async fn get_content_from_unknown_proposal() {
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_batcher_types/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum BatcherError {
HeightInProgress,
#[error("Internal server error.")]
InternalError,
#[error("Missing retrospective block hash.")]
MissingRetrospectiveBlockHash,
#[error("Attempt to start proposal with no active height.")]
NoActiveHeight,
#[error(
Expand Down

0 comments on commit d1c7d4c

Please sign in to comment.