From d556b2ce0cc8962025d0b8c2028cbed5e7e2e75a Mon Sep 17 00:00:00 2001 From: Yair <92672946+yair-starkware@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:01:14 +0200 Subject: [PATCH] feat(batcher): add l1-provider client (#1870) --- crates/batcher/src/batcher.rs | 3 +++ crates/batcher/src/proposal_manager.rs | 11 +++++--- crates/batcher/src/proposal_manager_test.rs | 4 +++ crates/batcher/src/transaction_provider.rs | 28 ++++++++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/batcher/src/batcher.rs b/crates/batcher/src/batcher.rs index c448283ec9..24f90d67aa 100644 --- a/crates/batcher/src/batcher.rs +++ b/crates/batcher/src/batcher.rs @@ -40,6 +40,7 @@ use crate::proposal_manager::{ ProposalStatus, StartHeightError, }; +use crate::transaction_provider::DummyL1ProviderClient; type OutputStreamReceiver = tokio::sync::mpsc::UnboundedReceiver; type InputStreamSender = tokio::sync::mpsc::Sender; @@ -247,6 +248,7 @@ impl Batcher { } pub fn create_batcher(config: BatcherConfig, mempool_client: SharedMempoolClient) -> Batcher { + let l1_provider_client = Arc::new(DummyL1ProviderClient); let (storage_reader, storage_writer) = papyrus_storage::open_storage(config.storage.clone()) .expect("Failed to open batcher's storage"); @@ -258,6 +260,7 @@ pub fn create_batcher(config: BatcherConfig, mempool_client: SharedMempoolClient let storage_reader = Arc::new(storage_reader); let storage_writer = Box::new(storage_writer); let proposal_manager = Box::new(ProposalManager::new( + l1_provider_client, mempool_client.clone(), block_builder_factory, storage_reader.clone(), diff --git a/crates/batcher/src/proposal_manager.rs b/crates/batcher/src/proposal_manager.rs index cadcc0b2c1..bb5321b72c 100644 --- a/crates/batcher/src/proposal_manager.rs +++ b/crates/batcher/src/proposal_manager.rs @@ -17,7 +17,7 @@ use tracing::{debug, error, info, instrument, Instrument}; use crate::batcher::BatcherStorageReaderTrait; use crate::block_builder::{BlockBuilderError, BlockBuilderFactoryTrait, BlockExecutionArtifacts}; -use crate::transaction_provider::ProposeTransactionProvider; +use crate::transaction_provider::{ProposeTransactionProvider, SharedL1ProviderClient}; #[derive(Debug, Error)] pub enum StartHeightError { @@ -101,6 +101,7 @@ pub trait ProposalManagerTrait: Send + Sync { /// /// Triggered by the consensus. pub(crate) struct ProposalManager { + l1_provider_client: SharedL1ProviderClient, mempool_client: SharedMempoolClient, storage_reader: Arc, active_height: Option, @@ -171,8 +172,10 @@ impl ProposalManagerTrait for ProposalManager { let height = self.active_height.expect("No active height."); - let tx_provider = - ProposeTransactionProvider { mempool_client: self.mempool_client.clone() }; + let tx_provider = ProposeTransactionProvider { + mempool_client: self.mempool_client.clone(), + l1_provider_client: self.l1_provider_client.clone(), + }; let mut block_builder = self.block_builder_factory.create_block_builder( height, retrospective_block_hash, @@ -253,11 +256,13 @@ impl ProposalManagerTrait for ProposalManager { impl ProposalManager { pub fn new( + l1_provider_client: SharedL1ProviderClient, mempool_client: SharedMempoolClient, block_builder_factory: Arc, storage_reader: Arc, ) -> Self { Self { + l1_provider_client, mempool_client, storage_reader, active_proposal: Arc::new(Mutex::new(None)), diff --git a/crates/batcher/src/proposal_manager_test.rs b/crates/batcher/src/proposal_manager_test.rs index a30c27f2f8..d749a6b2fa 100644 --- a/crates/batcher/src/proposal_manager_test.rs +++ b/crates/batcher/src/proposal_manager_test.rs @@ -23,6 +23,7 @@ use crate::proposal_manager::{ ProposalOutput, StartHeightError, }; +use crate::transaction_provider::MockL1ProviderClient; const INITIAL_HEIGHT: BlockNumber = BlockNumber(3); const BLOCK_GENERATION_TIMEOUT: tokio::time::Duration = tokio::time::Duration::from_secs(1); @@ -38,6 +39,7 @@ fn output_streaming() -> ( struct MockDependencies { block_builder_factory: MockBlockBuilderFactoryTrait, + l1_provider_client: MockL1ProviderClient, mempool_client: MockMempoolClient, storage_reader: MockBatcherStorageReaderTrait, } @@ -82,6 +84,7 @@ fn mock_dependencies() -> MockDependencies { let mut storage_reader = MockBatcherStorageReaderTrait::new(); storage_reader.expect_height().returning(|| Ok(INITIAL_HEIGHT)); MockDependencies { + l1_provider_client: MockL1ProviderClient::new(), block_builder_factory: MockBlockBuilderFactoryTrait::new(), mempool_client: MockMempoolClient::new(), storage_reader, @@ -90,6 +93,7 @@ fn mock_dependencies() -> MockDependencies { fn init_proposal_manager(mock_dependencies: MockDependencies) -> ProposalManager { ProposalManager::new( + Arc::new(mock_dependencies.l1_provider_client), Arc::new(mock_dependencies.mempool_client), Arc::new(mock_dependencies.block_builder_factory), Arc::new(mock_dependencies.storage_reader), diff --git a/crates/batcher/src/transaction_provider.rs b/crates/batcher/src/transaction_provider.rs index a0d1751e7e..f0472301b3 100644 --- a/crates/batcher/src/transaction_provider.rs +++ b/crates/batcher/src/transaction_provider.rs @@ -1,9 +1,12 @@ +use std::sync::Arc; + use async_trait::async_trait; #[cfg(test)] use mockall::automock; -use starknet_api::executable_transaction::Transaction; +use starknet_api::executable_transaction::{L1HandlerTransaction, Transaction}; use starknet_mempool_types::communication::{MempoolClientError, SharedMempoolClient}; use thiserror::Error; +use tracing::warn; #[derive(Clone, Debug, Error)] pub enum TransactionProviderError { @@ -25,6 +28,9 @@ pub trait TransactionProvider: Send + Sync { pub struct ProposeTransactionProvider { pub mempool_client: SharedMempoolClient, + // TODO: remove allow(dead_code) when L1 transactions are added. + #[allow(dead_code)] + pub l1_provider_client: SharedL1ProviderClient, } #[async_trait] @@ -59,3 +65,23 @@ impl TransactionProvider for ValidateTransactionProvider { Ok(NextTxs::Txs(buffer)) } } + +// TODO: Remove L1Provider code when the communication module of l1-provider is added. +#[cfg_attr(test, automock)] +#[async_trait] +pub trait L1ProviderClient: Send + Sync { + #[allow(dead_code)] + fn get_txs(&self, n_txs: usize) -> Vec; +} + +pub type SharedL1ProviderClient = Arc; + +pub struct DummyL1ProviderClient; + +#[async_trait] +impl L1ProviderClient for DummyL1ProviderClient { + fn get_txs(&self, _n_txs: usize) -> Vec { + warn!("Dummy L1 provider client is used, no L1 transactions are provided."); + vec![] + } +}