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::Round, quorum_cert::QuorumCert, timeout_2chain::TwoChainTimeoutCertificate,
timeout_certificate::TimeoutCertificate,
};
use anyhow::{ensure, Context};
use diem_types::{
block_info::BlockInfo, ledger_info::LedgerInfoWithSignatures,
validator_verifier::ValidatorVerifier,
};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter};
#[derive(Deserialize, Serialize, Clone, Eq, PartialEq)]
pub struct SyncInfo {
highest_quorum_cert: QuorumCert,
#[serde(alias = "highest_commit_cert")]
highest_ordered_cert: Option<QuorumCert>,
#[serde(default, skip_serializing_if = "Option::is_none")]
highest_ledger_info: Option<LedgerInfoWithSignatures>,
highest_timeout_cert: Option<TimeoutCertificate>,
#[serde(default, skip_serializing_if = "Option::is_none")]
highest_2chain_timeout_cert: Option<TwoChainTimeoutCertificate>,
}
impl Debug for SyncInfo {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl Display for SyncInfo {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let htc_repr = match self.highest_timeout_certificate() {
Some(tc) => format!("{}", tc.round()),
None => "None".to_string(),
};
write!(
f,
"SyncInfo[HQC: {}, HOC: {}, HTC: {}, HLI: {}]",
self.highest_certified_round(),
self.highest_ordered_round(),
htc_repr,
self.highest_ledger_info(),
)
}
}
impl SyncInfo {
pub fn new_decoupled(
highest_quorum_cert: QuorumCert,
highest_ordered_cert: QuorumCert,
highest_ledger_info: Option<LedgerInfoWithSignatures>,
highest_timeout_cert: Option<TimeoutCertificate>,
highest_2chain_timeout_cert: Option<TwoChainTimeoutCertificate>,
) -> Self {
let highest_timeout_cert = highest_timeout_cert
.filter(|tc| tc.round() > highest_quorum_cert.certified_block().round());
let highest_2chain_timeout_cert = highest_2chain_timeout_cert
.filter(|tc| tc.round() > highest_quorum_cert.certified_block().round());
let highest_ordered_cert =
Some(highest_ordered_cert).filter(|hoc| hoc != &highest_quorum_cert);
assert!(
highest_timeout_cert.is_none() || highest_2chain_timeout_cert.is_none(),
"Only one timeout cert should be carried"
);
Self {
highest_quorum_cert,
highest_ordered_cert,
highest_ledger_info,
highest_timeout_cert,
highest_2chain_timeout_cert,
}
}
pub fn new(
highest_quorum_cert: QuorumCert,
highest_ordered_cert: QuorumCert,
highest_timeout_cert: Option<TimeoutCertificate>,
highest_2chain_timeout_cert: Option<TwoChainTimeoutCertificate>,
) -> Self {
Self::new_decoupled(
highest_quorum_cert,
highest_ordered_cert,
None,
highest_timeout_cert,
highest_2chain_timeout_cert,
)
}
pub fn highest_quorum_cert(&self) -> &QuorumCert {
&self.highest_quorum_cert
}
pub fn highest_ordered_cert(&self) -> &QuorumCert {
self.highest_ordered_cert
.as_ref()
.unwrap_or(&self.highest_quorum_cert)
}
pub fn highest_ledger_info(&self) -> &LedgerInfoWithSignatures {
self.highest_ledger_info
.as_ref()
.unwrap_or_else(|| self.highest_ordered_cert().ledger_info())
}
pub fn highest_timeout_certificate(&self) -> Option<&TimeoutCertificate> {
self.highest_timeout_cert.as_ref()
}
pub fn highest_2chain_timeout_cert(&self) -> Option<&TwoChainTimeoutCertificate> {
self.highest_2chain_timeout_cert.as_ref()
}
pub fn highest_certified_round(&self) -> Round {
self.highest_quorum_cert.certified_block().round()
}
pub fn highest_timeout_round(&self) -> Round {
std::cmp::max(
self.highest_timeout_certificate()
.map_or(0, |tc| tc.round()),
self.highest_2chain_timeout_cert()
.map_or(0, |tc| tc.round()),
)
}
pub fn highest_ordered_round(&self) -> Round {
self.highest_ordered_cert().commit_info().round()
}
pub fn highest_ledger_info_round(&self) -> Round {
self.highest_ledger_info().commit_info().round()
}
pub fn highest_round(&self) -> Round {
std::cmp::max(self.highest_certified_round(), self.highest_timeout_round())
}
pub fn verify(&self, validator: &ValidatorVerifier) -> anyhow::Result<()> {
let epoch = self.highest_quorum_cert.certified_block().epoch();
ensure!(
epoch == self.highest_ordered_cert().certified_block().epoch(),
"Multi epoch in SyncInfo - HOC and HQC"
);
if let Some(tc) = &self.highest_timeout_cert {
ensure!(epoch == tc.epoch(), "Multi epoch in SyncInfo - TC and HQC");
}
ensure!(
self.highest_quorum_cert.certified_block().round()
>= self.highest_ordered_cert().certified_block().round(),
"HQC has lower round than HOC"
);
ensure!(
self.highest_ordered_cert().certified_block().round()
>= self.highest_ledger_info_round(),
"HOC has lower round than HLI"
);
ensure!(
*self.highest_ordered_cert().commit_info() != BlockInfo::empty(),
"HOC has no committed block"
);
ensure!(
*self.highest_ledger_info().commit_info() != BlockInfo::empty(),
"HLI has empty commit info"
);
ensure!(
self.highest_timeout_cert.is_none() || self.highest_2chain_timeout_cert.is_none(),
"Only One timeout cert should be carried"
);
self.highest_quorum_cert
.verify(validator)
.and_then(|_| {
self.highest_ordered_cert
.as_ref()
.map_or(Ok(()), |cert| cert.verify(validator))
})
.and_then(|_| {
if let Some(tc) = &self.highest_timeout_cert {
tc.verify(validator)?;
}
Ok(())
})
.and_then(|_| {
if let Some(hli) = self.highest_ledger_info.as_ref() {
if hli.commit_info().round() > 0 {
hli.verify_signatures(validator)?;
}
}
Ok(())
})
.and_then(|_| {
if let Some(tc) = &self.highest_2chain_timeout_cert {
tc.verify(validator)?;
}
Ok(())
})
.context("Fail to verify SyncInfo")?;
Ok(())
}
pub fn epoch(&self) -> u64 {
self.highest_quorum_cert.certified_block().epoch()
}
pub fn has_newer_certificates(&self, other: &SyncInfo) -> bool {
self.highest_certified_round() > other.highest_certified_round()
|| self.highest_timeout_round() > other.highest_timeout_round()
|| self.highest_ordered_round() > other.highest_ordered_round()
|| self.highest_ledger_info_round() > other.highest_ledger_info_round()
}
}