Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/collation-generation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ thiserror = "1.0.30"
parity-scale-codec = { version = "3.1.2", default-features = false, features = ["bit-vec", "derive"] }

[dev-dependencies]
async-trait = "0.1.53"
polkadot-node-subsystem-test-helpers = { path = "../subsystem-test-helpers" }
test-helpers = { package = "polkadot-primitives-test-helpers", path = "../../primitives/test-helpers" }
92 changes: 56 additions & 36 deletions node/collation-generation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,11 @@ async fn handle_new_activations<Context: SubsystemContext>(
for (core_idx, core) in availability_cores.into_iter().enumerate() {
let _availability_core_timer = metrics.time_new_activations_availability_core();

let (scheduled_core, assumption) = match core {
let (core_para_id, assumption) = match &core {
CoreState::Scheduled(scheduled_core) =>
(scheduled_core, OccupiedCoreAssumption::Free),
CoreState::Occupied(_occupied_core) => {
// TODO: https://github.com/paritytech/polkadot/issues/1573
gum::trace!(
target: LOG_TARGET,
core_idx = %core_idx,
relay_parent = ?relay_parent,
"core is occupied. Keep going.",
);
continue
},
(scheduled_core.para_id, OccupiedCoreAssumption::Free),
CoreState::Occupied(occupied_core) =>
(occupied_core.para_id(), OccupiedCoreAssumption::Included),
CoreState::Free => {
gum::trace!(
target: LOG_TARGET,
Expand All @@ -225,13 +217,13 @@ async fn handle_new_activations<Context: SubsystemContext>(
},
};

if scheduled_core.para_id != config.para_id {
if core_para_id != config.para_id {
gum::trace!(
target: LOG_TARGET,
core_idx = %core_idx,
relay_parent = ?relay_parent,
our_para = %config.para_id,
their_para = %scheduled_core.para_id,
their_para = %core_para_id,
"core is not assigned to our para. Keep going.",
);
continue
Expand All @@ -243,7 +235,7 @@ async fn handle_new_activations<Context: SubsystemContext>(

let validation_data = match request_persisted_validation_data(
relay_parent,
scheduled_core.para_id,
core_para_id,
assumption,
ctx.sender(),
)
Expand All @@ -257,16 +249,41 @@ async fn handle_new_activations<Context: SubsystemContext>(
core_idx = %core_idx,
relay_parent = ?relay_parent,
our_para = %config.para_id,
their_para = %scheduled_core.para_id,
their_para = %core_para_id,
"validation data is not available",
);
continue
},
};

if config.collator.is_collating(relay_parent, &validation_data).await {
let _ = ctx
.send_message(AllMessages::CollatorProtocol(
CollatorProtocolMessage::PreConnectAsCollator(relay_parent),
))
.await;
gum::debug!(
target: LOG_TARGET,
para_id = %core_para_id,
relay_parent = ?relay_parent,
"Sent pre-connect request",
);
}

if core.is_occupied() {
// TODO: https://github.com/paritytech/polkadot/issues/1573
gum::trace!(
target: LOG_TARGET,
core_idx = %core_idx,
relay_parent = ?relay_parent,
"core is occupied. Keep going.",
);
continue
}

let validation_code_hash = match obtain_current_validation_code_hash(
relay_parent,
scheduled_core.para_id,
core_para_id,
assumption,
ctx.sender(),
)
Expand All @@ -279,7 +296,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
core_idx = %core_idx,
relay_parent = ?relay_parent,
our_para = %config.para_id,
their_para = %scheduled_core.para_id,
their_para = %core_para_id,
"validation code hash is not found.",
);
continue
Expand All @@ -294,18 +311,21 @@ async fn handle_new_activations<Context: SubsystemContext>(
Box::pin(async move {
let persisted_validation_data_hash = validation_data.hash();

let (collation, result_sender) =
match (task_config.collator)(relay_parent, &validation_data).await {
Some(collation) => collation.into_inner(),
None => {
gum::debug!(
target: LOG_TARGET,
para_id = %scheduled_core.para_id,
"collator returned no collation on collate",
);
return
},
};
let (collation, result_sender) = match task_config
.collator
.produce_collation(relay_parent, &validation_data)
.await
{
Some(collation) => collation.into_inner(),
None => {
gum::debug!(
target: LOG_TARGET,
para_id = %core_para_id,
"collator returned no collation on collate",
);
return
},
};

// Apply compression to the block data.
let pov = {
Expand All @@ -320,7 +340,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
if encoded_size > validation_data.max_pov_size as usize {
gum::debug!(
target: LOG_TARGET,
para_id = %scheduled_core.para_id,
para_id = %core_para_id,
size = encoded_size,
max_size = validation_data.max_pov_size,
"PoV exceeded maximum size"
Expand All @@ -336,7 +356,7 @@ async fn handle_new_activations<Context: SubsystemContext>(

let signature_payload = collator_signature_payload(
&relay_parent,
&scheduled_core.para_id,
&core_para_id,
&persisted_validation_data_hash,
&pov_hash,
&validation_code_hash,
Expand All @@ -348,7 +368,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
Err(err) => {
gum::error!(
target: LOG_TARGET,
para_id = %scheduled_core.para_id,
para_id = %core_para_id,
err = ?err,
"failed to calculate erasure root",
);
Expand All @@ -369,7 +389,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
commitments_hash: commitments.hash(),
descriptor: CandidateDescriptor {
signature: task_config.key.sign(&signature_payload),
para_id: scheduled_core.para_id,
para_id: core_para_id,
relay_parent,
collator: task_config.key.public(),
persisted_validation_data_hash,
Expand All @@ -385,7 +405,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
candidate_hash = ?ccr.hash(),
?pov_hash,
?relay_parent,
para_id = %scheduled_core.para_id,
para_id = %core_para_id,
"candidate is generated",
);
metrics.on_collation_generated();
Expand All @@ -398,7 +418,7 @@ async fn handle_new_activations<Context: SubsystemContext>(
{
gum::warn!(
target: LOG_TARGET,
para_id = %scheduled_core.para_id,
para_id = %core_para_id,
err = ?err,
"failed to send collation result",
);
Expand Down
27 changes: 14 additions & 13 deletions node/collation-generation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
mod handle_new_activations {
use super::super::*;
use ::test_helpers::{dummy_hash, dummy_head_data, dummy_validator};
use futures::{
lock::Mutex,
task::{Context as FuturesContext, Poll},
Future,
};
use futures::lock::Mutex;
use polkadot_node_primitives::{
BlockData, Collation, CollationResult, MaybeCompressedPoV, PoV,
BlockData, Collation, CollationResult, Collator, MaybeCompressedPoV, PoV,
};
use polkadot_node_subsystem::{
errors::RuntimeApiError,
Expand All @@ -35,7 +31,6 @@ mod handle_new_activations {
use polkadot_primitives::v2::{
CollatorPair, Id as ParaId, PersistedValidationData, ScheduledCore, ValidationCode,
};
use std::pin::Pin;

fn test_collation() -> Collation {
Collation {
Expand All @@ -62,14 +57,20 @@ mod handle_new_activations {
persisted_validation_data
}

// Box<dyn Future<Output = Collation> + Unpin + Send
struct TestCollator;

impl Future for TestCollator {
type Output = Option<CollationResult>;
#[async_trait::async_trait]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a small test for the pre-connect functionality as well, I think.

impl Collator for TestCollator {
async fn produce_collation(
&self,
_: Hash,
_: &PersistedValidationData,
) -> Option<CollationResult> {
Some(CollationResult { collation: test_collation(), result_sender: None })
}

fn poll(self: Pin<&mut Self>, _cx: &mut FuturesContext) -> Poll<Self::Output> {
Poll::Ready(Some(CollationResult { collation: test_collation(), result_sender: None }))
async fn is_collating(&self, _: Hash, _: &PersistedValidationData) -> bool {
false
}
}

Expand All @@ -78,7 +79,7 @@ mod handle_new_activations {
fn test_config<Id: Into<ParaId>>(para_id: Id) -> Arc<CollationGenerationConfig> {
Arc::new(CollationGenerationConfig {
key: CollatorPair::generate().0,
collator: Box::new(|_: Hash, _vd: &PersistedValidationData| TestCollator.boxed()),
collator: Box::new(TestCollator),
para_id: para_id.into(),
})
}
Expand Down
Loading