use serde::{Deserialize, Serialize};
use super::primitives::*;
use super::transaction::Transaction;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Block {
EBB(BlockEBB),
BFT(BlockBFT),
Praos(BlockPraos),
}
impl Block {
pub fn slot(&self) -> Slot {
match self {
Block::EBB(b) => b.slot,
Block::BFT(b) => b.slot,
Block::Praos(b) => b.slot,
}
}
pub fn id(&self) -> &str {
match self {
Block::EBB(b) => &b.id,
Block::BFT(b) => &b.id,
Block::Praos(b) => &b.id,
}
}
pub fn height(&self) -> BlockHeight {
match self {
Block::EBB(b) => b.height,
Block::BFT(b) => b.height,
Block::Praos(b) => b.height,
}
}
pub fn ancestor(&self) -> &str {
match self {
Block::EBB(b) => &b.ancestor,
Block::BFT(b) => &b.ancestor,
Block::Praos(b) => &b.ancestor,
}
}
pub fn is_ebb(&self) -> bool {
matches!(self, Block::EBB(_))
}
pub fn is_bft(&self) -> bool {
matches!(self, Block::BFT(_))
}
pub fn is_praos(&self) -> bool {
matches!(self, Block::Praos(_))
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockEBB {
#[serde(rename = "type")]
pub block_type: String,
pub era: String,
pub id: DigestBlake2b256,
pub ancestor: DigestBlake2b256,
pub slot: Slot,
pub height: BlockHeight,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockBFT {
#[serde(rename = "type")]
pub block_type: String,
pub era: String,
pub id: DigestBlake2b256,
pub ancestor: DigestBlake2b256,
pub slot: Slot,
pub height: BlockHeight,
pub size: BlockSize,
pub protocol: ProtocolVersionByron,
pub issuer: BlockIssuerByron,
#[serde(default)]
pub transactions: Vec<Transaction>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockPraos {
#[serde(rename = "type")]
pub block_type: String,
pub era: String,
pub id: DigestBlake2b256,
pub ancestor: DigestBlake2b256,
pub slot: Slot,
pub height: BlockHeight,
pub size: BlockSize,
pub protocol: ProtocolVersionPraos,
pub issuer: BlockIssuerPraos,
#[serde(default)]
pub transactions: Vec<Transaction>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockSize {
pub bytes: NumberOfBytes,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolVersionByron {
#[serde(default)]
pub software: Option<SoftwareVersion>,
#[serde(default)]
pub update: Option<serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SoftwareVersion {
pub app_name: String,
pub number: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolVersionPraos {
pub major: u32,
pub minor: u32,
#[serde(default)]
pub patch: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockIssuerByron {
pub verification_key: VerificationKey,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockIssuerPraos {
pub verification_key: VerificationKeyHash,
pub vrf_verification_key: VrfVerificationKey,
#[serde(default)]
pub leader_value: Option<CertifiedVrf>,
#[serde(default)]
pub operational_certificate: Option<OperationalCertificate>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CertifiedVrf {
pub output: String,
pub proof: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OperationalCertificate {
pub kes_verification_key: KesVerificationKey,
pub count: u64,
#[serde(default)]
pub kes_period: Option<u64>,
#[serde(default)]
pub issue_number: Option<u64>,
}