action_lock.rs

Overview

This file implements the authority switch protocol for managing block production and attestation locking in a multi-threaded blockchain node environment. Its primary responsibility is to maintain and update locks on blocks in various rounds, coordinate block producer threads, process incoming requests related to round changes, and handle the lifecycle and state transitions of blocks as they move through prefinalization and finalization stages.

The protocol ensures consensus on the "locked" block per round, protects against conflicting attestations, and orchestrates block production with respect to the network's current state and authority votes. Durability of locks is guaranteed via persistent storage to avoid stake slashing on restarts.

Main Entities and Structures

BlockRef

pub struct BlockRef {
    block_seq_no: BlockSeqNo,
    block_identifier: BlockIdentifier,
}

Usage Example:

let block_ref = BlockRef::try_from(&block_state)?;

StartBlockProducerThreadInitialParameters

pub struct StartBlockProducerThreadInitialParameters {
    thread_identifier: ThreadIdentifier,
    parent_block_identifier: BlockIdentifier,
    parent_block_seq_no: BlockSeqNo,
    round: BlockRound,
    nacked_blocks_bad_block: Vec<Arc<Envelope<GoshBLS, AckiNackiBlock>>>,
    proof_of_valid_start: Vec<Envelope<GoshBLS, Lock>>,
}

BlockProducerCommand

pub enum BlockProducerCommand {
    Start(StartBlockProducerThreadInitialParameters),
}

ActionLock

pub struct ActionLock {
    parent_block_producer_selector_data: ProducerSelector,
    parent_block: BlockRef,
    parent_prefinalization_proof: Option<Envelope<GoshBLS, AttestationData>>,
    locked_round: BlockRound,
    locked_block: Option<(BlockRound, BlockRef)>,
    locked_bad_block_nacks: HashSet<BlockIdentifier>,
}

Authority

pub struct Authority {
    authorities: HashMap<ThreadIdentifier, Arc<Mutex<ThreadAuthority>>>,
    round_buckets: RoundTime,
    data_dir: PathBuf,
    node_identifier: NodeIdentifier,
    bls_keys_map: Arc<Mutex<HashMap<PubKey, (Secret, RndSeed)>>>,
    block_repository: RepositoryImpl,
    block_state_repository: BlockStateRepository,
    network_direct_tx: NetDirectSender<NodeIdentifier, NetworkMessage>,
    bp_production_count: Arc<AtomicI32>,
    network_broadcast_tx: NetBroadcastSender<NetworkMessage>,
    node_joining_timeout: Duration,
    action_lock_db: ActionLockStorage,
    max_lookback_block_height_distance: usize,
    self_addr: SocketAddr,
    action_lock_collections: HashMap<ThreadIdentifier, ActionLockCollection>,
}

ThreadAuthority

pub struct ThreadAuthority {
    thread_id: ThreadIdentifier,
    round_buckets: RoundTime,
    node_identifier: NodeIdentifier,
    bls_keys_map: Arc<Mutex<HashMap<PubKey, (Secret, RndSeed)>>>,

    action_lock: ActionLockCollection,

    confirmed_bad_block_nacks: HashMap<SiblingsBlockHeightKey, HashSet<BlockIdentifier>>,
    collecting_next_round: HashMap<(SiblingsBlockHeightKey, BlockRound), CollectedAuthoritySwitchRoundRequests>,
    closed_round: HashMap<SiblingsBlockHeightKey, BlockRound>,

    block_producers: Option<std::sync::mpsc::Sender<BlockProducerCommand>>,
    block_repository: RepositoryImpl,
    block_state_repository: BlockStateRepository,
    network_direct_tx: NetDirectSender<NodeIdentifier, NetworkMessage>,
    bp_production_count: Arc<AtomicI32>,
    network_broadcast_tx: NetBroadcastSender<NetworkMessage>,
    node_joining_timeout: Duration,
    last_node_joining_sent: Instant,
    max_lookback_block_height_distance: usize,
    self_node_authority_tx: Option<XInstrumentedSender<(NetworkMessage, SocketAddr)>>,
    self_addr: SocketAddr,
}

ActionLockCollection

pub struct ActionLockCollection {
    preloaded: HashMap<BlockHeight, Option<ActionLock>>,
    data_dir: PathBuf,
    action_lock_db: ActionLockStorage,
}

Key Functions and Methods

Authority::get_thread_authority

ThreadAuthority::try_lock_send_attestation_action

pub fn try_lock_send_attestation_action(
    &mut self,
    block_identifier: &BlockIdentifier,
) -> ActionLockResult

Usage Example:

match thread_authority.try_lock_send_attestation_action(&block_id) {
    ActionLockResult::OkSendAttestation => { /* proceed with attestation */ }
    ActionLockResult::Rejected => { /* do not attest */ }
}

ThreadAuthority::on_block_producer_stalled

pub fn on_block_producer_stalled(&mut self) -> OnBlockProducerStalledResult

ThreadAuthority::start_next_round

fn start_next_round(
    &mut self,
    parent_block: BlockState,
    block_height: BlockHeight,
) -> OnBlockProducerStalledResult

ThreadAuthority::on_next_round_incoming_request

pub fn on_next_round_incoming_request(
    &mut self,
    next_round_message: NextRound,
    sender: Option<SocketAddr>,
    unprocessed_blocks_cache: UnfinalizedCandidateBlockCollection,
) -> OnNextRoundIncomingRequestResult

ThreadAuthority::on_next_round_success

pub fn on_next_round_success(&mut self, next_round_success: &NextRoundSuccess)

ActionLockCollection Methods

Important Implementation Details

Interactions with Other System Components

Visual Diagram

classDiagram
class Authority {
+get_thread_authority()
}
class ThreadAuthority {
+try_lock_send_attestation_action()
+on_block_producer_stalled()
+start_next_round()
+on_next_round_incoming_request()
+on_next_round_success()
+register_block_producer()
+register_self_node_authority_tx()
}
class ActionLockCollection {
+get_mut()
+get()
+insert()
+remove()
+drop_old_locks()
+save()
+preload()
}
class ActionLock {
-parent_block_producer_selector_data
-parent_block
-parent_prefinalization_proof
-locked_round
-locked_block
-locked_bad_block_nacks
}
class BlockRef {
-block_seq_no
-block_identifier
}
class BlockProducerCommand {
<<enum>>
}
Authority "1" --> "*" ThreadAuthority : manages
ThreadAuthority "1" --> "1" ActionLockCollection : owns
ActionLockCollection "1" --> "*" ActionLock : manages
ActionLock *-- BlockRef : references
ThreadAuthority --> BlockProducerCommand : sends

This diagram depicts the main relationships:

Error Enums

Additional Notes

For related concepts, see topics on Block State Management, BLS Cryptography, Network Messaging, and Consensus Protocols.