From 7fc654e2269ee4dac5b479430f247237cb97770d Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 26 Nov 2024 17:15:26 +0200 Subject: [PATCH] chore(batcher): verify block number on propose block --- crates/starknet_batcher/src/batcher.rs | 32 +++++++++++++++++++-- crates/starknet_batcher/src/batcher_test.rs | 8 +++--- crates/starknet_batcher_types/src/errors.rs | 2 ++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/starknet_batcher/src/batcher.rs b/crates/starknet_batcher/src/batcher.rs index 8a12399602..b2c4f9e2c3 100644 --- a/crates/starknet_batcher/src/batcher.rs +++ b/crates/starknet_batcher/src/batcher.rs @@ -120,7 +120,11 @@ 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)?; + verify_block_input( + active_height, + propose_block_input.block_info.block_number, + propose_block_input.retrospective_block_hash, + )?; let proposal_id = propose_block_input.proposal_id; let deadline = deadline_as_instant(propose_block_input.deadline)?; @@ -154,7 +158,11 @@ 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)?; + verify_block_input( + active_height, + validate_block_input.block_info.block_number, + validate_block_input.retrospective_block_hash, + )?; let proposal_id = validate_block_input.proposal_id; let deadline = deadline_as_instant(validate_block_input.deadline)?; @@ -413,6 +421,17 @@ pub fn deadline_as_instant(deadline: chrono::DateTime) -> BatcherResult, +) -> BatcherResult<()> { + verify_non_empty_retrospective_block_hash(height, retrospective_block_hash)?; + verify_block_number(height, block_number)?; + + Ok(()) +} + +fn verify_non_empty_retrospective_block_hash( height: BlockNumber, retrospective_block_hash: Option, ) -> BatcherResult<()> { @@ -421,5 +440,14 @@ fn verify_block_input( { return Err(BatcherError::MissingRetrospectiveBlockHash); } + + Ok(()) +} + +fn verify_block_number(height: BlockNumber, block_number: BlockNumber) -> BatcherResult<()> { + if block_number != height { + return Err(BatcherError::InvalidBlockNumber { active_height: height, block_number }); + } + Ok(()) } diff --git a/crates/starknet_batcher/src/batcher_test.rs b/crates/starknet_batcher/src/batcher_test.rs index b8f670a076..f90f384f5d 100644 --- a/crates/starknet_batcher/src/batcher_test.rs +++ b/crates/starknet_batcher/src/batcher_test.rs @@ -10,7 +10,7 @@ use futures::FutureExt; use mockall::automock; use mockall::predicate::{always, eq}; use rstest::{fixture, rstest}; -use starknet_api::block::{BlockHashAndNumber, BlockNumber}; +use starknet_api::block::{BlockHashAndNumber, BlockInfo, BlockNumber}; use starknet_api::core::{ContractAddress, Nonce, StateDiffCommitment}; use starknet_api::executable_transaction::Transaction; use starknet_api::hash::PoseidonHash; @@ -231,7 +231,7 @@ async fn validate_block_full_flow() { proposal_id: PROPOSAL_ID, deadline: deadline(), retrospective_block_hash: None, - block_info: Default::default(), + block_info: BlockInfo { block_number: INITIAL_HEIGHT, ..Default::default() }, }; batcher.validate_block(validate_block_input).await.unwrap(); @@ -350,7 +350,7 @@ async fn send_finish_to_an_invalid_proposal() { proposal_id: PROPOSAL_ID, deadline: deadline(), retrospective_block_hash: None, - block_info: Default::default(), + block_info: BlockInfo { block_number: INITIAL_HEIGHT, ..Default::default() }, }; batcher.validate_block(validate_block_input).await.unwrap(); @@ -383,7 +383,7 @@ async fn propose_block_full_flow() { proposal_id: PROPOSAL_ID, retrospective_block_hash: None, deadline: chrono::Utc::now() + chrono::Duration::seconds(1), - block_info: Default::default(), + block_info: BlockInfo { block_number: INITIAL_HEIGHT, ..Default::default() }, }) .await .unwrap(); diff --git a/crates/starknet_batcher_types/src/errors.rs b/crates/starknet_batcher_types/src/errors.rs index a3a028a815..b2fc3e5968 100644 --- a/crates/starknet_batcher_types/src/errors.rs +++ b/crates/starknet_batcher_types/src/errors.rs @@ -20,6 +20,8 @@ pub enum BatcherError { HeightInProgress, #[error("Internal server error.")] InternalError, + #[error("Invalid block number. The active height is {active_height}, got {block_number}.")] + InvalidBlockNumber { active_height: BlockNumber, block_number: BlockNumber }, #[error("Missing retrospective block hash.")] MissingRetrospectiveBlockHash, #[error("Attempt to start proposal with no active height.")]