soil-babe 0.2.0

BABE consensus algorithm for Soil
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
// This file is part of Soil.

// Copyright (C) Soil contributors.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! BABE testsuite

use super::*;
use authorship::claim_slot;
use soil_client::block_builder::{BlockBuilder, BlockBuilderBuilder};
use soil_client::client_api::{BlockchainEvents, Finalizer};
use soil_client::consensus::{NoNetwork as DummyOracle, Proposal, ProposeArgs};
use soil_client::import::{BoxBlockImport, BoxJustificationImport};
use soil_client::transaction_pool::RejectAllTxPool;
use soil_consensus::epochs::{EpochIdentifier, EpochIdentifierPosition};
use soil_consensus::slots::BackoffAuthoringOnFinalizedHeadLagging;
use soil_test::network::{Block as TestBlock, *};
use std::{cell::RefCell, task::Poll};
use subsoil::application_crypto::key_types::BABE;
use subsoil::consensus::babe::{
	inherents::{BabeCreateInherentDataProviders, InherentDataProvider},
	make_vrf_sign_data, AllowedSlots, AuthorityId, AuthorityPair, Slot,
};
use subsoil::consensus::slots::SlotDuration;
use subsoil::core::crypto::Pair;
use subsoil::keyring::Sr25519Keyring;
use subsoil::keystore::{testing::MemoryKeystore, Keystore};
use subsoil::runtime::{
	generic::{Digest, DigestItem},
	traits::Block as BlockT,
};
use soil_test_node_runtime_client::DefaultTestClientBuilderExt;

type Item = DigestItem;

type Error = soil_client::blockchain::Error;

type TestClient = soil_test_node_runtime_client::client::Client<
	soil_test_node_runtime_client::Backend,
	soil_test_node_runtime_client::ExecutorDispatch,
	TestBlock,
	soil_test_node_runtime_client::runtime::RuntimeApi,
>;

#[derive(Copy, Clone, PartialEq)]
enum Stage {
	PreSeal,
	PostSeal,
}

type Mutator = Arc<dyn Fn(&mut TestHeader, Stage) + Send + Sync>;

type BabeBlockImport = PanickingBlockImport<
	crate::BabeBlockImport<
		TestBlock,
		TestClient,
		Arc<TestClient>,
		BabeCreateInherentDataProviders<TestBlock>,
		soil_consensus::LongestChain<soil_test_node_runtime_client::Backend, Block>,
	>,
>;

const SLOT_DURATION_MS: u64 = 1000;

#[derive(Clone)]
struct DummyFactory {
	client: Arc<TestClient>,
	epoch_changes: SharedEpochChanges<TestBlock, Epoch>,
	mutator: Mutator,
}

struct DummyProposer {
	factory: DummyFactory,
	parent_hash: Hash,
}

impl Environment<TestBlock> for DummyFactory {
	type CreateProposer = future::Ready<Result<DummyProposer, Error>>;
	type Proposer = DummyProposer;
	type Error = Error;

	fn init(&mut self, parent_header: &<TestBlock as BlockT>::Header) -> Self::CreateProposer {
		future::ready(Ok(DummyProposer {
			factory: self.clone(),
			parent_hash: parent_header.hash(),
		}))
	}
}

impl DummyProposer {
	fn propose_with(
		&mut self,
		pre_digests: Digest,
	) -> future::Ready<Result<Proposal<TestBlock>, Error>> {
		let block_builder = BlockBuilderBuilder::new(&*self.factory.client)
			.on_parent_block(self.parent_hash)
			.fetch_parent_block_number(&*self.factory.client)
			.unwrap()
			.with_inherent_digests(pre_digests)
			.build()
			.unwrap();

		let mut block = match block_builder.build().map_err(|e| e.into()) {
			Ok(b) => b.block,
			Err(e) => return future::ready(Err(e)),
		};

		// mutate the block header according to the mutator.
		(self.factory.mutator)(&mut block.header, Stage::PreSeal);

		future::ready(Ok(Proposal { block, storage_changes: Default::default() }))
	}
}

impl Proposer<TestBlock> for DummyProposer {
	type Error = Error;
	type Proposal = future::Ready<Result<Proposal<TestBlock>, Error>>;

	fn propose(mut self, args: ProposeArgs<TestBlock>) -> Self::Proposal {
		self.propose_with(args.inherent_digests)
	}
}

thread_local! {
	static MUTATOR: RefCell<Mutator> = RefCell::new(Arc::new(|_, _|()));
}

#[derive(Clone)]
pub struct PanickingBlockImport<B>(B);

#[async_trait::async_trait]
impl<BI> BlockImport<TestBlock> for PanickingBlockImport<BI>
where
	BI: BlockImport<TestBlock> + Send + Sync,
{
	type Error = BI::Error;

	async fn import_block(
		&self,
		block: BlockImportParams<TestBlock>,
	) -> Result<ImportResult, Self::Error> {
		Ok(self.0.import_block(block).await.expect("importing block failed"))
	}

	async fn check_block(
		&self,
		block: BlockCheckParams<TestBlock>,
	) -> Result<ImportResult, Self::Error> {
		Ok(self.0.check_block(block).await.expect("checking block failed"))
	}
}

type BabePeer = Peer<Option<PeerData>, BabeBlockImport>;

#[derive(Default)]
pub struct BabeTestNet {
	peers: Vec<BabePeer>,
}

type TestHeader = <TestBlock as BlockT>::Header;

pub struct TestVerifier {
	inner: BabeVerifier<TestBlock, PeersFullClient>,
	mutator: Mutator,
}

#[async_trait::async_trait]
impl Verifier<TestBlock> for TestVerifier {
	/// Verify the given data and return the BlockImportParams and an optional
	/// new set of validators to import. If not, err with an Error-Message
	/// presented to the User in the logs.
	async fn verify(
		&self,
		mut block: BlockImportParams<TestBlock>,
	) -> Result<BlockImportParams<TestBlock>, String> {
		// apply post-sealing mutations (i.e. stripping seal, if desired).
		(self.mutator)(&mut block.header, Stage::PostSeal);
		self.inner.verify(block).await
	}
}

pub struct PeerData {
	link: BabeLink<TestBlock>,
	block_import: Mutex<Option<BoxBlockImport<TestBlock>>>,
}

impl TestNetFactory for BabeTestNet {
	type Verifier = TestVerifier;
	type PeerData = Option<PeerData>;
	type BlockImport = BabeBlockImport;

	fn make_block_import(
		&self,
		client: PeersClient,
	) -> (
		BlockImportAdapter<Self::BlockImport>,
		Option<BoxJustificationImport<Block>>,
		Option<PeerData>,
	) {
		let client = client.as_client();

		let config = crate::configuration(&*client).expect("config available");
		let (_, longest_chain) = TestClientBuilder::new().build_with_longest_chain();
		let (block_import, link) = crate::block_import(
			config,
			client.clone(),
			client.clone(),
			Arc::new(move |_, _| async {
				let timestamp = subsoil::timestamp::InherentDataProvider::from_system_time();
				let slot = InherentDataProvider::from_timestamp_and_slot_duration(
					*timestamp,
					SlotDuration::from_millis(SLOT_DURATION_MS),
				);
				Ok((slot, timestamp))
			}) as BabeCreateInherentDataProviders<TestBlock>,
			longest_chain,
			OffchainTransactionPoolFactory::new(RejectAllTxPool::default()),
		)
		.expect("can initialize block-import");

		let block_import = PanickingBlockImport(block_import);

		let data_block_import =
			Mutex::new(Some(Box::new(block_import.clone()) as BoxBlockImport<_>));
		(
			BlockImportAdapter::new(block_import),
			None,
			Some(PeerData { link, block_import: data_block_import }),
		)
	}

	fn make_verifier(&self, client: PeersClient, maybe_link: &Option<PeerData>) -> Self::Verifier {
		let client = client.as_client();
		trace!(target: LOG_TARGET, "Creating a verifier");

		// ensure block import and verifier are linked correctly.
		let data = maybe_link
			.as_ref()
			.expect("babe link always provided to verifier instantiation");

		TestVerifier {
			inner: BabeVerifier {
				client: client.clone(),
				slot_duration: SlotDuration::from_millis(SLOT_DURATION_MS),
				config: data.link.config.clone(),
				epoch_changes: data.link.epoch_changes.clone(),
				telemetry: None,
			},
			mutator: MUTATOR.with(|m| m.borrow().clone()),
		}
	}

	fn peer(&mut self, i: usize) -> &mut BabePeer {
		trace!(target: LOG_TARGET, "Retrieving a peer");
		&mut self.peers[i]
	}

	fn peers(&self) -> &Vec<BabePeer> {
		trace!(target: LOG_TARGET, "Retrieving peers");
		&self.peers
	}

	fn peers_mut(&mut self) -> &mut Vec<BabePeer> {
		trace!(target: "babe", "Retrieving peers, mutable");
		&mut self.peers
	}

	fn mut_peers<F: FnOnce(&mut Vec<BabePeer>)>(&mut self, closure: F) {
		closure(&mut self.peers);
	}
}

#[tokio::test]
#[should_panic(expected = "No BABE pre-runtime digest found")]
async fn rejects_empty_block() {
	subsoil::tracing::try_init_simple();
	let mut net = BabeTestNet::new(3);
	let block_builder = |builder: BlockBuilder<_, _>| builder.build().unwrap().block;
	net.mut_peers(|peer| {
		peer[0].generate_blocks(1, BlockOrigin::NetworkInitialSync, block_builder);
	})
}

fn create_keystore(authority: Sr25519Keyring) -> KeystorePtr {
	let keystore = MemoryKeystore::new();
	keystore
		.sr25519_generate_new(BABE, Some(&authority.to_seed()))
		.expect("Generates authority key");
	keystore.into()
}

async fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static) {
	subsoil::tracing::try_init_simple();
	let mutator = Arc::new(mutator) as Mutator;

	MUTATOR.with(|m| *m.borrow_mut() = mutator.clone());

	let net = BabeTestNet::new(3);

	let peers = [Sr25519Keyring::Alice, Sr25519Keyring::Bob, Sr25519Keyring::Charlie];

	let net = Arc::new(Mutex::new(net));
	let mut import_notifications = Vec::new();
	let mut babe_futures = Vec::new();

	for (peer_id, auth_id) in peers.iter().enumerate() {
		let mut net = net.lock();
		let peer = net.peer(peer_id);
		let client = peer.client().as_client();
		let select_chain = peer.select_chain().expect("Full client has select_chain");

		let keystore = create_keystore(*auth_id);

		let mut got_own = false;
		let mut got_other = false;

		let data = peer.data.as_ref().expect("babe link set up during initialization");

		let environ = DummyFactory {
			client: client.clone(),
			epoch_changes: data.link.epoch_changes.clone(),
			mutator: mutator.clone(),
		};

		import_notifications.push(
			// run each future until we get one of our own blocks with number higher than 5
			// that was produced locally.
			client
				.import_notification_stream()
				.take_while(move |n| {
					future::ready(
						n.header.number() < &5 || {
							if n.origin == BlockOrigin::Own {
								got_own = true;
							} else {
								got_other = true;
							}

							// continue until we have at least one block of our own
							// and one of another peer.
							!(got_own && got_other)
						},
					)
				})
				.for_each(|_| future::ready(())),
		);

		let client_clone = client.clone();
		babe_futures.push(
			start_babe(BabeParams {
				block_import: data.block_import.lock().take().expect("import set up during init"),
				select_chain,
				client,
				env: environ,
				sync_oracle: DummyOracle,
				create_inherent_data_providers: Box::new(move |parent, _| {
					// Get the slot of the parent header and just increase this slot.
					//
					// Below we will running everything in one big future. If we would use
					// time based slot, it can happen that on babe instance imports a block from
					// another babe instance and then tries to build a block in the same slot making
					// this test fail.
					let parent_header = client_clone.header(parent).ok().flatten().unwrap();
					let slot = Slot::from(
						find_pre_digest::<TestBlock>(&parent_header).unwrap().slot() + 1,
					);

					async move { Ok((InherentDataProvider::new(slot),)) }
				}),
				force_authoring: false,
				backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()),
				babe_link: data.link.clone(),
				keystore,
				justification_sync_link: (),
				block_proposal_slot_portion: SlotProportion::new(0.5),
				max_block_proposal_slot_portion: None,
				telemetry: None,
			})
			.expect("Starts babe"),
		);
	}
	future::select(
		futures::future::poll_fn(move |cx| {
			let mut net = net.lock();
			net.poll(cx);
			for p in net.peers() {
				if let Some((h, e)) = p.failed_verifications().into_iter().next() {
					panic!("Verification failed for {:?}: {}", h, e);
				}
			}

			Poll::<()>::Pending
		}),
		future::select(future::join_all(import_notifications), future::join_all(babe_futures)),
	)
	.await;
}

#[cfg(ignore_flaky_test)] // https://github.com/paritytech/polkadot-sdk/issues/48
#[tokio::test]
async fn authoring_blocks() {
	run_one_test(|_, _| ()).await;
}

#[tokio::test]
#[should_panic(expected = "importing block failed: Other(NoPreRuntimeDigest)")]
async fn rejects_missing_inherent_digest() {
	run_one_test(|header: &mut TestHeader, stage| {
		let v = std::mem::take(&mut header.digest_mut().logs);
		header.digest_mut().logs = v
			.into_iter()
			.filter(|v| stage == Stage::PostSeal || v.as_babe_pre_digest().is_none())
			.collect()
	})
	.await;
}

#[cfg(ignore_flaky_test)] // https://github.com/paritytech/polkadot-sdk/issues/48
#[tokio::test]
#[should_panic(expected = "has a bad seal")]
async fn rejects_missing_seals() {
	run_one_test(|header: &mut TestHeader, stage| {
		let v = std::mem::take(&mut header.digest_mut().logs);
		header.digest_mut().logs = v
			.into_iter()
			.filter(|v| stage == Stage::PreSeal || v.as_babe_seal().is_none())
			.collect()
	})
	.await;
}

#[tokio::test]
#[should_panic(expected = "Expected epoch change to happen")]
async fn rejects_missing_consensus_digests() {
	run_one_test(|header: &mut TestHeader, stage| {
		let v = std::mem::take(&mut header.digest_mut().logs);
		header.digest_mut().logs = v
			.into_iter()
			.filter(|v| stage == Stage::PostSeal || v.as_next_epoch_descriptor().is_none())
			.collect()
	})
	.await;
}

#[test]
fn wrong_consensus_engine_id_rejected() {
	subsoil::tracing::try_init_simple();
	let sig = AuthorityPair::generate().0.sign(b"");
	let bad_seal: Item = DigestItem::Seal([0; 4], sig.to_vec());
	assert!(bad_seal.as_babe_pre_digest().is_none());
	assert!(bad_seal.as_babe_seal().is_none())
}

#[test]
fn malformed_pre_digest_rejected() {
	subsoil::tracing::try_init_simple();
	let bad_seal: Item = DigestItem::Seal(BABE_ENGINE_ID, [0; 64].to_vec());
	assert!(bad_seal.as_babe_pre_digest().is_none());
}

#[test]
fn sig_is_not_pre_digest() {
	subsoil::tracing::try_init_simple();
	let sig = AuthorityPair::generate().0.sign(b"");
	let bad_seal: Item = DigestItem::Seal(BABE_ENGINE_ID, sig.to_vec());
	assert!(bad_seal.as_babe_pre_digest().is_none());
	assert!(bad_seal.as_babe_seal().is_some())
}

#[test]
fn claim_epoch_slots() {
	// We don't require the full claim information, thus as a shorter alias we're
	// going to use a simple integer value. Generally not verbose enough, but good enough
	// to be used within the narrow scope of a single test.
	// 0 -> None (i.e. unable to claim the slot),
	// 1 -> Primary
	// 2 -> Secondary
	// 3 -> Secondary with VRF
	const EPOCH_DURATION: u64 = 10;

	let authority = Sr25519Keyring::Alice;
	let keystore = create_keystore(authority);

	let mut epoch: Epoch = subsoil::consensus::babe::Epoch {
		start_slot: 0.into(),
		authorities: vec![(authority.public().into(), 1)],
		randomness: [0; 32],
		epoch_index: 1,
		duration: EPOCH_DURATION,
		config: BabeEpochConfiguration {
			c: (3, 10),
			allowed_slots: AllowedSlots::PrimaryAndSecondaryPlainSlots,
		},
	}
	.into();

	let claim_slot_wrap = |s, e| match claim_slot(Slot::from(s as u64), &e, &keystore) {
		None => 0,
		Some((PreDigest::Primary(_), _)) => 1,
		Some((PreDigest::SecondaryPlain(_), _)) => 2,
		Some((PreDigest::SecondaryVRF(_), _)) => 3,
	};

	// With secondary mechanism we should be able to claim all slots.
	let claims: Vec<_> = (0..EPOCH_DURATION)
		.into_iter()
		.map(|slot| claim_slot_wrap(slot, epoch.clone()))
		.collect();
	assert_eq!(claims, [1, 2, 2, 1, 2, 2, 2, 2, 2, 1]);

	// With secondary with VRF mechanism we should be able to claim all the slots.
	epoch.config.allowed_slots = AllowedSlots::PrimaryAndSecondaryVRFSlots;
	let claims: Vec<_> = (0..EPOCH_DURATION)
		.into_iter()
		.map(|slot| claim_slot_wrap(slot, epoch.clone()))
		.collect();
	assert_eq!(claims, [1, 3, 3, 1, 3, 3, 3, 3, 3, 1]);

	// Otherwise with only vrf-based primary slots we are able to claim a subset of the slots.
	epoch.config.allowed_slots = AllowedSlots::PrimarySlots;
	let claims: Vec<_> = (0..EPOCH_DURATION)
		.into_iter()
		.map(|slot| claim_slot_wrap(slot, epoch.clone()))
		.collect();
	assert_eq!(claims, [1, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
}

#[test]
fn claim_vrf_check() {
	let authority = Sr25519Keyring::Alice;
	let keystore = create_keystore(authority);

	let public = authority.public();

	let epoch: Epoch = subsoil::consensus::babe::Epoch {
		start_slot: 0.into(),
		authorities: vec![(public.into(), 1)],
		randomness: [0; 32],
		epoch_index: 1,
		duration: 10,
		config: BabeEpochConfiguration {
			c: (3, 10),
			allowed_slots: AllowedSlots::PrimaryAndSecondaryVRFSlots,
		},
	}
	.into();

	// We leverage the predictability of claim types given a constant randomness.

	// We expect a Primary claim for slot 0

	let pre_digest = match claim_slot(0.into(), &epoch, &keystore).unwrap().0 {
		PreDigest::Primary(d) => d,
		v => panic!("Unexpected pre-digest variant {:?}", v),
	};
	let data = make_vrf_sign_data(&epoch.randomness.clone(), 0.into(), epoch.epoch_index);
	let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
	assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);

	// We expect a SecondaryVRF claim for slot 1
	let pre_digest = match claim_slot(1.into(), &epoch, &keystore).unwrap().0 {
		PreDigest::SecondaryVRF(d) => d,
		v => panic!("Unexpected pre-digest variant {:?}", v),
	};
	let data = make_vrf_sign_data(&epoch.randomness.clone(), 1.into(), epoch.epoch_index);
	let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
	assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);

	// Check that correct epoch index has been used if epochs are skipped (primary VRF)
	let slot = Slot::from(103);
	let claim = match claim_slot(slot, &epoch, &keystore).unwrap().0 {
		PreDigest::Primary(d) => d,
		v => panic!("Unexpected claim variant {:?}", v),
	};
	let fixed_epoch = epoch.clone_for_slot(slot);
	let data = make_vrf_sign_data(&epoch.randomness.clone(), slot, fixed_epoch.epoch_index);
	let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
	assert_eq!(fixed_epoch.epoch_index, 11);
	assert_eq!(claim.vrf_signature.pre_output, sign.pre_output);

	// Check that correct epoch index has been used if epochs are skipped (secondary VRF)
	let slot = Slot::from(100);
	let pre_digest = match claim_slot(slot, &epoch, &keystore).unwrap().0 {
		PreDigest::SecondaryVRF(d) => d,
		v => panic!("Unexpected claim variant {:?}", v),
	};
	let fixed_epoch = epoch.clone_for_slot(slot);
	let data = make_vrf_sign_data(&epoch.randomness.clone(), slot, fixed_epoch.epoch_index);
	let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
	assert_eq!(fixed_epoch.epoch_index, 11);
	assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);
}

// Propose and import a new BABE block on top of the given parent.
async fn propose_and_import_block(
	parent: &TestHeader,
	slot: Option<Slot>,
	proposer_factory: &mut DummyFactory,
	block_import: &mut BoxBlockImport<TestBlock>,
) -> Hash {
	let mut proposer = proposer_factory.init(parent).await.unwrap();

	let slot = slot.unwrap_or_else(|| {
		let parent_pre_digest = find_pre_digest::<TestBlock>(parent).unwrap();
		parent_pre_digest.slot() + 1
	});

	let pre_digest = subsoil::runtime::generic::Digest {
		logs: vec![Item::babe_pre_digest(PreDigest::SecondaryPlain(SecondaryPlainPreDigest {
			authority_index: 0,
			slot,
		}))],
	};

	let parent_hash = parent.hash();

	let mut block = proposer.propose_with(pre_digest).await.unwrap().block;

	let epoch_descriptor = proposer_factory
		.epoch_changes
		.shared_data()
		.epoch_descriptor_for_child_of(
			descendent_query(&*proposer_factory.client),
			&parent_hash,
			*parent.number(),
			slot,
		)
		.unwrap()
		.unwrap();

	let seal = {
		// sign the pre-sealed hash of the block and then
		// add it to a digest item.
		let pair = AuthorityPair::from_seed(&[1; 32]);
		let pre_hash = block.header.hash();
		let signature = pair.sign(pre_hash.as_ref());
		Item::babe_seal(signature)
	};

	let post_hash = {
		block.header.digest_mut().push(seal.clone());
		let h = block.header.hash();
		block.header.digest_mut().pop();
		h
	};

	let mut import = BlockImportParams::new(BlockOrigin::Own, block.header);
	import.post_digests.push(seal);
	import.body = Some(block.extrinsics);
	import
		.insert_intermediate(INTERMEDIATE_KEY, BabeIntermediate::<TestBlock> { epoch_descriptor });
	import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
	let import_result = block_import.import_block(import).await.unwrap();

	match import_result {
		ImportResult::Imported(_) => {},
		_ => panic!("expected block to be imported"),
	}

	post_hash
}

// Propose and import n valid BABE blocks that are built on top of the given parent.
// The proposer takes care of producing epoch change digests according to the epoch
// duration (which is set to 6 slots in the test runtime).
async fn propose_and_import_blocks(
	client: &PeersFullClient,
	proposer_factory: &mut DummyFactory,
	block_import: &mut BoxBlockImport<TestBlock>,
	parent_hash: Hash,
	n: usize,
) -> Vec<Hash> {
	let mut hashes = Vec::with_capacity(n);
	let mut parent_header = client.header(parent_hash).unwrap().unwrap();

	for _ in 0..n {
		let block_hash =
			propose_and_import_block(&parent_header, None, proposer_factory, block_import).await;
		hashes.push(block_hash);
		parent_header = client.header(block_hash).unwrap().unwrap();
	}

	hashes
}

#[tokio::test]
async fn importing_block_one_sets_genesis_epoch() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");
	let client = peer.client().as_client();

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let genesis_header = client.header(client.chain_info().genesis_hash).unwrap().unwrap();

	let block_hash = propose_and_import_block(
		&genesis_header,
		Some(999.into()),
		&mut proposer_factory,
		&mut block_import,
	)
	.await;

	let genesis_epoch = Epoch::genesis(&data.link.config, 999.into());

	let epoch_changes = data.link.epoch_changes.shared_data();
	let epoch_for_second_block = epoch_changes
		.epoch_data_for_child_of(descendent_query(&*client), &block_hash, 1, 1000.into(), |slot| {
			Epoch::genesis(&data.link.config, slot)
		})
		.unwrap()
		.unwrap();

	assert_eq!(epoch_for_second_block, genesis_epoch);
}

#[tokio::test]
async fn revert_prunes_epoch_changes_and_removes_weights() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let backend = peer.client().as_backend();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");
	let epoch_changes = data.link.epoch_changes.clone();

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	// Test scenario.
	// Information for epoch 19 is produced on three different forks at block #13.
	// One branch starts before the revert point (epoch data should be maintained).
	// One branch starts after the revert point (epoch data should be removed).
	//
	//                        *----------------- F(#13) --#18                  < fork #2
	//                       /
	// A(#1) ---- B(#7) ----#8----+-----#12----- C(#13) ---- D(#19) ------#21  < canon
	//   \                        ^       \
	//    \                    revert      *---- G(#13) ---- H(#19) ---#20     < fork #3
	//     \                   to #10
	//      *-----E(#7)---#11                                          < fork #1
	let canon = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		21,
	)
	.await;
	let fork1 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[0], 10)
			.await;
	let fork2 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[7], 10)
			.await;
	let fork3 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[11], 8)
			.await;

	// We should be tracking a total of 9 epochs in the fork tree
	assert_eq!(epoch_changes.shared_data().tree().iter().count(), 8);
	// And only one root
	assert_eq!(epoch_changes.shared_data().tree().roots().count(), 1);

	// Revert canon chain to block #10 (best(21) - 11)
	revert(client.clone(), backend, 11).expect("revert should work for baked test scenario");

	// Load and check epoch changes.

	let actual_nodes =
		aux_schema::load_epoch_changes::<Block, TestClient>(&*client, &data.link.config)
			.expect("load epoch changes")
			.shared_data()
			.tree()
			.iter()
			.map(|(h, _, _)| *h)
			.collect::<Vec<_>>();

	let expected_nodes = vec![
		canon[0], // A
		canon[6], // B
		fork2[4], // F
		fork1[5], // E
	];

	assert_eq!(actual_nodes, expected_nodes);

	let weight_data_check = |hashes: &[Hash], expected: bool| {
		hashes.iter().all(|hash| {
			aux_schema::load_block_weight(&*client, hash).unwrap().is_some() == expected
		})
	};
	assert!(weight_data_check(&canon[..10], true));
	assert!(weight_data_check(&canon[10..], false));
	assert!(weight_data_check(&fork1, true));
	assert!(weight_data_check(&fork2, true));
	assert!(weight_data_check(&fork3, false));
}

#[tokio::test]
async fn revert_not_allowed_for_finalized() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let backend = peer.client().as_backend();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let canon = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		3,
	)
	.await;

	// Finalize best block
	client.finalize_block(canon[2], None, false).unwrap();

	// Revert canon chain to last finalized block
	revert(client.clone(), backend, 100).expect("revert should work for baked test scenario");

	let weight_data_check = |hashes: &[Hash], expected: bool| {
		hashes.iter().all(|hash| {
			aux_schema::load_block_weight(&*client, hash).unwrap().is_some() == expected
		})
	};
	assert!(weight_data_check(&canon, true));
}

#[tokio::test]
async fn importing_epoch_change_block_prunes_tree() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");
	let epoch_changes = data.link.epoch_changes.clone();

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	// This is the block tree that we're going to use in this test. Each node
	// represents an epoch change block, the epoch duration is 6 slots.
	//
	//    *---- F (#7)
	//   /                 *------ G (#19) - H (#25)
	//  /                 /
	// A (#1) - B (#7) - C (#13) - D (#19) - E (#25)
	//                              \
	//                               *------ I (#25)

	// Create and import the canon chain and keep track of fork blocks (A, C, D)
	// from the diagram above.
	let canon = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		30,
	)
	.await;

	// Create the forks
	let fork_1 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[0], 10)
			.await;
	let fork_2 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[12], 15)
			.await;
	let fork_3 =
		propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, canon[18], 10)
			.await;

	// We should be tracking a total of 9 epochs in the fork tree
	assert_eq!(epoch_changes.shared_data().tree().iter().count(), 9);

	// And only one root
	assert_eq!(epoch_changes.shared_data().tree().roots().count(), 1);

	// We finalize block #13 from the canon chain, so on the next epoch
	// change the tree should be pruned, to not contain F (#7).
	client.finalize_block(canon[12], None, false).unwrap();
	propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().best_hash,
		7,
	)
	.await;

	let nodes: Vec<_> = epoch_changes.shared_data().tree().iter().map(|(h, _, _)| *h).collect();

	// no hashes from the first fork must exist on the tree
	assert!(!nodes.iter().any(|h| fork_1.contains(h)));

	// but the epoch changes from the other forks must still exist
	assert!(nodes.iter().any(|h| fork_2.contains(h)));
	assert!(nodes.iter().any(|h| fork_3.contains(h)));

	// finalizing block #25 from the canon chain should prune out the second fork
	client.finalize_block(canon[24], None, false).unwrap();
	propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().best_hash,
		8,
	)
	.await;

	let nodes: Vec<_> = epoch_changes.shared_data().tree().iter().map(|(h, _, _)| *h).collect();

	// no hashes from the other forks must exist on the tree
	assert!(!nodes.iter().any(|h| fork_2.contains(h)));
	assert!(!nodes.iter().any(|h| fork_3.contains(h)));

	// Check that we contain the nodes that we care about
	assert!(nodes.iter().any(|h| *h == canon[18]));
	assert!(nodes.iter().any(|h| *h == canon[24]));
}

#[tokio::test]
#[should_panic(expected = "Slot number must increase: parent slot: 999, this slot: 999")]
async fn verify_slots_are_strictly_increasing() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let genesis_header = client.header(client.chain_info().genesis_hash).unwrap().unwrap();

	// we should have no issue importing this block
	let b1 = propose_and_import_block(
		&genesis_header,
		Some(999.into()),
		&mut proposer_factory,
		&mut block_import,
	)
	.await;

	let b1 = client.header(b1).unwrap().unwrap();

	// we should fail to import this block since the slot number didn't increase.
	// we will panic due to the `PanickingBlockImport` defined above.
	propose_and_import_block(&b1, Some(999.into()), &mut proposer_factory, &mut block_import).await;
}

#[tokio::test]
async fn obsolete_blocks_aux_data_cleanup() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");
	let client = peer.client().as_client();

	// Register the handler (as done by `babe_start`)
	let client_clone = client.clone();
	let on_finality = move |summary: &FinalityNotification<TestBlock>| {
		aux_storage_cleanup(client_clone.as_ref(), summary)
	};
	client.register_finality_action(Box::new(on_finality));

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let aux_data_check = |hashes: &[Hash], expected: bool| {
		hashes.iter().all(|hash| {
			aux_schema::load_block_weight(&*peer.client().as_backend(), hash)
				.unwrap()
				.is_some() == expected
		})
	};

	// Create the following test scenario:
	//
	//  /--- --B3 --- B4                       ( < fork2 )
	// G --- A1 --- A2 --- A3 --- A4           ( < fork1 )
	//                      \-----C4 --- C5    ( < fork3 )

	let fork1_hashes = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		4,
	)
	.await;
	let fork2_hashes = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		2,
	)
	.await;
	let fork3_hashes = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		fork1_hashes[2],
		2,
	)
	.await;

	// Check that aux data is present for all but the genesis block.
	assert!(aux_data_check(&[client.chain_info().genesis_hash], false));
	assert!(aux_data_check(&fork1_hashes, true));
	assert!(aux_data_check(&fork2_hashes, true));
	assert!(aux_data_check(&fork3_hashes, true));

	// Finalize A3
	client.finalize_block(fork1_hashes[2], None, true).unwrap();

	// Wiped: A1, A2
	assert!(aux_data_check(&fork1_hashes[..2], false));
	// Present: A3, A4
	assert!(aux_data_check(&fork1_hashes[2..], true));
	// Wiped: B3, B4
	assert!(aux_data_check(&fork2_hashes, false));
	// Present C4, C5
	assert!(aux_data_check(&fork3_hashes, true));

	client.finalize_block(fork1_hashes[3], None, true).unwrap();

	// Wiped: A3
	assert!(aux_data_check(&fork1_hashes[2..3], false));
	// Present: A4
	assert!(aux_data_check(&fork1_hashes[3..], true));
	// Wiped C4, C5
	assert!(aux_data_check(&fork3_hashes, false));
}

#[tokio::test]
async fn allows_skipping_epochs() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let epoch_changes = data.link.epoch_changes.clone();
	let epoch_length = data.link.config.epoch_length;

	// we create all of the blocks in epoch 0 as well as a block in epoch 1
	let blocks = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		epoch_length as usize + 1,
	)
	.await;

	// the first block in epoch 0 (#1) announces both epoch 0 and 1 (this is a
	// special genesis epoch)
	let epoch0 = epoch_changes
		.shared_data()
		.epoch(&EpochIdentifier {
			position: EpochIdentifierPosition::Genesis0,
			hash: blocks[0],
			number: 1,
		})
		.unwrap()
		.clone();

	assert_eq!(epoch0.epoch_index, 0);
	assert_eq!(epoch0.start_slot, Slot::from(1));

	let epoch1 = epoch_changes
		.shared_data()
		.epoch(&EpochIdentifier {
			position: EpochIdentifierPosition::Genesis1,
			hash: blocks[0],
			number: 1,
		})
		.unwrap()
		.clone();

	assert_eq!(epoch1.epoch_index, 1);
	assert_eq!(epoch1.start_slot, Slot::from(epoch_length + 1));

	// the first block in epoch 1 (#7) announces epoch 2. we will be skipping
	// this epoch and therefore re-using its data for epoch 3
	let epoch2 = epoch_changes
		.shared_data()
		.epoch(&EpochIdentifier {
			position: EpochIdentifierPosition::Regular,
			hash: blocks[epoch_length as usize],
			number: epoch_length + 1,
		})
		.unwrap()
		.clone();

	assert_eq!(epoch2.epoch_index, 2);
	assert_eq!(epoch2.start_slot, Slot::from(epoch_length * 2 + 1));

	// we now author a block that belongs to epoch 3, thereby skipping epoch 2
	let last_block = client.expect_header(*blocks.last().unwrap()).unwrap();
	let block = propose_and_import_block(
		&last_block,
		Some((epoch_length * 3 + 1).into()),
		&mut proposer_factory,
		&mut block_import,
	)
	.await;

	// and the first block in epoch 3 (#8) announces epoch 4
	let epoch4 = epoch_changes
		.shared_data()
		.epoch(&EpochIdentifier {
			position: EpochIdentifierPosition::Regular,
			hash: block,
			number: epoch_length + 2,
		})
		.unwrap()
		.clone();

	assert_eq!(epoch4.epoch_index, 4);
	assert_eq!(epoch4.start_slot, Slot::from(epoch_length * 4 + 1));

	// if we try to get the epoch data for a slot in epoch 3
	let epoch3 = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&block,
			epoch_length + 2,
			(epoch_length * 3 + 2).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	// we get back the data for epoch 2
	assert_eq!(epoch3, epoch2);

	// but if we try to get the epoch data for a slot in epoch 4
	let epoch4_ = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&block,
			epoch_length + 2,
			(epoch_length * 4 + 1).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	// we get epoch 4 as expected
	assert_eq!(epoch4, epoch4_);
}

#[tokio::test]
async fn allows_skipping_epochs_on_some_forks() {
	let mut net = BabeTestNet::new(1);

	let peer = net.peer(0);
	let data = peer.data.as_ref().expect("babe link set up during initialization");

	let client = peer.client().as_client();
	let mut block_import = data.block_import.lock().take().expect("import set up during init");

	let mut proposer_factory = DummyFactory {
		client: client.clone(),
		epoch_changes: data.link.epoch_changes.clone(),
		mutator: Arc::new(|_, _| ()),
	};

	let epoch_changes = data.link.epoch_changes.clone();
	let epoch_length = data.link.config.epoch_length;

	// we create all of the blocks in epoch 0 as well as two blocks in epoch 1
	let blocks = propose_and_import_blocks(
		&client,
		&mut proposer_factory,
		&mut block_import,
		client.chain_info().genesis_hash,
		epoch_length as usize + 1,
	)
	.await;

	// we now author a block that belongs to epoch 2, built on top of the last
	// authored block in epoch 1.
	let last_block = client.expect_header(*blocks.last().unwrap()).unwrap();

	let epoch2_block = propose_and_import_block(
		&last_block,
		Some((epoch_length * 2 + 1).into()),
		&mut proposer_factory,
		&mut block_import,
	)
	.await;

	// if we try to get the epoch data for a slot in epoch 2, we get the data that
	// was previously announced when epoch 1 started
	let epoch2 = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&epoch2_block,
			epoch_length + 2,
			(epoch_length * 2 + 2).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	// we now author a block that belongs to epoch 3, built on top of the last
	// authored block in epoch 1. authoring this block means we're skipping epoch 2
	// entirely on this fork
	let epoch3_block = propose_and_import_block(
		&last_block,
		Some((epoch_length * 3 + 1).into()),
		&mut proposer_factory,
		&mut block_import,
	)
	.await;

	// if we try to get the epoch data for a slot in epoch 3
	let epoch3_ = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&epoch3_block,
			epoch_length + 2,
			(epoch_length * 3 + 2).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	// we get back the data for epoch 2
	assert_eq!(epoch3_, epoch2);

	// if we try to get the epoch data for a slot in epoch 4 in the fork
	// where we skipped epoch 2, we should get the epoch data for epoch 4
	// that was announced at the beginning of epoch 3
	let epoch_data = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&epoch3_block,
			epoch_length + 2,
			(epoch_length * 4 + 1).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	assert!(epoch_data != epoch3_);

	// if we try to get the epoch data for a slot in epoch 4 in the fork
	// where we didn't skip epoch 2, we should get back the data for epoch 3,
	// that was announced when epoch 2 started in that fork
	let epoch_data = epoch_changes
		.shared_data()
		.epoch_data_for_child_of(
			descendent_query(&*client),
			&epoch2_block,
			epoch_length + 2,
			(epoch_length * 4 + 1).into(),
			|slot| Epoch::genesis(&data.link.config, slot),
		)
		.unwrap()
		.unwrap();

	assert!(epoch_data != epoch3_);

	let epoch3 = epoch_changes
		.shared_data()
		.epoch(&EpochIdentifier {
			position: EpochIdentifierPosition::Regular,
			hash: epoch2_block,
			number: epoch_length + 2,
		})
		.unwrap()
		.clone();

	assert_eq!(epoch_data, epoch3);
}