mod.rs 71.9 KiB
Newer Older

	#[test]
	fn collator_signature_payload_is_valid() {
		// if this fails, collator signature verification code has to be updated.
		let h = Hash::default();
		assert_eq!(h.as_ref().len(), 32);

		let _payload = collator_signature_payload(
			&Hash::repeat_byte(1),
			&5u32.into(),
			&Hash::repeat_byte(2),
			&Hash::repeat_byte(3),
			&Hash::repeat_byte(4).into(),
		);
	}

	#[test]
	fn test_byzantine_threshold() {
		assert_eq!(byzantine_threshold(0), 0);
		assert_eq!(byzantine_threshold(1), 0);
		assert_eq!(byzantine_threshold(2), 0);
		assert_eq!(byzantine_threshold(3), 0);
		assert_eq!(byzantine_threshold(4), 1);
		assert_eq!(byzantine_threshold(5), 1);
		assert_eq!(byzantine_threshold(6), 1);
		assert_eq!(byzantine_threshold(7), 2);
	}

	#[test]
	fn test_supermajority_threshold() {
		assert_eq!(supermajority_threshold(0), 0);
		assert_eq!(supermajority_threshold(1), 1);
		assert_eq!(supermajority_threshold(2), 2);
		assert_eq!(supermajority_threshold(3), 3);
		assert_eq!(supermajority_threshold(4), 3);
		assert_eq!(supermajority_threshold(5), 4);
		assert_eq!(supermajority_threshold(6), 5);
		assert_eq!(supermajority_threshold(7), 5);
	}

	#[test]
	fn balance_bigger_than_usize() {
		let zero_b: Balance = 0;
		let zero_u: usize = 0;

		assert!(zero_b.leading_zeros() >= zero_u.leading_zeros());

	#[test]
	fn test_backed_candidate_injected_core_index() {
		let initial_validator_indices = bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1];
		let mut candidate = BackedCandidate::new(
			dummy_committed_candidate_receipt(),
			vec![],
			initial_validator_indices.clone(),
			None,
		);

		// No core index supplied, ElasticScalingMVP is off.
		let (validator_indices, core_index) = candidate.validator_indices_and_core_index(false);
		assert_eq!(validator_indices, initial_validator_indices.as_bitslice());
		assert!(core_index.is_none());

		// No core index supplied, ElasticScalingMVP is on. Still, decoding will be ok if backing
		// group size is <= 8, to give a chance to parachains that don't have multiple cores
		// assigned.
		let (validator_indices, core_index) = candidate.validator_indices_and_core_index(true);
		assert_eq!(validator_indices, initial_validator_indices.as_bitslice());
		assert!(core_index.is_none());

		let encoded_validator_indices = candidate.validator_indices.clone();
		candidate.set_validator_indices_and_core_index(validator_indices.into(), core_index);
		assert_eq!(candidate.validator_indices, encoded_validator_indices);

		// No core index supplied, ElasticScalingMVP is on. Decoding is corrupted if backing group
		// size larger than 8.
		let candidate = BackedCandidate::new(
			dummy_committed_candidate_receipt(),
			vec![],
			bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1, 0, 1, 0, 1, 0],
			None,
		);
		let (validator_indices, core_index) = candidate.validator_indices_and_core_index(true);
		assert_eq!(validator_indices, bitvec![u8, bitvec::order::Lsb0; 0].as_bitslice());
		assert!(core_index.is_some());

		// Core index supplied, ElasticScalingMVP is off. Core index will be treated as normal
		// validator indices. Runtime will check against this.
		let candidate = BackedCandidate::new(
			dummy_committed_candidate_receipt(),
			vec![],
			bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1],
			Some(CoreIndex(10)),
		);
		let (validator_indices, core_index) = candidate.validator_indices_and_core_index(false);
		assert_eq!(
			validator_indices,
			bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0]
		);
		assert!(core_index.is_none());

		// Core index supplied, ElasticScalingMVP is on.
		let mut candidate = BackedCandidate::new(
			dummy_committed_candidate_receipt(),
			vec![],
			bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1],
			Some(CoreIndex(10)),
		);
		let (validator_indices, core_index) = candidate.validator_indices_and_core_index(true);
		assert_eq!(validator_indices, bitvec![u8, bitvec::order::Lsb0; 0, 1, 0, 1]);
		assert_eq!(core_index, Some(CoreIndex(10)));

		let encoded_validator_indices = candidate.validator_indices.clone();
		candidate.set_validator_indices_and_core_index(validator_indices.into(), core_index);
		assert_eq!(candidate.validator_indices, encoded_validator_indices);
	}