) -> Result<(), BlockAnnounceError> {
let candidate_hash = if let CompactStatement::Candidate(h) = self.statement.payload() {
h
} else {
return Err(BlockAnnounceError(
"`CompactStatement` isn't the candidate variant!".into(),
));
};
if *candidate_hash != self.receipt.hash() {
return Err(BlockAnnounceError(
"Receipt candidate hash doesn't match candidate hash in statement".into(),
));
}
if polkadot_parachain::primitives::HeadData(encoded_header).hash() != self.receipt.descriptor.para_head
{
return Err(BlockAnnounceError(
"Receipt para head hash doesn't match the hash of the header in the block announcement".into(),
));
}
Ok(())
}
/// Check the signature of the statement.
///
/// Returns an `Err(_)` if it failed.
fn check_signature(&self, relay_chain_client: &Arc
) -> Result<(), BlockAnnounceError>
where
P: ProvideRuntimeApi + Send + Sync + 'static,
P::Api: ParachainHost,
{
let runtime_api = relay_chain_client.runtime_api();
let validator_index = self.statement.validator_index();
let runtime_api_block_id = BlockId::Hash(self.receipt.descriptor.relay_parent);
let session_index = match runtime_api.session_index_for_child(&runtime_api_block_id) {
Ok(r) => r,
Err(e) => {
return Err(BlockAnnounceError(format!("{:?}", e)));
}
};
let signing_context = SigningContext {
parent_hash: self.receipt.descriptor.relay_parent,
session_index,
};
// Check that the signer is a legit validator.
let authorities = match runtime_api.validators(&runtime_api_block_id) {
Ok(r) => r,
Err(e) => {
return Err(BlockAnnounceError(format!("{:?}", e)));
}
};
let signer = match authorities.get(validator_index as usize) {
Some(r) => r,
None => {
return Err(BlockAnnounceError(
"block accouncement justification signer is a validator index out of bound"
.to_string(),
));
}
};
// Check statement is correctly signed.
if self
.statement
.check_signature(&signing_context, &signer)
.is_err()
{
return Err(BlockAnnounceError(
"block announcement justification signature is invalid".to_string(),
));
}
Ok(())
}
}
impl TryFrom for BlockAnnounceData {
type Error = ();
fn try_from(stmt: SignedFullStatement) -> Result {
let receipt = if let Statement::Seconded(receipt) = stmt.payload() {
receipt.to_plain()
} else {
return Err(());
};
Ok(BlockAnnounceData {
receipt,
statement: stmt.convert_payload(),
})
}
}
/// Parachain specific block announce validator.
///
/// This block announce validator is required if the parachain is running
/// with the relay chain provided consensus to make sure each node only
/// imports a reasonable number of blocks per round. The relay chain provided
/// consensus doesn't have any authorities and so it could happen that without
/// this special block announce validator a node would need to import *millions*
/// of blocks per round, which is clearly not doable.
///
/// To solve this problem, each block announcement is delayed until a collator
/// has received a [`Statement::Seconded`] for its `PoV`. This message tells the
/// collator that its `PoV` was validated successfully by a parachain validator and
/// that it is very likely that this `PoV` will be included in the relay chain. Every
/// collator that doesn't receive the message for its `PoV` will not announce its block.
/// For more information on the block announcement, see [`WaitToAnnounce`].
///
/// For each block announcement that is received, the generic block announcement validation
/// will call this validator and provides the extra data that was attached to the announcement.
/// We call this extra data `justification`.
/// It is expected that the attached data is a SCALE encoded [`BlockAnnounceData`]. The
/// statement is checked to be a [`CompactStatement::Candidate`] and that it is signed by an active
/// parachain validator.
///
/// If no justification was provided we check if the block announcement is at the tip of the known
/// chain. If it is at the tip, it is required to provide a justification or otherwise we reject
/// it. However, if the announcement is for a block below the tip the announcement is accepted
/// as it probably comes from a node that is currently syncing the chain.
pub struct BlockAnnounceValidator {
phantom: PhantomData,
relay_chain_client: Arc,
relay_chain_backend: Arc,
para_id: ParaId,
relay_chain_sync_oracle: Box,
wait_on_relay_chain_block: WaitOnRelayChainBlock,
}
impl BlockAnnounceValidator {
/// Create a new [`BlockAnnounceValidator`].
pub fn new(
relay_chain_client: Arc,
para_id: ParaId,
relay_chain_sync_oracle: Box,
relay_chain_backend: Arc,
relay_chain_blockchain_events: Arc,
) -> Self {
Self {
phantom: Default::default(),
relay_chain_client,
para_id,
relay_chain_sync_oracle,
relay_chain_backend: relay_chain_backend.clone(),
wait_on_relay_chain_block: WaitOnRelayChainBlock::new(
relay_chain_backend,
relay_chain_blockchain_events,
),
}
}
}
impl BlockAnnounceValidator
where
P: ProvideRuntimeApi + Send + Sync + 'static,
P::Api: ParachainHost,
B: Backend + 'static,
// Rust bug: https://github.com/rust-lang/rust/issues/24159
sc_client_api::StateBackendFor: sc_client_api::StateBackend>,
{
/// Handle a block announcement with empty data (no statement) attached to it.
fn handle_empty_block_announce_data(
&self,
header: Block::Header,
) -> impl Future