optimistic_shard_state.rs

Overview

This file defines the OptimisticShardState abstraction, which encapsulates an underlying shard state representation that can exist either as a fully parsed structured form (ShardStateUnsplit), a raw serialized form (Cell), or both simultaneously. The design allows efficient lazy deserialization and serialization of shard states within a concurrent environment, supporting optimistic caching and mutation.

The core functionality revolves around managing these dual representations while providing thread-safe access and mutation through synchronization primitives. This approach optimizes performance by avoiding unnecessary conversions between structured and serialized forms unless explicitly required.

Key Components

Enum: OptimisticShardStateRepresentation

This enum represents the internal state of the shard data and has three variants:

Implementation Details


Struct: OptimisticShardState

The main struct encapsulating an OptimisticShardStateRepresentation inside a Mutex (from parking_lot) wrapped in an Arc to provide thread-safe shared ownership with interior mutability.

Fields

Traits Implemented


Methods

make_an_independent_copy(&self) -> Self

Creates a new OptimisticShardState containing a clone of the current internal representation, ensuring complete independence from the original instance.

Usage Example:

let original: OptimisticShardState = ...;
let independent_copy = original.make_an_independent_copy();

into_cell(&self) -> Arc<Cell>

Returns the serialized Cell representation of the shard state. If the current representation lacks the cell form, it serializes the ShardStateUnsplit and updates the internal state to Both.

Important Implementation Detail:


into_shard_state(&self) -> Arc<ShardStateUnsplit>

Returns the structured ShardStateUnsplit representation. If only the Cell form is available, it deserializes the Cell into a ShardStateUnsplit, updates the internal state to Both, and returns the parsed state.

Implementation Notes:


clone_representation(&self) -> OptimisticShardStateRepresentation

Provides a clone of the internal enum representation for external usage or inspection without locking the whole struct.


Serialization and Deserialization


Interaction with Other System Components


Important Implementation Details and Algorithms


Usage Examples

use std::sync::Arc;
use tvm_block::ShardStateUnsplit;
use tvm_types::Cell;

// Create OptimisticShardState from ShardStateUnsplit
let shard_state: Arc<ShardStateUnsplit> = Arc::new(ShardStateUnsplit::default());
let optimistic_state = OptimisticShardState::from(shard_state.clone());

// Get serialized cell representation
let cell: Arc<Cell> = optimistic_state.into_cell();

// Get structured shard state (deserializes if needed)
let parsed_shard_state: Arc<ShardStateUnsplit> = optimistic_state.into_shard_state();

// Clone representation
let cloned_repr = optimistic_state.clone_representation();

Mermaid Diagram: Structure of optimistic_shard_state.rs

classDiagram
class OptimisticShardState {
-data: Arc<Mutex<OptimisticShardStateRepresentation>>
+make_an_independent_copy()
+into_cell()
+into_shard_state()
+clone_representation()
}
class OptimisticShardStateRepresentation {
<<enum>>
+Both
+AsShardState
+AsCell
}
OptimisticShardState --> OptimisticShardStateRepresentation
OptimisticShardStateRepresentation o-- ShardStateUnsplit : contains
OptimisticShardStateRepresentation o-- Cell : contains

This diagram illustrates the core data structure, showing that OptimisticShardState contains a mutex-protected instance of the enum OptimisticShardStateRepresentation, which in turn holds either or both of ShardStateUnsplit and Cell instances.