1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

mod confirmed;
mod generic;
mod lite;
mod timeout;
mod validated;

use std::collections::BTreeSet;

pub use generic::GenericCertificate;
use linera_base::{
    crypto::{ValidatorPublicKey, ValidatorSignature},
    data_types::{BlockHeight, Round},
    identifiers::{BlobId, ChainId},
};
use linera_execution::committee::Epoch;
pub use lite::LiteCertificate;
use serde::{Deserialize, Serialize};

use crate::types::{ConfirmedBlock, Timeout, ValidatedBlock};

/// Certificate for a [`ValidatedBlock`] instance.
/// A validated block certificate means the block is valid (but not necessarily finalized yet).
/// Since only one block per round is validated,
/// there can be at most one such certificate in every round.
pub type ValidatedBlockCertificate = GenericCertificate<ValidatedBlock>;

/// Certificate for a [`ConfirmedBlock`] instance.
/// A confirmed block certificate means that the block is finalized:
/// It is the agreed block at that height on that chain.
pub type ConfirmedBlockCertificate = GenericCertificate<ConfirmedBlock>;

/// Certificate for a [`Timeout`] instance.
/// A timeout certificate means that the next consensus round has begun.
pub type TimeoutCertificate = GenericCertificate<Timeout>;

/// Enum wrapping all types of certificates that can be created.
/// A certified statement from the committee.
/// Every certificate is a statement signed by the quorum of the committee.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(with_testing, derive(Eq, PartialEq))]
pub enum Certificate {
    /// Certificate for [`ValidatedBlock`].
    Validated(ValidatedBlockCertificate),
    /// Certificate for [`ConfirmedBlock`].
    Confirmed(ConfirmedBlockCertificate),
    /// Certificate for [`Timeout`].
    Timeout(TimeoutCertificate),
}

impl Certificate {
    pub fn round(&self) -> Round {
        match self {
            Certificate::Validated(cert) => cert.round,
            Certificate::Confirmed(cert) => cert.round,
            Certificate::Timeout(cert) => cert.round,
        }
    }

    pub fn height(&self) -> BlockHeight {
        match self {
            Certificate::Validated(cert) => cert.value().inner().block().header.height,
            Certificate::Confirmed(cert) => cert.value().inner().block().header.height,
            Certificate::Timeout(cert) => cert.inner().height,
        }
    }

    pub fn chain_id(&self) -> ChainId {
        match self {
            Certificate::Validated(cert) => cert.value().inner().block().header.chain_id,
            Certificate::Confirmed(cert) => cert.value().inner().block().header.chain_id,
            Certificate::Timeout(cert) => cert.inner().chain_id,
        }
    }

    pub fn signatures(&self) -> &Vec<(ValidatorPublicKey, ValidatorSignature)> {
        match self {
            Certificate::Validated(cert) => cert.signatures(),
            Certificate::Confirmed(cert) => cert.signatures(),
            Certificate::Timeout(cert) => cert.signatures(),
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
#[repr(u8)]
pub enum CertificateKind {
    Timeout = 0,
    Validated = 1,
    Confirmed = 2,
}

pub trait CertificateValue: Clone {
    const KIND: CertificateKind;

    fn chain_id(&self) -> ChainId;

    fn epoch(&self) -> Epoch;

    fn height(&self) -> BlockHeight;

    fn required_blob_ids(&self) -> BTreeSet<BlobId>;
}

impl CertificateValue for Timeout {
    const KIND: CertificateKind = CertificateKind::Timeout;

    fn chain_id(&self) -> ChainId {
        self.chain_id
    }

    fn epoch(&self) -> Epoch {
        self.epoch
    }

    fn height(&self) -> BlockHeight {
        self.height
    }

    fn required_blob_ids(&self) -> BTreeSet<BlobId> {
        BTreeSet::new()
    }
}

impl CertificateValue for ValidatedBlock {
    const KIND: CertificateKind = CertificateKind::Validated;

    fn chain_id(&self) -> ChainId {
        self.block().header.chain_id
    }

    fn epoch(&self) -> Epoch {
        self.block().header.epoch
    }

    fn height(&self) -> BlockHeight {
        self.block().header.height
    }

    fn required_blob_ids(&self) -> BTreeSet<BlobId> {
        self.block().required_blob_ids()
    }
}

impl CertificateValue for ConfirmedBlock {
    const KIND: CertificateKind = CertificateKind::Confirmed;

    fn chain_id(&self) -> ChainId {
        self.block().header.chain_id
    }

    fn epoch(&self) -> Epoch {
        self.block().header.epoch
    }

    fn height(&self) -> BlockHeight {
        self.block().header.height
    }

    fn required_blob_ids(&self) -> BTreeSet<BlobId> {
        self.block().required_blob_ids()
    }
}