Skip to content
Snippets Groups Projects
Commit bb69c82c authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

collation-generation: Support compressed PoVs directly (#4825)

This adds support for returning a compressed PoV directly to the
collation generation. This is mainly to not requiring to compress a PoV
twice.
parent 59cddb9f
No related merge requests found
...@@ -309,9 +309,7 @@ async fn handle_new_activations<Context: SubsystemContext>( ...@@ -309,9 +309,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
// Apply compression to the block data. // Apply compression to the block data.
let pov = { let pov = {
let pov = polkadot_node_primitives::maybe_compress_pov( let pov = collation.proof_of_validity.into_compressed();
collation.proof_of_validity,
);
let encoded_size = pov.encoded_size(); let encoded_size = pov.encoded_size();
// As long as `POV_BOMB_LIMIT` is at least `max_pov_size`, this ensures // As long as `POV_BOMB_LIMIT` is at least `max_pov_size`, this ensures
......
...@@ -22,7 +22,9 @@ mod handle_new_activations { ...@@ -22,7 +22,9 @@ mod handle_new_activations {
task::{Context as FuturesContext, Poll}, task::{Context as FuturesContext, Poll},
Future, Future,
}; };
use polkadot_node_primitives::{BlockData, Collation, CollationResult, PoV, POV_BOMB_LIMIT}; use polkadot_node_primitives::{
BlockData, Collation, CollationResult, MaybeCompressedPoV, PoV,
};
use polkadot_node_subsystem::{ use polkadot_node_subsystem::{
errors::RuntimeApiError, errors::RuntimeApiError,
messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest}, messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest},
...@@ -41,7 +43,7 @@ mod handle_new_activations { ...@@ -41,7 +43,7 @@ mod handle_new_activations {
horizontal_messages: vec![], horizontal_messages: vec![],
new_validation_code: None, new_validation_code: None,
head_data: dummy_head_data(), head_data: dummy_head_data(),
proof_of_validity: PoV { block_data: BlockData(Vec::new()) }, proof_of_validity: MaybeCompressedPoV::Raw(PoV { block_data: BlockData(Vec::new()) }),
processed_downward_messages: 0_u32, processed_downward_messages: 0_u32,
hrmp_watermark: 0_u32.into(), hrmp_watermark: 0_u32.into(),
} }
...@@ -49,16 +51,8 @@ mod handle_new_activations { ...@@ -49,16 +51,8 @@ mod handle_new_activations {
fn test_collation_compressed() -> Collation { fn test_collation_compressed() -> Collation {
let mut collation = test_collation(); let mut collation = test_collation();
let compressed = PoV { let compressed = collation.proof_of_validity.clone().into_compressed();
block_data: BlockData( collation.proof_of_validity = MaybeCompressedPoV::Compressed(compressed);
sp_maybe_compressed_blob::compress(
&collation.proof_of_validity.block_data.0,
POV_BOMB_LIMIT,
)
.unwrap(),
),
};
collation.proof_of_validity = compressed;
collation collation
} }
...@@ -309,7 +303,8 @@ mod handle_new_activations { ...@@ -309,7 +303,8 @@ mod handle_new_activations {
// we expect a single message to be sent, containing a candidate receipt. // we expect a single message to be sent, containing a candidate receipt.
// we don't care too much about the `commitments_hash` right now, but let's ensure that we've calculated the // we don't care too much about the `commitments_hash` right now, but let's ensure that we've calculated the
// correct descriptor // correct descriptor
let expect_pov_hash = test_collation_compressed().proof_of_validity.hash(); let expect_pov_hash =
test_collation_compressed().proof_of_validity.into_compressed().hash();
let expect_validation_data_hash = test_validation_data().hash(); let expect_validation_data_hash = test_validation_data().hash();
let expect_relay_parent = Hash::repeat_byte(4); let expect_relay_parent = Hash::repeat_byte(4);
let expect_validation_code_hash = ValidationCode(vec![1, 2, 3]).hash(); let expect_validation_code_hash = ValidationCode(vec![1, 2, 3]).hash();
......
...@@ -257,6 +257,29 @@ impl PoV { ...@@ -257,6 +257,29 @@ impl PoV {
} }
} }
/// A type that represents a maybe compressed [`PoV`].
#[derive(Clone, Encode, Decode)]
#[cfg(not(target_os = "unknown"))]
pub enum MaybeCompressedPoV {
/// A raw [`PoV`], aka not compressed.
Raw(PoV),
/// The given [`PoV`] is already compressed.
Compressed(PoV),
}
#[cfg(not(target_os = "unknown"))]
impl MaybeCompressedPoV {
/// Convert into a compressed [`PoV`].
///
/// If `self == Raw` it is compressed using [`maybe_compress_pov`].
pub fn into_compressed(self) -> PoV {
match self {
Self::Raw(raw) => maybe_compress_pov(raw),
Self::Compressed(compressed) => compressed,
}
}
}
/// The output of a collator. /// The output of a collator.
/// ///
/// This differs from `CandidateCommitments` in two ways: /// This differs from `CandidateCommitments` in two ways:
...@@ -264,6 +287,7 @@ impl PoV { ...@@ -264,6 +287,7 @@ impl PoV {
/// - does not contain the erasure root; that's computed at the Polkadot level, not at Cumulus /// - does not contain the erasure root; that's computed at the Polkadot level, not at Cumulus
/// - contains a proof of validity. /// - contains a proof of validity.
#[derive(Clone, Encode, Decode)] #[derive(Clone, Encode, Decode)]
#[cfg(not(target_os = "unknown"))]
pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> { pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> {
/// Messages destined to be interpreted by the Relay chain itself. /// Messages destined to be interpreted by the Relay chain itself.
pub upward_messages: Vec<UpwardMessage>, pub upward_messages: Vec<UpwardMessage>,
...@@ -274,7 +298,7 @@ pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> { ...@@ -274,7 +298,7 @@ pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> {
/// The head-data produced as a result of execution. /// The head-data produced as a result of execution.
pub head_data: HeadData, pub head_data: HeadData,
/// Proof to verify the state transition of the parachain. /// Proof to verify the state transition of the parachain.
pub proof_of_validity: PoV, pub proof_of_validity: MaybeCompressedPoV,
/// The number of messages processed from the DMQ. /// The number of messages processed from the DMQ.
pub processed_downward_messages: u32, pub processed_downward_messages: u32,
/// The mark which specifies the block number up to which all inbound HRMP messages are processed. /// The mark which specifies the block number up to which all inbound HRMP messages are processed.
...@@ -283,6 +307,7 @@ pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> { ...@@ -283,6 +307,7 @@ pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> {
/// Signal that is being returned when a collation was seconded by a validator. /// Signal that is being returned when a collation was seconded by a validator.
#[derive(Debug)] #[derive(Debug)]
#[cfg(not(target_os = "unknown"))]
pub struct CollationSecondedSignal { pub struct CollationSecondedSignal {
/// The hash of the relay chain block that was used as context to sign [`Self::statement`]. /// The hash of the relay chain block that was used as context to sign [`Self::statement`].
pub relay_parent: Hash, pub relay_parent: Hash,
...@@ -293,6 +318,7 @@ pub struct CollationSecondedSignal { ...@@ -293,6 +318,7 @@ pub struct CollationSecondedSignal {
} }
/// Result of the [`CollatorFn`] invocation. /// Result of the [`CollatorFn`] invocation.
#[cfg(not(target_os = "unknown"))]
pub struct CollationResult { pub struct CollationResult {
/// The collation that was build. /// The collation that was build.
pub collation: Collation, pub collation: Collation,
...@@ -304,6 +330,7 @@ pub struct CollationResult { ...@@ -304,6 +330,7 @@ pub struct CollationResult {
pub result_sender: Option<futures::channel::oneshot::Sender<CollationSecondedSignal>>, pub result_sender: Option<futures::channel::oneshot::Sender<CollationSecondedSignal>>,
} }
#[cfg(not(target_os = "unknown"))]
impl CollationResult { impl CollationResult {
/// Convert into the inner values. /// Convert into the inner values.
pub fn into_inner( pub fn into_inner(
...@@ -319,6 +346,7 @@ impl CollationResult { ...@@ -319,6 +346,7 @@ impl CollationResult {
/// [`ValidationData`] that provides information about the state of the parachain on the relay chain. /// [`ValidationData`] that provides information about the state of the parachain on the relay chain.
/// ///
/// Returns an optional [`CollationResult`]. /// Returns an optional [`CollationResult`].
#[cfg(not(target_os = "unknown"))]
pub type CollatorFn = Box< pub type CollatorFn = Box<
dyn Fn( dyn Fn(
Hash, Hash,
...@@ -329,6 +357,7 @@ pub type CollatorFn = Box< ...@@ -329,6 +357,7 @@ pub type CollatorFn = Box<
>; >;
/// Configuration for the collation generator /// Configuration for the collation generator
#[cfg(not(target_os = "unknown"))]
pub struct CollationGenerationConfig { pub struct CollationGenerationConfig {
/// Collator's authentication key, so it can sign things. /// Collator's authentication key, so it can sign things.
pub key: CollatorPair, pub key: CollatorPair,
...@@ -338,6 +367,7 @@ pub struct CollationGenerationConfig { ...@@ -338,6 +367,7 @@ pub struct CollationGenerationConfig {
pub para_id: ParaId, pub para_id: ParaId,
} }
#[cfg(not(target_os = "unknown"))]
impl std::fmt::Debug for CollationGenerationConfig { impl std::fmt::Debug for CollationGenerationConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "CollationGenerationConfig {{ ... }}") write!(f, "CollationGenerationConfig {{ ... }}")
...@@ -371,8 +401,8 @@ impl Proof { ...@@ -371,8 +401,8 @@ impl Proof {
} }
} }
/// Possible errors when converting from `Vec<Vec<u8>>` into [`Proof`].
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
///
pub enum MerkleProofError { pub enum MerkleProofError {
#[error("Merkle max proof depth exceeded {0} > {} .", MERKLE_PROOF_MAX_DEPTH)] #[error("Merkle max proof depth exceeded {0} > {} .", MERKLE_PROOF_MAX_DEPTH)]
/// This error signifies that the Proof length exceeds the trie's max depth /// This error signifies that the Proof length exceeds the trie's max depth
......
...@@ -20,7 +20,8 @@ use futures::channel::oneshot; ...@@ -20,7 +20,8 @@ use futures::channel::oneshot;
use futures_timer::Delay; use futures_timer::Delay;
use parity_scale_codec::{Decode, Encode}; use parity_scale_codec::{Decode, Encode};
use polkadot_node_primitives::{ use polkadot_node_primitives::{
Collation, CollationResult, CollationSecondedSignal, CollatorFn, PoV, Statement, Collation, CollationResult, CollationSecondedSignal, CollatorFn, MaybeCompressedPoV, PoV,
Statement,
}; };
use polkadot_primitives::v1::{CollatorId, CollatorPair}; use polkadot_primitives::v1::{CollatorId, CollatorPair};
use sp_core::{traits::SpawnNamed, Pair}; use sp_core::{traits::SpawnNamed, Pair};
...@@ -175,7 +176,7 @@ impl Collator { ...@@ -175,7 +176,7 @@ impl Collator {
horizontal_messages: Vec::new(), horizontal_messages: Vec::new(),
new_validation_code: None, new_validation_code: None,
head_data: head_data.encode().into(), head_data: head_data.encode().into(),
proof_of_validity: pov.clone(), proof_of_validity: MaybeCompressedPoV::Raw(pov.clone()),
processed_downward_messages: 0, processed_downward_messages: 0,
hrmp_watermark: validation_data.relay_parent_number, hrmp_watermark: validation_data.relay_parent_number,
}; };
...@@ -273,11 +274,16 @@ mod tests { ...@@ -273,11 +274,16 @@ mod tests {
fn validate_collation(collator: &Collator, parent_head: HeadData, collation: Collation) { fn validate_collation(collator: &Collator, parent_head: HeadData, collation: Collation) {
use polkadot_node_core_pvf::testing::validate_candidate; use polkadot_node_core_pvf::testing::validate_candidate;
let block_data = match collation.proof_of_validity {
MaybeCompressedPoV::Raw(pov) => pov.block_data,
MaybeCompressedPoV::Compressed(_) => panic!("Only works with uncompressed povs"),
};
let ret_buf = validate_candidate( let ret_buf = validate_candidate(
collator.validation_code(), collator.validation_code(),
&ValidationParams { &ValidationParams {
parent_head: parent_head.encode().into(), parent_head: parent_head.encode().into(),
block_data: collation.proof_of_validity.block_data, block_data,
relay_parent_number: 1, relay_parent_number: 1,
relay_parent_storage_root: Default::default(), relay_parent_storage_root: Default::default(),
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment