// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi 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.
// Pezkuwi 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 Pezkuwi. If not, see .
#![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 codec::{Decode, Encode};
use pezkuwi_primitives::{
AppVerify, CandidateCommitments, CandidateDescriptorV2, CandidateHash, CandidateReceiptV2,
CollatorId, CollatorSignature, CommittedCandidateReceiptV2, CoreIndex, Hash, HashT, HeadData,
Id, Id as ParaId, InternalVersion, MutateDescriptorV2, PersistedValidationData, SessionIndex,
ValidationCode, ValidationCodeHash, ValidatorId,
};
use pezsp_application_crypto::{sr25519, ByteArray};
use pezsp_keyring::Sr25519Keyring;
use pezsp_runtime::{generic::Digest, traits::BlakeTwo256};
pub use rand;
use scale_info::TypeInfo;
const MAX_POV_SIZE: u32 = 1_000_000;
/// The legacy descriptor of a legacy candidate receipt.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
pub struct CandidateDescriptor {
/// The ID of the para this is a candidate for.
pub para_id: Id,
/// The hash of the relay-chain block this is executed in the context of.
pub relay_parent: H,
/// The collator's sr25519 public key.
pub collator: CollatorId,
/// The blake2-256 hash of the persisted validation data. This is extra data derived from
/// relay-chain state which may vary based on bitfields included before the candidate.
/// Thus it cannot be derived entirely from the relay-parent.
pub persisted_validation_data_hash: Hash,
/// The blake2-256 hash of the PoV.
pub pov_hash: Hash,
/// The root of a block's erasure encoding Merkle tree.
pub erasure_root: Hash,
/// Signature on blake2-256 of components of this receipt:
/// The teyrchain index, the relay parent, the validation data hash, and the `pov_hash`.
pub signature: CollatorSignature,
/// Hash of the para header that is being generated by this candidate.
pub para_head: Hash,
/// The blake2-256 hash of the validation code bytes.
pub validation_code_hash: ValidationCodeHash,
}
impl> CandidateDescriptor {
/// Check the signature of the collator within this descriptor.
pub fn check_collator_signature(&self) -> Result<(), ()> {
check_collator_signature(
&self.relay_parent,
&self.para_id,
&self.persisted_validation_data_hash,
&self.pov_hash,
&self.validation_code_hash,
&self.collator,
&self.signature,
)
}
}
/// A legacy candidate-receipt.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
pub struct CandidateReceipt {
/// The descriptor of the candidate.
pub descriptor: CandidateDescriptor,
/// The hash of the encoded commitments made as a result of candidate execution.
pub commitments_hash: Hash,
}
impl CandidateReceipt {
/// Get a reference to the candidate descriptor.
pub fn descriptor(&self) -> &CandidateDescriptor {
&self.descriptor
}
/// Computes the blake2-256 hash of the receipt.
pub fn hash(&self) -> CandidateHash
where
H: Encode,
{
CandidateHash(BlakeTwo256::hash_of(self))
}
}
impl From> for CandidateReceipt {
fn from(value: CandidateReceiptV2) -> Self {
Self { descriptor: value.descriptor.into(), commitments_hash: value.commitments_hash }
}
}
impl> From> for CandidateReceiptV2 {
fn from(value: CandidateReceipt) -> Self {
Self { descriptor: value.descriptor.into(), commitments_hash: value.commitments_hash }
}
}
/// A legacy candidate-receipt with commitments directly included.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
pub struct CommittedCandidateReceipt {
/// The descriptor of the candidate.
pub descriptor: CandidateDescriptor,
/// The commitments of the candidate receipt.
pub commitments: CandidateCommitments,
}
impl CommittedCandidateReceipt {
/// Get a reference to the candidate descriptor.
pub fn descriptor(&self) -> &CandidateDescriptor {
&self.descriptor
}
}
impl CommittedCandidateReceipt {
/// Transforms this into a plain `CandidateReceipt`.
pub fn to_plain(&self) -> CandidateReceipt {
CandidateReceipt {
descriptor: self.descriptor.clone(),
commitments_hash: self.commitments.hash(),
}
}
/// Computes the hash of the committed candidate receipt.
///
/// This computes the canonical hash, not the hash of the directly encoded data.
/// Thus this is a shortcut for `candidate.to_plain().hash()`.
pub fn hash(&self) -> CandidateHash
where
H: Encode,
{
self.to_plain().hash()
}
/// Does this committed candidate receipt corresponds to the given [`CandidateReceipt`]?
pub fn corresponds_to(&self, receipt: &CandidateReceipt) -> bool
where
H: PartialEq,
{
receipt.descriptor == self.descriptor && receipt.commitments_hash == self.commitments.hash()
}
}
impl PartialOrd for CommittedCandidateReceipt {
fn partial_cmp(&self, other: &Self) -> Option {
Some(self.cmp(other))
}
}
impl Ord for CommittedCandidateReceipt {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
// TODO: compare signatures or something more sane
// https://github.com/pezkuwichain/pezkuwi-sdk/issues/132
self.descriptor()
.para_id
.cmp(&other.descriptor().para_id)
.then_with(|| self.commitments.head_data.cmp(&other.commitments.head_data))
}
}
impl From> for CommittedCandidateReceipt {
fn from(value: CommittedCandidateReceiptV2) -> Self {
Self { descriptor: value.descriptor.into(), commitments: value.commitments }
}
}
impl From> for CandidateDescriptor {
fn from(value: CandidateDescriptorV2) -> Self {
Self {
para_id: value.para_id(),
relay_parent: value.relay_parent(),
collator: value.rebuild_collator_field_for_tests(),
persisted_validation_data_hash: value.persisted_validation_data_hash(),
pov_hash: value.pov_hash(),
erasure_root: value.erasure_root(),
signature: value.rebuild_signature_field_for_tests(),
para_head: value.para_head(),
validation_code_hash: value.validation_code_hash(),
}
}
}
fn clone_into_array(slice: &[T]) -> A
where
A: Default + AsMut<[T]>,
T: Clone,
{
let mut a = A::default();
>::as_mut(&mut a).clone_from_slice(slice);
a
}
impl> From> for CandidateDescriptorV2 {
fn from(value: CandidateDescriptor) -> Self {
let collator = value.collator.as_slice();
CandidateDescriptorV2::new_from_raw(
value.para_id,
value.relay_parent,
InternalVersion(collator[0]),
u16::from_ne_bytes(clone_into_array(&collator[1..=2])),
SessionIndex::from_ne_bytes(clone_into_array(&collator[3..=6])),
clone_into_array(&collator[7..]),
value.persisted_validation_data_hash,
value.pov_hash,
value.erasure_root,
value.signature.into_inner().0,
value.para_head,
value.validation_code_hash,
)
}
}
impl> From> for CommittedCandidateReceiptV2 {
fn from(value: CommittedCandidateReceipt) -> Self {
Self { descriptor: value.descriptor.into(), commitments: value.commitments }
}
}
/// Get a collator signature payload on a relay-parent, block-data combo.
pub fn collator_signature_payload>(
relay_parent: &H,
para_id: &Id,
persisted_validation_data_hash: &Hash,
pov_hash: &Hash,
validation_code_hash: &ValidationCodeHash,
) -> [u8; 132] {
// 32-byte hash length is protected in a test below.
let mut payload = [0u8; 132];
payload[0..32].copy_from_slice(relay_parent.as_ref());
u32::from(*para_id).using_encoded(|s| payload[32..32 + s.len()].copy_from_slice(s));
payload[36..68].copy_from_slice(persisted_validation_data_hash.as_ref());
payload[68..100].copy_from_slice(pov_hash.as_ref());
payload[100..132].copy_from_slice(validation_code_hash.as_ref());
payload
}
pub(crate) fn check_collator_signature>(
relay_parent: &H,
para_id: &Id,
persisted_validation_data_hash: &Hash,
pov_hash: &Hash,
validation_code_hash: &ValidationCodeHash,
collator: &CollatorId,
signature: &CollatorSignature,
) -> Result<(), ()> {
let payload = collator_signature_payload(
relay_parent,
para_id,
persisted_validation_data_hash,
pov_hash,
validation_code_hash,
);
if signature.verify(&payload[..], collator) {
Ok(())
} else {
Err(())
}
}
/// Creates a candidate receipt with filler data.
pub fn dummy_candidate_receipt>(relay_parent: H) -> CandidateReceipt {
CandidateReceipt:: {
commitments_hash: dummy_candidate_commitments(dummy_head_data()).hash(),
descriptor: dummy_candidate_descriptor(relay_parent),
}
}
/// Creates a v2 candidate receipt with filler data.
pub fn dummy_candidate_receipt_v2 + Copy>(relay_parent: H) -> CandidateReceiptV2 {
CandidateReceiptV2:: {
commitments_hash: dummy_candidate_commitments(dummy_head_data()).hash(),
descriptor: dummy_candidate_descriptor_v2(relay_parent),
}
}
/// Creates a committed candidate receipt with filler data.
pub fn dummy_committed_candidate_receipt>(
relay_parent: H,
) -> CommittedCandidateReceipt {
CommittedCandidateReceipt:: {
descriptor: dummy_candidate_descriptor::(relay_parent),
commitments: dummy_candidate_commitments(dummy_head_data()),
}
}
/// Creates a v2 committed candidate receipt with filler data.
pub fn dummy_committed_candidate_receipt_v2 + Copy>(
relay_parent: H,
) -> CommittedCandidateReceiptV2 {
CommittedCandidateReceiptV2 {
descriptor: dummy_candidate_descriptor_v2::(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