Skip to content

Commit

Permalink
feat(consensus): add try_sync to context, and implement it in sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware committed Dec 24, 2024
1 parent 7509732 commit 1ca198e
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sequencing/papyrus_consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ mock! {
precommits: Vec<Vote>,
) -> Result<(), ConsensusError>;

async fn try_sync(&mut self, height: BlockNumber) -> bool;

async fn set_height_and_round(&mut self, height: BlockNumber, round: Round);
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sequencing/papyrus_consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ pub trait ConsensusContext {
precommits: Vec<Vote>,
) -> Result<(), ConsensusError>;

/// Attempt to learn of a decision from the sync protocol.
/// Returns true if a decision was learned so consensus can proceed.
async fn try_sync(&mut self, height: BlockNumber) -> bool;

/// Update the context with the current height and round.
/// Must be called at the beginning of each height.
async fn set_height_and_round(&mut self, height: BlockNumber, round: Round);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ impl ConsensusContext for PapyrusConsensusContext {
Ok(())
}

async fn try_sync(&mut self, _height: BlockNumber) -> bool {
// TODO(Asmaa): Implement this.
false
}

async fn set_height_and_round(&mut self, _height: BlockNumber, _round: Round) {
// No-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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};
Expand Down Expand Up @@ -97,6 +98,7 @@ enum HandledProposalPart {
const BUILD_PROPOSAL_MARGIN: Duration = Duration::from_millis(1000);

pub struct SequencerConsensusContext {
state_sync_client: SharedStateSyncClient,
batcher: Arc<dyn BatcherClient>,
validators: Vec<ValidatorId>,
// Proposal building/validating returns immediately, leaving the actual processing to a spawned
Expand Down Expand Up @@ -127,6 +129,7 @@ pub struct SequencerConsensusContext {

impl SequencerConsensusContext {
pub fn new(
state_sync_client: SharedStateSyncClient,
batcher: Arc<dyn BatcherClient>,
outbound_proposal_sender: mpsc::Sender<(u64, mpsc::Receiver<ProposalPart>)>,
vote_broadcast_client: BroadcastTopicClient<ConsensusMessage>,
Expand All @@ -135,6 +138,7 @@ impl SequencerConsensusContext {
cende_ambassador: Arc<dyn CendeContext>,
) -> Self {
Self {
state_sync_client,
batcher,
outbound_proposal_sender,
vote_broadcast_client,
Expand Down Expand Up @@ -340,6 +344,15 @@ impl ConsensusContext for SequencerConsensusContext {
Ok(())
}

async fn try_sync(&mut self, height: BlockNumber) -> bool {
let sync_block = self.state_sync_client.get_block(height).await;
if let Ok(Some(sync_block)) = sync_block {
self.batcher.add_sync_block(sync_block).await.unwrap();
return true;
}
false
}

async fn set_height_and_round(&mut self, height: BlockNumber, round: Round) {
if self.current_height.map(|h| height > h).unwrap_or(true) {
self.current_height = Some(height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_consensus_manager/src/consensus_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions crates/starknet_state_sync_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ 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"] }
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"] }
3 changes: 3 additions & 0 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit 1ca198e

Please sign in to comment.