#![deny(missing_docs)]
use codec::Encode;
use error::{Error, Result};
use futures::{channel::oneshot, future::FutureExt, select};
use polkadot_node_primitives::{
AvailableData, Collation, CollationGenerationConfig, CollationSecondedSignal, PoV,
SubmitCollationParams,
};
use polkadot_node_subsystem::{
messages::{CollationGenerationMessage, CollatorProtocolMessage, RuntimeApiMessage},
overseer, ActiveLeavesUpdate, FromOrchestra, OverseerSignal, SpawnedSubsystem,
SubsystemContext, SubsystemError, SubsystemResult, SubsystemSender,
};
use polkadot_node_subsystem_util::{
request_claim_queue, request_persisted_validation_data, request_session_index_for_child,
request_validation_code_hash, request_validators,
runtime::{request_node_features, ClaimQueueSnapshot},
};
use polkadot_primitives::{
collator_signature_payload,
node_features::FeatureIndex,
vstaging::{
transpose_claim_queue, CandidateDescriptorV2, CandidateReceiptV2 as CandidateReceipt,
CommittedCandidateReceiptV2, TransposedClaimQueue,
},
CandidateCommitments, CandidateDescriptor, CollatorPair, CoreIndex, Hash, Id as ParaId,
NodeFeatures, OccupiedCoreAssumption, PersistedValidationData, SessionIndex,
ValidationCodeHash,
};
use schnellru::{ByLength, LruMap};
use sp_core::crypto::Pair;
use std::sync::Arc;
mod error;
#[cfg(test)]
mod tests;
mod metrics;
use self::metrics::Metrics;
const LOG_TARGET: &'static str = "parachain::collation-generation";
pub struct CollationGenerationSubsystem {
config: Option<Arc<CollationGenerationConfig>>,
session_info_cache: SessionInfoCache,
metrics: Metrics,
}
#[overseer::contextbounds(CollationGeneration, prefix = self::overseer)]
impl CollationGenerationSubsystem {
pub fn new(metrics: Metrics) -> Self {
Self { config: None, metrics, session_info_cache: SessionInfoCache::new() }
}
async fn run<Context>(mut self, mut ctx: Context) {
loop {
select! {
incoming = ctx.recv().fuse() => {
if self.handle_incoming::<Context>(incoming, &mut ctx).await {
break;
}
},
}
}
}
async fn handle_incoming<Context>(
&mut self,
incoming: SubsystemResult<FromOrchestra<<Context as SubsystemContext>::Message>>,
ctx: &mut Context,
) -> bool {
match incoming {
Ok(FromOrchestra::Signal(OverseerSignal::ActiveLeaves(ActiveLeavesUpdate {
activated,
..
}))) => {
if let Err(err) = self.handle_new_activation(activated.map(|v| v.hash), ctx).await {
gum::warn!(target: LOG_TARGET, err = ?err, "failed to handle new activation");
}
false
},
Ok(FromOrchestra::Signal(OverseerSignal::Conclude)) => true,
Ok(FromOrchestra::Communication {
msg: CollationGenerationMessage::Initialize(config),
}) => {
if self.config.is_some() {
gum::error!(target: LOG_TARGET, "double initialization");
} else {
self.config = Some(Arc::new(config));
}
false
},
Ok(FromOrchestra::Communication {
msg: CollationGenerationMessage::Reinitialize(config),
}) => {
self.config = Some(Arc::new(config));
false
},
Ok(FromOrchestra::Communication {
msg: CollationGenerationMessage::SubmitCollation(params),
}) => {
if let Err(err) = self.handle_submit_collation(params, ctx).await {
gum::error!(target: LOG_TARGET, ?err, "Failed to submit collation");
}
false
},
Ok(FromOrchestra::Signal(OverseerSignal::BlockFinalized(..))) => false,
Err(err) => {
gum::error!(
target: LOG_TARGET,
err = ?err,
"error receiving message from subsystem context: {:?}",
err
);
true
},
}
}
async fn handle_submit_collation<Context>(
&mut self,
params: SubmitCollationParams,
ctx: &mut Context,
) -> Result<()> {
let Some(config) = &self.config else {
return Err(Error::SubmittedBeforeInit);
};
let _timer = self.metrics.time_submit_collation();
let SubmitCollationParams {
relay_parent,
collation,
parent_head,
validation_code_hash,
result_sender,
core_index,
} = params;
let mut validation_data = match request_persisted_validation_data(
relay_parent,
config.para_id,
OccupiedCoreAssumption::TimedOut,
ctx.sender(),
)
.await
.await??
{
Some(v) => v,
None => {
gum::debug!(
target: LOG_TARGET,
relay_parent = ?relay_parent,
our_para = %config.para_id,
"No validation data for para - does it exist at this relay-parent?",
);
return Ok(())
},
};
validation_data.parent_head = parent_head;
let claim_queue = request_claim_queue(relay_parent, ctx.sender()).await.await??;
let session_index =
request_session_index_for_child(relay_parent, ctx.sender()).await.await??;
let session_info =
self.session_info_cache.get(relay_parent, session_index, ctx.sender()).await?;
let collation = PreparedCollation {
collation,
relay_parent,
para_id: config.para_id,
validation_data,
validation_code_hash,
n_validators: session_info.n_validators,
core_index,
session_index,
};
construct_and_distribute_receipt(
collation,
config.key.clone(),
ctx.sender(),
result_sender,
&mut self.metrics,
session_info.v2_receipts,
&transpose_claim_queue(claim_queue),
)
.await?;
Ok(())
}
async fn handle_new_activation<Context>(
&mut self,
maybe_activated: Option<Hash>,
ctx: &mut Context,
) -> Result<()> {
let Some(config) = &self.config else {
return Ok(());
};
let Some(relay_parent) = maybe_activated else { return Ok(()) };
if config.collator.is_none() {
return Ok(())
}
let para_id = config.para_id;
let _timer = self.metrics.time_new_activation();
let session_index =
request_session_index_for_child(relay_parent, ctx.sender()).await.await??;
let session_info =
self.session_info_cache.get(relay_parent, session_index, ctx.sender()).await?;
let n_validators = session_info.n_validators;
let claim_queue =
ClaimQueueSnapshot::from(request_claim_queue(relay_parent, ctx.sender()).await.await??);
let cores_to_build_on = claim_queue
.iter_claims_at_depth(0)
.filter_map(|(core_idx, para_id)| (para_id == config.para_id).then_some(core_idx))
.collect::<Vec<_>>();
if cores_to_build_on.is_empty() {
return Ok(())
}
let mut validation_data = match request_persisted_validation_data(
relay_parent,
para_id,
OccupiedCoreAssumption::Included,
ctx.sender(),
)
.await
.await??
{
Some(v) => v,
None => {
gum::debug!(
target: LOG_TARGET,
relay_parent = ?relay_parent,
our_para = %para_id,
"validation data is not available",
);
return Ok(())
},
};
let validation_code_hash = match request_validation_code_hash(
relay_parent,
para_id,
OccupiedCoreAssumption::Included,
ctx.sender(),
)
.await
.await??
{
Some(v) => v,
None => {
gum::debug!(
target: LOG_TARGET,
relay_parent = ?relay_parent,
our_para = %para_id,
"validation code hash is not found.",
);
return Ok(())
},
};
let task_config = config.clone();
let metrics = self.metrics.clone();
let mut task_sender = ctx.sender().clone();
ctx.spawn(
"chained-collation-builder",
Box::pin(async move {
let transposed_claim_queue = transpose_claim_queue(claim_queue.0);
for core_index in cores_to_build_on {
let collator_fn = match task_config.collator.as_ref() {
Some(x) => x,
None => return,
};
let (collation, result_sender) =
match collator_fn(relay_parent, &validation_data).await {
Some(collation) => collation.into_inner(),
None => {
gum::debug!(
target: LOG_TARGET,
?para_id,
"collator returned no collation on collate",
);
return
},
};
let parent_head = collation.head_data.clone();
if let Err(err) = construct_and_distribute_receipt(
PreparedCollation {
collation,
para_id,
relay_parent,
validation_data: validation_data.clone(),
validation_code_hash,
n_validators,
core_index,
session_index,
},
task_config.key.clone(),
&mut task_sender,
result_sender,
&metrics,
session_info.v2_receipts,
&transposed_claim_queue,
)
.await
{
gum::error!(
target: LOG_TARGET,
"Failed to construct and distribute collation: {}",
err
);
return
}
validation_data.parent_head = parent_head;
}
}),
)?;
Ok(())
}
}
#[overseer::subsystem(CollationGeneration, error=SubsystemError, prefix=self::overseer)]
impl<Context> CollationGenerationSubsystem {
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = async move {
self.run(ctx).await;
Ok(())
}
.boxed();
SpawnedSubsystem { name: "collation-generation-subsystem", future }
}
}
#[derive(Clone)]
struct PerSessionInfo {
v2_receipts: bool,
n_validators: usize,
}
struct SessionInfoCache(LruMap<SessionIndex, PerSessionInfo>);
impl SessionInfoCache {
fn new() -> Self {
Self(LruMap::new(ByLength::new(2)))
}
async fn get<Sender: SubsystemSender<RuntimeApiMessage>>(
&mut self,
relay_parent: Hash,
session_index: SessionIndex,
sender: &mut Sender,
) -> Result<PerSessionInfo> {
if let Some(info) = self.0.get(&session_index) {
return Ok(info.clone())
}
let n_validators =
request_validators(relay_parent, &mut sender.clone()).await.await??.len();
let node_features = request_node_features(relay_parent, session_index, sender)
.await?
.unwrap_or(NodeFeatures::EMPTY);
let info = PerSessionInfo {
v2_receipts: node_features
.get(FeatureIndex::CandidateReceiptV2 as usize)
.map(|b| *b)
.unwrap_or(false),
n_validators,
};
self.0.insert(session_index, info);
Ok(self.0.get(&session_index).expect("Just inserted").clone())
}
}
struct PreparedCollation {
collation: Collation,
para_id: ParaId,
relay_parent: Hash,
validation_data: PersistedValidationData,
validation_code_hash: ValidationCodeHash,
n_validators: usize,
core_index: CoreIndex,
session_index: SessionIndex,
}
async fn construct_and_distribute_receipt(
collation: PreparedCollation,
key: CollatorPair,
sender: &mut impl overseer::CollationGenerationSenderTrait,
result_sender: Option<oneshot::Sender<CollationSecondedSignal>>,
metrics: &Metrics,
v2_receipts: bool,
transposed_claim_queue: &TransposedClaimQueue,
) -> Result<()> {
let PreparedCollation {
collation,
para_id,
relay_parent,
validation_data,
validation_code_hash,
n_validators,
core_index,
session_index,
} = collation;
let persisted_validation_data_hash = validation_data.hash();
let parent_head_data = validation_data.parent_head.clone();
let parent_head_data_hash = validation_data.parent_head.hash();
let pov = {
let pov = collation.proof_of_validity.into_compressed();
let encoded_size = pov.encoded_size();
if encoded_size > validation_data.max_pov_size as usize {
return Err(Error::POVSizeExceeded(encoded_size, validation_data.max_pov_size as usize))
}
pov
};
let pov_hash = pov.hash();
let signature_payload = collator_signature_payload(
&relay_parent,
¶_id,
&persisted_validation_data_hash,
&pov_hash,
&validation_code_hash,
);
let erasure_root = erasure_root(n_validators, validation_data, pov.clone())?;
let commitments = CandidateCommitments {
upward_messages: collation.upward_messages,
horizontal_messages: collation.horizontal_messages,
new_validation_code: collation.new_validation_code,
head_data: collation.head_data,
processed_downward_messages: collation.processed_downward_messages,
hrmp_watermark: collation.hrmp_watermark,
};
let receipt = if v2_receipts {
let ccr = CommittedCandidateReceiptV2 {
descriptor: CandidateDescriptorV2::new(
para_id,
relay_parent,
core_index,
session_index,
persisted_validation_data_hash,
pov_hash,
erasure_root,
commitments.head_data.hash(),
validation_code_hash,
),
commitments,
};
ccr.check_core_index(&transposed_claim_queue)
.map_err(Error::CandidateReceiptCheck)?;
ccr.to_plain()
} else {
if commitments.core_selector().map_err(Error::CandidateReceiptCheck)?.is_some() {
gum::warn!(
target: LOG_TARGET,
?pov_hash,
?relay_parent,
para_id = %para_id,
"Candidate commitments contain UMP signal without v2 receipts being enabled.",
);
}
CandidateReceipt {
commitments_hash: commitments.hash(),
descriptor: CandidateDescriptor {
signature: key.sign(&signature_payload),
para_id,
relay_parent,
collator: key.public(),
persisted_validation_data_hash,
pov_hash,
erasure_root,
para_head: commitments.head_data.hash(),
validation_code_hash,
}
.into(),
}
};
gum::debug!(
target: LOG_TARGET,
candidate_hash = ?receipt.hash(),
?pov_hash,
?relay_parent,
para_id = %para_id,
?core_index,
"candidate is generated",
);
metrics.on_collation_generated();
sender
.send_message(CollatorProtocolMessage::DistributeCollation {
candidate_receipt: receipt,
parent_head_data_hash,
pov,
parent_head_data,
result_sender,
core_index,
})
.await;
Ok(())
}
fn erasure_root(
n_validators: usize,
persisted_validation: PersistedValidationData,
pov: PoV,
) -> Result<Hash> {
let available_data =
AvailableData { validation_data: persisted_validation, pov: Arc::new(pov) };
let chunks = polkadot_erasure_coding::obtain_chunks_v1(n_validators, &available_data)?;
Ok(polkadot_erasure_coding::branches(&chunks).root())
}