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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::{
    common::{Author, Payload, Round},
    quorum_cert::{QuorumCert, QuorumCertUnchecked},
    vote_data::VoteData,
};
use diem_crypto::hash::HashValue;
use diem_crypto_derive::{BCSCryptoHash, CryptoHasher};
use diem_types::{
    block_info::BlockInfo,
    ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
    transaction::SignedTransactionUnchecked,
};
use mirai_annotations::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub enum BlockType {
    Proposal {
        /// T of the block (e.g. one or more transaction(s)
        payload: Payload,
        /// Author of the block that can be validated by the author's public
        /// key and the signature
        author: Author,
    },
    /// NIL blocks don't have authors or signatures: they're generated upon
    /// timeouts to fill in the gaps in the rounds.
    NilBlock,
    /// A genesis block is the first committed block in any epoch that is
    /// identically constructed on all validators by any (potentially
    /// different) LedgerInfo that justifies the epoch change
    /// from the previous epoch.  The genesis block is used as the first
    /// root block of the BlockTree for all epochs.
    Genesis,
}

#[derive(Deserialize)]
pub enum BlockTypeUnchecked {
    Proposal {
        payload: Vec<SignedTransactionUnchecked>,
        author: Author,
    },
    NilBlock,
    Genesis,
}

impl From<BlockTypeUnchecked> for BlockType {
    fn from(t: BlockTypeUnchecked) -> Self {
        match t {
            BlockTypeUnchecked::Proposal { payload, author } => {
                Self::Proposal {
                    payload: payload.into_iter().map(Into::into).collect(),
                    author,
                }
            }
            BlockTypeUnchecked::NilBlock => Self::NilBlock,
            BlockTypeUnchecked::Genesis => Self::Genesis,
        }
    }
}

#[derive(
    Deserialize,
    Serialize,
    Clone,
    Debug,
    PartialEq,
    Eq,
    CryptoHasher,
    BCSCryptoHash,
)]
/// Block has the core data of a consensus block that should be persistent when
/// necessary. Each block must know the id of its parent and keep the
/// QuorurmCertificate to that parent.
pub struct BlockData {
    /// Epoch number corresponds to the set of validators that are active for
    /// this block.
    epoch: u64,
    /// The round of a block is an internal monotonically increasing counter
    /// used by Consensus protocol.
    round: Round,
    /// The approximate physical time a block is proposed by a proposer.  This
    /// timestamp is used for
    /// * Time-dependent logic in smart contracts (the current time of
    ///   execution)
    /// * Clients determining if they are relatively up-to-date with respect to
    ///   the block chain.
    ///
    /// It makes the following guarantees:
    ///   1. Time Monotonicity: Time is monotonically increasing in the block
    /// chain.      (i.e. If H1 < H2, H1.Time < H2.Time).
    ///   2. If a block of transactions B is agreed on with timestamp T, then
    /// at least      f+1 honest validators think that T is in the past. An
    /// honest validator will      only vote on a block when its own clock
    /// >= timestamp T.   3. If a block of transactions B has a QC with
    /// timestamp T, an honest validator      will not serve such a block
    /// to other validators until its own clock >= timestamp T.
    ///   4. Current: an honest validator is not issuing blocks with a
    /// timestamp in the       future. Currently we consider a block is
    /// malicious if it was issued more       that 5 minutes in the future.
    timestamp_usecs: u64,
    /// Contains the quorum certified ancestor and whether the quorum certified
    /// ancestor was voted on successfully
    quorum_cert: QuorumCert,
    /// If a block is a real proposal, contains its author and signature.
    block_type: BlockType,
}

#[derive(Deserialize)]
pub struct BlockDataUnchecked {
    pub epoch: u64,
    pub round: Round,
    pub timestamp_usecs: u64,
    pub quorum_cert: QuorumCertUnchecked,
    pub block_type: BlockTypeUnchecked,
}

impl From<BlockDataUnchecked> for BlockData {
    fn from(b: BlockDataUnchecked) -> Self {
        Self {
            epoch: b.epoch,
            round: b.round,
            timestamp_usecs: b.timestamp_usecs,
            quorum_cert: b.quorum_cert.into(),
            block_type: b.block_type.into(),
        }
    }
}

impl BlockData {
    pub fn author(&self) -> Option<Author> {
        if let BlockType::Proposal { author, .. } = self.block_type {
            Some(author)
        } else {
            None
        }
    }

    pub fn block_type(&self) -> &BlockType { &self.block_type }

    pub fn epoch(&self) -> u64 { self.epoch }

    pub fn parent_id(&self) -> HashValue {
        self.quorum_cert.certified_block().id()
    }

    pub fn payload(&self) -> Option<&Payload> {
        if let BlockType::Proposal { payload, .. } = &self.block_type {
            Some(payload)
        } else {
            None
        }
    }

    pub fn round(&self) -> Round { self.round }

    pub fn timestamp_usecs(&self) -> u64 { self.timestamp_usecs }

    pub fn quorum_cert(&self) -> &QuorumCert { &self.quorum_cert }

    pub fn is_genesis_block(&self) -> bool {
        matches!(self.block_type, BlockType::Genesis)
    }

    pub fn is_nil_block(&self) -> bool {
        matches!(self.block_type, BlockType::NilBlock)
    }

    pub fn vrf_round_seed(&self, seed: &[u8]) -> Vec<u8> {
        let mut round_seed = seed.to_vec();
        // Make 3 continuous rounds have the same leader.
        // Round 0 has no leader, so we use "round+1" here.
        let leader_round = (self.round + 1) / 3;
        round_seed.extend_from_slice(&leader_round.to_be_bytes());
        round_seed
    }

    pub fn new_genesis_from_ledger_info(ledger_info: &LedgerInfo) -> Self {
        assert!(ledger_info.ends_epoch());
        let ancestor = BlockInfo::new(
            ledger_info.epoch(),
            0,                 /* round */
            HashValue::zero(), /* parent block id */
            ledger_info.transaction_accumulator_hash(),
            ledger_info.version(),
            ledger_info.timestamp_usecs(),
            None,
            ledger_info.pivot_decision().cloned(),
        );

        // Genesis carries a placeholder quorum certificate to its parent id
        // with LedgerInfo carrying information about version from the
        // last LedgerInfo of previous epoch.
        let genesis_quorum_cert = QuorumCert::new(
            VoteData::new(ancestor.clone(), ancestor.clone()),
            LedgerInfoWithSignatures::new(
                LedgerInfo::new(ancestor, HashValue::zero()),
                BTreeMap::new(),
            ),
        );

        BlockData::new_genesis(
            ledger_info.timestamp_usecs(),
            genesis_quorum_cert,
        )
    }

    #[cfg(any(test, feature = "fuzzing"))]
    // This method should only used by tests and fuzzers to produce arbitrary
    // BlockData types.
    pub fn new_for_testing(
        epoch: u64, round: Round, timestamp_usecs: u64,
        quorum_cert: QuorumCert, block_type: BlockType,
    ) -> Self {
        Self {
            epoch,
            round,
            timestamp_usecs,
            quorum_cert,
            block_type,
        }
    }

    pub fn new_genesis(timestamp_usecs: u64, quorum_cert: QuorumCert) -> Self {
        assume!(quorum_cert.certified_block().epoch() < u64::max_value()); // unlikely to be false in this universe
        Self {
            epoch: quorum_cert.certified_block().epoch() + 1,
            round: 0,
            timestamp_usecs,
            quorum_cert,
            block_type: BlockType::Genesis,
        }
    }

    pub fn new_nil(round: Round, quorum_cert: QuorumCert) -> Self {
        // We want all the NIL blocks to agree on the timestamps even though
        // they're generated independently by different validators,
        // hence we're using the timestamp of a parent + 1.
        assume!(
            quorum_cert.certified_block().timestamp_usecs() < u64::max_value()
        ); // unlikely to be false in this universe
        let timestamp_usecs = quorum_cert.certified_block().timestamp_usecs();

        Self {
            epoch: quorum_cert.certified_block().epoch(),
            round,
            timestamp_usecs,
            quorum_cert,
            block_type: BlockType::NilBlock,
        }
    }

    pub fn new_proposal(
        payload: Payload, author: Author, round: Round, timestamp_usecs: u64,
        quorum_cert: QuorumCert,
    ) -> Self {
        Self {
            epoch: quorum_cert.certified_block().epoch(),
            round,
            timestamp_usecs,
            quorum_cert,
            block_type: BlockType::Proposal { payload, author },
        }
    }
}