optimistic_state.rs

Overview

This file defines the core structures and logic for managing an optimistic state representation of a blockchain shard's state in an asynchronous and concurrent environment. The optimistic state enables independent processing and verification of blocks and messages before final confirmation on the main chain, facilitating parallelism and efficiency in blockchain state management.

The file includes a trait OptimisticState defining the interface for optimistic state implementations, and a concrete implementation OptimisticStateImpl that encapsulates the shard's state, message queues, thread references, and related metadata. It supports serialization/deserialization, application of new blocks, state cropping based on thread partitions, and management of cross-thread messages and slashing messages.

Key Components

Trait: OptimisticState

Defines the behavior expected from any optimistic state implementation.

Associated Types

Required Methods


Struct: CrossThreadMessageData

Represents metadata about messages that cross thread boundaries.

Fields


Struct: OptimisticStateImpl

Concrete implementation of the OptimisticState trait, representing the optimistic state of a shard.

Fields

Important Methods

Debug and Default Implementations


Struct: TrimmedOptimisticStateImpl

A trimmed variant of OptimisticStateImpl used for serialization without the full shard state, which is stored separately as a Cell.

Fields

Conversion


Important Implementation Details and Algorithms


Interactions with Other Modules


Usage Examples

Applying a Block to an Optimistic State

let mut optimistic_state: OptimisticStateImpl = ...; // existing state
let block_candidate: AckiNackiBlock = ...; // new block to apply
let shared_services: SharedServices = ...;
let block_state_repo: BlockStateRepository = ...;
let nack_set_cache = Arc::new(Mutex::new(FixedSizeHashSet::new()));
let accounts_repo: AccountsRepository = ...;
let message_db: MessageDurableStorage = ...;

let (cross_thread_ref_data, added_messages) = optimistic_state
    .apply_block(&block_candidate, &shared_services, block_state_repo, nack_set_cache, accounts_repo, message_db)?;

Cropping the State to a Specific Thread

let thread_id: ThreadIdentifier = ...;
let threads_table: ThreadsTable = ...;
let message_db: MessageDurableStorage = ...;

optimistic_state.crop(&thread_id, &threads_table, message_db)?;

Serializing and Saving the State

let file_path = Path::new("state_file.dat");
optimistic_state.save_to_file(file_path)?;

Loading State from a File

let loaded_state = OptimisticStateImpl::load_from_file(file_path)?;

Data Structure Diagram

classDiagram
class OptimisticStateImpl {
-block_seq_no: BlockSeqNo
-block_id: BlockIdentifier
-shard_state: OptimisticShardState
-messages: ThreadMessageQueueState
-high_priority_messages: ThreadMessageQueueState
-block_info: BlockInfo
-threads_table: ThreadsTable
-thread_id: ThreadIdentifier
-thread_refs_state: ThreadReferencesState
-cropped: Option
-changed_accounts: HashMap
-cached_accounts: HashMap
+make_an_independent_copy()
+deserialize_from_buf()
+zero()
+set_shard_state()
+save_to_file()
+load_from_file()
+apply_block()
+crop()
+add_slashing_messages()
+get_thread_refs()
}
class OptimisticState {
<<interface>>
+get_share_stare_refs()
+get_block_seq_no()
+get_block_id()
+get_shard_state()
+get_shard_state_as_cell()
+get_block_info()
+serialize_into_buf()
+apply_block()
+get_thread_id()
+get_produced_threads_table()
+set_produced_threads_table()
+crop()
+get_thread_for_account()
+does_routing_belong_to_the_state()
+get_internal_message_queue_length()
+does_state_has_messages_to_other_threads()
+add_slashing_messages()
+get_thread_refs()
}
OptimisticStateImpl ..|> OptimisticState
class CrossThreadMessageData {
+block_id: BlockIdentifier
+src_account_routing: AccountRouting
+dest_account_routing: AccountRouting
}
class TrimmedOptimisticStateImpl {
+block_seq_no: BlockSeqNo
+block_id: BlockIdentifier
+messages: ThreadMessageQueueState
+high_priority_messages: ThreadMessageQueueState
+block_info: BlockInfo
+threads_table: ThreadsTable
+thread_id: ThreadIdentifier
+thread_refs_state: ThreadReferencesState
+cropped: Option
}
OptimisticStateImpl --> "1" OptimisticShardState
OptimisticStateImpl --> "1" ThreadMessageQueueState : messages
OptimisticStateImpl --> "1" ThreadMessageQueueState : high_priority_messages
OptimisticStateImpl --> "1" BlockInfo
OptimisticStateImpl --> "1" ThreadsTable
OptimisticStateImpl --> "1" ThreadReferencesState

Additional Notes

For further details on related types and concepts, see Block Identifiers and Sequence Numbers, Thread and Shard Management, Message Queues, and Shard State Updates.