mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-18 09:25:40 +00:00
extract some grandpa types to Primitives crate (#12208)
* extract some grandpa types to primitives * fmt * fmt
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
marker::PhantomData,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@@ -42,9 +43,25 @@ use crate::{AuthorityList, Commit, Error};
|
||||
/// nodes, and are used by syncing nodes to prove authority set handoffs.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, Eq, Debug)]
|
||||
pub struct GrandpaJustification<Block: BlockT> {
|
||||
pub(crate) round: u64,
|
||||
pub(crate) commit: Commit<Block>,
|
||||
pub(crate) votes_ancestries: Vec<Block::Header>,
|
||||
/// The GRANDPA justification for block finality.
|
||||
pub justification: sp_finality_grandpa::GrandpaJustification<Block::Header>,
|
||||
_block: PhantomData<Block>,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> From<sp_finality_grandpa::GrandpaJustification<Block::Header>>
|
||||
for GrandpaJustification<Block>
|
||||
{
|
||||
fn from(justification: sp_finality_grandpa::GrandpaJustification<Block::Header>) -> Self {
|
||||
Self { justification, _block: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> Into<sp_finality_grandpa::GrandpaJustification<Block::Header>>
|
||||
for GrandpaJustification<Block>
|
||||
{
|
||||
fn into(self) -> sp_finality_grandpa::GrandpaJustification<Block::Header> {
|
||||
self.justification
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
@@ -53,8 +70,8 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
pub fn from_commit<C>(
|
||||
client: &Arc<C>,
|
||||
round: u64,
|
||||
commit: Commit<Block>,
|
||||
) -> Result<GrandpaJustification<Block>, Error>
|
||||
commit: Commit<Block::Header>,
|
||||
) -> Result<Self, Error>
|
||||
where
|
||||
C: HeaderBackend<Block>,
|
||||
{
|
||||
@@ -108,7 +125,7 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(GrandpaJustification { round, commit, votes_ancestries })
|
||||
Ok(sp_finality_grandpa::GrandpaJustification { round, commit, votes_ancestries }.into())
|
||||
}
|
||||
|
||||
/// Decode a GRANDPA justification and validate the commit and the votes'
|
||||
@@ -118,15 +135,17 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
finalized_target: (Block::Hash, NumberFor<Block>),
|
||||
set_id: u64,
|
||||
voters: &VoterSet<AuthorityId>,
|
||||
) -> Result<GrandpaJustification<Block>, ClientError>
|
||||
) -> Result<Self, ClientError>
|
||||
where
|
||||
NumberFor<Block>: finality_grandpa::BlockNumberOps,
|
||||
{
|
||||
let justification = GrandpaJustification::<Block>::decode(&mut &*encoded)
|
||||
.map_err(|_| ClientError::JustificationDecode)?;
|
||||
|
||||
if (justification.commit.target_hash, justification.commit.target_number) !=
|
||||
finalized_target
|
||||
if (
|
||||
justification.justification.commit.target_hash,
|
||||
justification.justification.commit.target_number,
|
||||
) != finalized_target
|
||||
{
|
||||
let msg = "invalid commit target in grandpa justification".to_string();
|
||||
Err(ClientError::BadJustification(msg))
|
||||
@@ -157,9 +176,10 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
{
|
||||
use finality_grandpa::Chain;
|
||||
|
||||
let ancestry_chain = AncestryChain::<Block>::new(&self.votes_ancestries);
|
||||
let ancestry_chain = AncestryChain::<Block>::new(&self.justification.votes_ancestries);
|
||||
|
||||
match finality_grandpa::validate_commit(&self.commit, voters, &ancestry_chain) {
|
||||
match finality_grandpa::validate_commit(&self.justification.commit, voters, &ancestry_chain)
|
||||
{
|
||||
Ok(ref result) if result.is_valid() => {},
|
||||
_ => {
|
||||
let msg = "invalid commit in grandpa justification".to_string();
|
||||
@@ -171,6 +191,7 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
// should serve as the root block for populating ancestry (i.e.
|
||||
// collect all headers from all precommit blocks to the base)
|
||||
let base_hash = self
|
||||
.justification
|
||||
.commit
|
||||
.precommits
|
||||
.iter()
|
||||
@@ -186,12 +207,12 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut visited_hashes = HashSet::new();
|
||||
for signed in self.commit.precommits.iter() {
|
||||
for signed in self.justification.commit.precommits.iter() {
|
||||
if !sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
&finality_grandpa::Message::Precommit(signed.precommit.clone()),
|
||||
&signed.id,
|
||||
&signed.signature,
|
||||
self.round,
|
||||
self.justification.round,
|
||||
set_id,
|
||||
&mut buf,
|
||||
) {
|
||||
@@ -220,8 +241,12 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
}
|
||||
}
|
||||
|
||||
let ancestry_hashes: HashSet<_> =
|
||||
self.votes_ancestries.iter().map(|h: &Block::Header| h.hash()).collect();
|
||||
let ancestry_hashes: HashSet<_> = self
|
||||
.justification
|
||||
.votes_ancestries
|
||||
.iter()
|
||||
.map(|h: &Block::Header| h.hash())
|
||||
.collect();
|
||||
|
||||
if visited_hashes != ancestry_hashes {
|
||||
return Err(ClientError::BadJustification(
|
||||
@@ -235,7 +260,7 @@ impl<Block: BlockT> GrandpaJustification<Block> {
|
||||
|
||||
/// The target block number and hash that this justifications proves finality for.
|
||||
pub fn target(&self) -> (NumberFor<Block>, Block::Hash) {
|
||||
(self.commit.target_number, self.commit.target_hash)
|
||||
(self.justification.commit.target_number, self.justification.commit.target_hash)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user