bp_selector.rs

Overview

This file defines the ProducerSelector struct and related functionality for selecting a block producer (BP) node from a set of block keepers (BlockKeeperSet). The selection is deterministic and based on a seed derived from a block identifier, ensuring a reproducible shuffled order of block keeper nodes. The main purpose of ProducerSelector is to identify which node is the current producer and to calculate relative positions of nodes within the shuffled order, supporting consensus or leader election mechanisms.

Types and Imports

ProducerSelector Struct

pub struct ProducerSelector {
    rng_seed_block_id: BlockIdentifier,
    index: usize,
}

Fields

Derives and Traits

Implementation Details and Methods

get_producer_node_id

pub fn get_producer_node_id(&self, bk_set: &BlockKeeperSet) -> anyhow::Result<NodeIdentifier>
let producer_selector = ProducerSelector { rng_seed_block_id: some_block_id, index: 2 };
let producer_node = producer_selector.get_producer_node_id(&bk_set)?;
println!("Current producer node is {:?}", producer_node);

is_node_bp

pub fn is_node_bp(&self, bk_set: &BlockKeeperSet, node_id: &NodeIdentifier) -> anyhow::Result<bool>

check_whether_this_node_is_bp_based_on_bk_set_and_index_offset

pub fn check_whether_this_node_is_bp_based_on_bk_set_and_index_offset(
    &self,
    bk_set: &BlockKeeperSet,
    node_id: &NodeIdentifier,
    offset: usize,
) -> bool

get_distance_from_bp

pub fn get_distance_from_bp(&self, bk_set: &BlockKeeperSet, node_id: &NodeIdentifier) -> Option<usize>
if let Some(distance) = producer_selector.get_distance_from_bp(&bk_set, &my_node_id) {
    println!("Distance from producer is {}", distance);
}

move_index

pub fn move_index(self, diff: usize, bk_set_size: usize) -> Self

Interaction with Other Modules

Important Implementation Details

Tests Summary

Mermaid Diagram: Class Structure of ProducerSelector

classDiagram
class ProducerSelector {
-rng_seed_block_id: BlockIdentifier
-index: usize
+get_producer_node_id()
+is_node_bp()
+check_whether_this_node_is_bp_based_on_bk_set_and_index_offset()
+get_distance_from_bp()
+move_index()
}

This diagram illustrates the core data and methods encapsulated within the ProducerSelector class, showing its responsibilities for producer selection and position calculations.