espresso_types/v0/header.rs
1use committable::Commitment;
2use serde::{Deserialize, Serialize};
3use vbs::version::Version;
4
5use crate::{
6 v0_1::{self, ChainConfig},
7 v0_2, v0_3, v0_4, v0_5, v0_6, v0_7,
8};
9
10/// Each variant represents a specific minor version header.
11#[derive(Clone, Debug, Hash, PartialEq, Eq)]
12pub enum Header {
13 V1(v0_1::Header),
14 V2(v0_2::Header),
15 V3(v0_3::Header),
16 V4(v0_4::Header),
17 V5(v0_5::Header),
18 V6(v0_6::Header),
19 V7(v0_7::Header),
20}
21
22/// Enum to represent the first field of different versions of a header
23///
24/// In v1 headers, the first field is a ChainConfig, which contains either the chain config or its commitment.
25/// For versions > 0.1, the first field contains the version.
26///
27/// This enum has the same variant names and types in the same positions (0 and 1) as the Either enum,
28/// ensuring identical serialization and deserialization for the Left and Right variants.
29/// However, it will deserialize successfully in one additional case due to the Version variant.
30///
31/// Variants:
32/// - Left: Represents the ChainConfig variant in v1 headers.
33/// - Right: Represents the chain config commitment variant in v1 headers.
34/// - Version: Represents the versioned header for versions > 0.1.
35#[derive(Clone, Debug, Deserialize, Serialize)]
36pub(crate) enum EitherOrVersion {
37 Left(ChainConfig),
38 Right(Commitment<ChainConfig>),
39 Version(Version),
40}
41
42/// Headers with versions greater than 0.1 are serialized as `VersionedHeader` with the version field as `EitherOrVersion`.
43/// This ensures that the first field is deserialized as the `EitherOrVersion::Version` variant.
44/// This approach is necessary because `serde_flatten()` cannot be used with bincode,
45/// as bincode does not support the `deserialize_any()`.
46#[derive(Clone, Debug, Deserialize, Serialize)]
47pub struct VersionedHeader<Fields> {
48 pub(crate) version: EitherOrVersion,
49 pub(crate) fields: Fields,
50}