block_producer.rs

Overview

The block_producer.rs file implements the BlockProducer struct, which is responsible for orchestrating the production of blocks within a specific thread of the blockchain system. It manages the lifecycle of block production, including starting and stopping block generation, handling production timeouts, updating candidate blocks with required attestations, and broadcasting produced blocks to the network.

This module interacts heavily with block state repositories, the underlying repository implementation, cryptographic BLS signing utilities, network messaging, and various supporting services such as attestation target evaluation and optimistic state management. It also manages synchronization with external commands and ensures proper handling of production continuation after restarts.


Key Components

Structs and Enums

ProductionStatus

BlockProducer

UpdateCommonSectionResult


Functions and Methods

BlockProducer::main_loop

pub fn main_loop(&mut self) -> anyhow::Result<()>

BlockProducer::start_production

pub fn start_production(&mut self, next_bp_command: Option<BlockProducerCommand>) -> anyhow::Result<Option<(BlockIdentifier, BlockSeqNo)>>

BlockProducer::find_thread_last_block_id_this_node_can_continue

pub(crate) fn find_thread_last_block_id_this_node_can_continue(
    &mut self,
    next_bp_command: Option<BlockProducerCommand>
) -> anyhow::Result<Option<(BlockIdentifier, BlockSeqNo, BlockRound)>>

BlockProducer::on_production_timeout

pub(crate) fn on_production_timeout(
    &mut self,
    producer_tails: &mut Option<(BlockIdentifier, BlockSeqNo)>,
    memento: Option<BlockProducerMemento>
) -> anyhow::Result<(bool, Option<BlockProducerMemento>)>

BlockProducer::_does_block_have_a_valid_sibling

pub(crate) fn _does_block_have_a_valid_sibling(
    &self,
    candidate_block: &Envelope<GoshBLS, AckiNackiBlock>
) -> anyhow::Result<bool>

BlockProducer::update_candidate_common_section

fn update_candidate_common_section(
    &mut self,
    candidate_block: &mut AckiNackiBlock,
    share_state_ids: Option<HashMap<ThreadIdentifier, BlockIdentifier>>,
    optimistic_state: &OptimisticStateImpl,
) -> anyhow::Result<UpdateCommonSectionResult>

BlockProducer::get_producer_selector

pub fn get_producer_selector(
    &self,
    parent_block_id: &BlockIdentifier,
) -> anyhow::Result<ProducerSelector>

BlockProducer::broadcast_candidate_block

pub(crate) fn broadcast_candidate_block(
    &self,
    block_id: &BlockIdentifier,
    candidate_block: NetworkMessage,
    mut ext_msg_feedbacks: ExtMsgFeedbackList,
) -> anyhow::Result<()>

BlockProducer::_broadcast_candidate_block_that_was_possibly_produced_by_another_node

pub(crate) fn _broadcast_candidate_block_that_was_possibly_produced_by_another_node(
    &self,
    candidate_block: Envelope<GoshBLS, AckiNackiBlock>,
) -> anyhow::Result<()>

BlockProducer::execute_restarted_producer

fn execute_restarted_producer(
    &mut self,
    _block_id_to_continue: BlockIdentifier,
    _block_seq_no_to_continue: BlockSeqNo,
) -> anyhow::Result<SynchronizationResult<NetworkMessage>>

Important Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram: Class Diagram of BlockProducer

classDiagram
class BlockProducer {
+self_addr: SocketAddr
+thread_id: ThreadIdentifier
+repository: RepositoryImpl
+block_state_repository: BlockStateRepository
+production_process: TVMBlockProducerProcess
+feedback_sender: InstrumentedSender<ExtMsgFeedbackList>
+received_acks: Arc<Mutex<Vec<Envelope>>>
+received_nacks: Arc<Mutex<Vec<Envelope>>>
+shared_services: SharedServices
+bls_keys_map: Arc<Mutex<HashMap>>
+last_broadcasted_produced_candidate_block_time: Instant
+last_block_attestations: Arc<Mutex<CollectedAttestations>>
+attestations_target_service: AttestationTargetsService
+self_tx: XInstrumentedSender<(NetworkMessage, SocketAddr)>
+self_authority_tx: XInstrumentedSender<(NetworkMessage, SocketAddr)>
+broadcast_tx: NetBroadcastSender<NetworkMessage>
+node_identifier: NodeIdentifier
+production_timeout: Duration
+save_state_frequency: u32
+bp_production_count: Arc<AtomicI32>
+production_status: Option<ProductionStatus>
+producing_status: bool
+external_messages: ExternalMessagesThreadState
+is_state_sync_requested: Arc<Mutex<Option<BlockSeqNo>>>
+control_rx: Receiver<BlockProducerCommand>
+save_optimistic_service_sender: InstrumentedSender<Arc<OptimisticStateImpl>>
+main_loop()
+start_production()
+find_thread_last_block_id_this_node_can_continue()
+on_production_timeout()
+update_candidate_common_section()
+get_producer_selector()
+broadcast_candidate_block()
+execute_restarted_producer()
}
class ProductionStatus {
+init_params: StartBlockProducerThreadInitialParameters
+last_produced: Option<(BlockIdentifier, BlockSeqNo)>
}
BlockProducer --> ProductionStatus

Usage Notes and Examples