From 46b4f986f28c0effa15a881f52765796557673df Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Thu, 7 Nov 2024 14:36:18 +0200 Subject: [PATCH] feat(batcher): add l1-provider client --- 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 d9b05f14ca..6855c4d481 100644 --- a/crates/batcher/src/batcher.rs +++ b/crates/batcher/src/batcher.rs @@ -37,6 +37,7 @@ use crate::proposal_manager::{ ProposalOutput, StartHeightError, }; +use crate::transaction_provider::DummyL1ProviderClient; struct Proposal { tx_stream: OutputStream, @@ -183,6 +184,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"); @@ -194,6 +196,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 de0ce51398..2cc5bdc37e 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 { @@ -94,6 +94,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, @@ -167,8 +168,10 @@ impl ProposalManagerTrait for ProposalManager { let block_builder = self.block_builder_factory.create_block_builder(height, retrospective_block_hash)?; - 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 active_proposal = self.active_proposal.clone(); let executed_proposals = self.executed_proposals.clone(); @@ -218,11 +221,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 2cb4176118..5bbf081196 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![] + } +}