Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(starknet_batcher): verify block number on propose block #2340

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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 tx_provider = ProposeTransactionProvider::new(
self.mempool_client.clone(),
Expand Down Expand Up @@ -172,7 +176,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,
)?;

// A channel to send the transactions to include in the block being validated.
let (input_tx_sender, input_tx_receiver) =
Expand Down Expand Up @@ -451,6 +459,17 @@ pub fn deadline_as_instant(deadline: chrono::DateTime<Utc>) -> BatcherResult<tok
}

fn verify_block_input(
height: BlockNumber,
block_number: BlockNumber,
retrospective_block_hash: Option<BlockHashAndNumber>,
) -> 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<BlockHashAndNumber>,
) -> BatcherResult<()> {
Expand All @@ -459,5 +478,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(())
}
8 changes: 4 additions & 4 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::FutureExt;
use mockall::automock;
use mockall::predicate::{always, eq};
use rstest::rstest;
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::core::{ContractAddress, Nonce, StateDiffCommitment};
use starknet_api::executable_transaction::Transaction;
use starknet_api::hash::PoseidonHash;
Expand Down Expand Up @@ -263,7 +263,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();

Expand Down Expand Up @@ -387,7 +387,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();

Expand Down Expand Up @@ -424,7 +424,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();
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("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.")]
Expand Down
Loading