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
273
274
275
#![forbid(unsafe_code)]
use crate::ledger_info::{LedgerInfo, LedgerInfoWithSignatures};
use anyhow::{ensure, format_err, Result};
#[cfg(any(test, feature = "fuzzing"))]
use proptest::{collection::vec, prelude::*};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub trait Verifier: Debug + Send + Sync {
fn verify(&self, ledger_info: &LedgerInfoWithSignatures) -> Result<()>;
fn epoch_change_verification_required(&self, epoch: u64) -> bool;
fn is_ledger_info_stale(&self, ledger_info: &LedgerInfo) -> bool;
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EpochChangeProof {
pub ledger_info_with_sigs: Vec<LedgerInfoWithSignatures>,
pub more: bool,
}
impl EpochChangeProof {
pub fn new(ledger_info_with_sigs: Vec<LedgerInfoWithSignatures>, more: bool) -> Self {
Self {
ledger_info_with_sigs,
more,
}
}
pub fn epoch(&self) -> Result<u64> {
self.ledger_info_with_sigs
.first()
.map(|li| li.ledger_info().epoch())
.ok_or_else(|| format_err!("Empty EpochChangeProof"))
}
pub fn verify(&self, verifier: &dyn Verifier) -> Result<&LedgerInfoWithSignatures> {
ensure!(
!self.ledger_info_with_sigs.is_empty(),
"The EpochChangeProof is empty"
);
ensure!(
!verifier
.is_ledger_info_stale(self.ledger_info_with_sigs.last().unwrap().ledger_info()),
"The EpochChangeProof is stale as our verifier is already ahead \
of the entire EpochChangeProof"
);
let mut verifier_ref = verifier;
for ledger_info_with_sigs in self
.ledger_info_with_sigs
.iter()
.skip_while(|&ledger_info_with_sigs| {
verifier.is_ledger_info_stale(ledger_info_with_sigs.ledger_info())
})
{
verifier_ref.verify(ledger_info_with_sigs)?;
verifier_ref = ledger_info_with_sigs
.ledger_info()
.next_epoch_state()
.ok_or_else(|| format_err!("LedgerInfo doesn't carry a ValidatorSet"))?;
}
Ok(self.ledger_info_with_sigs.last().unwrap())
}
}
#[cfg(any(test, feature = "fuzzing"))]
impl Arbitrary for EpochChangeProof {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<LedgerInfoWithSignatures>(), 0..10), any::<bool>())
.prop_map(|(ledger_infos_with_sigs, more)| Self::new(ledger_infos_with_sigs, more))
.boxed()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{block_info::BlockInfo, epoch_state::EpochState, waypoint::Waypoint};
#[test]
fn verify_epoch_change_proof() {
use crate::{ledger_info::LedgerInfo, validator_verifier::random_validator_verifier};
use diem_crypto::hash::HashValue;
use std::collections::BTreeMap;
let all_epoch: Vec<u64> = (1..=10).collect();
let mut valid_ledger_info = vec![];
let mut validator_verifier = vec![];
let (mut current_signers, mut current_verifier) = random_validator_verifier(1, None, true);
let mut current_version = 123;
for epoch in &all_epoch {
validator_verifier.push(current_verifier.clone());
let (next_signers, next_verifier) =
random_validator_verifier((*epoch + 1) as usize, None, true);
let epoch_state = EpochState {
epoch: *epoch + 1,
verifier: next_verifier.clone(),
};
let ledger_info = LedgerInfo::new(
BlockInfo::new(
*epoch,
0,
HashValue::zero(),
HashValue::zero(),
current_version,
0,
Some(epoch_state),
),
HashValue::zero(),
);
let signatures = current_signers
.iter()
.map(|s| (s.author(), s.sign(&ledger_info)))
.collect();
valid_ledger_info.push(LedgerInfoWithSignatures::new(ledger_info, signatures));
current_signers = next_signers;
current_verifier = next_verifier;
current_version += 1;
}
let proof_1 = EpochChangeProof::new(valid_ledger_info.clone(), false);
assert!(proof_1
.verify(&EpochState {
epoch: all_epoch[0],
verifier: validator_verifier[0].clone(),
})
.is_ok());
let proof_2 =
EpochChangeProof::new(valid_ledger_info[2..5].to_vec(), false);
assert!(proof_2
.verify(&EpochState {
epoch: all_epoch[2],
verifier: validator_verifier[2].clone()
})
.is_ok());
assert!(proof_1
.verify(&EpochState {
epoch: all_epoch[4],
verifier: validator_verifier[4].clone()
})
.is_ok());
let proof_3 = EpochChangeProof::new(vec![], false);
assert!(proof_3
.verify(&EpochState {
epoch: all_epoch[0],
verifier: validator_verifier[0].clone()
})
.is_err());
let mut list = valid_ledger_info[3..5].to_vec();
list.extend_from_slice(&valid_ledger_info[8..9]);
let proof_4 = EpochChangeProof::new(list, false);
assert!(proof_4
.verify(&EpochState {
epoch: all_epoch[3],
verifier: validator_verifier[3].clone()
})
.is_err());
let mut list = valid_ledger_info.clone();
list.reverse();
let proof_5 = EpochChangeProof::new(list, false);
assert!(proof_5
.verify(&EpochState {
epoch: all_epoch[9],
verifier: validator_verifier[9].clone()
})
.is_err());
let proof_6 = EpochChangeProof::new(
vec![LedgerInfoWithSignatures::new(
valid_ledger_info[0].ledger_info().clone(),
BTreeMap::new(),
)],
false,
);
assert!(proof_6
.verify(&EpochState {
epoch: all_epoch[0],
verifier: validator_verifier[0].clone()
})
.is_err());
let waypoint_for_1_to_2 =
Waypoint::new_epoch_boundary(valid_ledger_info[0].ledger_info()).unwrap();
assert!(proof_1.verify(&waypoint_for_1_to_2).is_ok());
let waypoint_for_5_to_6 =
Waypoint::new_epoch_boundary(valid_ledger_info[4].ledger_info()).unwrap();
assert!(proof_1.verify(&waypoint_for_5_to_6).is_ok());
let waypoint_for_3_to_4 =
Waypoint::new_epoch_boundary(valid_ledger_info[2].ledger_info()).unwrap();
let proof_7 =
EpochChangeProof::new(valid_ledger_info[4..8].to_vec(), false);
assert!(proof_7.verify(&waypoint_for_3_to_4).is_err());
let proof_8 = EpochChangeProof::new(valid_ledger_info[..1].to_vec(), false);
assert!(proof_8.verify(&waypoint_for_3_to_4).is_err());
}
}