block_info.rs
Overview
This file defines the BlockInfo struct, which acts as a transparent wrapper around the tvm_block::BlkPrevInfo type, providing serialization and deserialization support using custom formatting. It also implements dereferencing traits to allow seamless access to the inner BlkPrevInfo instance. Additionally, the file introduces the BlockEndLT struct, a simple wrapper around a u64 value with derived traits for ordering, equality, and serialization.
The primary role of this file is to facilitate the handling, serialization, and deserialization of block-related metadata, especially previous block information, in a format suitable for external representation or storage.
Structs and Implementations
BlockInfo
pub struct BlockInfo {
inner: tvm_block::BlkPrevInfo,
}
Purpose:
Encapsulates an instance oftvm_block::BlkPrevInfoand exposes it with controlled serialization/deserialization behavior.Attributes:
inner: The core data of typetvm_block::BlkPrevInfo. Theserde_asattribute uses a custom serializer/deserializer format defined byBlkPrevInfoFormat.
Derives:
Debug,Clone,Default,Serialize,Deserializeserde(transparent)ensures that serialization acts as ifBlockInfowas just the inner type, respecting the custom format.
Traits Implemented:
std::ops::Derefandstd::ops::DerefMut
These allow treating aBlockInfoinstance as a reference totvm_block::BlkPrevInfo, enabling ergonomic access to its methods and fields without explicit unwrapping.fn deref(&self) -> &Self::Target { &self.inner } fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner }From<tvm_block::BlkPrevInfo>
Enables conversion from atvm_block::BlkPrevInfointo aBlockInfoby wrapping the inner value.fn from(value: tvm_block::BlkPrevInfo) -> Self { Self { inner: value } }
Usage Example:
use tvm_block::BlkPrevInfo;
let blk_prev_info = BlkPrevInfo::default();
let block_info = BlockInfo::from(blk_prev_info);
// Access inner fields/methods transparently
let some_field = block_info.some_field();
BlockEndLT
pub struct BlockEndLT(pub u64);
Purpose:
Represents a block end logical time (LT) value as a newtype overu64for type safety and clarity.Derives:
Ord,PartialOrdfor ordering comparisonsEq,PartialEqfor equality checksSerialize,Deserializefor (de)serialization supportDebug,Clonefor debugging and cloning capabilities
Usage Example:
let block_end_lt = BlockEndLT(123456789);
Implementation Details
The use of
serde_ascombined with theBlkPrevInfoFormatserializer/deserializer indicates customized serialization logic, likely to handle complex or non-standard data structures withintvm_block::BlkPrevInfo. This ensures data compatibility and proper encoding/decoding when exchanging or storing block information.The
serde(transparent)attribute onBlockInfoimplies that the serialized output will match that of the innerBlkPrevInfotype, preserving backward compatibility or consistency with existing data formats.Deref coercions implemented allow
BlockInfoto be used interchangeably withtvm_block::BlkPrevInfoin most contexts, reducing boilerplate code and enhancing ergonomics.
Interaction with Other System Components
tvm_block::BlkPrevInfo
The core data structure wrapped byBlockInfo, presumably defined in an external crate or module namedtvm_block. This file depends on that type for block previous information representation.BlkPrevInfoFormat
A custom serialization format imported from the sibling moduleblk_prev_info_format. This format is used to serialize and deserializeBlkPrevInfowithinBlockInfo, allowing tailored data handling as per system requirements.Serialization and deserialization traits (
Serialize,Deserialize) enableBlockInfoandBlockEndLTto be easily converted to and from data formats such as JSON, BSON, or others, facilitating communication with storage layers, network protocols, or APIs.
Visual Diagram
classDiagram
class BlockInfo {
-inner: BlkPrevInfo
+deref() : &BlkPrevInfo
+deref_mut() : &mut BlkPrevInfo
+from(BlkPrevInfo) : BlockInfo
}
class BlkPrevInfoFormat
class tvm_block::BlkPrevInfo
class BlockEndLT {
+u64
}
BlockInfo o-- tvm_block::BlkPrevInfo : wraps
BlockInfo ..> BlkPrevInfoFormat : uses for serialization
References
serde::Serialize and serde::Deserialize traits for serialization support.
serde_with::serde_asfor custom serialization formatting.std::ops::DerefandDerefMuttraits for transparent access to wrapped types.BlkPrevInfoFormatcustom serializer/deserializer, details in blk_prev_info_format.tvm_block::BlkPrevInfodata structure for block previous information, details in tvm_block.