Files
pezkuwi-subxt/polkadot/primitives/test-helpers/src/lib.rs
T
Bernhard Schuster 0f1a9fb1eb remove Default from CandidateDescriptor (#4484)
* remove Default from CandidateHash

* Apply suggestions from code review

Co-authored-by: Andronik Ordian <write@reusable.software>

* chore: fmt

* remove backed candidate default

* Partial migration away from CandidateReceipt::default

* Remove more CandidateReceipt defaults

* fmt

* Mostly remove CommittedCandidateReceipt default usage

* Remove CommittedCandidateReceipt

* Remove more Defaults from polakdot primitives v1 + fmt

* Remove more Default from polkadot primites v1

* WIP trying to get overseer example + tests to compile

* feat: add primitives test helpers

* reduce deps of helper

* update primitive helpers

* make candidate validation compile

* fixup cargo lock

* make av-store compile

* fixup disputes coordinator tests

* test: fixup backing

* test: fixup approval voting

* fixup bitfield signing

* test: fixup runtime-api

* test: fixup availability dist

* foxi[ pverseer test]

* remove some Defaults, remove bounds from `dummy`

All `fn dummy` in primitives need to be removed anyways.
This aids in the transition.

* it's a test helper, so always use std

* test: fixup parachains runtime tests

Excluding benches.

* fix keyring

* fix paras runtime properly, no more default

* Remove fn dummy() usage from approval voting

* Move TestCandidateBuilder out of av store to test helpers

* Make candidate validation tests pass

* Make most dispute coirdinator tests pass

* Make provisioner tests work

* Make availability recovery tests work with test helpers

* Update polkadot-collator-protocol tests

* Update statement distribution tests

* Update polkadot overseer examples and tests

* Derive default for validation code so we don't break unrelated things

* Make para runtime test pass (no bench)

* Some more work

* chore: cargo fmt

* cargo fix

* avoid some Default::default

* fixup dispute coordinator test

* remove unused crate deps

* remove Default::default wherever possible, replace by dummy_* for the most part

* chore: cargo fmt

* Remove some warnings

* Remove CommittedCandidateReceipt dummy

* Remove CandidateReceipt dummy

* Remove CandidateDescriptor dummy

* Remove commented out code

* Fix para runtime tests

* chore: nightly

* Some updates to the builder

* Dynamically adjust mock head data size

* Make dispute cooridinator tests work

* Fix test candidate_backing_reorders_votes work

* +nightly-2021-10-29 fmt

* Spelling and remove a default use in builder

* Various clean up

* More small updates

* fmt

* More small updates

* Doc comments for test helpers

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=kusama-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras_inherent --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/kusama/src/weights/runtime_parachains_paras_inherent.rs

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=polkadot-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras_inherent --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/polkadot/src/weights/runtime_parachains_paras_inherent.rs

* Update lib.rs

* review comments

* fix warnings

* fix test by using correct candidate receipt relay parent

Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: emostov <32168567+emostov@users.noreply.github.com>
Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Gavin Wood <gavin@parity.io>
2021-12-10 12:12:07 +00:00

227 lines
6.8 KiB
Rust

// 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/>.
#![forbid(unused_crate_dependencies)]
#![forbid(unused_extern_crates)]
//! A set of primitive constructors, to aid in crafting meaningful testcase while reducing repetition.
//!
//! Note that `dummy_` prefixed values are meant to be fillers, that should not matter, and will
//! contain randomness based data.
use polkadot_primitives::v1::{
CandidateCommitments, CandidateDescriptor, CandidateReceipt, CollatorId, CollatorSignature,
CommittedCandidateReceipt, Hash, HeadData, Id as ParaId, ValidationCode, ValidationCodeHash,
ValidatorId,
};
use sp_application_crypto::sr25519;
use sp_keyring::Sr25519Keyring;
use sp_runtime::generic::Digest;
/// Creates a candidate receipt with filler data.
pub fn dummy_candidate_receipt<H: AsRef<[u8]>>(relay_parent: H) -> CandidateReceipt<H> {
CandidateReceipt::<H> {
commitments_hash: dummy_candidate_commitments(dummy_head_data()).hash(),
descriptor: dummy_candidate_descriptor(relay_parent),
}
}
/// Creates a committed candidate receipt with filler data.
pub fn dummy_committed_candidate_receipt<H: AsRef<[u8]>>(
relay_parent: H,
) -> CommittedCandidateReceipt<H> {
CommittedCandidateReceipt::<H> {
descriptor: dummy_candidate_descriptor::<H>(relay_parent),
commitments: dummy_candidate_commitments(dummy_head_data()),
}
}
/// Create a candidate receipt with a bogus signature and filler data. Optionally set the commitment
/// hash with the `commitments` arg.
pub fn dummy_candidate_receipt_bad_sig(
relay_parent: Hash,
commitments: impl Into<Option<Hash>>,
) -> CandidateReceipt<Hash> {
let commitments_hash = if let Some(commitments) = commitments.into() {
commitments
} else {
dummy_candidate_commitments(dummy_head_data()).hash()
};
CandidateReceipt::<Hash> {
commitments_hash,
descriptor: dummy_candidate_descriptor_bad_sig(relay_parent),
}
}
/// Create candidate commitments with filler data.
pub fn dummy_candidate_commitments(head_data: impl Into<Option<HeadData>>) -> CandidateCommitments {
CandidateCommitments {
head_data: head_data.into().unwrap_or(dummy_head_data()),
upward_messages: vec![],
new_validation_code: None,
horizontal_messages: vec![],
processed_downward_messages: 0,
hrmp_watermark: 0_u32,
}
}
/// Create meaningless dummy hash.
pub fn dummy_hash() -> Hash {
Hash::zero()
}
/// Create meaningless dummy digest.
pub fn dummy_digest() -> Digest {
Digest::default()
}
/// Create a candidate descriptor with a bogus signature and filler data.
pub fn dummy_candidate_descriptor_bad_sig(relay_parent: Hash) -> CandidateDescriptor<Hash> {
let zeros = Hash::zero();
CandidateDescriptor::<Hash> {
para_id: 0.into(),
relay_parent,
collator: dummy_collator(),
persisted_validation_data_hash: zeros,
pov_hash: zeros,
erasure_root: zeros,
signature: dummy_collator_signature(),
para_head: zeros,
validation_code_hash: dummy_validation_code().hash(),
}
}
/// Create a candidate descriptor with filler data.
pub fn dummy_candidate_descriptor<H: AsRef<[u8]>>(relay_parent: H) -> CandidateDescriptor<H> {
let collator = sp_keyring::Sr25519Keyring::Ferdie;
let invalid = Hash::zero();
let descriptor = make_valid_candidate_descriptor(
1.into(),
relay_parent,
invalid,
invalid,
invalid,
invalid,
invalid,
collator,
);
descriptor
}
/// Create meaningless validation code.
pub fn dummy_validation_code() -> ValidationCode {
ValidationCode(vec![1, 2, 3])
}
/// Create meaningless head data.
pub fn dummy_head_data() -> HeadData {
HeadData(vec![])
}
/// Create a meaningless collator id.
pub fn dummy_collator() -> CollatorId {
CollatorId::from(sr25519::Public::from_raw([0; 32]))
}
/// Create a meaningless validator id.
pub fn dummy_validator() -> ValidatorId {
ValidatorId::from(sr25519::Public::from_raw([0; 32]))
}
/// Create a meaningless collator signature.
pub fn dummy_collator_signature() -> CollatorSignature {
CollatorSignature::from(sr25519::Signature([0u8; 64]))
}
/// Create a new candidate descriptor, and apply a valid signature
/// using the provided `collator` key.
pub fn make_valid_candidate_descriptor<H: AsRef<[u8]>>(
para_id: ParaId,
relay_parent: H,
persisted_validation_data_hash: Hash,
pov_hash: Hash,
validation_code_hash: impl Into<ValidationCodeHash>,
para_head: Hash,
erasure_root: Hash,
collator: Sr25519Keyring,
) -> CandidateDescriptor<H> {
let validation_code_hash = validation_code_hash.into();
let payload = polkadot_primitives::v1::collator_signature_payload::<H>(
&relay_parent,
&para_id,
&persisted_validation_data_hash,
&pov_hash,
&validation_code_hash,
);
let signature = collator.sign(&payload).into();
let descriptor = CandidateDescriptor {
para_id,
relay_parent,
collator: collator.public().into(),
persisted_validation_data_hash,
pov_hash,
erasure_root,
signature,
para_head,
validation_code_hash,
};
assert!(descriptor.check_collator_signature().is_ok());
descriptor
}
/// After manually modifying the candidate descriptor, resign with a defined collator key.
pub fn resign_candidate_descriptor_with_collator<H: AsRef<[u8]>>(
descriptor: &mut CandidateDescriptor<H>,
collator: Sr25519Keyring,
) {
descriptor.collator = collator.public().into();
let payload = polkadot_primitives::v1::collator_signature_payload::<H>(
&descriptor.relay_parent,
&descriptor.para_id,
&descriptor.persisted_validation_data_hash,
&descriptor.pov_hash,
&descriptor.validation_code_hash,
);
let signature = collator.sign(&payload).into();
descriptor.signature = signature;
}
/// Builder for `CandidateReceipt`.
pub struct TestCandidateBuilder {
pub para_id: ParaId,
pub pov_hash: Hash,
pub relay_parent: Hash,
pub commitments_hash: Hash,
}
impl std::default::Default for TestCandidateBuilder {
fn default() -> Self {
let zeros = Hash::zero();
Self { para_id: 0.into(), pov_hash: zeros, relay_parent: zeros, commitments_hash: zeros }
}
}
impl TestCandidateBuilder {
/// Build a `CandidateReceipt`.
pub fn build(self) -> CandidateReceipt {
let mut descriptor = dummy_candidate_descriptor(self.relay_parent);
descriptor.para_id = self.para_id;
descriptor.pov_hash = self.pov_hash;
CandidateReceipt { descriptor, commitments_hash: self.commitments_hash }
}
}