tests.rs 15.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2020 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 aux-schema of approval voting.

use super::*;
use polkadot_primitives::v1::Id as ParaId;

22
23
24
25
26
27
28
const DATA_COL: u32 = 0;
const NUM_COLUMNS: u32 = 1;

const TEST_CONFIG: Config = Config {
	col_data: DATA_COL,
};

29
30
31
32
33
34
pub(crate) fn write_stored_blocks(tx: &mut DBTransaction, range: StoredBlockRange) {
	tx.put_vec(
		DATA_COL,
		&STORED_BLOCKS_KEY[..],
		range.encode(),
	);
35
36
}

37
38
39
40
41
42
pub(crate) fn write_blocks_at_height(tx: &mut DBTransaction, height: BlockNumber, blocks: &[Hash]) {
	tx.put_vec(
		DATA_COL,
		&blocks_at_height_key(height)[..],
		blocks.encode(),
	);
43
44
}

45
46
47
48
49
50
51
pub(crate) fn write_block_entry(tx: &mut DBTransaction, block_hash: &Hash, entry: &BlockEntry) {
	tx.put_vec(
		DATA_COL,
		&block_entry_key(block_hash)[..],
		entry.encode(),
	);
}
52

53
54
55
56
57
58
pub(crate) fn write_candidate_entry(tx: &mut DBTransaction, candidate_hash: &CandidateHash, entry: &CandidateEntry) {
	tx.put_vec(
		DATA_COL,
		&candidate_entry_key(candidate_hash)[..],
		entry.encode(),
	);
59
60
61
62
63
64
65
66
}

fn make_bitvec(len: usize) -> BitVec<BitOrderLsb0, u8> {
	bitvec::bitvec![BitOrderLsb0, u8; 0; len]
}

fn make_block_entry(
	block_hash: Hash,
67
68
	parent_hash: Hash,
	block_number: BlockNumber,
69
70
71
72
	candidates: Vec<(CoreIndex, CandidateHash)>,
) -> BlockEntry {
	BlockEntry {
		block_hash,
73
74
		parent_hash,
		block_number,
75
		session: 1,
76
77
		slot: Slot::from(1),
		relay_vrf_story: [0u8; 32],
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		approved_bitfield: make_bitvec(candidates.len()),
		candidates,
		children: Vec::new(),
	}
}

fn make_candidate(para_id: ParaId, relay_parent: Hash) -> CandidateReceipt {
	let mut c = CandidateReceipt::default();

	c.descriptor.para_id = para_id;
	c.descriptor.relay_parent = relay_parent;

	c
}

#[test]
fn read_write() {
95
	let store = kvdb_memorydb::create(NUM_COLUMNS);
96
97
98
99
100
101
102
103
104
105

	let hash_a = Hash::repeat_byte(1);
	let hash_b = Hash::repeat_byte(2);
	let candidate_hash = CandidateHash(Hash::repeat_byte(3));

	let range = StoredBlockRange(10, 20);
	let at_height = vec![hash_a, hash_b];

	let block_entry = make_block_entry(
		hash_a,
106
107
		Default::default(),
		1,
108
109
110
111
112
113
114
115
116
117
118
		vec![(CoreIndex(0), candidate_hash)],
	);

	let candidate_entry = CandidateEntry {
		candidate: Default::default(),
		session: 5,
		block_assignments: vec![
			(hash_a, ApprovalEntry {
				tranches: Vec::new(),
				backing_group: GroupIndex(1),
				our_assignment: None,
119
				our_approval_sig: None,
120
121
122
123
124
125
126
				assignments: Default::default(),
				approved: false,
			})
		].into_iter().collect(),
		approvals: Default::default(),
	};

127
128
129
130
131
132
133
134
	let mut tx = DBTransaction::new();

	write_stored_blocks(&mut tx, range.clone());
	write_blocks_at_height(&mut tx, 1, &at_height);
	write_block_entry(&mut tx, &hash_a, &block_entry);
	write_candidate_entry(&mut tx, &candidate_hash, &candidate_entry);

	store.write(tx).unwrap();
135

136
137
138
139
140
141
142
	assert_eq!(load_stored_blocks(&store, &TEST_CONFIG).unwrap(), Some(range));
	assert_eq!(load_blocks_at_height(&store, &TEST_CONFIG, 1).unwrap(), at_height);
	assert_eq!(load_block_entry(&store, &TEST_CONFIG, &hash_a).unwrap(), Some(block_entry));
	assert_eq!(
		load_candidate_entry(&store, &TEST_CONFIG, &candidate_hash).unwrap(),
		Some(candidate_entry),
	);
143
144
145
146
147
148
149
150

	let delete_keys = vec![
		STORED_BLOCKS_KEY.to_vec(),
		blocks_at_height_key(1).to_vec(),
		block_entry_key(&hash_a).to_vec(),
		candidate_entry_key(&candidate_hash).to_vec(),
	];

151
152
153
154
155
156
	let mut tx = DBTransaction::new();
	for key in delete_keys {
		tx.delete(DATA_COL, &key[..]);
	}

	store.write(tx).unwrap();
157

158
159
160
161
	assert!(load_stored_blocks(&store, &TEST_CONFIG).unwrap().is_none());
	assert!(load_blocks_at_height(&store, &TEST_CONFIG, 1).unwrap().is_empty());
	assert!(load_block_entry(&store, &TEST_CONFIG, &hash_a).unwrap().is_none());
	assert!(load_candidate_entry(&store, &TEST_CONFIG, &candidate_hash).unwrap().is_none());
162
163
164
165
}

#[test]
fn add_block_entry_works() {
166
	let store = kvdb_memorydb::create(NUM_COLUMNS);
167
168
169
170
171
172
173
174

	let parent_hash = Hash::repeat_byte(1);
	let block_hash_a = Hash::repeat_byte(2);
	let block_hash_b = Hash::repeat_byte(69);

	let candidate_hash_a = CandidateHash(Hash::repeat_byte(3));
	let candidate_hash_b = CandidateHash(Hash::repeat_byte(4));

175
176
	let block_number = 10;

177
178
	let block_entry_a = make_block_entry(
		block_hash_a,
179
180
		parent_hash,
		block_number,
181
182
183
184
185
		vec![(CoreIndex(0), candidate_hash_a)],
	);

	let block_entry_b = make_block_entry(
		block_hash_b,
186
187
		parent_hash,
		block_number,
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		vec![(CoreIndex(0), candidate_hash_a), (CoreIndex(1), candidate_hash_b)],
	);

	let n_validators = 10;

	let mut new_candidate_info = HashMap::new();
	new_candidate_info.insert(candidate_hash_a, NewCandidateInfo {
		candidate: make_candidate(1.into(), parent_hash),
		backing_group: GroupIndex(0),
		our_assignment: None,
	});

	add_block_entry(
		&store,
202
		&TEST_CONFIG,
203
204
205
206
207
208
209
210
211
212
213
214
215
		block_entry_a.clone(),
		n_validators,
		|h| new_candidate_info.get(h).map(|x| x.clone()),
	).unwrap();

	new_candidate_info.insert(candidate_hash_b, NewCandidateInfo {
		candidate: make_candidate(2.into(), parent_hash),
		backing_group: GroupIndex(1),
		our_assignment: None,
	});

	add_block_entry(
		&store,
216
		&TEST_CONFIG,
217
218
219
220
221
		block_entry_b.clone(),
		n_validators,
		|h| new_candidate_info.get(h).map(|x| x.clone()),
	).unwrap();

222
223
	assert_eq!(load_block_entry(&store, &TEST_CONFIG, &block_hash_a).unwrap(), Some(block_entry_a));
	assert_eq!(load_block_entry(&store, &TEST_CONFIG, &block_hash_b).unwrap(), Some(block_entry_b));
224

225
226
	let candidate_entry_a = load_candidate_entry(&store, &TEST_CONFIG, &candidate_hash_a)
		.unwrap().unwrap();
227
228
	assert_eq!(candidate_entry_a.block_assignments.keys().collect::<Vec<_>>(), vec![&block_hash_a, &block_hash_b]);

229
230
	let candidate_entry_b = load_candidate_entry(&store, &TEST_CONFIG, &candidate_hash_b)
		.unwrap().unwrap();
231
232
233
234
235
	assert_eq!(candidate_entry_b.block_assignments.keys().collect::<Vec<_>>(), vec![&block_hash_b]);
}

#[test]
fn add_block_entry_adds_child() {
236
	let store = kvdb_memorydb::create(NUM_COLUMNS);
237
238
239
240
241
242
243

	let parent_hash = Hash::repeat_byte(1);
	let block_hash_a = Hash::repeat_byte(2);
	let block_hash_b = Hash::repeat_byte(69);

	let mut block_entry_a = make_block_entry(
		block_hash_a,
244
245
		parent_hash,
		1,
246
247
248
249
250
		Vec::new(),
	);

	let block_entry_b = make_block_entry(
		block_hash_b,
251
252
		block_hash_a,
		2,
253
254
255
256
257
258
259
		Vec::new(),
	);

	let n_validators = 10;

	add_block_entry(
		&store,
260
		&TEST_CONFIG,
261
262
263
264
265
266
267
		block_entry_a.clone(),
		n_validators,
		|_| None,
	).unwrap();

	add_block_entry(
		&store,
268
		&TEST_CONFIG,
269
270
271
272
273
274
275
		block_entry_b.clone(),
		n_validators,
		|_| None,
	).unwrap();

	block_entry_a.children.push(block_hash_b);

276
277
	assert_eq!(load_block_entry(&store, &TEST_CONFIG, &block_hash_a).unwrap(), Some(block_entry_a));
	assert_eq!(load_block_entry(&store, &TEST_CONFIG, &block_hash_b).unwrap(), Some(block_entry_b));
278
279
280
281
}

#[test]
fn canonicalize_works() {
282
	let store = kvdb_memorydb::create(NUM_COLUMNS);
283
284
285
286

	//   -> B1 -> C1 -> D1
	// A -> B2 -> C2 -> D2
	//
287
	// We'll canonicalize C1. Everytning except D1 should disappear.
288
289
290
291
292
293
294
295
296
297
298
	//
	// Candidates:
	// Cand1 in B2
	// Cand2 in C2
	// Cand3 in C2 and D1
	// Cand4 in D1
	// Cand5 in D2
	// Only Cand3 and Cand4 should remain after canonicalize.

	let n_validators = 10;

299
300
301
	let mut tx = DBTransaction::new();
	write_stored_blocks(&mut tx, StoredBlockRange(1, 5));
	store.write(tx).unwrap();
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	let genesis = Hash::repeat_byte(0);

	let block_hash_a = Hash::repeat_byte(1);
	let block_hash_b1 = Hash::repeat_byte(2);
	let block_hash_b2 = Hash::repeat_byte(3);
	let block_hash_c1 = Hash::repeat_byte(4);
	let block_hash_c2 = Hash::repeat_byte(5);
	let block_hash_d1 = Hash::repeat_byte(6);
	let block_hash_d2 = Hash::repeat_byte(7);

	let cand_hash_1 = CandidateHash(Hash::repeat_byte(10));
	let cand_hash_2 = CandidateHash(Hash::repeat_byte(11));
	let cand_hash_3 = CandidateHash(Hash::repeat_byte(12));
	let cand_hash_4 = CandidateHash(Hash::repeat_byte(13));
	let cand_hash_5 = CandidateHash(Hash::repeat_byte(15));

319
320
321
322
323
324
325
326
327
	let block_entry_a = make_block_entry(block_hash_a, genesis, 1, Vec::new());
	let block_entry_b1 = make_block_entry(block_hash_b1, block_hash_a, 2, Vec::new());
	let block_entry_b2 = make_block_entry(
		block_hash_b2,
		block_hash_a,
		2,
		vec![(CoreIndex(0), cand_hash_1)],
	);
	let block_entry_c1 = make_block_entry(block_hash_c1, block_hash_b1, 3, Vec::new());
328
329
	let block_entry_c2 = make_block_entry(
		block_hash_c2,
330
331
		block_hash_b2,
		3,
332
333
334
335
		vec![(CoreIndex(0), cand_hash_2), (CoreIndex(1), cand_hash_3)],
	);
	let block_entry_d1 = make_block_entry(
		block_hash_d1,
336
337
		block_hash_c1,
		4,
338
339
		vec![(CoreIndex(0), cand_hash_3), (CoreIndex(1), cand_hash_4)],
	);
340
341
342
343
344
345
	let block_entry_d2 = make_block_entry(
		block_hash_d2,
		block_hash_c2,
		4,
		vec![(CoreIndex(0), cand_hash_5)],
	);
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


	let candidate_info = {
		let mut candidate_info = HashMap::new();
		candidate_info.insert(cand_hash_1, NewCandidateInfo {
			candidate: make_candidate(1.into(), genesis),
			backing_group: GroupIndex(1),
			our_assignment: None,
		});

		candidate_info.insert(cand_hash_2, NewCandidateInfo {
			candidate: make_candidate(2.into(), block_hash_a),
			backing_group: GroupIndex(2),
			our_assignment: None,
		});

		candidate_info.insert(cand_hash_3, NewCandidateInfo {
			candidate: make_candidate(3.into(), block_hash_a),
			backing_group: GroupIndex(3),
			our_assignment: None,
		});

		candidate_info.insert(cand_hash_4, NewCandidateInfo {
			candidate: make_candidate(4.into(), block_hash_b1),
			backing_group: GroupIndex(4),
			our_assignment: None,
		});

		candidate_info.insert(cand_hash_5, NewCandidateInfo {
			candidate: make_candidate(5.into(), block_hash_c1),
			backing_group: GroupIndex(5),
			our_assignment: None,
		});

		candidate_info
	};

	// now insert all the blocks.
	let blocks = vec![
385
386
387
388
389
390
391
		block_entry_a.clone(),
		block_entry_b1.clone(),
		block_entry_b2.clone(),
		block_entry_c1.clone(),
		block_entry_c2.clone(),
		block_entry_d1.clone(),
		block_entry_d2.clone(),
392
393
	];

394
	for block_entry in blocks {
395
396
		add_block_entry(
			&store,
397
			&TEST_CONFIG,
398
399
400
401
402
403
404
405
406
407
			block_entry,
			n_validators,
			|h| candidate_info.get(h).map(|x| x.clone()),
		).unwrap();
	}

	let check_candidates_in_store = |expected: Vec<(CandidateHash, Option<Vec<_>>)>| {
		for (c_hash, in_blocks) in expected {
			let (entry, in_blocks) = match in_blocks {
				None => {
408
					assert!(load_candidate_entry(&store, &TEST_CONFIG, &c_hash).unwrap().is_none());
409
410
411
					continue
				}
				Some(i) => (
412
					load_candidate_entry(&store, &TEST_CONFIG, &c_hash).unwrap().unwrap(),
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
					i,
				),
			};

			assert_eq!(entry.block_assignments.len(), in_blocks.len());

			for x in in_blocks {
				assert!(entry.block_assignments.contains_key(&x));
			}
		}
	};

	let check_blocks_in_store = |expected: Vec<(Hash, Option<Vec<_>>)>| {
		for (hash, with_candidates) in expected {
			let (entry, with_candidates) = match with_candidates {
				None => {
429
					assert!(load_block_entry(&store, &TEST_CONFIG, &hash).unwrap().is_none());
430
431
432
					continue
				}
				Some(i) => (
433
					load_block_entry(&store, &TEST_CONFIG, &hash).unwrap().unwrap(),
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
					i,
				),
			};

			assert_eq!(entry.candidates.len(), with_candidates.len());

			for x in with_candidates {
				assert!(entry.candidates.iter().position(|&(_, ref c)| c == &x).is_some());
			}
		}
	};

	check_candidates_in_store(vec![
		(cand_hash_1, Some(vec![block_hash_b2])),
		(cand_hash_2, Some(vec![block_hash_c2])),
		(cand_hash_3, Some(vec![block_hash_c2, block_hash_d1])),
		(cand_hash_4, Some(vec![block_hash_d1])),
		(cand_hash_5, Some(vec![block_hash_d2])),
	]);

	check_blocks_in_store(vec![
		(block_hash_a, Some(vec![])),
		(block_hash_b1, Some(vec![])),
		(block_hash_b2, Some(vec![cand_hash_1])),
		(block_hash_c1, Some(vec![])),
		(block_hash_c2, Some(vec![cand_hash_2, cand_hash_3])),
		(block_hash_d1, Some(vec![cand_hash_3, cand_hash_4])),
		(block_hash_d2, Some(vec![cand_hash_5])),
	]);

464
	canonicalize(&store, &TEST_CONFIG, 3, block_hash_c1).unwrap();
465

466
	assert_eq!(load_stored_blocks(&store, &TEST_CONFIG).unwrap().unwrap(), StoredBlockRange(4, 5));
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

	check_candidates_in_store(vec![
		(cand_hash_1, None),
		(cand_hash_2, None),
		(cand_hash_3, Some(vec![block_hash_d1])),
		(cand_hash_4, Some(vec![block_hash_d1])),
		(cand_hash_5, None),
	]);

	check_blocks_in_store(vec![
		(block_hash_a, None),
		(block_hash_b1, None),
		(block_hash_b2, None),
		(block_hash_c1, None),
		(block_hash_c2, None),
		(block_hash_d1, Some(vec![cand_hash_3, cand_hash_4])),
		(block_hash_d2, None),
	]);
}
486
487
488

#[test]
fn force_approve_works() {
489
	let store = kvdb_memorydb::create(NUM_COLUMNS);
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
	let n_validators = 10;

	let mut tx = DBTransaction::new();
	write_stored_blocks(&mut tx, StoredBlockRange(1, 4));
	store.write(tx).unwrap();

	let candidate_hash = CandidateHash(Hash::repeat_byte(42));
	let single_candidate_vec = vec![(CoreIndex(0), candidate_hash)];
	let candidate_info = {
		let mut candidate_info = HashMap::new();
		candidate_info.insert(candidate_hash, NewCandidateInfo {
			candidate: make_candidate(1.into(), Default::default()),
			backing_group: GroupIndex(1),
			our_assignment: None,
		});

		candidate_info
	};


	let block_hash_a = Hash::repeat_byte(1); // 1
	let block_hash_b = Hash::repeat_byte(2);
	let block_hash_c = Hash::repeat_byte(3);
	let block_hash_d = Hash::repeat_byte(4); // 4

	let block_entry_a = make_block_entry(block_hash_a, Default::default(), 1, single_candidate_vec.clone());
	let block_entry_b = make_block_entry(block_hash_b, block_hash_a, 2, single_candidate_vec.clone());
	let block_entry_c = make_block_entry(block_hash_c, block_hash_b, 3, single_candidate_vec.clone());
	let block_entry_d = make_block_entry(block_hash_d, block_hash_c, 4, single_candidate_vec.clone());

	let blocks = vec![
		block_entry_a.clone(),
		block_entry_b.clone(),
		block_entry_c.clone(),
		block_entry_d.clone(),
	];

	for block_entry in blocks {
		add_block_entry(
			&store,
530
			&TEST_CONFIG,
531
532
533
534
535
536
			block_entry,
			n_validators,
			|h| candidate_info.get(h).map(|x| x.clone()),
		).unwrap();
	}

537
	let approved_hashes = force_approve(&store, TEST_CONFIG,  block_hash_d, 2).unwrap();
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

	assert!(load_block_entry(
		&store,
		&TEST_CONFIG,
		&block_hash_a,
	).unwrap().unwrap().approved_bitfield.all());
	assert!(load_block_entry(
		&store,
		&TEST_CONFIG,
		&block_hash_b,
	).unwrap().unwrap().approved_bitfield.all());
	assert!(load_block_entry(
		&store,
		&TEST_CONFIG,
		&block_hash_c,
	).unwrap().unwrap().approved_bitfield.not_any());
	assert!(load_block_entry(
		&store,
		&TEST_CONFIG,
		&block_hash_d,
	).unwrap().unwrap().approved_bitfield.not_any());
559
560
561
562
	assert_eq!(
		approved_hashes,
		vec![block_hash_b, block_hash_a],
	);
563
564
565
566
567
}

#[test]
fn load_all_blocks_works() {
	let store = kvdb_memorydb::create(NUM_COLUMNS);
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
	let parent_hash = Hash::repeat_byte(1);
	let block_hash_a = Hash::repeat_byte(2);
	let block_hash_b = Hash::repeat_byte(69);
	let block_hash_c = Hash::repeat_byte(42);

	let block_number = 10;

	let block_entry_a = make_block_entry(
		block_hash_a,
		parent_hash,
		block_number,
		vec![],
	);

	let block_entry_b = make_block_entry(
		block_hash_b,
		parent_hash,
		block_number,
		vec![],
	);

	let block_entry_c = make_block_entry(
		block_hash_c,
		block_hash_a,
		block_number + 1,
		vec![],
	);

	let n_validators = 10;

	add_block_entry(
		&store,
		&TEST_CONFIG,
		block_entry_a.clone(),
		n_validators,
		|_| None
	).unwrap();

	// add C before B to test sorting.
	add_block_entry(
		&store,
		&TEST_CONFIG,
		block_entry_c.clone(),
		n_validators,
		|_| None
	).unwrap();

	add_block_entry(
		&store,
		&TEST_CONFIG,
		block_entry_b.clone(),
		n_validators,
		|_| None
	).unwrap();

	assert_eq!(
		load_all_blocks(
			&store,
			&TEST_CONFIG
		).unwrap(),
		vec![block_hash_a, block_hash_b, block_hash_c],
	)
631
}