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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{
epoch_manager::LivenessStorageData,
persistent_liveness_storage::{
LedgerRecoveryData, PersistentLivenessStorage, RecoveryData, RootMetadata,
},
};
use anyhow::Result;
use consensus_types::{
block::Block, quorum_cert::QuorumCert, timeout_2chain::TwoChainTimeoutCertificate,
timeout_certificate::TimeoutCertificate, vote::Vote,
};
use diem_crypto::HashValue;
use diem_infallible::Mutex;
use diem_types::{
epoch_change::EpochChangeProof,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
on_chain_config::ValidatorSet,
};
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};
use storage_interface::DbReader;
pub struct MockSharedStorage {
pub block: Mutex<HashMap<HashValue, Block>>,
pub qc: Mutex<HashMap<HashValue, QuorumCert>>,
pub lis: Mutex<HashMap<u64, LedgerInfoWithSignatures>>,
pub last_vote: Mutex<Option<Vote>>,
pub highest_timeout_certificate: Mutex<Option<TimeoutCertificate>>,
pub highest_2chain_timeout_certificate: Mutex<Option<TwoChainTimeoutCertificate>>,
pub validator_set: ValidatorSet,
}
impl MockSharedStorage {
pub fn new(validator_set: ValidatorSet) -> Self {
MockSharedStorage {
block: Mutex::new(HashMap::new()),
qc: Mutex::new(HashMap::new()),
lis: Mutex::new(HashMap::new()),
last_vote: Mutex::new(None),
highest_timeout_certificate: Mutex::new(None),
highest_2chain_timeout_certificate: Mutex::new(None),
validator_set,
}
}
}
pub struct MockStorage {
pub shared_storage: Arc<MockSharedStorage>,
storage_ledger: Mutex<LedgerInfo>,
}
impl MockStorage {
pub fn new_with_ledger_info(
shared_storage: Arc<MockSharedStorage>,
ledger_info: LedgerInfo,
) -> Self {
let li = if ledger_info.ends_epoch() {
ledger_info.clone()
} else {
let validator_set = Some(shared_storage.validator_set.clone());
LedgerInfo::mock_genesis(validator_set)
};
let lis = LedgerInfoWithSignatures::new(li, BTreeMap::new());
shared_storage
.lis
.lock()
.insert(lis.ledger_info().version(), lis);
MockStorage {
shared_storage,
storage_ledger: Mutex::new(ledger_info),
}
}
pub fn get_ledger_info(&self) -> LedgerInfo {
self.storage_ledger.lock().clone()
}
pub fn commit_to_storage(&self, ledger: LedgerInfo) {
*self.storage_ledger.lock() = ledger;
if let Err(e) = self.verify_consistency() {
panic!("invalid db after commit: {}", e);
}
}
pub fn get_validator_set(&self) -> &ValidatorSet {
&self.shared_storage.validator_set
}
pub fn get_ledger_recovery_data(&self) -> LedgerRecoveryData {
LedgerRecoveryData::new(LedgerInfoWithSignatures::new(
self.storage_ledger.lock().clone(),
BTreeMap::new(),
))
}
pub fn try_start(&self) -> Result<RecoveryData> {
let ledger_recovery_data = self.get_ledger_recovery_data();
let mut blocks: Vec<_> = self
.shared_storage
.block
.lock()
.clone()
.into_iter()
.map(|(_, v)| v)
.collect();
let quorum_certs = self
.shared_storage
.qc
.lock()
.clone()
.into_iter()
.map(|(_, v)| v)
.collect();
blocks.sort_by_key(Block::round);
RecoveryData::new(
self.shared_storage.last_vote.lock().clone(),
ledger_recovery_data,
blocks,
RootMetadata::new_empty(),
quorum_certs,
self.shared_storage
.highest_timeout_certificate
.lock()
.clone(),
self.shared_storage
.highest_2chain_timeout_certificate
.lock()
.clone(),
)
}
pub fn verify_consistency(&self) -> Result<()> {
self.try_start().map(|_| ())
}
pub fn start_for_testing(validator_set: ValidatorSet) -> (RecoveryData, Arc<Self>) {
let shared_storage = Arc::new(MockSharedStorage::new(validator_set.clone()));
let genesis_li = LedgerInfo::mock_genesis(Some(validator_set));
let storage = Self::new_with_ledger_info(shared_storage, genesis_li);
let recovery_data = storage
.start()
.expect_recovery_data("Mock storage should never fail constructing recovery data");
(recovery_data, Arc::new(storage))
}
}
impl PersistentLivenessStorage for MockStorage {
fn save_tree(&self, blocks: Vec<Block>, quorum_certs: Vec<QuorumCert>) -> Result<()> {
let should_check_for_consistency = !(self.shared_storage.block.lock().is_empty()
&& self.shared_storage.qc.lock().is_empty());
for block in blocks {
self.shared_storage.block.lock().insert(block.id(), block);
}
for qc in quorum_certs {
self.shared_storage
.qc
.lock()
.insert(qc.certified_block().id(), qc);
}
if should_check_for_consistency {
if let Err(e) = self.verify_consistency() {
panic!("invalid db after save tree: {}", e);
}
}
Ok(())
}
fn prune_tree(&self, block_id: Vec<HashValue>) -> Result<()> {
for id in block_id {
self.shared_storage.block.lock().remove(&id);
self.shared_storage.qc.lock().remove(&id);
}
if let Err(e) = self.verify_consistency() {
panic!("invalid db after prune tree: {}", e);
}
Ok(())
}
fn save_vote(&self, last_vote: &Vote) -> Result<()> {
self.shared_storage
.last_vote
.lock()
.replace(last_vote.clone());
Ok(())
}
fn recover_from_ledger(&self) -> LedgerRecoveryData {
self.get_ledger_recovery_data()
}
fn start(&self) -> LivenessStorageData {
match self.try_start() {
Ok(recovery_data) => LivenessStorageData::RecoveryData(recovery_data),
Err(_) => LivenessStorageData::LedgerRecoveryData(self.recover_from_ledger()),
}
}
fn save_highest_timeout_cert(
&self,
highest_timeout_certificate: TimeoutCertificate,
) -> Result<()> {
self.shared_storage
.highest_timeout_certificate
.lock()
.replace(highest_timeout_certificate);
Ok(())
}
fn save_highest_2chain_timeout_cert(
&self,
highest_timeout_certificate: &TwoChainTimeoutCertificate,
) -> Result<()> {
self.shared_storage
.highest_2chain_timeout_certificate
.lock()
.replace(highest_timeout_certificate.clone());
Ok(())
}
fn retrieve_epoch_change_proof(&self, version: u64) -> Result<EpochChangeProof> {
let lis = self
.shared_storage
.lis
.lock()
.get(&version)
.cloned()
.ok_or_else(|| anyhow::anyhow!("LedgerInfo for version not found"))?;
Ok(EpochChangeProof::new(vec![lis], false))
}
fn diem_db(&self) -> Arc<dyn DbReader> {
unimplemented!()
}
}
pub struct EmptyStorage;
impl EmptyStorage {
pub fn new() -> Self {
Self
}
pub fn start_for_testing() -> (RecoveryData, Arc<Self>) {
let storage = Arc::new(EmptyStorage::new());
let recovery_data = storage
.start()
.expect_recovery_data("Empty storage should never fail constructing recovery data");
(recovery_data, storage)
}
}
impl PersistentLivenessStorage for EmptyStorage {
fn save_tree(&self, _: Vec<Block>, _: Vec<QuorumCert>) -> Result<()> {
Ok(())
}
fn prune_tree(&self, _: Vec<HashValue>) -> Result<()> {
Ok(())
}
fn save_vote(&self, _: &Vote) -> Result<()> {
Ok(())
}
fn recover_from_ledger(&self) -> LedgerRecoveryData {
LedgerRecoveryData::new(LedgerInfoWithSignatures::new(
LedgerInfo::mock_genesis(None),
BTreeMap::new(),
))
}
fn start(&self) -> LivenessStorageData {
match RecoveryData::new(
None,
self.recover_from_ledger(),
vec![],
RootMetadata::new_empty(),
vec![],
None,
None,
) {
Ok(recovery_data) => LivenessStorageData::RecoveryData(recovery_data),
Err(e) => {
eprintln!("{}", e);
panic!("Construct recovery data during genesis should never fail");
}
}
}
fn save_highest_timeout_cert(&self, _: TimeoutCertificate) -> Result<()> {
Ok(())
}
fn save_highest_2chain_timeout_cert(&self, _: &TwoChainTimeoutCertificate) -> Result<()> {
Ok(())
}
fn retrieve_epoch_change_proof(&self, _version: u64) -> Result<EpochChangeProof> {
unimplemented!()
}
fn diem_db(&self) -> Arc<dyn DbReader> {
unimplemented!()
}
}