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
use crate::{
common::{Author, Payload, Round},
quorum_cert::QuorumCert,
vote_data::VoteData,
};
use diem_crypto::hash::HashValue;
use diem_crypto_derive::{BCSCryptoHash, CryptoHasher};
use diem_types::{
block_info::BlockInfo,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
};
use mirai_annotations::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub enum BlockType {
Proposal {
payload: Payload,
author: Author,
},
NilBlock,
Genesis,
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, CryptoHasher, BCSCryptoHash)]
pub struct BlockData {
epoch: u64,
round: Round,
timestamp_usecs: u64,
quorum_cert: QuorumCert,
block_type: BlockType,
}
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 new_genesis_from_ledger_info(ledger_info: &LedgerInfo) -> Self {
assert!(ledger_info.ends_epoch());
let ancestor = BlockInfo::new(
ledger_info.epoch(),
0, HashValue::zero(), ledger_info.transaction_accumulator_hash(),
ledger_info.version(),
ledger_info.timestamp_usecs(),
None,
);
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"))]
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()); 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 {
assume!(quorum_cert.certified_block().timestamp_usecs() < u64::max_value()); 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 },
}
}
pub fn is_reconfiguration_suffix(&self) -> bool {
self.quorum_cert.certified_block().has_reconfiguration()
}
}
#[test]
fn test_reconfiguration_suffix() {
use diem_types::{
account_address::AccountAddress, epoch_state::EpochState, on_chain_config::ValidatorSet,
};
let reconfig_block_info = BlockInfo::new(
1,
1,
HashValue::random(),
HashValue::random(),
100,
1,
Some(EpochState::empty()),
);
let quorum_cert = QuorumCert::new(
VoteData::new(reconfig_block_info, BlockInfo::random(0)),
LedgerInfoWithSignatures::new(
LedgerInfo::new(
BlockInfo::genesis(HashValue::random(), ValidatorSet::empty()),
HashValue::zero(),
),
BTreeMap::new(),
),
);
let reconfig_suffix_block =
BlockData::new_proposal(vec![], AccountAddress::random(), 2, 2, quorum_cert);
assert!(reconfig_suffix_block.is_reconfiguration_suffix());
}