pub struct Consensus<V: ValuePayload + 'static, A: ValidatorAddress + 'static> { /* private fields */ }Expand description
Pathfinder consensus engine.
This is the main consensus engine for Starknet nodes that implements Byzantine Fault Tolerant (BFT) consensus using the Malachite implementation of Tendermint. It’s generic over validator addresses and consensus values, making it suitable for Starknet’s consensus requirements.
§Generic Parameters
V: Your consensus value type (must implementValuePayload)A: Your validator address type (must implementValidatorAddress)
§Usage
let config = Config::new(my_address);
let mut consensus = Consensus::new(config);
// Start consensus at a height
consensus.handle_command(ConsensusCommand::StartHeight(height, validator_set));
// Poll for events
while let Some(event) = consensus.next_event().await {
// Handle events
}§Crash Recovery
The consensus engine supports crash recovery through write-ahead logging:
let validator_sets = Arc::new(StaticValidatorSetProvider::new(validator_set));
let mut consensus = Consensus::recover(config, validator_sets);Implementations§
Source§impl<V: ValuePayload + 'static, A: ValidatorAddress + 'static> Consensus<V, A>
impl<V: ValuePayload + 'static, A: ValidatorAddress + 'static> Consensus<V, A>
Sourcepub fn recover<P: ValidatorSetProvider<A> + 'static>(
config: Config<A>,
validator_sets: Arc<P>,
) -> Self
pub fn recover<P: ValidatorSetProvider<A> + 'static>( config: Config<A>, validator_sets: Arc<P>, ) -> Self
Recover recent heights from the write-ahead log.
This method is used to recover consensus state after a crash or restart. It reads the write-ahead log and reconstructs the consensus state for all incomplete heights.
§Arguments
config: The consensus configurationvalidator_sets: A provider for validator sets at different heights
§Example
let validator_sets = Arc::new(StaticValidatorSetProvider::new(validator_set));
let mut consensus = Consensus::recover(config, validator_sets);Sourcepub fn handle_command(&mut self, cmd: ConsensusCommand<V, A>)
pub fn handle_command(&mut self, cmd: ConsensusCommand<V, A>)
Feed a command into the consensus engine.
This method is the primary way to interact with the consensus engine. Commands include starting new heights, submitting proposals, and processing votes.
§Arguments
cmd: The command to process
§Example
// Start a new height
consensus.handle_command(ConsensusCommand::StartHeight(height, validator_set));
// Submit a proposal
consensus.handle_command(ConsensusCommand::Proposal(signed_proposal));
// Process a vote
consensus.handle_command(ConsensusCommand::Vote(signed_vote));Sourcepub async fn next_event(&mut self) -> Option<ConsensusEvent<V, A>>
pub async fn next_event(&mut self) -> Option<ConsensusEvent<V, A>>
Poll all engines for an event.
This method should be called regularly to process events from the consensus engine. Events include requests for proposals, decisions, gossip messages, and errors.
§Returns
Returns Some(event) if an event is available, or None if no events
are ready.
§Example
while let Some(event) = consensus.next_event().await {
match event {
ConsensusEvent::RequestProposal { height, round } => {
// Build and submit a proposal
}
ConsensusEvent::Decision { height, value } => {
// Consensus reached, process the value
}
ConsensusEvent::Gossip(message) => {
// Send message to peers
}
ConsensusEvent::Error(error) => {
// Handle error
}
}
}