core: grandpa: verify commit target in justification (#2201)

This commit is contained in:
André Silva
2019-04-04 15:56:49 +01:00
committed by Gav Wood
parent 6920b169cd
commit cb3c912b1a
2 changed files with 15 additions and 6 deletions
@@ -546,8 +546,9 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> GrandpaBlockImport<B, E, Block, RA
justification: Justification,
enacts_change: bool,
) -> Result<(), ConsensusError> {
let justification = GrandpaJustification::decode_and_verify(
let justification = GrandpaJustification::decode_and_verify_finalizes(
justification,
(hash, number),
self.authority_set.set_id(),
&self.authority_set.current_authorities(),
);
@@ -93,18 +93,26 @@ impl<Block: BlockT<Hash=H256>> GrandpaJustification<Block> {
}
/// Decode a GRANDPA justification and validate the commit and the votes'
/// ancestry proofs.
pub(crate) fn decode_and_verify(
/// ancestry proofs finalize the given block.
pub(crate) fn decode_and_verify_finalizes(
encoded: Vec<u8>,
finalized_target: (Block::Hash, NumberFor<Block>),
set_id: u64,
voters: &VoterSet<AuthorityId>,
) -> Result<GrandpaJustification<Block>, ClientError> where
NumberFor<Block>: grandpa::BlockNumberOps,
{
GrandpaJustification::<Block>::decode(&mut &*encoded).ok_or_else(|| {
let justification = GrandpaJustification::<Block>::decode(&mut &*encoded).ok_or_else(|| {
let msg = "failed to decode grandpa justification".to_string();
ClientErrorKind::BadJustification(msg).into()
}).and_then(|just| just.verify(set_id, voters).map(|_| just))
ClientError::from(ClientErrorKind::BadJustification(msg))
})?;
if (justification.commit.target_hash, justification.commit.target_number) != finalized_target {
let msg = "invalid commit target in grandpa justification".to_string();
Err(ClientErrorKind::BadJustification(msg).into())
} else {
justification.verify(set_id, voters).map(|_| justification)
}
}
/// Validate the commit and the votes' ancestry proofs.