Skip to content

Commit

Permalink
feat(batcher): add l1-provider client
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Nov 10, 2024
1 parent 56c06d5 commit 4e50633
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 @@ -37,6 +37,7 @@ use crate::proposal_manager::{
ProposalOutput,
StartHeightError,
};
use crate::transaction_provider::DummyL1ProviderClient;

struct Proposal {
tx_stream: OutputStream,
Expand Down Expand Up @@ -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");

Expand All @@ -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(),
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 @@ -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<dyn BatcherStorageReaderTrait>,
active_height: Option<BlockNumber>,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -218,11 +221,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::MockL1ProviderClientTrait;

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: MockL1ProviderClientTrait,
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: MockL1ProviderClientTrait::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 L1ProviderClientTrait: Send + Sync {
#[allow(dead_code)]
fn get_txs(&self, n_txs: usize) -> Vec<L1HandlerTransaction>;
}

pub type SharedL1ProviderClient = Arc<dyn L1ProviderClientTrait>;

pub struct DummyL1ProviderClient;

#[async_trait]
impl L1ProviderClientTrait 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 4e50633

Please sign in to comment.