This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtests.rs
More file actions
2119 lines (1719 loc) · 54.4 KB
/
tests.rs
File metadata and controls
2119 lines (1719 loc) · 54.4 KB
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the subsystem.
//!
//! These primarily revolve around having a backend which is shared between
//! both the test code and the tested subsystem, and which also gives the
//! test code the ability to wait for write operations to occur.
use super::*;
use std::{
collections::{BTreeMap, HashMap, HashSet},
sync::{
atomic::{AtomicU64, Ordering as AtomicOrdering},
Arc,
},
};
use assert_matches::assert_matches;
use futures::channel::oneshot;
use parity_scale_codec::Encode;
use parking_lot::Mutex;
use sp_core::testing::TaskExecutor;
use polkadot_node_subsystem::{
jaeger, messages::AllMessages, ActivatedLeaf, ActiveLeavesUpdate, LeafStatus,
};
use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_primitives::v2::{BlakeTwo256, ConsensusLog, HashT};
#[derive(Default)]
struct TestBackendInner {
leaves: LeafEntrySet,
block_entries: HashMap<Hash, BlockEntry>,
blocks_by_number: BTreeMap<BlockNumber, Vec<Hash>>,
stagnant_at: BTreeMap<Timestamp, Vec<Hash>>,
// earlier wakers at the back.
write_wakers: Vec<oneshot::Sender<()>>,
}
#[derive(Clone)]
struct TestBackend {
inner: Arc<Mutex<TestBackendInner>>,
}
impl TestBackend {
// Yields a receiver which will be woken up on some future write
// to the backend along with its position (starting at 0) in the
// queue.
//
// Our tests assume that there is only one task calling this function
// and the index is useful to get a waker that will trigger after
// some known amount of writes to the backend that happen internally
// inside the subsystem.
//
// It's important to call this function at points where no writes
// are pending to the backend. This requires knowing some details
// about the internals of the subsystem, so the abstraction leaks
// somewhat, but this is acceptable enough.
fn await_next_write(&self) -> (usize, oneshot::Receiver<()>) {
let (tx, rx) = oneshot::channel();
let mut inner = self.inner.lock();
let pos = inner.write_wakers.len();
inner.write_wakers.insert(0, tx);
(pos, rx)
}
// Assert the backend contains only the given blocks and no others.
// This does not check the stagnant_at mapping because that is
// pruned lazily by the subsystem as opposed to eagerly.
fn assert_contains_only(&self, blocks: Vec<(BlockNumber, Hash)>) {
let hashes: Vec<_> = blocks.iter().map(|(_, h)| *h).collect();
let mut by_number: HashMap<_, HashSet<_>> = HashMap::new();
for (number, hash) in blocks {
by_number.entry(number).or_default().insert(hash);
}
let inner = self.inner.lock();
assert_eq!(inner.block_entries.len(), hashes.len());
assert_eq!(inner.blocks_by_number.len(), by_number.len());
for leaf in inner.leaves.clone().into_hashes_descending() {
assert!(hashes.contains(&leaf));
}
for (number, hashes_at_number) in by_number {
let at = inner.blocks_by_number.get(&number).unwrap();
for hash in at {
assert!(hashes_at_number.contains(&hash));
}
}
}
fn assert_stagnant_at_state(&self, stagnant_at: Vec<(Timestamp, Vec<Hash>)>) {
let inner = self.inner.lock();
assert_eq!(inner.stagnant_at.len(), stagnant_at.len());
for (at, hashes) in stagnant_at {
let stored_hashes = inner.stagnant_at.get(&at).unwrap();
assert_eq!(hashes.len(), stored_hashes.len());
for hash in hashes {
assert!(stored_hashes.contains(&hash));
}
}
}
}
impl Default for TestBackend {
fn default() -> Self {
TestBackend { inner: Default::default() }
}
}
impl Backend for TestBackend {
fn load_block_entry(&self, hash: &Hash) -> Result<Option<BlockEntry>, Error> {
Ok(self.inner.lock().block_entries.get(hash).map(|e| e.clone()))
}
fn load_leaves(&self) -> Result<LeafEntrySet, Error> {
Ok(self.inner.lock().leaves.clone())
}
fn load_stagnant_at(&self, timestamp: Timestamp) -> Result<Vec<Hash>, Error> {
Ok(self.inner.lock().stagnant_at.get(×tamp).map_or(Vec::new(), |s| s.clone()))
}
fn load_stagnant_at_up_to(
&self,
up_to: Timestamp,
max_elements: usize,
) -> Result<Vec<(Timestamp, Vec<Hash>)>, Error> {
Ok(self
.inner
.lock()
.stagnant_at
.range(..=up_to)
.enumerate()
.take_while(|(idx, _)| *idx < max_elements)
.map(|(_, (t, v))| (*t, v.clone()))
.collect())
}
fn load_first_block_number(&self) -> Result<Option<BlockNumber>, Error> {
Ok(self.inner.lock().blocks_by_number.range(..).map(|(k, _)| *k).next())
}
fn load_blocks_by_number(&self, number: BlockNumber) -> Result<Vec<Hash>, Error> {
Ok(self
.inner
.lock()
.blocks_by_number
.get(&number)
.map_or(Vec::new(), |v| v.clone()))
}
fn write<I>(&mut self, ops: I) -> Result<(), Error>
where
I: IntoIterator<Item = BackendWriteOp>,
{
let ops: Vec<_> = ops.into_iter().collect();
// Early return if empty because empty writes shouldn't
// trigger wakeups (they happen on an interval)
if ops.is_empty() {
return Ok(())
}
let mut inner = self.inner.lock();
for op in ops {
match op {
BackendWriteOp::WriteBlockEntry(entry) => {
inner.block_entries.insert(entry.block_hash, entry);
},
BackendWriteOp::WriteBlocksByNumber(number, hashes) => {
inner.blocks_by_number.insert(number, hashes);
},
BackendWriteOp::WriteViableLeaves(leaves) => {
inner.leaves = leaves;
},
BackendWriteOp::WriteStagnantAt(time, hashes) => {
inner.stagnant_at.insert(time, hashes);
},
BackendWriteOp::DeleteBlocksByNumber(number) => {
inner.blocks_by_number.remove(&number);
},
BackendWriteOp::DeleteBlockEntry(hash) => {
inner.block_entries.remove(&hash);
},
BackendWriteOp::DeleteStagnantAt(time) => {
inner.stagnant_at.remove(&time);
},
}
}
if let Some(waker) = inner.write_wakers.pop() {
let _ = waker.send(());
}
Ok(())
}
}
#[derive(Clone)]
pub struct TestClock(Arc<AtomicU64>);
impl TestClock {
fn new(initial: u64) -> Self {
TestClock(Arc::new(AtomicU64::new(initial)))
}
fn inc_by(&self, duration: u64) {
self.0.fetch_add(duration, AtomicOrdering::Relaxed);
}
}
impl Clock for TestClock {
fn timestamp_now(&self) -> Timestamp {
self.0.load(AtomicOrdering::Relaxed)
}
}
const TEST_STAGNANT_INTERVAL: Duration = Duration::from_millis(20);
type VirtualOverseer = test_helpers::TestSubsystemContextHandle<ChainSelectionMessage>;
fn test_harness<T: Future<Output = VirtualOverseer>>(
test: impl FnOnce(TestBackend, TestClock, VirtualOverseer) -> T,
) {
let pool = TaskExecutor::new();
let (context, virtual_overseer) = test_helpers::make_subsystem_context(pool);
let backend = TestBackend::default();
let clock = TestClock::new(0);
let subsystem = crate::run(
context,
backend.clone(),
StagnantCheckInterval::new(TEST_STAGNANT_INTERVAL),
StagnantCheckMode::CheckAndPrune,
Box::new(clock.clone()),
);
let test_fut = test(backend, clock, virtual_overseer);
let test_and_conclude = async move {
let mut virtual_overseer = test_fut.await;
virtual_overseer.send(OverseerSignal::Conclude.into()).await;
// Ensure no messages are pending when the subsystem shuts down.
assert!(virtual_overseer.try_recv().await.is_none());
};
futures::executor::block_on(futures::future::join(subsystem, test_and_conclude));
}
// Answer requests from the subsystem about the finalized block.
async fn answer_finalized_block_info(
overseer: &mut VirtualOverseer,
finalized_number: BlockNumber,
finalized_hash: Hash,
) {
assert_matches!(
overseer.recv().await,
AllMessages::ChainApi(ChainApiMessage::FinalizedBlockNumber(tx)) => {
let _ = tx.send(Ok(finalized_number));
}
);
assert_matches!(
overseer.recv().await,
AllMessages::ChainApi(ChainApiMessage::FinalizedBlockHash(n, tx)) => {
assert_eq!(n, finalized_number);
let _ = tx.send(Ok(Some(finalized_hash)));
}
);
}
async fn answer_header_request(
overseer: &mut VirtualOverseer,
maybe_header: impl Into<Option<Header>>,
) {
assert_matches!(
overseer.recv().await,
AllMessages::ChainApi(ChainApiMessage::BlockHeader(hash, tx)) => {
let maybe_header = maybe_header.into();
assert!(maybe_header.as_ref().map_or(true, |h| h.hash() == hash));
let _ = tx.send(Ok(maybe_header));
}
)
}
async fn answer_weight_request(
overseer: &mut VirtualOverseer,
hash: Hash,
weight: impl Into<Option<BlockWeight>>,
) {
assert_matches!(
overseer.recv().await,
AllMessages::ChainApi(ChainApiMessage::BlockWeight(h, tx)) => {
assert_eq!(h, hash);
let _ = tx.send(Ok(weight.into()));
}
)
}
fn child_header(parent_number: BlockNumber, parent_hash: Hash) -> Header {
Header {
parent_hash,
number: parent_number + 1,
state_root: Default::default(),
extrinsics_root: Default::default(),
digest: Default::default(),
}
}
fn salt_header(header: &mut Header, salt: impl Encode) {
header.state_root = BlakeTwo256::hash_of(&salt)
}
fn add_reversions(header: &mut Header, reversions: impl IntoIterator<Item = BlockNumber>) {
for log in reversions.into_iter().map(ConsensusLog::Revert) {
header.digest.logs.push(log.into())
}
}
// Builds a chain on top of the given base, with one block for each
// provided weight.
fn construct_chain_on_base(
weights: impl IntoIterator<Item = BlockWeight>,
base_number: BlockNumber,
base_hash: Hash,
mut mutate: impl FnMut(&mut Header),
) -> (Hash, Vec<(Header, BlockWeight)>) {
let mut parent_number = base_number;
let mut parent_hash = base_hash;
let mut chain = Vec::new();
for weight in weights {
let mut header = child_header(parent_number, parent_hash);
mutate(&mut header);
parent_number = header.number;
parent_hash = header.hash();
chain.push((header, weight));
}
(parent_hash, chain)
}
// import blocks 1-by-1. If `finalized_base` is supplied,
// it will be answered before the first block in `answers.
async fn import_blocks_into(
virtual_overseer: &mut VirtualOverseer,
backend: &TestBackend,
mut finalized_base: Option<(BlockNumber, Hash)>,
blocks: Vec<(Header, BlockWeight)>,
) {
for (header, weight) in blocks {
let (_, write_rx) = backend.await_next_write();
let hash = header.hash();
virtual_overseer
.send(
OverseerSignal::ActiveLeaves(ActiveLeavesUpdate::start_work(ActivatedLeaf {
hash,
number: header.number,
status: LeafStatus::Fresh,
span: Arc::new(jaeger::Span::Disabled),
}))
.into(),
)
.await;
if let Some((f_n, f_h)) = finalized_base.take() {
answer_finalized_block_info(virtual_overseer, f_n, f_h).await;
}
answer_header_request(virtual_overseer, header.clone()).await;
answer_weight_request(virtual_overseer, hash, weight).await;
write_rx.await.unwrap();
}
}
async fn import_chains_into_empty(
virtual_overseer: &mut VirtualOverseer,
backend: &TestBackend,
finalized_number: BlockNumber,
finalized_hash: Hash,
chains: Vec<Vec<(Header, BlockWeight)>>,
) {
for (i, chain) in chains.into_iter().enumerate() {
let finalized_base = Some((finalized_number, finalized_hash)).filter(|_| i == 0);
import_blocks_into(virtual_overseer, backend, finalized_base, chain).await;
}
}
// Import blocks all at once. This assumes that the ancestor is known/finalized
// but none of the other blocks.
// import blocks 1-by-1. If `finalized_base` is supplied,
// it will be answered before the first block.
//
// some pre-blocks may need to be supplied to answer ancestry requests
// that gather batches beyond the beginning of the new chain.
// pre-blocks are those already known by the subsystem, however,
// the subsystem has no way of knowin that until requesting ancestry.
async fn import_all_blocks_into(
virtual_overseer: &mut VirtualOverseer,
backend: &TestBackend,
finalized_base: Option<(BlockNumber, Hash)>,
pre_blocks: Vec<Header>,
blocks: Vec<(Header, BlockWeight)>,
) {
assert!(blocks.len() > 1, "gap only makes sense if importing multiple blocks");
let head = blocks.last().unwrap().0.clone();
let head_hash = head.hash();
let (_, write_rx) = backend.await_next_write();
virtual_overseer
.send(
OverseerSignal::ActiveLeaves(ActiveLeavesUpdate::start_work(ActivatedLeaf {
hash: head_hash,
number: head.number,
status: LeafStatus::Fresh,
span: Arc::new(jaeger::Span::Disabled),
}))
.into(),
)
.await;
if let Some((f_n, f_h)) = finalized_base {
answer_finalized_block_info(virtual_overseer, f_n, f_h).await;
}
// Head is always fetched first.
answer_header_request(virtual_overseer, head).await;
// Answer header and ancestry requests until the parent of head
// is imported.
{
let find_block_header = |expected_hash| {
pre_blocks
.iter()
.cloned()
.chain(blocks.iter().map(|(h, _)| h.clone()))
.find(|hdr| hdr.hash() == expected_hash)
.unwrap()
};
let mut behind_head = 0;
loop {
let nth_ancestor_of_head = |n: usize| {
// blocks: [d, e, f, head]
// pre: [a, b, c]
//
// [a, b, c, d, e, f, head]
// [6, 5, 4, 3, 2, 1, 0]
let new_ancestry_end = blocks.len() - 1;
if n > new_ancestry_end {
// [6, 5, 4] -> [2, 1, 0]
let n_in_pre = n - blocks.len();
let pre_blocks_end = pre_blocks.len() - 1;
pre_blocks[pre_blocks_end - n_in_pre].clone()
} else {
let blocks_end = blocks.len() - 1;
blocks[blocks_end - n].0.clone()
}
};
match virtual_overseer.recv().await {
AllMessages::ChainApi(ChainApiMessage::Ancestors {
hash: h,
k,
response_channel: tx,
}) => {
let prev_response = nth_ancestor_of_head(behind_head);
assert_eq!(h, prev_response.hash());
let _ = tx.send(Ok((0..k as usize)
.map(|n| n + behind_head + 1)
.map(nth_ancestor_of_head)
.map(|h| h.hash())
.collect()));
for _ in 0..k {
assert_matches!(
virtual_overseer.recv().await,
AllMessages::ChainApi(ChainApiMessage::BlockHeader(h, tx)) => {
let header = find_block_header(h);
let _ = tx.send(Ok(Some(header)));
}
)
}
behind_head = behind_head + k as usize;
},
AllMessages::ChainApi(ChainApiMessage::BlockHeader(h, tx)) => {
let header = find_block_header(h);
let _ = tx.send(Ok(Some(header)));
// Assuming that `determine_new_blocks` uses these
// instead of ancestry: 1.
behind_head += 1;
},
AllMessages::ChainApi(ChainApiMessage::BlockWeight(h, tx)) => {
let (_, weight) = blocks.iter().find(|(hdr, _)| hdr.hash() == h).unwrap();
let _ = tx.send(Ok(Some(*weight)));
// Last weight has been returned. Time to go.
if h == head_hash {
break
}
},
_ => panic!("unexpected message"),
}
}
}
write_rx.await.unwrap();
}
async fn finalize_block(
virtual_overseer: &mut VirtualOverseer,
backend: &TestBackend,
block_number: BlockNumber,
block_hash: Hash,
) {
let (_, write_rx) = backend.await_next_write();
virtual_overseer
.send(OverseerSignal::BlockFinalized(block_hash, block_number).into())
.await;
write_rx.await.unwrap();
}
fn extract_info_from_chain(
i: usize,
chain: &[(Header, BlockWeight)],
) -> (BlockNumber, Hash, BlockWeight) {
let &(ref header, weight) = &chain[i];
(header.number, header.hash(), weight)
}
fn assert_backend_contains<'a>(
backend: &TestBackend,
headers: impl IntoIterator<Item = &'a Header>,
) {
for header in headers {
let hash = header.hash();
assert!(
backend.load_blocks_by_number(header.number).unwrap().contains(&hash),
"blocks at {} does not contain {}",
header.number,
hash,
);
assert!(backend.load_block_entry(&hash).unwrap().is_some(), "no entry found for {}", hash);
}
}
fn assert_backend_contains_chains(backend: &TestBackend, chains: Vec<Vec<(Header, BlockWeight)>>) {
for chain in chains {
assert_backend_contains(backend, chain.iter().map(|&(ref hdr, _)| hdr))
}
}
fn assert_leaves(backend: &TestBackend, leaves: Vec<Hash>) {
assert_eq!(
backend
.load_leaves()
.unwrap()
.into_hashes_descending()
.into_iter()
.collect::<Vec<_>>(),
leaves,
);
}
async fn assert_leaves_query(virtual_overseer: &mut VirtualOverseer, leaves: Vec<Hash>) {
assert!(!leaves.is_empty(), "empty leaves impossible. answer finalized query");
let (tx, rx) = oneshot::channel();
virtual_overseer
.send(FromOrchestra::Communication { msg: ChainSelectionMessage::Leaves(tx) })
.await;
assert_eq!(rx.await.unwrap(), leaves);
}
async fn assert_finalized_leaves_query(
virtual_overseer: &mut VirtualOverseer,
finalized_number: BlockNumber,
finalized_hash: Hash,
) {
let (tx, rx) = oneshot::channel();
virtual_overseer
.send(FromOrchestra::Communication { msg: ChainSelectionMessage::Leaves(tx) })
.await;
answer_finalized_block_info(virtual_overseer, finalized_number, finalized_hash).await;
assert_eq!(rx.await.unwrap(), vec![finalized_hash]);
}
async fn best_leaf_containing(
virtual_overseer: &mut VirtualOverseer,
required: Hash,
) -> Option<Hash> {
let (tx, rx) = oneshot::channel();
virtual_overseer
.send(FromOrchestra::Communication {
msg: ChainSelectionMessage::BestLeafContaining(required, tx),
})
.await;
rx.await.unwrap()
}
async fn approve_block(
virtual_overseer: &mut VirtualOverseer,
backend: &TestBackend,
approved: Hash,
) {
let (_, write_rx) = backend.await_next_write();
virtual_overseer
.send(FromOrchestra::Communication { msg: ChainSelectionMessage::Approved(approved) })
.await;
write_rx.await.unwrap()
}
#[test]
fn no_op_subsystem_run() {
test_harness(|_, _, virtual_overseer| async move { virtual_overseer });
}
#[test]
fn import_direct_child_of_finalized_on_empty() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
let child = child_header(finalized_number, finalized_hash);
let child_hash = child.hash();
let child_weight = 1;
let child_number = child.number;
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
vec![(child.clone(), child_weight)],
)
.await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), child_number);
assert_backend_contains(&backend, &[child]);
assert_leaves(&backend, vec![child_hash]);
assert_leaves_query(&mut virtual_overseer, vec![child_hash]).await;
virtual_overseer
})
}
#[test]
fn import_chain_on_finalized_incrementally() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
let (head_hash, chain) =
construct_chain_on_base(vec![1, 2, 3, 4, 5], finalized_number, finalized_hash, |_| {});
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain.clone(),
)
.await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), 1);
assert_backend_contains(&backend, chain.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![head_hash]);
assert_leaves_query(&mut virtual_overseer, vec![head_hash]).await;
virtual_overseer
})
}
#[test]
fn import_two_subtrees_on_finalized() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
let (a_hash, chain_a) =
construct_chain_on_base(vec![1], finalized_number, finalized_hash, |_| {});
let (b_hash, chain_b) =
construct_chain_on_base(vec![2], finalized_number, finalized_hash, |h| {
salt_header(h, b"b")
});
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
import_blocks_into(&mut virtual_overseer, &backend, None, chain_b.clone()).await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), 1);
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![b_hash, a_hash]);
assert_leaves_query(&mut virtual_overseer, vec![b_hash, a_hash]).await;
virtual_overseer
})
}
#[test]
fn import_two_subtrees_on_nonzero_finalized() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 100;
let finalized_hash = Hash::repeat_byte(0);
let (a_hash, chain_a) =
construct_chain_on_base(vec![1], finalized_number, finalized_hash, |_| {});
let (b_hash, chain_b) =
construct_chain_on_base(vec![2], finalized_number, finalized_hash, |h| {
salt_header(h, b"b")
});
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
import_blocks_into(&mut virtual_overseer, &backend, None, chain_b.clone()).await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), 101);
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![b_hash, a_hash]);
assert_leaves_query(&mut virtual_overseer, vec![b_hash, a_hash]).await;
virtual_overseer
})
}
#[test]
fn leaves_ordered_by_weight_and_then_number() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3
// A1 <- B2
// F <- C1 <- C2
//
// expected_leaves: [(C2, 3), (A3, 2), (B2, 2)]
let (a3_hash, chain_a) =
construct_chain_on_base(vec![1, 1, 2], finalized_number, finalized_hash, |_| {});
let (_, a1_hash, _) = extract_info_from_chain(0, &chain_a);
let (b2_hash, chain_b) =
construct_chain_on_base(vec![2], 1, a1_hash, |h| salt_header(h, b"b"));
let (c2_hash, chain_c) =
construct_chain_on_base(vec![1, 3], finalized_number, finalized_hash, |h| {
salt_header(h, b"c")
});
import_chains_into_empty(
&mut virtual_overseer,
&backend,
finalized_number,
finalized_hash,
vec![chain_a.clone(), chain_b.clone(), chain_c.clone()],
)
.await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), 1);
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_c.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![c2_hash, a3_hash, b2_hash]);
assert_leaves_query(&mut virtual_overseer, vec![c2_hash, a3_hash, b2_hash]).await;
virtual_overseer
});
}
#[test]
fn subtrees_imported_even_with_gaps() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3
// A2 <- B3 <- B4 <- B5
let (a3_hash, chain_a) =
construct_chain_on_base(vec![1, 2, 3], finalized_number, finalized_hash, |_| {});
let (_, a2_hash, _) = extract_info_from_chain(1, &chain_a);
let (b5_hash, chain_b) =
construct_chain_on_base(vec![4, 4, 5], 2, a2_hash, |h| salt_header(h, b"b"));
import_all_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
Vec::new(),
chain_a.clone(),
)
.await;
import_all_blocks_into(
&mut virtual_overseer,
&backend,
None,
vec![chain_a[0].0.clone(), chain_a[1].0.clone()],
chain_b.clone(),
)
.await;
assert_eq!(backend.load_first_block_number().unwrap().unwrap(), 1);
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![b5_hash, a3_hash]);
assert_leaves_query(&mut virtual_overseer, vec![b5_hash, a3_hash]).await;
virtual_overseer
});
}
#[test]
fn reversion_removes_viability_of_chain() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3.
//
// A3 reverts A1
let (_a3_hash, chain_a) =
construct_chain_on_base(vec![1, 2, 3], finalized_number, finalized_hash, |h| {
if h.number == 3 {
add_reversions(h, Some(1))
}
});
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![]);
assert_finalized_leaves_query(&mut virtual_overseer, finalized_number, finalized_hash)
.await;
virtual_overseer
});
}
#[test]
fn reversion_removes_viability_and_finds_ancestor_as_leaf() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3.
//
// A3 reverts A2
let (_a3_hash, chain_a) =
construct_chain_on_base(vec![1, 2, 3], finalized_number, finalized_hash, |h| {
if h.number == 3 {
add_reversions(h, Some(2))
}
});
let (_, a1_hash, _) = extract_info_from_chain(0, &chain_a);
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![a1_hash]);
assert_leaves_query(&mut virtual_overseer, vec![a1_hash]).await;
virtual_overseer
});
}
#[test]
fn ancestor_of_unviable_is_not_leaf_if_has_children() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3.
// A1 <- B2
//
// A3 reverts A2
let (a2_hash, chain_a) =
construct_chain_on_base(vec![1, 2], finalized_number, finalized_hash, |_| {});
let (_, a1_hash, _) = extract_info_from_chain(0, &chain_a);
let (_a3_hash, chain_a_ext) =
construct_chain_on_base(vec![3], 2, a2_hash, |h| add_reversions(h, Some(2)));
let (b2_hash, chain_b) =
construct_chain_on_base(vec![1], 1, a1_hash, |h| salt_header(h, b"b"));
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
import_blocks_into(&mut virtual_overseer, &backend, None, chain_b.clone()).await;
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![a2_hash, b2_hash]);
import_blocks_into(&mut virtual_overseer, &backend, None, chain_a_ext.clone()).await;
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_a_ext.iter().map(|&(ref h, _)| h));
assert_backend_contains(&backend, chain_b.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![b2_hash]);
assert_leaves_query(&mut virtual_overseer, vec![b2_hash]).await;
virtual_overseer
});
}
#[test]
fn self_and_future_reversions_are_ignored() {
test_harness(|backend, _, mut virtual_overseer| async move {
let finalized_number = 0;
let finalized_hash = Hash::repeat_byte(0);
// F <- A1 <- A2 <- A3.
//
// A3 reverts itself and future blocks. ignored.
let (a3_hash, chain_a) =
construct_chain_on_base(vec![1, 2, 3], finalized_number, finalized_hash, |h| {
if h.number == 3 {
add_reversions(h, vec![3, 4, 100])
}
});
import_blocks_into(
&mut virtual_overseer,
&backend,
Some((finalized_number, finalized_hash)),
chain_a.clone(),
)
.await;
assert_backend_contains(&backend, chain_a.iter().map(|&(ref h, _)| h));
assert_leaves(&backend, vec![a3_hash]);
assert_leaves_query(&mut virtual_overseer, vec![a3_hash]).await;