Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
grandpa: Introduce named AuthorityList type.
  • Loading branch information
jimpo committed Oct 30, 2019
commit 2bfe54804dda435d638bd7b30cd7264e314f4e9c
7 changes: 5 additions & 2 deletions core/finality-grandpa/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ pub type SetId = u64;
/// The round indicator.
pub type RoundNumber = u64;

// A list of Grandpa authorities with associated weights.
pub type AuthorityList = Vec<(AuthorityId, AuthorityWeight)>;

/// A scheduled change of authority set.
#[cfg_attr(feature = "std", derive(Serialize))]
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug)]
pub struct ScheduledChange<N> {
/// The new authorities after the change, along with their respective weights.
pub next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
pub next_authorities: AuthorityList,
/// The number of blocks to delay.
pub delay: N,
}
Expand Down Expand Up @@ -172,6 +175,6 @@ decl_runtime_apis! {
/// When called at block B, it will return the set of authorities that should be
/// used to finalize descendants of this block (B+1, B+2, ...). The block B itself
/// is finalized by the authorities from block B-1.
fn grandpa_authorities() -> Vec<(AuthorityId, AuthorityWeight)>;
fn grandpa_authorities() -> AuthorityList;
}
}
8 changes: 4 additions & 4 deletions core/finality-grandpa/src/authorities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use grandpa::voter_set::VoterSet;
use codec::{Encode, Decode};
use log::{debug, info};
use substrate_telemetry::{telemetry, CONSENSUS_INFO};
use fg_primitives::AuthorityId;
use fg_primitives::{AuthorityId, AuthorityList};

use std::cmp::Ord;
use std::fmt::Debug;
Expand Down Expand Up @@ -86,7 +86,7 @@ pub(crate) struct Status<H, N> {
/// A set of authorities.
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
pub(crate) struct AuthoritySet<H, N> {
pub(crate) current_authorities: Vec<(AuthorityId, u64)>,
pub(crate) current_authorities: AuthorityList,
pub(crate) set_id: u64,
// Tree of pending standard changes across forks. Standard changes are
// enacted on finality and must be enacted (i.e. finalized) in-order across
Expand All @@ -103,7 +103,7 @@ where H: PartialEq,
N: Ord,
{
/// Get a genesis set with given authorities.
pub(crate) fn genesis(initial: Vec<(AuthorityId, u64)>) -> Self {
pub(crate) fn genesis(initial: AuthorityList) -> Self {
AuthoritySet {
current_authorities: initial,
set_id: 0,
Expand Down Expand Up @@ -390,7 +390,7 @@ pub(crate) enum DelayKind<N> {
#[derive(Debug, Clone, Encode, PartialEq)]
pub(crate) struct PendingChange<H, N> {
/// The new authorities and weights to apply.
pub(crate) next_authorities: Vec<(AuthorityId, u64)>,
pub(crate) next_authorities: AuthorityList,
/// How deep in the chain the announcing block must be
/// before the change is applied.
pub(crate) delay: N,
Expand Down
8 changes: 4 additions & 4 deletions core/finality-grandpa/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use fork_tree::ForkTree;
use grandpa::round::State as RoundState;
use sr_primitives::traits::{Block as BlockT, NumberFor};
use log::{info, warn};
use fg_primitives::{AuthorityId, AuthorityWeight, SetId, RoundNumber};
use fg_primitives::{AuthorityId, AuthorityList, SetId, RoundNumber};

use crate::authorities::{AuthoritySet, SharedAuthoritySet, PendingChange, DelayKind};
use crate::consensus_changes::{SharedConsensusChanges, ConsensusChanges};
Expand Down Expand Up @@ -55,15 +55,15 @@ type V0VoterSetState<H, N> = (RoundNumber, RoundState<H, N>);

#[derive(Debug, Clone, Encode, Decode, PartialEq)]
struct V0PendingChange<H, N> {
next_authorities: Vec<(AuthorityId, AuthorityWeight)>,
next_authorities: AuthorityList,
delay: N,
canon_height: N,
canon_hash: H,
}

#[derive(Debug, Clone, Encode, Decode, PartialEq)]
struct V0AuthoritySet<H, N> {
current_authorities: Vec<(AuthorityId, AuthorityWeight)>,
current_authorities: AuthorityList,
set_id: SetId,
pending_changes: Vec<V0PendingChange<H, N>>,
}
Expand Down Expand Up @@ -266,7 +266,7 @@ pub(crate) fn load_persistent<Block: BlockT, B, G>(
-> ClientResult<PersistentData<Block>>
where
B: AuxStore,
G: FnOnce() -> ClientResult<Vec<(AuthorityId, AuthorityWeight)>>,
G: FnOnce() -> ClientResult<AuthorityList>,
{
let version: Option<u32> = load_decode(backend, VERSION_KEY)?;
let consensus_changes = load_decode(backend, CONSENSUS_CHANGES_KEY)?
Expand Down
3 changes: 2 additions & 1 deletion core/finality-grandpa/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ use gossip::{
GossipMessage, FullCatchUpMessage, FullCommitMessage, VoteMessage, GossipValidator
};
use fg_primitives::{
AuthorityPair, AuthorityId, AuthoritySignature, SetId as SetIdNumber, RoundNumber,
AuthorityPair, AuthorityId, AuthorityList, AuthoritySignature,
SetId as SetIdNumber, RoundNumber,
};

pub mod gossip;
Expand Down
4 changes: 2 additions & 2 deletions core/finality-grandpa/src/communication/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sr_primitives::traits::NumberFor;

use crate::environment::SharedVoterSetState;
use super::gossip::{self, GossipValidator};
use super::{AuthorityId, VoterSet, Round, SetId};
use super::{AuthorityId, AuthorityList, VoterSet, Round, SetId};

enum Event {
MessagesFor(Hash, mpsc::UnboundedSender<network_gossip::TopicNotification>),
Expand Down Expand Up @@ -200,7 +200,7 @@ fn make_test_network() -> (
)
}

fn make_ids(keys: &[Ed25519Keyring]) -> Vec<(AuthorityId, u64)> {
fn make_ids(keys: &[Ed25519Keyring]) -> AuthorityList {
keys.iter()
.map(|key| key.clone().public().into())
.map(|id| (id, 1))
Expand Down
30 changes: 15 additions & 15 deletions core/finality-grandpa/src/finality_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use sr_primitives::{
};
use primitives::{H256, Blake2Hasher};
use substrate_telemetry::{telemetry, CONSENSUS_INFO};
use fg_primitives::AuthorityId;
use fg_primitives::{AuthorityId, AuthorityList};

use crate::justification::GrandpaJustification;

Expand All @@ -60,7 +60,7 @@ const MAX_FRAGMENTS_IN_PROOF: usize = 8;
/// GRANDPA authority set related methods for the finality proof provider.
pub trait AuthoritySetForFinalityProver<Block: BlockT>: Send + Sync {
/// Call GrandpaApi::grandpa_authorities at given block.
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<Vec<(AuthorityId, u64)>>;
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<AuthorityList>;
/// Prove call of GrandpaApi::grandpa_authorities at given block.
fn prove_authorities(&self, block: &BlockId<Block>) -> ClientResult<Vec<Vec<u8>>>;
}
Expand All @@ -72,7 +72,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> AuthoritySetForFinalityProver<Block> fo
E: CallExecutor<Block, Blake2Hasher> + 'static + Clone + Send + Sync,
RA: Send + Sync,
{
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<Vec<(AuthorityId, u64)>> {
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<AuthorityList> {
self.executor().call(
block,
"GrandpaApi_grandpa_authorities",
Expand All @@ -98,7 +98,7 @@ pub trait AuthoritySetForFinalityChecker<Block: BlockT>: Send + Sync {
hash: Block::Hash,
header: Block::Header,
proof: Vec<Vec<u8>>,
) -> ClientResult<Vec<(AuthorityId, u64)>>;
) -> ClientResult<AuthorityList>;
}

/// FetchChecker-based implementation of AuthoritySetForFinalityChecker.
Expand All @@ -108,7 +108,7 @@ impl<Block: BlockT> AuthoritySetForFinalityChecker<Block> for Arc<dyn FetchCheck
hash: Block::Hash,
header: Block::Header,
proof: Vec<Vec<u8>>,
) -> ClientResult<Vec<(AuthorityId, u64)>> {
) -> ClientResult<AuthorityList> {
let request = RemoteCallRequest {
block: hash,
header,
Expand All @@ -119,7 +119,7 @@ impl<Block: BlockT> AuthoritySetForFinalityChecker<Block> for Arc<dyn FetchCheck

self.check_execution_proof(&request, proof)
.and_then(|authorities| {
let authorities: Vec<(AuthorityId, u64)> = Decode::decode(&mut &authorities[..])
let authorities: AuthorityList = Decode::decode(&mut &authorities[..])
.map_err(|err| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(), err
))?;
Expand Down Expand Up @@ -189,7 +189,7 @@ pub struct FinalityEffects<Header: HeaderT> {
/// New authorities set id that should be applied starting from block.
pub new_set_id: u64,
/// New authorities set that should be applied starting from block.
pub new_authorities: Vec<(AuthorityId, u64)>,
pub new_authorities: AuthorityList,
}

/// Single fragment of proof-of-finality.
Expand Down Expand Up @@ -408,7 +408,7 @@ pub(crate) fn prove_finality<Block: BlockT<Hash=H256>, B: BlockchainBackend<Bloc
pub(crate) fn check_finality_proof<Block: BlockT<Hash=H256>, B>(
blockchain: &B,
current_set_id: u64,
current_authorities: Vec<(AuthorityId, u64)>,
current_authorities: AuthorityList,
authorities_provider: &dyn AuthoritySetForFinalityChecker<Block>,
remote_proof: Vec<u8>,
) -> ClientResult<FinalityEffects<Block::Header>>
Expand All @@ -427,7 +427,7 @@ pub(crate) fn check_finality_proof<Block: BlockT<Hash=H256>, B>(
fn do_check_finality_proof<Block: BlockT<Hash=H256>, B, J>(
blockchain: &B,
current_set_id: u64,
current_authorities: Vec<(AuthorityId, u64)>,
current_authorities: AuthorityList,
authorities_provider: &dyn AuthoritySetForFinalityChecker<Block>,
remote_proof: Vec<u8>,
) -> ClientResult<FinalityEffects<Block::Header>>
Expand Down Expand Up @@ -522,12 +522,12 @@ fn check_finality_proof_fragment<Block: BlockT<Hash=H256>, B, J>(

/// Authorities set from initial authorities set or finality effects.
enum AuthoritiesOrEffects<Header: HeaderT> {
Authorities(u64, Vec<(AuthorityId, u64)>),
Authorities(u64, AuthorityList),
Effects(FinalityEffects<Header>),
}

impl<Header: HeaderT> AuthoritiesOrEffects<Header> {
pub fn extract_authorities(self) -> (u64, Vec<(AuthorityId, u64)>) {
pub fn extract_authorities(self) -> (u64, AuthorityList) {
match self {
AuthoritiesOrEffects::Authorities(set_id, authorities) => (set_id, authorities),
AuthoritiesOrEffects::Effects(effects) => (effects.new_set_id, effects.new_authorities),
Expand Down Expand Up @@ -581,10 +581,10 @@ pub(crate) mod tests {

impl<GetAuthorities, ProveAuthorities> AuthoritySetForFinalityProver<Block> for (GetAuthorities, ProveAuthorities)
where
GetAuthorities: Send + Sync + Fn(BlockId<Block>) -> ClientResult<Vec<(AuthorityId, u64)>>,
GetAuthorities: Send + Sync + Fn(BlockId<Block>) -> ClientResult<AuthorityList>,
ProveAuthorities: Send + Sync + Fn(BlockId<Block>) -> ClientResult<Vec<Vec<u8>>>,
{
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<Vec<(AuthorityId, u64)>> {
fn authorities(&self, block: &BlockId<Block>) -> ClientResult<AuthorityList> {
self.0(*block)
}

Expand All @@ -597,14 +597,14 @@ pub(crate) mod tests {

impl<Closure> AuthoritySetForFinalityChecker<Block> for ClosureAuthoritySetForFinalityChecker<Closure>
where
Closure: Send + Sync + Fn(H256, Header, Vec<Vec<u8>>) -> ClientResult<Vec<(AuthorityId, u64)>>,
Closure: Send + Sync + Fn(H256, Header, Vec<Vec<u8>>) -> ClientResult<AuthorityList>,
{
fn check_authorities_proof(
&self,
hash: H256,
header: Header,
proof: Vec<Vec<u8>>,
) -> ClientResult<Vec<(AuthorityId, u64)>> {
) -> ClientResult<AuthorityList> {
self.0(hash, header, proof)
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/finality-grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use sr_primitives::generic::BlockId;
use sr_primitives::traits::{
NumberFor, Block as BlockT, DigestFor, ProvideRuntimeApi
};
use fg_primitives::{GrandpaApi, AuthorityPair};
use fg_primitives::{GrandpaApi, AuthorityList, AuthorityPair};
use keystore::KeyStorePtr;
use inherents::InherentDataProviders;
use consensus_common::SelectChain;
Expand Down Expand Up @@ -108,7 +108,7 @@ use environment::{Environment, VoterSetState};
use import::GrandpaBlockImport;
use until_imported::UntilGlobalMessageBlocksImported;
use communication::NetworkBridge;
use fg_primitives::{AuthoritySignature, SetId, AuthorityWeight};
use fg_primitives::{AuthoritySignature, SetId};

// Re-export these two because it's just so damn convenient.
pub use fg_primitives::{AuthorityId, ScheduledChange};
Expand Down Expand Up @@ -295,7 +295,7 @@ pub(crate) struct NewAuthoritySet<H, N> {
pub(crate) canon_number: N,
pub(crate) canon_hash: H,
pub(crate) set_id: SetId,
pub(crate) authorities: Vec<(AuthorityId, AuthorityWeight)>,
pub(crate) authorities: AuthorityList,
}

/// Commands issued to the voter.
Expand Down
10 changes: 5 additions & 5 deletions core/finality-grandpa/src/light_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use sr_primitives::Justification;
use sr_primitives::traits::{
NumberFor, Block as BlockT, Header as HeaderT, ProvideRuntimeApi, DigestFor,
};
use fg_primitives::{self, GrandpaApi, AuthorityId};
use fg_primitives::{self, GrandpaApi, AuthorityId, AuthorityList};
use sr_primitives::generic::BlockId;
use primitives::{H256, Blake2Hasher};

Expand Down Expand Up @@ -110,7 +110,7 @@ struct LightImportData<Block: BlockT<Hash=H256>> {
#[derive(Debug, Encode, Decode)]
struct LightAuthoritySet {
set_id: u64,
authorities: Vec<(AuthorityId, u64)>,
authorities: AuthorityList,
}

impl<B, E, Block: BlockT<Hash=H256>, RA> GrandpaLightBlockImport<B, E, Block, RA> {
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> FinalityProofImport<Block>

impl LightAuthoritySet {
/// Get a genesis set with given authorities.
pub fn genesis(initial: Vec<(AuthorityId, u64)>) -> Self {
pub fn genesis(initial: AuthorityList) -> Self {
LightAuthoritySet {
set_id: fg_primitives::SetId::default(),
authorities: initial,
Expand All @@ -207,12 +207,12 @@ impl LightAuthoritySet {
}

/// Get latest authorities set.
pub fn authorities(&self) -> Vec<(AuthorityId, u64)> {
pub fn authorities(&self) -> AuthorityList {
self.authorities.clone()
}

/// Set new authorities set.
pub fn update(&mut self, set_id: u64, authorities: Vec<(AuthorityId, u64)>) {
pub fn update(&mut self, set_id: u64, authorities: AuthorityList) {
self.set_id = set_id;
std::mem::replace(&mut self.authorities, authorities);
}
Expand Down
14 changes: 7 additions & 7 deletions core/finality-grandpa/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use codec::Decode;
use sr_primitives::traits::{ApiRef, ProvideRuntimeApi, Header as HeaderT};
use sr_primitives::generic::{BlockId, DigestItem};
use primitives::{NativeOrEncoded, ExecutionContext, crypto::Public};
use fg_primitives::{GRANDPA_ENGINE_ID, AuthorityId};
use fg_primitives::{GRANDPA_ENGINE_ID, AuthorityList};

use authorities::AuthoritySet;
use finality_proof::{FinalityProofProvider, AuthoritySetForFinalityProver, AuthoritySetForFinalityChecker};
Expand Down Expand Up @@ -187,11 +187,11 @@ impl Future for Exit {

#[derive(Default, Clone)]
pub(crate) struct TestApi {
genesis_authorities: Vec<(AuthorityId, u64)>,
genesis_authorities: AuthorityList,
}

impl TestApi {
pub fn new(genesis_authorities: Vec<(AuthorityId, u64)>) -> Self {
pub fn new(genesis_authorities: AuthorityList) -> Self {
TestApi {
genesis_authorities,
}
Expand Down Expand Up @@ -270,13 +270,13 @@ impl GrandpaApi<Block> for RuntimeApi {
_: ExecutionContext,
_: Option<()>,
_: Vec<u8>,
) -> Result<NativeOrEncoded<Vec<(AuthorityId, u64)>>> {
) -> Result<NativeOrEncoded<AuthorityList>> {
Ok(self.inner.genesis_authorities.clone()).map(NativeOrEncoded::Native)
}
}

impl AuthoritySetForFinalityProver<Block> for TestApi {
fn authorities(&self, block: &BlockId<Block>) -> Result<Vec<(AuthorityId, u64)>> {
fn authorities(&self, block: &BlockId<Block>) -> Result<AuthorityList> {
let runtime_api = RuntimeApi { inner: self.clone() };
runtime_api.GrandpaApi_grandpa_authorities_runtime_api_impl(block, ExecutionContext::Syncing, None, Vec::new())
.map(|v| match v {
Expand All @@ -296,15 +296,15 @@ impl AuthoritySetForFinalityChecker<Block> for TestApi {
_hash: <Block as BlockT>::Hash,
_header: <Block as BlockT>::Header,
proof: Vec<Vec<u8>>,
) -> Result<Vec<(AuthorityId, u64)>> {
) -> Result<AuthorityList> {
Decode::decode(&mut &proof[0][..])
.map_err(|_| unreachable!("incorrect value is passed as GRANDPA authorities proof"))
}
}

const TEST_GOSSIP_DURATION: Duration = Duration::from_millis(500);

fn make_ids(keys: &[Ed25519Keyring]) -> Vec<(AuthorityId, u64)> {
fn make_ids(keys: &[Ed25519Keyring]) -> AuthorityList {
keys.iter().map(|key| key.clone().public().into()).map(|id| (id, 1)).collect()
}

Expand Down
Loading