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
use crate::block_storage::{BlockReader, BlockStore};
use consensus_types::{
block::{block_test_utils::certificate_for_genesis, Block},
common::Round,
executed_block::ExecutedBlock,
quorum_cert::QuorumCert,
sync_info::SyncInfo,
};
use diem_crypto::HashValue;
use diem_logger::Level;
use diem_types::{ledger_info::LedgerInfo, validator_signer::ValidatorSigner};
use std::{future::Future, sync::Arc, time::Duration};
use tokio::{runtime, time::timeout};
mod mock_state_computer;
mod mock_storage;
#[cfg(any(test, feature = "fuzzing"))]
mod mock_txn_manager;
use crate::util::mock_time_service::SimulatedTimeService;
use consensus_types::{block::block_test_utils::gen_test_certificate, common::Payload};
use diem_types::block_info::BlockInfo;
pub use mock_state_computer::{
EmptyStateComputer, MockStateComputer, RandomComputeResultStateComputer,
};
pub use mock_storage::{EmptyStorage, MockSharedStorage, MockStorage};
pub use mock_txn_manager::MockTransactionManager;
pub const TEST_TIMEOUT: Duration = Duration::from_secs(60);
pub fn build_simple_tree() -> (Vec<Arc<ExecutedBlock>>, Arc<BlockStore>) {
let mut inserter = TreeInserter::default();
let block_store = inserter.block_store();
let genesis = block_store.ordered_root();
let genesis_block_id = genesis.id();
let genesis_block = block_store
.get_block(genesis_block_id)
.expect("genesis block must exist");
assert_eq!(block_store.len(), 1);
assert_eq!(block_store.child_links(), block_store.len() - 1);
assert_eq!(block_store.block_exists(genesis_block.id()), true);
let a1 = inserter.insert_block_with_qc(certificate_for_genesis(), &genesis_block, 1);
let a2 = inserter.insert_block(&a1, 2, None);
let a3 = inserter.insert_block(&a2, 3, Some(genesis.block_info()));
let b1 = inserter.insert_block_with_qc(certificate_for_genesis(), &genesis_block, 4);
let b2 = inserter.insert_block(&b1, 5, None);
let c1 = inserter.insert_block(&b1, 6, None);
assert_eq!(block_store.len(), 7);
assert_eq!(block_store.child_links(), block_store.len() - 1);
(vec![genesis_block, a1, a2, a3, b1, b2, c1], block_store)
}
pub fn build_empty_tree() -> Arc<BlockStore> {
let (initial_data, storage) = EmptyStorage::start_for_testing();
Arc::new(BlockStore::new(
storage,
initial_data,
Arc::new(EmptyStateComputer),
10, Arc::new(SimulatedTimeService::new()),
))
}
pub struct TreeInserter {
signer: ValidatorSigner,
block_store: Arc<BlockStore>,
}
impl TreeInserter {
pub fn default() -> Self {
Self::new(ValidatorSigner::random(None))
}
pub fn new(signer: ValidatorSigner) -> Self {
let block_store = build_empty_tree();
Self {
signer,
block_store,
}
}
pub fn new_with_store(signer: ValidatorSigner, block_store: Arc<BlockStore>) -> Self {
Self {
signer,
block_store,
}
}
pub fn signer(&self) -> &ValidatorSigner {
&self.signer
}
pub fn block_store(&self) -> Arc<BlockStore> {
Arc::clone(&self.block_store)
}
pub fn insert_block(
&mut self,
parent: &ExecutedBlock,
round: Round,
committed_block: Option<BlockInfo>,
) -> Arc<ExecutedBlock> {
let parent_qc = self.create_qc_for_block(parent, committed_block);
self.insert_block_with_qc(parent_qc, parent, round)
}
pub fn insert_block_with_qc(
&mut self,
parent_qc: QuorumCert,
parent: &ExecutedBlock,
round: Round,
) -> Arc<ExecutedBlock> {
self.block_store
.insert_block_with_qc(self.create_block_with_qc(
parent_qc,
parent.timestamp_usecs() + 1,
round,
vec![],
))
.unwrap()
}
pub fn create_qc_for_block(
&self,
block: &ExecutedBlock,
committed_block: Option<BlockInfo>,
) -> QuorumCert {
gen_test_certificate(
vec![&self.signer],
block.block_info(),
block.quorum_cert().certified_block().clone(),
committed_block,
)
}
pub fn insert_qc_for_block(&self, block: &ExecutedBlock, committed_block: Option<BlockInfo>) {
self.block_store
.insert_single_quorum_cert(self.create_qc_for_block(block, committed_block))
.unwrap()
}
pub fn create_block_with_qc(
&self,
parent_qc: QuorumCert,
timestamp_usecs: u64,
round: Round,
payload: Payload,
) -> Block {
Block::new_proposal(payload, round, timestamp_usecs, parent_qc, &self.signer)
}
}
pub fn placeholder_ledger_info() -> LedgerInfo {
LedgerInfo::new(BlockInfo::empty(), HashValue::zero())
}
pub fn placeholder_sync_info() -> SyncInfo {
SyncInfo::new(
certificate_for_genesis(),
certificate_for_genesis(),
None,
None,
)
}
fn nocapture() -> bool {
::std::env::args().any(|arg| arg == "--nocapture")
}
pub fn consensus_runtime() -> runtime::Runtime {
if nocapture() {
::diem_logger::Logger::new().level(Level::Debug).init();
}
runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed to create Tokio runtime!")
}
pub fn timed_block_on<F>(runtime: &mut runtime::Runtime, f: F) -> <F as Future>::Output
where
F: Future,
{
runtime
.block_on(async { timeout(TEST_TIMEOUT, f).await })
.expect("test timed out")
}