use gossip message directly

This commit is contained in:
thiolliere
2019-10-31 15:22:14 +01:00
parent 8def75fe48
commit e0c0b209dd
3 changed files with 171 additions and 109 deletions
+48 -38
View File
@@ -22,31 +22,16 @@ use substrate_client::error::{Error as ClientError};
use sr_primitives::traits::{Block as BlockT};
use substrate_consensus_common::block_validation::{Validation, BlockAnnounceValidator};
use polkadot_primitives::{
Hash as PHash, parachain::{CandidateReceipt, ValidatorIndex, ValidatorSignature, ValidatorId},
};
use polkadot_statement_table::Statement;
use polkadot_primitives::parachain::ValidatorId;
use polkadot_statement_table::{SignedStatement, Statement};
use polkadot_validation::check_statement;
use polkadot_network::gossip::{GossipStatement, GossipMessage};
use codec::{Decode, Encode};
use std::marker::PhantomData;
/// Justification that a parachain block is the parachain block candidate of one of the relay chain
/// validator.
#[derive(Encode, Decode)]
pub struct BlockCandidateJustification {
/// Receipt of the parachain block candidate of the signer.
candidate_receipt: CandidateReceipt,
/// Signer of `signature`.
signer: ValidatorIndex,
/// Signature of the Candidate statement with `candidate_receipt`.
signature: ValidatorSignature,
/// The parent block of which the candidate must be include.
relay_chain_parent_hash: PHash,
}
/// Validate that data is a valid justification form a relay-chain validator that the block is a
/// Validate that data is a valid justification from a relay-chain validator that the block is a
/// valid parachain-block candidate.
pub struct JustifiedBlockAnnounceValidator<B> {
authorities: Vec<ValidatorId>,
@@ -66,34 +51,59 @@ impl<B: BlockT> BlockAnnounceValidator<B> for JustifiedBlockAnnounceValidator<B>
fn validate(&mut self, header: &B::Header, mut data: &[u8])
-> Result<Validation, Box<dyn std::error::Error + Send>>
{
let justification = BlockCandidateJustification::decode(&mut data)
// Check data is a gossip message.
let gossip_message = GossipMessage::decode(&mut data)
.map_err(|_| Box::new(ClientError::BadJustification(
"cannot decode block candidate justification".to_string()
"cannot decode block announced justification, must be a gossip message".to_string()
)) as Box<_>)?;
// Check the header in the candidate_receipt match header given header.
if header.encode() != justification.candidate_receipt.head_data.0 {
// Check message is a gossip statement.
let gossip_statement = match gossip_message {
GossipMessage::Statement(gossip_statement) => gossip_statement,
_ => return Err(Box::new(ClientError::BadJustification(
"block announced justification statement must be a gossip statement".to_string()
)) as Box<_>)
};
let GossipStatement {
relay_chain_leaf,
signed_statement: SignedStatement {
statement,
signature,
sender,
}
} = gossip_statement;
// Check that the signer is a legit validator.
let signer = self.authorities.get(sender as usize)
.ok_or_else(|| Box::new(ClientError::BadJustification(
"block accounced justification signer is a validator index out of bound".to_string()
)) as Box<_>)?;
// Check statement is correctly signed.
if !check_statement(
&statement,
&signature,
signer.clone(),
&relay_chain_leaf,
) {
return Err(Box::new(ClientError::BadJustification(
"block candidate header does not match its justification".to_string()
"block announced justification signature is invalid".to_string()
)) as Box<_>)
}
// Check that the signer is a legit validator.
let signer = self.authorities.get(justification.signer as usize)
.ok_or_else(|| Box::new(ClientError::BadJustification(
"block candidate justification signer is a validator index out of bound".to_string()
)) as Box<_>)?;
// Check statement is a candidate statement.
let candidate_receipt = match statement {
Statement::Candidate(candidate_receipt) => candidate_receipt,
_ => return Err(Box::new(ClientError::BadJustification(
"block announced justification statement must be a candidate statement".to_string()
)) as Box<_>)
};
// Check statement is signed.
let statement = Statement::Candidate(justification.candidate_receipt);
if !check_statement(
&statement,
&justification.signature,
signer.clone(),
&justification.relay_chain_parent_hash
) {
// Check the header in the candidate_receipt match header given header.
if header.encode() != candidate_receipt.head_data.0 {
return Err(Box::new(ClientError::BadJustification(
"block candidate justification signature is invalid".to_string()
"block announced header does not match the one justified".to_string()
)) as Box<_>)
}