Skip to content

Commit

Permalink
feat(batcher): add l1-provider client (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware authored Nov 12, 2024
1 parent 2190f01 commit d556b2c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions crates/batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::proposal_manager::{
ProposalStatus,
StartHeightError,
};
use crate::transaction_provider::DummyL1ProviderClient;

type OutputStreamReceiver = tokio::sync::mpsc::UnboundedReceiver<Transaction>;
type InputStreamSender = tokio::sync::mpsc::Sender<Transaction>;
Expand Down Expand Up @@ -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");

Expand All @@ -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(),
Expand Down
11 changes: 8 additions & 3 deletions crates/batcher/src/proposal_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<dyn BatcherStorageReaderTrait>,
active_height: Option<BlockNumber>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -253,11 +256,13 @@ impl ProposalManagerTrait for ProposalManager {

impl ProposalManager {
pub fn new(
l1_provider_client: SharedL1ProviderClient,
mempool_client: SharedMempoolClient,
block_builder_factory: Arc<dyn BlockBuilderFactoryTrait + Send + Sync>,
storage_reader: Arc<dyn BatcherStorageReaderTrait>,
) -> Self {
Self {
l1_provider_client,
mempool_client,
storage_reader,
active_proposal: Arc::new(Mutex::new(None)),
Expand Down
4 changes: 4 additions & 0 deletions crates/batcher/src/proposal_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -38,6 +39,7 @@ fn output_streaming() -> (

struct MockDependencies {
block_builder_factory: MockBlockBuilderFactoryTrait,
l1_provider_client: MockL1ProviderClient,
mempool_client: MockMempoolClient,
storage_reader: MockBatcherStorageReaderTrait,
}
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
28 changes: 27 additions & 1 deletion crates/batcher/src/transaction_provider.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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<L1HandlerTransaction>;
}

pub type SharedL1ProviderClient = Arc<dyn L1ProviderClient>;

pub struct DummyL1ProviderClient;

#[async_trait]
impl L1ProviderClient for DummyL1ProviderClient {
fn get_txs(&self, _n_txs: usize) -> Vec<L1HandlerTransaction> {
warn!("Dummy L1 provider client is used, no L1 transactions are provided.");
vec![]
}
}

0 comments on commit d556b2c

Please sign in to comment.