cardano_sdk/chain/byron/
block.rs

1use cbored::{CborRepr, Encode};
2use cryptoxide::hashing::blake2b_256;
3
4use super::super::{
5    common::*,
6    hash::{ByronHash, HeaderHash, TxHash},
7};
8
9use super::tx::*;
10
11#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
12#[cborrepr(structure = "array")]
13pub struct Header {
14    pub protocol_magic: u64,
15    pub previous_hash: HeaderHash,
16    pub body_proof: BodyProof,
17    pub consensus: Consensus,
18    pub extra_data: AnyCbor,
19}
20
21#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
22#[cborrepr(structure = "array")]
23pub struct Consensus {
24    pub slot_id: EpochSlotId,
25    pub leader_key: Bytes,
26    pub chain_difficulty: ChainDifficulty,
27    pub block_signature: BlockSignature,
28}
29
30#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
31#[cborrepr(enumtype = "tagvariant")]
32pub enum BlockSignature {
33    Signature(Bytes),
34    ProxyLight(AnyCbor),
35    ProxyHeavy(AnyCbor),
36}
37
38#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
39#[cborrepr(structure = "array")]
40pub struct EpochSlotId {
41    pub epoch: u64,
42    pub slot_id: u16,
43}
44
45#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
46#[cborrepr(structure = "array")]
47pub struct ChainDifficulty(pub u64);
48
49#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
50#[cborrepr(structure = "array")]
51pub struct BodyProof {
52    pub tx: TxProof,
53    pub mpc: SscProof,
54    pub delegation: ByronHash,
55    pub update: AnyCbor,
56}
57
58#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
59#[cborrepr(structure = "array")]
60pub struct TxProof {
61    /// Number of Transactions in this tree
62    pub number: u32,
63    /// Root of the merkle tree of transactions
64    pub root: Bytes,
65    /// Hash of Sequence of TxWitnesses encoded in CBOR
66    pub witnesses_hash: TxHash,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, CborRepr)]
70#[cborrepr(enumtype = "tagvariant")]
71pub enum SscProof {
72    Commitments(ByronHash, ByronHash),
73    Openings(ByronHash, ByronHash),
74    Shares(ByronHash, ByronHash),
75    Certificate(ByronHash),
76}
77
78#[derive(Debug, Clone, CborRepr, PartialEq, Eq)]
79#[cborrepr(structure = "array")]
80pub struct Body {
81    pub txs: TxAuxs,
82    pub ssc: AnyCbor,
83    pub delegation: AnyCbor,
84    pub update: AnyCbor,
85}
86
87impl Header {
88    pub fn hash(&self) -> HeaderHash {
89        let mut writer = cbored::Writer::new();
90        self.encode(&mut writer);
91        let data = writer.finalize();
92        HeaderHash(blake2b_256(&data))
93    }
94}