Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

extract some grandpa types to Primitives crate #12208

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions client/finality-grandpa/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ where
"state is for completed round; completed rounds must have a prevote ghost; qed.",
);

let mut current_rounds = CurrentRounds::new();
let mut current_rounds = CurrentRounds::<Block>::new();
current_rounds.insert(last_round_number + 1, HasVoted::No);

let set_state = VoterSetState::Live {
Expand Down Expand Up @@ -255,7 +255,7 @@ where
let base = set_state.prevote_ghost
.expect("state is for completed round; completed rounds must have a prevote ghost; qed.");

let mut current_rounds = CurrentRounds::new();
let mut current_rounds = CurrentRounds::<Block>::new();
current_rounds.insert(last_round_number + 1, HasVoted::No);

VoterSetState::Live {
Expand Down Expand Up @@ -500,7 +500,7 @@ mod test {
use super::*;
use sp_core::{crypto::UncheckedFrom, H256};
use sp_finality_grandpa::AuthorityId;
use substrate_test_runtime_client;
use substrate_test_runtime_client::{self, runtime::Block};

fn dummy_id() -> AuthorityId {
AuthorityId::unchecked_from([1; 32])
Expand Down Expand Up @@ -574,7 +574,7 @@ mod test {
.unwrap(),
);

let mut current_rounds = CurrentRounds::new();
let mut current_rounds = CurrentRounds::<Block>::new();
current_rounds.insert(round_number + 1, HasVoted::No);

assert_eq!(
Expand Down Expand Up @@ -667,7 +667,7 @@ mod test {
.unwrap(),
);

let mut current_rounds = CurrentRounds::new();
let mut current_rounds = CurrentRounds::<Block>::new();
current_rounds.insert(round_number + 1, HasVoted::No);

assert_eq!(
Expand Down
16 changes: 8 additions & 8 deletions client/finality-grandpa/src/communication/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub(super) struct VoteMessage<Block: BlockT> {
/// The voter set ID this message is from.
pub(super) set_id: SetId,
/// The message itself.
pub(super) message: SignedMessage<Block>,
pub(super) message: SignedMessage<Block::Header>,
}

/// Network level commit message with topic information.
Expand All @@ -361,7 +361,7 @@ pub(super) struct FullCommitMessage<Block: BlockT> {
/// The voter set ID this message is from.
pub(super) set_id: SetId,
/// The compact commit message.
pub(super) message: CompactCommit<Block>,
pub(super) message: CompactCommit<Block::Header>,
}

/// V1 neighbor packet. Neighbor packets are sent from nodes to their peers
Expand Down Expand Up @@ -406,7 +406,7 @@ pub(super) struct FullCatchUpMessage<Block: BlockT> {
/// The voter set ID this message is from.
pub(super) set_id: SetId,
/// The compact commit message.
pub(super) message: CatchUp<Block>,
pub(super) message: CatchUp<Block::Header>,
}

/// Misbehavior that peers can perform.
Expand Down Expand Up @@ -1071,7 +1071,7 @@ impl<Block: BlockT> Inner<Block> {

let (base_hash, base_number) = last_completed_round.base;

let catch_up = CatchUp::<Block> {
let catch_up = CatchUp::<Block::Header> {
round_number: last_completed_round.number,
prevotes,
precommits,
Expand Down Expand Up @@ -1651,8 +1651,8 @@ mod tests {
use crate::communication;
use sc_network::config::Role;
use sc_network_gossip::Validator as GossipValidatorT;
use sc_network_test::Block;
use sp_core::{crypto::UncheckedFrom, H256};
use substrate_test_runtime_client::runtime::{Block, Header};

// some random config (not really needed)
fn config() -> crate::Config {
Expand Down Expand Up @@ -1856,7 +1856,7 @@ mod tests {
&VoteMessage {
round: Round(1),
set_id: SetId(set_id),
message: SignedMessage::<Block> {
message: SignedMessage::<Header> {
message: finality_grandpa::Message::Prevote(finality_grandpa::Prevote {
target_hash: Default::default(),
target_number: 10,
Expand All @@ -1872,7 +1872,7 @@ mod tests {
&VoteMessage {
round: Round(1),
set_id: SetId(set_id),
message: SignedMessage::<Block> {
message: SignedMessage::<Header> {
message: finality_grandpa::Message::Prevote(finality_grandpa::Prevote {
target_hash: Default::default(),
target_number: 10,
Expand Down Expand Up @@ -1943,7 +1943,7 @@ mod tests {
votes: Default::default(),
});

let mut current_rounds = environment::CurrentRounds::new();
let mut current_rounds = environment::CurrentRounds::<Block>::new();
current_rounds.insert(3, environment::HasVoted::No);

let set_state =
Expand Down
27 changes: 15 additions & 12 deletions client/finality-grandpa/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
round: Round,
set_id: SetId,
voters: Arc<VoterSet<AuthorityId>>,
has_voted: HasVoted<B>,
) -> (impl Stream<Item = SignedMessage<B>> + Unpin, OutgoingMessages<B>) {
has_voted: HasVoted<B::Header>,
) -> (impl Stream<Item = SignedMessage<B::Header>> + Unpin, OutgoingMessages<B>) {
self.note_round(round, set_id, &voters);

let keystore = keystore.and_then(|ks| {
Expand Down Expand Up @@ -675,15 +675,15 @@ pub(crate) struct OutgoingMessages<Block: BlockT> {
round: RoundNumber,
set_id: SetIdNumber,
keystore: Option<LocalIdKeystore>,
sender: mpsc::Sender<SignedMessage<Block>>,
sender: mpsc::Sender<SignedMessage<Block::Header>>,
network: Arc<Mutex<GossipEngine<Block>>>,
has_voted: HasVoted<Block>,
has_voted: HasVoted<Block::Header>,
telemetry: Option<TelemetryHandle>,
}

impl<B: BlockT> Unpin for OutgoingMessages<B> {}

impl<Block: BlockT> Sink<Message<Block>> for OutgoingMessages<Block> {
impl<Block: BlockT> Sink<Message<Block::Header>> for OutgoingMessages<Block> {
type Error = Error;

fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Expand All @@ -694,7 +694,10 @@ impl<Block: BlockT> Sink<Message<Block>> for OutgoingMessages<Block> {
})
}

fn start_send(mut self: Pin<&mut Self>, mut msg: Message<Block>) -> Result<(), Self::Error> {
fn start_send(
mut self: Pin<&mut Self>,
mut msg: Message<Block::Header>,
) -> Result<(), Self::Error> {
// if we've voted on this round previously under the same key, send that vote instead
match &mut msg {
finality_grandpa::Message::PrimaryPropose(ref mut vote) => {
Expand Down Expand Up @@ -784,7 +787,7 @@ impl<Block: BlockT> Sink<Message<Block>> for OutgoingMessages<Block> {
// checks a compact commit. returns the cost associated with processing it if
// the commit was bad.
fn check_compact_commit<Block: BlockT>(
msg: &CompactCommit<Block>,
msg: &CompactCommit<Block::Header>,
voters: &VoterSet<AuthorityId>,
round: Round,
set_id: SetId,
Expand Down Expand Up @@ -852,7 +855,7 @@ fn check_compact_commit<Block: BlockT>(
// checks a catch up. returns the cost associated with processing it if
// the catch up was bad.
fn check_catch_up<Block: BlockT>(
msg: &CatchUp<Block>,
msg: &CatchUp<Block::Header>,
voters: &VoterSet<AuthorityId>,
set_id: SetId,
telemetry: Option<TelemetryHandle>,
Expand Down Expand Up @@ -902,7 +905,7 @@ fn check_catch_up<Block: BlockT>(
) -> Result<usize, ReputationChange>
where
B: BlockT,
I: Iterator<Item = (Message<B>, &'a AuthorityId, &'a AuthoritySignature)>,
I: Iterator<Item = (Message<B::Header>, &'a AuthorityId, &'a AuthoritySignature)>,
{
use crate::communication::gossip::Misbehavior;

Expand Down Expand Up @@ -996,7 +999,7 @@ impl<Block: BlockT> CommitsOut<Block> {
}
}

impl<Block: BlockT> Sink<(RoundNumber, Commit<Block>)> for CommitsOut<Block> {
impl<Block: BlockT> Sink<(RoundNumber, Commit<Block::Header>)> for CommitsOut<Block> {
type Error = Error;

fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
Expand All @@ -1005,7 +1008,7 @@ impl<Block: BlockT> Sink<(RoundNumber, Commit<Block>)> for CommitsOut<Block> {

fn start_send(
self: Pin<&mut Self>,
input: (RoundNumber, Commit<Block>),
input: (RoundNumber, Commit<Block::Header>),
) -> Result<(), Self::Error> {
if !self.is_voter {
return Ok(())
Expand All @@ -1027,7 +1030,7 @@ impl<Block: BlockT> Sink<(RoundNumber, Commit<Block>)> for CommitsOut<Block> {
.map(|signed| (signed.precommit, (signed.signature, signed.id)))
.unzip();

let compact_commit = CompactCommit::<Block> {
let compact_commit = CompactCommit::<Block::Header> {
target_hash: commit.target_hash,
target_number: commit.target_number,
precommits,
Expand Down
62 changes: 37 additions & 25 deletions client/finality-grandpa/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct CompletedRound<Block: BlockT> {
/// The target block base used for voting in the round.
pub base: (Block::Hash, NumberFor<Block>),
/// All the votes observed in the round.
pub votes: Vec<SignedMessage<Block>>,
pub votes: Vec<SignedMessage<Block::Header>>,
}

// Data about last completed rounds within a single voter set. Stores
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<Block: BlockT> CompletedRounds<Block> {

/// A map with voter status information for currently live rounds,
/// which votes have we cast and what are they.
pub type CurrentRounds<Block> = BTreeMap<RoundNumber, HasVoted<Block>>;
pub type CurrentRounds<Block> = BTreeMap<RoundNumber, HasVoted<<Block as BlockT>::Header>>;

/// The state of the current voter set, whether it is currently active or not
/// and information related to the previously completed rounds. Current round
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<Block: BlockT> VoterSetState<Block> {
authority_set,
);

let mut current_rounds = CurrentRounds::new();
let mut current_rounds = CurrentRounds::<Block>::new();
current_rounds.insert(1, HasVoted::No);

VoterSetState::Live { completed_rounds, current_rounds }
Expand Down Expand Up @@ -258,27 +258,27 @@ impl<Block: BlockT> VoterSetState<Block> {

/// Whether we've voted already during a prior run of the program.
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
pub enum HasVoted<Block: BlockT> {
pub enum HasVoted<Header: HeaderT> {
/// Has not voted already in this round.
No,
/// Has voted in this round.
Yes(AuthorityId, Vote<Block>),
Yes(AuthorityId, Vote<Header>),
}

/// The votes cast by this voter already during a prior run of the program.
#[derive(Debug, Clone, Decode, Encode, PartialEq)]
pub enum Vote<Block: BlockT> {
pub enum Vote<Header: HeaderT> {
/// Has cast a proposal.
Propose(PrimaryPropose<Block>),
Propose(PrimaryPropose<Header>),
/// Has cast a prevote.
Prevote(Option<PrimaryPropose<Block>>, Prevote<Block>),
Prevote(Option<PrimaryPropose<Header>>, Prevote<Header>),
/// Has cast a precommit (implies prevote.)
Precommit(Option<PrimaryPropose<Block>>, Prevote<Block>, Precommit<Block>),
Precommit(Option<PrimaryPropose<Header>>, Prevote<Header>, Precommit<Header>),
}

impl<Block: BlockT> HasVoted<Block> {
impl<Header: HeaderT> HasVoted<Header> {
/// Returns the proposal we should vote with (if any.)
pub fn propose(&self) -> Option<&PrimaryPropose<Block>> {
pub fn propose(&self) -> Option<&PrimaryPropose<Header>> {
match self {
HasVoted::Yes(_, Vote::Propose(propose)) => Some(propose),
HasVoted::Yes(_, Vote::Prevote(propose, _)) |
Expand All @@ -288,7 +288,7 @@ impl<Block: BlockT> HasVoted<Block> {
}

/// Returns the prevote we should vote with (if any.)
pub fn prevote(&self) -> Option<&Prevote<Block>> {
pub fn prevote(&self) -> Option<&Prevote<Header>> {
match self {
HasVoted::Yes(_, Vote::Prevote(_, prevote)) |
HasVoted::Yes(_, Vote::Precommit(_, prevote, _)) => Some(prevote),
Expand All @@ -297,7 +297,7 @@ impl<Block: BlockT> HasVoted<Block> {
}

/// Returns the precommit we should vote with (if any.)
pub fn precommit(&self) -> Option<&Precommit<Block>> {
pub fn precommit(&self) -> Option<&Precommit<Header>> {
match self {
HasVoted::Yes(_, Vote::Precommit(_, _, precommit)) => Some(precommit),
_ => None,
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<Block: BlockT> SharedVoterSetState<Block> {
}

/// Return vote status information for the current round.
pub(crate) fn has_voted(&self, round: RoundNumber) -> HasVoted<Block> {
pub(crate) fn has_voted(&self, round: RoundNumber) -> HasVoted<Block::Header> {
match &*self.inner.read() {
VoterSetState::Live { current_rounds, .. } => current_rounds
.get(&round)
Expand Down Expand Up @@ -771,7 +771,7 @@ where
fn proposed(
&self,
round: RoundNumber,
propose: PrimaryPropose<Block>,
propose: PrimaryPropose<Block::Header>,
) -> Result<(), Self::Error> {
let local_id = match self.voter_set_state.voting_on(round) {
Some(id) => id,
Expand Down Expand Up @@ -811,13 +811,17 @@ where
Ok(())
}

fn prevoted(&self, round: RoundNumber, prevote: Prevote<Block>) -> Result<(), Self::Error> {
fn prevoted(
&self,
round: RoundNumber,
prevote: Prevote<Block::Header>,
) -> Result<(), Self::Error> {
let local_id = match self.voter_set_state.voting_on(round) {
Some(id) => id,
None => return Ok(()),
};

let report_prevote_metrics = |prevote: &Prevote<Block>| {
let report_prevote_metrics = |prevote: &Prevote<Block::Header>| {
telemetry!(
self.telemetry;
CONSENSUS_DEBUG;
Expand Down Expand Up @@ -873,14 +877,14 @@ where
fn precommitted(
&self,
round: RoundNumber,
precommit: Precommit<Block>,
precommit: Precommit<Block::Header>,
) -> Result<(), Self::Error> {
let local_id = match self.voter_set_state.voting_on(round) {
Some(id) => id,
None => return Ok(()),
};

let report_precommit_metrics = |precommit: &Precommit<Block>| {
let report_precommit_metrics = |precommit: &Precommit<Block::Header>| {
telemetry!(
self.telemetry;
CONSENSUS_DEBUG;
Expand Down Expand Up @@ -1065,7 +1069,7 @@ where
hash: Block::Hash,
number: NumberFor<Block>,
round: RoundNumber,
commit: Commit<Block>,
commit: Commit<Block::Header>,
) -> Result<(), Self::Error> {
finalize_block(
self.client.clone(),
Expand All @@ -1092,7 +1096,11 @@ where
fn prevote_equivocation(
&self,
_round: RoundNumber,
equivocation: finality_grandpa::Equivocation<Self::Id, Prevote<Block>, Self::Signature>,
equivocation: finality_grandpa::Equivocation<
Self::Id,
Prevote<Block::Header>,
Self::Signature,
>,
) {
warn!(target: "afg", "Detected prevote equivocation in the finality worker: {:?}", equivocation);
if let Err(err) = self.report_equivocation(equivocation.into()) {
Expand All @@ -1103,7 +1111,11 @@ where
fn precommit_equivocation(
&self,
_round: RoundNumber,
equivocation: finality_grandpa::Equivocation<Self::Id, Precommit<Block>, Self::Signature>,
equivocation: finality_grandpa::Equivocation<
Self::Id,
Precommit<Block::Header>,
Self::Signature,
>,
) {
warn!(target: "afg", "Detected precommit equivocation in the finality worker: {:?}", equivocation);
if let Err(err) = self.report_equivocation(equivocation.into()) {
Expand All @@ -1114,11 +1126,11 @@ where

pub(crate) enum JustificationOrCommit<Block: BlockT> {
Justification(GrandpaJustification<Block>),
Commit((RoundNumber, Commit<Block>)),
Commit((RoundNumber, Commit<Block::Header>)),
}

impl<Block: BlockT> From<(RoundNumber, Commit<Block>)> for JustificationOrCommit<Block> {
fn from(commit: (RoundNumber, Commit<Block>)) -> JustificationOrCommit<Block> {
impl<Block: BlockT> From<(RoundNumber, Commit<Block::Header>)> for JustificationOrCommit<Block> {
fn from(commit: (RoundNumber, Commit<Block::Header>)) -> JustificationOrCommit<Block> {
JustificationOrCommit::Commit(commit)
}
}
Expand Down
Loading