From b651202eee981e7d6bfe90e3971fe188b5c15d48 Mon Sep 17 00:00:00 2001 From: asmaa-starkware <163830216+asmaastarkware@users.noreply.github.com> Date: Wed, 25 Dec 2024 17:08:55 +0200 Subject: [PATCH] feat(consensus): add StateSyncClient to sequencer context (#2938) --- Cargo.lock | 3 +++ .../sequencing/papyrus_consensus_orchestrator/Cargo.toml | 1 + .../src/sequencer_consensus_context.rs | 5 +++++ .../src/sequencer_consensus_context_test.rs | 3 +++ .../starknet_consensus_manager/src/consensus_manager.rs | 1 + crates/starknet_state_sync_types/Cargo.toml | 8 ++++++++ crates/starknet_state_sync_types/src/communication.rs | 3 +++ 7 files changed, 24 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1b52facedc..46269280ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7414,6 +7414,7 @@ dependencies = [ "starknet-types-core", "starknet_api", "starknet_batcher_types", + "starknet_state_sync_types", "test-case", "tokio", "tokio-util", @@ -10808,12 +10809,14 @@ version = "0.0.0" dependencies = [ "async-trait", "futures", + "mockall", "papyrus_proc_macros", "papyrus_storage", "serde", "starknet-types-core", "starknet_api", "starknet_sequencer_infra", + "starknet_state_sync_types", "thiserror", ] diff --git a/crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml b/crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml index b6f66304b2..09e543d913 100644 --- a/crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml +++ b/crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml @@ -20,6 +20,7 @@ serde.workspace = true starknet-types-core.workspace = true starknet_api.workspace = true starknet_batcher_types = { workspace = true, features = ["testing"] } +starknet_state_sync_types = { workspace = true, features = ["testing"] } tokio = { workspace = true, features = ["full"] } tokio-util.workspace = true tracing.workspace = true diff --git a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context.rs b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context.rs index 8d7b858438..d25b781808 100644 --- a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context.rs +++ b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context.rs @@ -54,6 +54,7 @@ use starknet_batcher_types::batcher_types::{ ValidateBlockInput, }; use starknet_batcher_types::communication::BatcherClient; +use starknet_state_sync_types::communication::SharedStateSyncClient; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; use tracing::{debug, debug_span, info, instrument, trace, warn, Instrument}; @@ -96,6 +97,8 @@ enum HandledProposalPart { const BUILD_PROPOSAL_MARGIN: Duration = Duration::from_millis(1000); pub struct SequencerConsensusContext { + #[allow(dead_code)] + state_sync_client: SharedStateSyncClient, batcher: Arc, validators: Vec, // Proposal building/validating returns immediately, leaving the actual processing to a spawned @@ -126,6 +129,7 @@ pub struct SequencerConsensusContext { impl SequencerConsensusContext { pub fn new( + state_sync_client: SharedStateSyncClient, batcher: Arc, outbound_proposal_sender: mpsc::Sender<(u64, mpsc::Receiver)>, vote_broadcast_client: BroadcastTopicClient, @@ -134,6 +138,7 @@ impl SequencerConsensusContext { cende_ambassador: Arc, ) -> Self { Self { + state_sync_client, batcher, outbound_proposal_sender, vote_broadcast_client, diff --git a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs index b72fbe3900..78a034338d 100644 --- a/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs +++ b/crates/sequencing/papyrus_consensus_orchestrator/src/sequencer_consensus_context_test.rs @@ -41,6 +41,7 @@ use starknet_batcher_types::batcher_types::{ ValidateBlockInput, }; use starknet_batcher_types::communication::MockBatcherClient; +use starknet_state_sync_types::communication::MockStateSyncClient; use starknet_types_core::felt::Felt; use crate::cende::MockCendeContext; @@ -88,8 +89,10 @@ fn setup( mock_register_broadcast_topic().expect("Failed to create mock network"); let BroadcastTopicChannels { broadcast_topic_client: votes_topic_client, .. } = subscriber_channels; + let state_sync_client = MockStateSyncClient::new(); let context = SequencerConsensusContext::new( + Arc::new(state_sync_client), Arc::new(batcher), outbound_proposal_stream_sender, votes_topic_client, diff --git a/crates/starknet_consensus_manager/src/consensus_manager.rs b/crates/starknet_consensus_manager/src/consensus_manager.rs index b5fcf4bdec..c1aa115932 100644 --- a/crates/starknet_consensus_manager/src/consensus_manager.rs +++ b/crates/starknet_consensus_manager/src/consensus_manager.rs @@ -81,6 +81,7 @@ impl ConsensusManager { }; let context = SequencerConsensusContext::new( + Arc::clone(&self.state_sync_client), Arc::clone(&self.batcher_client), outbound_internal_sender, votes_broadcast_channels.broadcast_topic_client.clone(), diff --git a/crates/starknet_state_sync_types/Cargo.toml b/crates/starknet_state_sync_types/Cargo.toml index 5df2d7fa51..1bd51a2b81 100644 --- a/crates/starknet_state_sync_types/Cargo.toml +++ b/crates/starknet_state_sync_types/Cargo.toml @@ -5,12 +5,16 @@ edition.workspace = true license.workspace = true repository.workspace = true +[features] +testing = ["mockall"] + [lints] workspace = true [dependencies] async-trait.workspace = true futures.workspace = true +mockall = { workspace = true, optional = true } papyrus_proc_macros.workspace = true papyrus_storage.workspace = true serde = { workspace = true, features = ["derive"] } @@ -18,3 +22,7 @@ starknet-types-core.workspace = true starknet_api.workspace = true starknet_sequencer_infra.workspace = true thiserror.workspace = true + +[dev-dependencies] +# Enable self with "testing" feature in tests. +starknet_state_sync_types = { workspace = true, features = ["testing"] } diff --git a/crates/starknet_state_sync_types/src/communication.rs b/crates/starknet_state_sync_types/src/communication.rs index 4c1a56c794..2c694a2624 100644 --- a/crates/starknet_state_sync_types/src/communication.rs +++ b/crates/starknet_state_sync_types/src/communication.rs @@ -1,6 +1,8 @@ use std::sync::Arc; use async_trait::async_trait; +#[cfg(any(feature = "testing", test))] +use mockall::automock; use papyrus_proc_macros::handle_response_variants; use serde::{Deserialize, Serialize}; use starknet_api::block::BlockNumber; @@ -22,6 +24,7 @@ use thiserror::Error; use crate::errors::StateSyncError; use crate::state_sync_types::{StateSyncResult, SyncBlock}; +#[cfg_attr(any(test, feature = "testing"), automock)] #[async_trait] pub trait StateSyncClient: Send + Sync { /// Request for a block at a specific height.