forked from exonum/exonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_hooks.rs
309 lines (268 loc) · 10.9 KB
/
service_hooks.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
// Copyright 2020 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use assert_matches::assert_matches;
use exonum::{
helpers::Height,
runtime::{InstanceId, InstanceStatus, SnapshotExt, SUPERVISOR_INSTANCE_ID},
};
use exonum_explorer::BlockchainExplorer;
use exonum_merkledb::{BinaryValue, ObjectHash};
use exonum_rust_runtime::{RustRuntime, ServiceFactory};
use exonum_testkit::{Spec, TestKitBuilder};
use pretty_assertions::assert_eq;
pub use crate::{
hooks_service::{
AfterCommitInterface, AfterCommitService, AfterCommitServiceV2, SERVICE_ID, SERVICE_NAME,
},
supervisor::{StartMigration, Supervisor, SupervisorInterface},
};
mod hooks_service;
mod supervisor;
const SUPERVISOR_ID: InstanceId = SUPERVISOR_INSTANCE_ID;
#[tokio::test]
async fn test_after_commit() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(service.clone()).with_default_instance())
.build();
// Check that `after_commit` invoked on the correct height.
for i in 1..5 {
let block = testkit.create_block();
if i > 1 {
let arguments = &block[0].message().payload().arguments;
let height_from_tx = u64::from_bytes(arguments.into()).unwrap();
assert_eq!(height_from_tx, i - 1);
}
assert_eq!(service.counter() as u64, i);
let blockchain = testkit.blockchain();
let keypair = blockchain.service_keypair();
let tx = keypair.after_commit(SERVICE_ID, i);
assert!(testkit.is_tx_in_pool(&tx.object_hash()));
}
let snapshot = testkit.snapshot();
let expected_block_sizes = BlockchainExplorer::new(snapshot.as_ref())
.blocks(Height(1)..)
.all(|block| block.len() == if block.height() == Height(1) { 0 } else { 1 });
assert!(expected_block_sizes);
}
/// An auditor should not broadcast transactions.
#[tokio::test]
async fn test_after_commit_with_auditor() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::auditor()
.with_validators(2)
.with(Spec::new(service.clone()).with_default_instance())
.build();
for i in 1..5 {
let block = testkit.create_block();
assert!(block.is_empty());
assert_eq!(service.counter() as u64, i);
let blockchain = testkit.blockchain();
let keypair = blockchain.service_keypair();
let tx = keypair.after_commit(SERVICE_ID, i);
assert!(!testkit.is_tx_in_pool(&tx.object_hash()));
}
service.switch_to_generic_broadcast();
for i in 0..5 {
let block = testkit.create_block();
let expected_block_len = if i == 0 { 0 } else { 1 };
assert_eq!(block.len(), expected_block_len);
}
}
#[tokio::test]
async fn after_commit_not_called_after_service_stop() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(Supervisor).with_default_instance())
.with(Spec::new(service.clone()).with_default_instance())
.build();
service.switch_to_generic_broadcast();
let keys = testkit.us().service_keypair();
let tx = keys.stop_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should stop");
// Check that `after_commit` hook is not called for the stopped service.
for _ in 0..5 {
let block = testkit.create_block();
assert!(block.is_empty());
}
// Resume the service and check that `after_commit` starts being called again.
let tx = keys.resume_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should resume");
for _ in 0..5 {
let block = testkit.create_block();
assert_eq!(block.len(), 1);
}
}
#[tokio::test]
async fn after_commit_during_service_freeze() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(Supervisor).with_default_instance())
.with(Spec::new(service.clone()).with_default_instance())
.build();
let keys = testkit.us().service_keypair();
let tx = keys.freeze_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should freeze");
// `broadcaster` used in `after_commit` hook by default is not exposed when the service
// is frozen.
for _ in 0..5 {
let block = testkit.create_block();
assert!(block.is_empty());
}
// Generic broadcast is switched off, too, due to transaction filtering within testkit.
service.switch_to_generic_broadcast();
for _ in 0..5 {
let block = testkit.create_block();
assert!(block.is_empty());
}
}
#[tokio::test]
async fn after_commit_during_migration() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(Supervisor).with_default_instance())
.with(Spec::new(service.clone()).with_default_instance())
.with(Spec::migrating(AfterCommitServiceV2))
.build();
let keys = testkit.us().service_keypair();
let tx = keys.stop_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should stop");
let tx = keys.start_migration(
SUPERVISOR_ID,
StartMigration {
instance_id: SERVICE_ID,
new_artifact: AfterCommitServiceV2.artifact_id(),
migration_len: 10,
},
);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should start migrating");
// As with frozen service, the ordinary broadcast should be switched off.
for _ in 0..5 {
let block = testkit.create_block();
assert!(block.is_empty());
let snapshot = testkit.snapshot();
let service_state = snapshot.for_dispatcher().get_instance(SERVICE_ID).unwrap();
assert_matches!(service_state.status, Some(InstanceStatus::Migrating(_)));
}
// Generic broadcast is switched off, too, due to transaction filtering within testkit.
service.switch_to_generic_broadcast();
for _ in 0..5 {
let block = testkit.create_block();
assert!(block.is_empty());
let snapshot = testkit.snapshot();
let service_state = snapshot.for_dispatcher().get_instance(SERVICE_ID).unwrap();
assert_matches!(service_state.status, Some(InstanceStatus::Migrating(_)));
}
testkit.create_block();
testkit.create_block();
let snapshot = testkit.snapshot();
let service_state = snapshot.for_dispatcher().get_instance(SERVICE_ID).unwrap();
assert_matches!(service_state.status, Some(InstanceStatus::Stopped));
}
#[tokio::test]
async fn incorrect_txs_are_not_included_into_blocks() {
let service = AfterCommitService::new();
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(Supervisor).with_default_instance())
.with(Spec::new(service).with_default_instance())
.build();
let keys = testkit.us().service_keypair();
// Generate some transactions using the service, but do not commit them.
for _ in 0..5 {
let block = testkit.create_block_with_tx_hashes(&[]);
assert!(block.is_empty());
let new_tx = keys.after_commit(SERVICE_ID, testkit.height().0);
assert!(testkit.is_tx_in_pool(&new_tx.object_hash()));
}
let tx = keys.freeze_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should freeze");
// Check that transactions in the pool are not committed while the service is frozen.
let block = testkit.create_block();
assert!(block.is_empty());
// Resume the service.
let tx = keys.resume_service(SUPERVISOR_ID, SERVICE_ID);
let block = testkit.create_block_with_transaction(tx);
block[0].status().expect("Service should resume");
// Check that all previously created transactions have been committed.
let block = testkit.create_block();
assert_eq!(block.len(), 6); // 5 old transactions + 1 generated after resume
}
#[tokio::test]
async fn restart_testkit() {
let mut testkit = TestKitBuilder::validator()
.with_validators(3)
.with(Spec::new(AfterCommitService::new()).with_default_instance())
.build();
testkit.create_blocks_until(Height(5));
let stopped = testkit.stop();
assert_eq!(stopped.height(), Height(5));
assert_eq!(stopped.network().validators().len(), 3);
let service = AfterCommitService::new();
let rust_runtime = RustRuntime::builder().with_factory(service.clone());
let mut testkit = stopped.resume(rust_runtime);
for _ in 0..3 {
testkit.create_block();
}
// The counter is controlled by the service instance and thus is *not* persistent
// between reloads.
assert_eq!(service.counter(), 3);
// OTOH, the database state is fully persisted between reloads.
assert_eq!(testkit.height(), Height(8));
assert_eq!(testkit.network().validators().len(), 3);
let transactions_are_committed = (1..=8)
.map(|i| {
testkit
.blockchain()
.service_keypair()
.after_commit(SERVICE_ID, i)
.object_hash()
})
.all(|hash| {
let snapshot = testkit.snapshot();
BlockchainExplorer::new(snapshot.as_ref())
.transaction_without_proof(&hash)
.is_some()
});
assert!(transactions_are_committed);
}
#[test]
fn tx_pool_is_retained_on_restart() {
let mut testkit = TestKitBuilder::validator()
.with(Spec::new(AfterCommitService::new()).with_default_instance())
.build();
let tx_hashes: Vec<_> = (100..105)
.map(|i| {
let tx = testkit
.blockchain()
.service_keypair()
.after_commit(SERVICE_ID, i);
let tx_hash = tx.object_hash();
testkit.add_tx(tx);
assert!(testkit.is_tx_in_pool(&tx_hash));
tx_hash
})
.collect();
let stopped = testkit.stop();
let rust_runtime = RustRuntime::builder().with_factory(AfterCommitService::new());
let testkit = stopped.resume(rust_runtime);
assert!(tx_hashes
.iter()
.all(|tx_hash| testkit.is_tx_in_pool(tx_hash)));
}