Skip to main content

ogmios_client/schema/
block.rs

1//! Block types for Cardano.
2
3use serde::{Deserialize, Serialize};
4use super::primitives::*;
5use super::transaction::Transaction;
6
7/// A Cardano block - can be EBB, BFT (Byron), or Praos (Shelley+).
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(untagged)]
10pub enum Block {
11    /// Epoch Boundary Block (Byron era).
12    EBB(BlockEBB),
13    /// BFT block (Byron era).
14    BFT(BlockBFT),
15    /// Praos block (Shelley era and later).
16    Praos(BlockPraos),
17}
18
19impl Block {
20    /// Get the block's slot number.
21    pub fn slot(&self) -> Slot {
22        match self {
23            Block::EBB(b) => b.slot,
24            Block::BFT(b) => b.slot,
25            Block::Praos(b) => b.slot,
26        }
27    }
28
29    /// Get the block's ID (hash).
30    pub fn id(&self) -> &str {
31        match self {
32            Block::EBB(b) => &b.id,
33            Block::BFT(b) => &b.id,
34            Block::Praos(b) => &b.id,
35        }
36    }
37
38    /// Get the block's height.
39    pub fn height(&self) -> BlockHeight {
40        match self {
41            Block::EBB(b) => b.height,
42            Block::BFT(b) => b.height,
43            Block::Praos(b) => b.height,
44        }
45    }
46
47    /// Get the ancestor block ID.
48    pub fn ancestor(&self) -> &str {
49        match self {
50            Block::EBB(b) => &b.ancestor,
51            Block::BFT(b) => &b.ancestor,
52            Block::Praos(b) => &b.ancestor,
53        }
54    }
55
56    /// Check if this is an EBB block.
57    pub fn is_ebb(&self) -> bool {
58        matches!(self, Block::EBB(_))
59    }
60
61    /// Check if this is a BFT block.
62    pub fn is_bft(&self) -> bool {
63        matches!(self, Block::BFT(_))
64    }
65
66    /// Check if this is a Praos block.
67    pub fn is_praos(&self) -> bool {
68        matches!(self, Block::Praos(_))
69    }
70}
71
72/// Epoch Boundary Block (EBB) - Byron era.
73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
74#[serde(rename_all = "camelCase")]
75pub struct BlockEBB {
76    /// Block type identifier.
77    #[serde(rename = "type")]
78    pub block_type: String,
79    /// Era (always "byron" for EBB).
80    pub era: String,
81    /// Block ID (hash).
82    pub id: DigestBlake2b256,
83    /// Ancestor block ID.
84    pub ancestor: DigestBlake2b256,
85    /// Slot number.
86    pub slot: Slot,
87    /// Block height.
88    pub height: BlockHeight,
89}
90
91/// BFT block - Byron era.
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct BlockBFT {
95    /// Block type identifier.
96    #[serde(rename = "type")]
97    pub block_type: String,
98    /// Era (always "byron" for BFT).
99    pub era: String,
100    /// Block ID (hash).
101    pub id: DigestBlake2b256,
102    /// Ancestor block ID.
103    pub ancestor: DigestBlake2b256,
104    /// Slot number.
105    pub slot: Slot,
106    /// Block height.
107    pub height: BlockHeight,
108    /// Block size in bytes.
109    pub size: BlockSize,
110    /// Protocol version.
111    pub protocol: ProtocolVersionByron,
112    /// Block issuer information.
113    pub issuer: BlockIssuerByron,
114    /// Transactions in the block.
115    #[serde(default)]
116    pub transactions: Vec<Transaction>,
117}
118
119/// Praos block - Shelley era and later.
120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
121#[serde(rename_all = "camelCase")]
122pub struct BlockPraos {
123    /// Block type identifier.
124    #[serde(rename = "type")]
125    pub block_type: String,
126    /// Era (shelley, allegra, mary, alonzo, babbage, conway).
127    pub era: String,
128    /// Block ID (hash).
129    pub id: DigestBlake2b256,
130    /// Ancestor block ID.
131    pub ancestor: DigestBlake2b256,
132    /// Slot number.
133    pub slot: Slot,
134    /// Block height.
135    pub height: BlockHeight,
136    /// Block size in bytes.
137    pub size: BlockSize,
138    /// Protocol version.
139    pub protocol: ProtocolVersionPraos,
140    /// Block issuer information.
141    pub issuer: BlockIssuerPraos,
142    /// Transactions in the block.
143    #[serde(default)]
144    pub transactions: Vec<Transaction>,
145}
146
147/// Block size information.
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149#[serde(rename_all = "camelCase")]
150pub struct BlockSize {
151    /// Size in bytes.
152    pub bytes: NumberOfBytes,
153}
154
155/// Protocol version for Byron blocks.
156#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
157#[serde(rename_all = "camelCase")]
158pub struct ProtocolVersionByron {
159    /// Software version.
160    #[serde(default)]
161    pub software: Option<SoftwareVersion>,
162    /// Update proposal (if any).
163    #[serde(default)]
164    pub update: Option<serde_json::Value>,
165}
166
167/// Software version information.
168#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170pub struct SoftwareVersion {
171    /// Application name.
172    pub app_name: String,
173    /// Version number.
174    pub number: u32,
175}
176
177/// Protocol version for Praos blocks.
178#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(rename_all = "camelCase")]
180pub struct ProtocolVersionPraos {
181    /// Major version.
182    pub major: u32,
183    /// Minor version.
184    pub minor: u32,
185    /// Patch version (optional).
186    #[serde(default)]
187    pub patch: Option<u32>,
188}
189
190/// Block issuer for Byron blocks.
191#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
192#[serde(rename_all = "camelCase")]
193pub struct BlockIssuerByron {
194    /// Verification key hash.
195    pub verification_key: VerificationKey,
196}
197
198/// Block issuer for Praos blocks.
199#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
200#[serde(rename_all = "camelCase")]
201pub struct BlockIssuerPraos {
202    /// Verification key hash (pool ID).
203    pub verification_key: VerificationKeyHash,
204    /// VRF verification key.
205    pub vrf_verification_key: VrfVerificationKey,
206    /// Leader value (VRF output).
207    #[serde(default)]
208    pub leader_value: Option<CertifiedVrf>,
209    /// Operational certificate.
210    #[serde(default)]
211    pub operational_certificate: Option<OperationalCertificate>,
212}
213
214/// VRF certified output.
215#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
216#[serde(rename_all = "camelCase")]
217pub struct CertifiedVrf {
218    /// VRF output.
219    pub output: String,
220    /// VRF proof.
221    pub proof: String,
222}
223
224/// Operational certificate for block production.
225#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
226#[serde(rename_all = "camelCase")]
227pub struct OperationalCertificate {
228    /// KES verification key.
229    pub kes_verification_key: KesVerificationKey,
230    /// Counter.
231    pub count: u64,
232    /// KES period.
233    #[serde(default)]
234    pub kes_period: Option<u64>,
235    /// Issue number.
236    #[serde(default)]
237    pub issue_number: Option<u64>,
238}