Skip to content

Commit

Permalink
feat(consensus): add decision fn to consensus context API (#387)
Browse files Browse the repository at this point in the history
This is how consensus updates the rest of the node when a new block has been decided.
  • Loading branch information
matan-starkware authored Aug 14, 2024
1 parent ce82441 commit 13ae058
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
11 changes: 2 additions & 9 deletions crates/sequencing/papyrus_consensus/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use std::time::Duration;

use futures::channel::{mpsc, oneshot};
use futures::{Stream, StreamExt};
use papyrus_common::metrics as papyrus_metrics;
use papyrus_network::network_manager::ReportSender;
use papyrus_protobuf::consensus::{ConsensusMessage, Proposal};
use papyrus_protobuf::converters::ProtobufConversionError;
use starknet_api::block::{BlockHash, BlockNumber};
use tracing::{debug, info, instrument};
use tracing::{debug, instrument};

use crate::single_height_consensus::SingleHeightConsensus;
use crate::types::{
Expand Down Expand Up @@ -52,13 +51,7 @@ where
let decision = manager
.run_height(&mut context, current_height, validator_id, &mut network_receiver)
.await?;

info!(
"Finished consensus for height: {current_height}. Agreed on block with id: {:x}",
decision.block.id().0
);
debug!("Decision: {:?}", decision);
metrics::gauge!(papyrus_metrics::PAPYRUS_CONSENSUS_HEIGHT, current_height.0 as f64);
context.notify_decision(decision.block, decision.precommits).await?;
current_height = current_height.unchecked_next();
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/sequencing/papyrus_consensus/src/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ mock! {
content_receiver: mpsc::Receiver<Transaction>,
fin_receiver: oneshot::Receiver<BlockHash>,
) -> Result<(), ConsensusError>;

async fn notify_decision(
&self,
block: TestBlock,
precommits: Vec<Vote>,
) -> Result<(), ConsensusError>;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ use async_trait::async_trait;
use futures::channel::{mpsc, oneshot};
use futures::sink::SinkExt;
use futures::StreamExt;
use papyrus_common::metrics::PAPYRUS_CONSENSUS_HEIGHT;
use papyrus_network::network_manager::BroadcastSubscriberSender;
use papyrus_protobuf::consensus::{ConsensusMessage, Proposal};
use papyrus_protobuf::consensus::{ConsensusMessage, Proposal, Vote};
use papyrus_storage::body::BodyStorageReader;
use papyrus_storage::header::HeaderStorageReader;
use papyrus_storage::{StorageError, StorageReader};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ContractAddress;
use starknet_api::transaction::Transaction;
use tracing::{debug, debug_span, Instrument};
use tracing::{debug, debug_span, info, Instrument};

use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId};
use crate::ProposalWrapper;
Expand Down Expand Up @@ -225,6 +226,20 @@ impl ConsensusContext for PapyrusConsensusContext {
);
Ok(())
}

async fn notify_decision(
&self,
block: Self::Block,
precommits: Vec<Vote>,
) -> Result<(), ConsensusError> {
let height = precommits[0].height;
info!(
"Finished consensus for height: {height}. Agreed on block with id: {:x}",
block.id().0
);
metrics::gauge!(PAPYRUS_CONSENSUS_HEIGHT, height as f64);
Ok(())
}
}

const SLEEP_BETWEEN_CHECK_FOR_BLOCK: Duration = Duration::from_secs(10);
Expand Down
8 changes: 7 additions & 1 deletion crates/sequencing/papyrus_consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use futures::channel::{mpsc, oneshot};
use mockall::mock;
use papyrus_protobuf::consensus::ConsensusMessage;
use papyrus_protobuf::consensus::{ConsensusMessage, Vote};
use starknet_api::block::{BlockHash, BlockNumber};

use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId};
Expand Down Expand Up @@ -57,5 +57,11 @@ mock! {
content_receiver: mpsc::Receiver<u32>,
fin_receiver: oneshot::Receiver<BlockHash>,
) -> Result<(), ConsensusError>;

async fn notify_decision(
&self,
block: TestBlock,
precommits: Vec<Vote>,
) -> Result<(), ConsensusError>;
}
}
10 changes: 10 additions & 0 deletions crates/sequencing/papyrus_consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ pub trait ConsensusContext {
content_receiver: mpsc::Receiver<<Self::Block as ConsensusBlock>::ProposalChunk>,
fin_receiver: oneshot::Receiver<BlockHash>,
) -> Result<(), ConsensusError>;

/// Update the context that a decision has been reached for a given height.
/// - `block` identifies the decision.
/// - `precommits` - All precommits must be for the same `(block.id(), height, round)` and form
/// a quorum (>2/3 of the voting power) for this height.
async fn notify_decision(
&self,
block: Self::Block,
precommits: Vec<Vote>,
) -> Result<(), ConsensusError>;
}

#[derive(PartialEq)]
Expand Down

0 comments on commit 13ae058

Please sign in to comment.