-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathmocks.rs
491 lines (417 loc) · 16.1 KB
/
mocks.rs
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use crate::{
error::Error,
metadata_storage::MetadataStorageInterface,
storage_synchronizer::{NotificationMetadata, StorageSynchronizerInterface},
tests::utils::{create_empty_epoch_state, create_epoch_ending_ledger_info},
};
use anyhow::Result as AnyhowResult;
use aptos_crypto::HashValue;
use aptos_data_streaming_service::{
data_notification::NotificationId,
data_stream::{DataStreamId, DataStreamListener},
streaming_client::{DataStreamingClient, Epoch, NotificationAndFeedback},
};
use aptos_executor_types::{ChunkCommitNotification, ChunkExecutorTrait};
use aptos_storage_interface::{
cached_state_view::ShardedStateCache, state_delta::StateDelta, DbReader, DbReaderWriter,
DbWriter, ExecutedTrees, Order, Result, StateSnapshotReceiver,
};
use aptos_types::{
account_address::AccountAddress,
contract_event::EventWithVersion,
epoch_change::EpochChangeProof,
epoch_state::EpochState,
event::EventKey,
ledger_info::LedgerInfoWithSignatures,
proof::{
AccumulatorConsistencyProof, SparseMerkleProof, SparseMerkleRangeProof,
TransactionAccumulatorSummary,
},
state_proof::StateProof,
state_store::{
state_key::StateKey,
state_value::{StateValue, StateValueChunkWithProof},
ShardedStateUpdates,
},
transaction::{
AccountTransactionsWithProof, TransactionListWithProof, TransactionOutputListWithProof,
TransactionToCommit, TransactionWithProof, Version,
},
};
use async_trait::async_trait;
use mockall::mock;
use std::sync::Arc;
use tokio::task::JoinHandle;
// TODO(joshlind): if we see these as generally useful, we should
// modify the definitions in the rest of the code.
/// Creates a mock chunk executor
pub fn create_mock_executor() -> MockChunkExecutor {
MockChunkExecutor::new()
}
/// Creates a mock database reader
pub fn create_mock_db_reader() -> MockDatabaseReader {
MockDatabaseReader::new()
}
/// Creates a mock database writer
pub fn create_mock_db_writer() -> MockDatabaseWriter {
MockDatabaseWriter::new()
}
/// Creates a mock database reader writer
pub fn create_mock_reader_writer(
reader: Option<MockDatabaseReader>,
writer: Option<MockDatabaseWriter>,
) -> DbReaderWriter {
create_mock_reader_writer_with_version(reader, writer, 0)
}
/// Creates a mock database reader writer with the given
/// highest synced transaction version.
pub fn create_mock_reader_writer_with_version(
reader: Option<MockDatabaseReader>,
writer: Option<MockDatabaseWriter>,
highest_synced_version: u64,
) -> DbReaderWriter {
let mut reader = reader.unwrap_or_else(create_mock_db_reader);
reader
.expect_get_latest_version()
.returning(move || Ok(highest_synced_version));
reader
.expect_get_latest_epoch_state()
.returning(|| Ok(create_empty_epoch_state()));
reader
.expect_get_latest_ledger_info()
.returning(|| Ok(create_epoch_ending_ledger_info()));
let writer = writer.unwrap_or_else(create_mock_db_writer);
DbReaderWriter {
reader: Arc::new(reader),
writer: Arc::new(writer),
}
}
/// Creates a mock state snapshot receiver
pub fn create_mock_receiver() -> MockSnapshotReceiver {
MockSnapshotReceiver::new()
}
/// Creates a mock data streaming client
pub fn create_mock_streaming_client() -> MockStreamingClient {
MockStreamingClient::new()
}
/// Creates a mock storage synchronizer
pub fn create_mock_storage_synchronizer() -> MockStorageSynchronizer {
MockStorageSynchronizer::new()
}
/// Creates a mock storage synchronizer that is not currently handling
/// any pending storage data.
pub fn create_ready_storage_synchronizer(expect_reset_executor: bool) -> MockStorageSynchronizer {
let mut mock_storage_synchronizer = create_mock_storage_synchronizer();
mock_storage_synchronizer
.expect_pending_storage_data()
.return_const(false);
if expect_reset_executor {
mock_storage_synchronizer
.expect_finish_chunk_executor()
.return_const(());
mock_storage_synchronizer
.expect_reset_chunk_executor()
.return_const(Ok(()));
}
mock_storage_synchronizer
}
// This automatically creates a MockChunkExecutor.
mock! {
pub ChunkExecutor {}
impl ChunkExecutorTrait for ChunkExecutor {
fn execute_chunk<'a>(
&self,
txn_list_with_proof: TransactionListWithProof,
verified_target_li: &LedgerInfoWithSignatures,
epoch_change_li: Option<&'a LedgerInfoWithSignatures>,
) -> AnyhowResult<()>;
fn apply_chunk<'a>(
&self,
txn_output_list_with_proof: TransactionOutputListWithProof,
verified_target_li: &LedgerInfoWithSignatures,
epoch_change_li: Option<&'a LedgerInfoWithSignatures>,
) -> AnyhowResult<()>;
fn enqueue_chunk_by_execution<'a>(
&self,
txn_list_with_proof: TransactionListWithProof,
verified_target_li: &LedgerInfoWithSignatures,
epoch_change_li: Option<&'a LedgerInfoWithSignatures>,
) -> AnyhowResult<()>;
fn enqueue_chunk_by_transaction_outputs<'a>(
&self,
txn_output_list_with_proof: TransactionOutputListWithProof,
verified_target_li: &LedgerInfoWithSignatures,
epoch_change_li: Option<&'a LedgerInfoWithSignatures>,
) -> AnyhowResult<()>;
fn update_ledger(&self) -> AnyhowResult<()>;
fn commit_chunk(&self) -> AnyhowResult<ChunkCommitNotification>;
fn reset(&self) -> AnyhowResult<()>;
fn finish(&self);
}
}
// This automatically creates a MockDatabaseReader.
mock! {
pub DatabaseReader {}
impl DbReader for DatabaseReader {
fn get_epoch_ending_ledger_infos(
&self,
start_epoch: u64,
end_epoch: u64,
) -> Result<EpochChangeProof>;
fn get_transactions(
&self,
start_version: Version,
batch_size: u64,
ledger_version: Version,
fetch_events: bool,
) -> Result<TransactionListWithProof>;
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> Result<Option<TransactionWithProof>>;
fn get_transaction_by_version(
&self,
version: Version,
ledger_version: Version,
fetch_events: bool,
) -> Result<TransactionWithProof>;
fn get_transaction_outputs(
&self,
start_version: Version,
limit: u64,
ledger_version: Version,
) -> Result<TransactionOutputListWithProof>;
fn get_events(
&self,
event_key: &EventKey,
start: u64,
order: Order,
limit: u64,
ledger_version: Version,
) -> Result<Vec<EventWithVersion>>;
fn get_block_timestamp(&self, version: u64) -> Result<u64>;
fn get_last_version_before_timestamp(
&self,
_timestamp: u64,
_ledger_version: Version,
) -> Result<Version>;
fn get_latest_epoch_state(&self) -> Result<EpochState>;
fn get_latest_ledger_info_option(&self) -> Result<Option<LedgerInfoWithSignatures>>;
fn get_latest_ledger_info(&self) -> Result<LedgerInfoWithSignatures>;
fn get_latest_version(&self) -> Result<Version>;
fn get_latest_commit_metadata(&self) -> Result<(Version, u64)>;
fn get_account_transaction(
&self,
address: AccountAddress,
seq_num: u64,
include_events: bool,
ledger_version: Version,
) -> Result<Option<TransactionWithProof>>;
fn get_account_transactions(
&self,
address: AccountAddress,
seq_num: u64,
limit: u64,
include_events: bool,
ledger_version: Version,
) -> Result<AccountTransactionsWithProof>;
fn get_state_proof_with_ledger_info(
&self,
known_version: u64,
ledger_info: LedgerInfoWithSignatures,
) -> Result<StateProof>;
fn get_state_proof(&self, known_version: u64) -> Result<StateProof>;
fn get_state_value_with_proof_by_version(
&self,
state_key: &StateKey,
version: Version,
) -> Result<(Option<StateValue>, SparseMerkleProof)>;
fn get_latest_executed_trees(&self) -> Result<ExecutedTrees>;
fn get_epoch_ending_ledger_info(&self, known_version: u64) -> Result<LedgerInfoWithSignatures>;
fn get_accumulator_root_hash(&self, _version: Version) -> Result<HashValue>;
fn get_accumulator_consistency_proof(
&self,
_client_known_version: Option<Version>,
_ledger_version: Version,
) -> Result<AccumulatorConsistencyProof>;
fn get_accumulator_summary(
&self,
ledger_version: Version,
) -> Result<TransactionAccumulatorSummary>;
fn get_state_leaf_count(&self, version: Version) -> Result<usize>;
fn get_state_value_chunk_with_proof(
&self,
version: Version,
start_idx: usize,
chunk_size: usize,
) -> Result<StateValueChunkWithProof>;
fn get_epoch_snapshot_prune_window(&self) -> Result<usize>;
}
}
// This automatically creates a MockDatabaseWriter.
mock! {
pub DatabaseWriter {}
impl DbWriter for DatabaseWriter {
fn get_state_snapshot_receiver(
&self,
version: Version,
expected_root_hash: HashValue,
) -> Result<Box<dyn StateSnapshotReceiver<StateKey, StateValue>>>;
fn finalize_state_snapshot(
&self,
version: Version,
output_with_proof: TransactionOutputListWithProof,
ledger_infos: &[LedgerInfoWithSignatures],
) -> Result<()>;
fn save_transactions<'a, 'b>(
&self,
txns_to_commit: &[TransactionToCommit],
first_version: Version,
base_state_version: Option<Version>,
ledger_info_with_sigs: Option<&'a LedgerInfoWithSignatures>,
sync_commit: bool,
in_memory_state: StateDelta,
state_updates_until_last_checkpoint: Option<ShardedStateUpdates>,
sharded_state_cache: Option<&'b ShardedStateCache>,
) -> Result<()>;
}
}
// This automatically creates a MockMetadataStorage.
mock! {
pub MetadataStorage {}
impl MetadataStorageInterface for MetadataStorage {
fn is_snapshot_sync_complete(
&self,
target_ledger_info: &LedgerInfoWithSignatures,
) -> Result<bool, Error>;
fn get_last_persisted_state_value_index(
&self,
target_ledger_info: &LedgerInfoWithSignatures,
) -> Result<u64, Error>;
fn previous_snapshot_sync_target(&self) -> Result<Option<LedgerInfoWithSignatures>, Error>;
fn update_last_persisted_state_value_index(
&self,
target_ledger_info: &LedgerInfoWithSignatures,
last_persisted_state_value_index: u64,
snapshot_sync_completed: bool,
) -> Result<(), Error>;
}
impl Clone for MetadataStorage {
fn clone(&self) -> Self;
}
}
// This automatically creates a MockSnapshotReceiver.
mock! {
pub SnapshotReceiver {}
impl StateSnapshotReceiver<StateKey, StateValue> for SnapshotReceiver {
fn add_chunk(&mut self, chunk: Vec<(StateKey, StateValue)>, proof: SparseMerkleRangeProof) -> Result<()>;
fn finish(self) -> Result<()>;
fn finish_box(self: Box<Self>) -> Result<()>;
}
}
// This automatically creates a MockStreamingClient.
mock! {
pub StreamingClient {}
#[async_trait]
impl DataStreamingClient for StreamingClient {
async fn get_all_state_values(
&self,
version: Version,
start_index: Option<u64>,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn get_all_epoch_ending_ledger_infos(
&self,
start_epoch: Epoch,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn get_all_transaction_outputs(
&self,
start_version: Version,
end_version: Version,
proof_version: Version,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn get_all_transactions(
&self,
start_version: Version,
end_version: Version,
proof_version: Version,
include_events: bool,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn get_all_transactions_or_outputs(
&self,
start_version: Version,
end_version: Version,
proof_version: Version,
include_events: bool,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn continuously_stream_transaction_outputs(
&self,
start_version: Version,
start_epoch: Epoch,
target: Option<LedgerInfoWithSignatures>,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn continuously_stream_transactions(
&self,
start_version: Version,
start_epoch: Epoch,
include_events: bool,
target: Option<LedgerInfoWithSignatures>,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn continuously_stream_transactions_or_outputs(
&self,
start_version: Version,
start_epoch: Epoch,
include_events: bool,
target: Option<LedgerInfoWithSignatures>,
) -> AnyhowResult<DataStreamListener, aptos_data_streaming_service::error::Error>;
async fn terminate_stream_with_feedback(
&self,
data_stream_id: DataStreamId,
notification_and_feedback: Option<NotificationAndFeedback>,
) -> AnyhowResult<(), aptos_data_streaming_service::error::Error>;
}
impl Clone for StreamingClient {
fn clone(&self) -> Self;
}
}
// This automatically creates a MockStorageSynchronizer.
mock! {
pub StorageSynchronizer {}
#[async_trait]
impl StorageSynchronizerInterface for StorageSynchronizer {
async fn apply_transaction_outputs(
&mut self,
notification_metadata: NotificationMetadata,
output_list_with_proof: TransactionOutputListWithProof,
target_ledger_info: LedgerInfoWithSignatures,
end_of_epoch_ledger_info: Option<LedgerInfoWithSignatures>,
) -> AnyhowResult<(), crate::error::Error>;
async fn execute_transactions(
&mut self,
notification_metadata: NotificationMetadata,
transaction_list_with_proof: TransactionListWithProof,
target_ledger_info: LedgerInfoWithSignatures,
end_of_epoch_ledger_info: Option<LedgerInfoWithSignatures>,
) -> AnyhowResult<(), crate::error::Error>;
fn initialize_state_synchronizer(
&mut self,
epoch_change_proofs: Vec<LedgerInfoWithSignatures>,
target_ledger_info: LedgerInfoWithSignatures,
target_output_with_proof: TransactionOutputListWithProof,
) -> AnyhowResult<JoinHandle<()>, crate::error::Error>;
fn pending_storage_data(&self) -> bool;
async fn save_state_values(
&mut self,
notification_id: NotificationId,
state_value_chunk_with_proof: StateValueChunkWithProof,
) -> AnyhowResult<(), crate::error::Error>;
fn reset_chunk_executor(&self) -> AnyhowResult<(), crate::error::Error>;
fn finish_chunk_executor(&self);
}
impl Clone for StorageSynchronizer {
fn clone(&self) -> Self;
}
}