Skip to content

Commit

Permalink
chore(batcher): use thin block info to create block info
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 24, 2024
1 parent 66a6242 commit 2611e29
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
10 changes: 5 additions & 5 deletions crates/starknet_api/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl GasPrice {

/// Utility struct representing a non-zero gas price. Useful when a gas amount must be computed by
/// taking a fee amount and dividing by the gas price.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, derive_more::Display)]
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize, derive_more::Display)]
pub struct NonzeroGasPrice(GasPrice);

impl NonzeroGasPrice {
Expand Down Expand Up @@ -438,15 +438,15 @@ macro_rules! impl_try_from_uint_for_nonzero_gas_price {

impl_try_from_uint_for_nonzero_gas_price!(u8, u16, u32, u64, u128);

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
// TODO(Arni): Remove derive of Default. Gas prices should always be set.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct GasPriceVector {
pub l1_gas_price: NonzeroGasPrice,
pub l1_data_gas_price: NonzeroGasPrice,
pub l2_gas_price: NonzeroGasPrice,
}

// TODO(Arni): Remove derive of Default. Gas prices should always be set.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct GasPrices {
pub eth_gas_prices: GasPriceVector, // In wei.
pub strk_gas_prices: GasPriceVector, // In fri.
Expand Down Expand Up @@ -479,7 +479,7 @@ impl GasPrices {
)]
pub struct BlockTimestamp(pub u64);

#[derive(Clone, Debug, Deserialize, Default, Serialize)]
#[derive(Clone, Debug, Deserialize, Default, Eq, PartialEq, Serialize)]
pub struct BlockInfo {
pub block_number: BlockNumber,
pub block_timestamp: BlockTimestamp,
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Batcher {

self.proposal_manager
.propose_block(
active_height,
propose_block_input.block_info,
proposal_id,
propose_block_input.retrospective_block_hash,
deadline,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Batcher {

self.proposal_manager
.validate_block(
active_height,
validate_block_input.block_info,
proposal_id,
validate_block_input.retrospective_block_hash,
deadline,
Expand Down
36 changes: 26 additions & 10 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use assert_matches::assert_matches;
use async_trait::async_trait;
use blockifier::abi::constants;
use blockifier::test_utils::struct_impls::BlockInfoExt;
use chrono::Utc;
use futures::future::BoxFuture;
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;
Expand Down Expand Up @@ -55,6 +56,9 @@ const STREAMING_CHUNK_SIZE: usize = 3;
const BLOCK_GENERATION_TIMEOUT: tokio::time::Duration = tokio::time::Duration::from_secs(1);
const PROPOSAL_ID: ProposalId = ProposalId(0);

static BLOCK_INFO_AT_INITIAL_HEIGHT: LazyLock<BlockInfo> =
LazyLock::new(|| BlockInfo { block_number: INITIAL_HEIGHT, ..BlockInfo::create_for_testing() });

fn proposal_commitment() -> ProposalCommitment {
ProposalCommitment {
state_diff_commitment: StateDiffCommitment(PoseidonHash(felt!(u128::try_from(7).unwrap()))),
Expand Down Expand Up @@ -114,7 +118,13 @@ fn mock_proposal_manager_validate_flow() -> MockProposalManagerTraitWrapper {
proposal_manager
.expect_wrap_validate_block()
.times(1)
.with(eq(INITIAL_HEIGHT), eq(PROPOSAL_ID), eq(None), always(), always())
.with(
eq(BLOCK_INFO_AT_INITIAL_HEIGHT.clone()),
eq(PROPOSAL_ID),
eq(None),
always(),
always(),
)
.return_once(|_, _, _, _, tx_provider| {
{
async move {
Expand Down Expand Up @@ -331,7 +341,13 @@ async fn send_finish_to_an_invalid_proposal() {
proposal_manager
.expect_wrap_validate_block()
.times(1)
.with(eq(INITIAL_HEIGHT), eq(PROPOSAL_ID), eq(None), always(), always())
.with(
eq(BLOCK_INFO_AT_INITIAL_HEIGHT.clone()),
eq(PROPOSAL_ID),
eq(None),
always(),
always(),
)
.return_once(|_, _, _, _, _| { async move { Ok(()) } }.boxed());

let proposal_error = GetProposalResultError::BlockBuilderError(Arc::new(
Expand Down Expand Up @@ -548,7 +564,7 @@ async fn simulate_build_block_proposal(
trait ProposalManagerTraitWrapper: Send + Sync {
fn wrap_propose_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand All @@ -558,7 +574,7 @@ trait ProposalManagerTraitWrapper: Send + Sync {

fn wrap_validate_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand Down Expand Up @@ -589,15 +605,15 @@ trait ProposalManagerTraitWrapper: Send + Sync {
impl<T: ProposalManagerTraitWrapper> ProposalManagerTrait for T {
async fn propose_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
output_content_sender: tokio::sync::mpsc::UnboundedSender<Transaction>,
tx_provider: ProposeTransactionProvider,
) -> Result<(), GenerateProposalError> {
self.wrap_propose_block(
height,
block_info,
proposal_id,
retrospective_block_hash,
deadline,
Expand All @@ -609,14 +625,14 @@ impl<T: ProposalManagerTraitWrapper> ProposalManagerTrait for T {

async fn validate_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
tx_provider: ValidateTransactionProvider,
) -> Result<(), GenerateProposalError> {
self.wrap_validate_block(
height,
block_info,
proposal_id,
retrospective_block_hash,
deadline,
Expand Down
27 changes: 14 additions & 13 deletions crates/starknet_batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_state_reader::papyrus_state::PapyrusReader;
use papyrus_storage::StorageReader;
use serde::{Deserialize, Serialize};
use starknet_api::block::{
BlockHashAndNumber,
BlockInfo,
BlockNumber,
BlockTimestamp,
NonzeroGasPrice,
};
use starknet_api::block::{BlockHashAndNumber, BlockInfo, BlockTimestamp, NonzeroGasPrice};
use starknet_api::core::ContractAddress;
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::TransactionHash;
Expand Down Expand Up @@ -228,7 +222,7 @@ async fn collect_execution_results_and_stream_txs(
pub trait BlockBuilderFactoryTrait {
fn create_block_builder(
&self,
height: BlockNumber,
block_info: BlockInfo,
retrospective_block_hash: Option<BlockHashAndNumber>,
execution_params: BlockBuilderExecutionParams,
tx_provider: Box<dyn TransactionProvider>,
Expand Down Expand Up @@ -302,16 +296,23 @@ pub struct BlockBuilderFactory {
}

impl BlockBuilderFactory {
fn next_block_info(&self, height: BlockNumber) -> BlockBuilderResult<BlockInfo> {
// TODO(Arni): Remove this function once it overrides non of the parameters.
fn block_info_with_overrides(
&self,
next_block_info: BlockInfo,
) -> BlockBuilderResult<BlockInfo> {
Ok(BlockInfo {
block_number: height,
block_number: next_block_info.block_number,
// TODO: Override the timestamp.
block_timestamp: BlockTimestamp(chrono::Utc::now().timestamp().try_into()?),
// TODO: Override the sequencer address with proposer address.
sequencer_address: self.block_builder_config.sequencer_address,
// TODO (yael 7/10/2024): add logic to compute gas prices
// TODO: Override the gas prices.
gas_prices: {
let tmp_val = NonzeroGasPrice::MIN;
gas_prices(tmp_val, tmp_val, tmp_val, tmp_val, tmp_val, tmp_val)
},
// TODO: Override the use_kzg_da flag.
use_kzg_da: self.block_builder_config.use_kzg_da,
})
}
Expand Down Expand Up @@ -355,15 +356,15 @@ impl BlockBuilderFactory {
impl BlockBuilderFactoryTrait for BlockBuilderFactory {
fn create_block_builder(
&self,
height: BlockNumber,
next_block_info: BlockInfo,
retrospective_block_hash: Option<BlockHashAndNumber>,
execution_params: BlockBuilderExecutionParams,
tx_provider: Box<dyn TransactionProvider>,
output_content_sender: Option<tokio::sync::mpsc::UnboundedSender<Transaction>>,
abort_signal_receiver: tokio::sync::oneshot::Receiver<()>,
) -> BlockBuilderResult<Box<dyn BlockBuilderTrait>> {
// TODO(Arni): Get block info as a parameter for this function.
let next_block_info = self.next_block_info(height)?;
let next_block_info = self.block_info_with_overrides(next_block_info)?;
let executor = self.preprocess_and_create_transaction_executor(
next_block_info,
retrospective_block_hash,
Expand Down
14 changes: 7 additions & 7 deletions crates/starknet_batcher/src/proposal_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use indexmap::IndexMap;
use starknet_api::block::{BlockHashAndNumber, BlockNumber};
use starknet_api::block::{BlockHashAndNumber, BlockInfo};
use starknet_api::block_hash::state_diff_hash::calculate_state_diff_hash;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::Transaction;
Expand Down Expand Up @@ -62,7 +62,7 @@ pub(crate) enum InternalProposalStatus {
pub trait ProposalManagerTrait: Send + Sync {
async fn propose_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand All @@ -72,7 +72,7 @@ pub trait ProposalManagerTrait: Send + Sync {

async fn validate_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand Down Expand Up @@ -140,7 +140,7 @@ impl ProposalManagerTrait for ProposalManager {
#[instrument(skip(self, tx_sender, tx_provider), err, fields(self.active_height))]
async fn propose_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand All @@ -155,7 +155,7 @@ impl ProposalManagerTrait for ProposalManager {
let (abort_signal_sender, abort_signal_receiver) = tokio::sync::oneshot::channel();

let block_builder = self.block_builder_factory.create_block_builder(
height,
block_info,
retrospective_block_hash,
BlockBuilderExecutionParams { deadline, fail_on_err: false },
Box::new(tx_provider),
Expand All @@ -174,7 +174,7 @@ impl ProposalManagerTrait for ProposalManager {
#[instrument(skip(self, tx_provider), err, fields(self.active_height))]
async fn validate_block(
&mut self,
height: BlockNumber,
block_info: BlockInfo,
proposal_id: ProposalId,
retrospective_block_hash: Option<BlockHashAndNumber>,
deadline: tokio::time::Instant,
Expand All @@ -188,7 +188,7 @@ impl ProposalManagerTrait for ProposalManager {
let (abort_signal_sender, abort_signal_receiver) = tokio::sync::oneshot::channel();

let block_builder = self.block_builder_factory.create_block_builder(
height,
block_info,
retrospective_block_hash,
BlockBuilderExecutionParams { deadline, fail_on_err: true },
Box::new(tx_provider),
Expand Down
22 changes: 16 additions & 6 deletions crates/starknet_batcher/src/proposal_manager_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use assert_matches::assert_matches;
use blockifier::test_utils::struct_impls::BlockInfoExt;
use rstest::{fixture, rstest};
use starknet_api::block::BlockNumber;
use starknet_api::block::{BlockInfo, BlockNumber};
use starknet_api::executable_transaction::Transaction;
use starknet_batcher_types::batcher_types::ProposalId;
use starknet_mempool_types::communication::MockMempoolClient;
Expand Down Expand Up @@ -32,6 +33,9 @@ const BLOCK_GENERATION_TIMEOUT: tokio::time::Duration = tokio::time::Duration::f
const MAX_L1_HANDLER_TXS_PER_BLOCK_PROPOSAL: usize = 3;
const INPUT_CHANNEL_SIZE: usize = 30;

static BLOCK_INFO_AT_INITIAL_HEIGHT: LazyLock<BlockInfo> =
LazyLock::new(|| BlockInfo { block_number: INITIAL_HEIGHT, ..BlockInfo::create_for_testing() });

#[fixture]
fn output_streaming() -> (
tokio::sync::mpsc::UnboundedSender<Transaction>,
Expand Down Expand Up @@ -118,7 +122,7 @@ async fn propose_block_non_blocking(
let (output_sender, _receiver) = output_streaming();
proposal_manager
.propose_block(
INITIAL_HEIGHT,
BLOCK_INFO_AT_INITIAL_HEIGHT.clone(),
proposal_id,
None,
proposal_deadline(),
Expand All @@ -144,7 +148,13 @@ async fn validate_block(
proposal_id: ProposalId,
) {
proposal_manager
.validate_block(INITIAL_HEIGHT, proposal_id, None, proposal_deadline(), tx_provider)
.validate_block(
BLOCK_INFO_AT_INITIAL_HEIGHT.clone(),
proposal_id,
None,
proposal_deadline(),
tx_provider,
)
.await
.unwrap();

Expand Down Expand Up @@ -211,7 +221,7 @@ async fn multiple_proposals_generation_fail(
let (output_sender_0, _rec_0) = output_streaming();
proposal_manager
.propose_block(
INITIAL_HEIGHT,
BLOCK_INFO_AT_INITIAL_HEIGHT.clone(),
ProposalId(0),
None,
proposal_deadline(),
Expand All @@ -225,7 +235,7 @@ async fn multiple_proposals_generation_fail(
let (output_sender_1, _rec_1) = output_streaming();
let another_generate_request = proposal_manager
.propose_block(
INITIAL_HEIGHT,
BLOCK_INFO_AT_INITIAL_HEIGHT.clone(),
ProposalId(1),
None,
proposal_deadline(),
Expand Down

0 comments on commit 2611e29

Please sign in to comment.