preprocessing.rs

Overview

This file implements the preprocessing logic for optimistic shard state updates in a multi-threaded blockchain system. It primarily handles the integration and preparation of shard state, cross-thread references, and message queues before the main block processing occurs. The preprocessing includes merging thread tables, importing migrating accounts with their inboxes, settling accounts, and preparing message queues with settled and high-priority messages such as slashing and epoch-related messages.

The key output of this file is a PreprocessingResult struct that contains the updated shard state, the thread table, and collections of redirected and settled messages.


Structs and Types

PreprocessingResult

pub struct PreprocessingResult {
    pub state: State,
    pub threads_table: ThreadsTable,
    pub redirected_messages: HashMap<AccountRouting, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>,
    pub settled_messages: HashMap<AccountAddress, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>,
}

Functions

preprocess

pub fn preprocess<'a, I, TRepo>(
    parent_block_state: State,
    refs: I,
    descendant_thread_identifier: &ThreadIdentifier,
    repository: &TRepo,
    slashing_messages: Vec<Arc<WrappedMessage>>,
    epoch_block_keeper_data: Vec<BlockKeeperData>,
    message_db: MessageDurableStorage,
    metrics: Option<BlockProductionMetrics>,
) -> anyhow::Result<PreprocessingResult>
where
    I: std::iter::Iterator<Item = &'a CrossThreadRefData>,
    CrossThreadRefData: 'a,
    TRepo: CrossThreadRefDataRead,
let preprocessing_result = preprocess(
    parent_state,
    cross_thread_refs_iter,
    &descendant_thread_id,
    &repository,
    slashing_msgs,
    epoch_data,
    message_storage,
    Some(metrics),
)?;

convert_slashing_messages

pub fn convert_slashing_messages(
    slashing_messages: Vec<Arc<WrappedMessage>>,
) -> anyhow::Result<HashMap<AccountAddress, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>>

convert_epoch_messages

pub fn convert_epoch_messages(
    high_priority_map: &mut HashMap<AccountAddress, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>,
    epoch_message: Vec<BlockKeeperData>,
) -> anyhow::Result<()>

import_migrating_accounts_with_their_inboxes

fn import_migrating_accounts_with_their_inboxes(
    all_referenced_blocks: &[CrossThreadRefData],
    in_table: &ThreadsTable,
    descendant_thread_identifier: &ThreadIdentifier,
    mut state: State,
    message_db: MessageDurableStorage,
    metrics: Option<BlockProductionMetrics>,
) -> anyhow::Result<State>

settle_accounts

fn settle_accounts(
    shard_state: &mut OptimisticShardState,
    migrated_accounts: Vec<WrappedAccount>,
    removed_accounts: Vec<AccountAddress>,
) -> anyhow::Result<()>

Important Implementation Details and Algorithms


Interactions with Other Components


Visual Diagram

flowchart TD
A[preprocess] --> B[Update Thread References]
B --> C[Fetch Cross-Thread References]
C --> D[Sort & Merge Referenced Blocks]
D --> E[Merge Threads Table]
E --> F[Crop Shard State]
F --> G[import_migrating_accounts_with_their_inboxes]
G --> H[Settle Accounts]
H --> I[Build Settled Messages Queue]
I --> J[convert_slashing_messages]
J --> K[convert_epoch_messages]
K --> L[Build High-Priority Messages Queue]
L --> M[Return PreprocessingResult]
G -.-> F
H -.-> G
I -.-> H
J -.-> I
K -.-> J
L -.-> K

References to Related Topics