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

Commit

Permalink
grandpa: Re-add Grandpa runtime API for genesis authority set.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Oct 31, 2019
1 parent c3b1a98 commit b803eb8
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//! 4. Adds the retrieved external addresses as priority nodes to the peerset.

use authority_discovery_primitives::{AuthorityDiscoveryApi, AuthorityId, Signature};
use client::{blockchain::HeaderBackend, runtime_api::StorageProof};
use client::blockchain::HeaderBackend;
use error::{Error, Result};
use futures::{prelude::*, sync::mpsc::Receiver};
use log::{debug, error, log_enabled, warn};
Expand Down
2 changes: 2 additions & 0 deletions core/finality-grandpa/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
client = { package = "substrate-client", path = "../../client", default-features = false }
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
sr-primitives = { path = "../../sr-primitives", default-features = false }
Expand All @@ -14,6 +15,7 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] }
[features]
default = ["std"]
std = [
"client/std",
"codec/std",
"sr-primitives/std",
"rstd/std",
Expand Down
23 changes: 23 additions & 0 deletions core/finality-grandpa/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern crate alloc;
use serde::Serialize;
use codec::{Encode, Decode, Input, Codec};
use sr_primitives::{ConsensusEngineId, RuntimeDebug};
use client::decl_runtime_apis;
use rstd::borrow::Cow;
use rstd::vec::Vec;

Expand Down Expand Up @@ -209,3 +210,25 @@ impl<'a> Decode for VersionedAuthorityList<'a> {
Ok(authorities.into())
}
}

decl_runtime_apis! {
/// APIs for integrating the GRANDPA finality gadget into runtimes.
/// This should be implemented on the runtime side.
///
/// This is primarily used for negotiating authority-set changes for the
/// gadget. GRANDPA uses a signaling model of changing authority sets:
/// changes should be signaled with a delay of N blocks, and then automatically
/// applied in the runtime after those N blocks have passed.
///
/// The consensus protocol will coordinate the handoff externally.
#[api_version(2)]
pub trait GrandpaApi {
/// Get the current GRANDPA authorities and weights. This should not change except
/// for when changes are scheduled and the corresponding delay has passed.
///
/// 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() -> AuthorityList;
}
}
20 changes: 16 additions & 4 deletions core/finality-grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ use log::{debug, error, info};
use futures::sync::mpsc;
use client::{
BlockchainEvents, CallExecutor, Client, backend::Backend, error::Error as ClientError,
ExecutionStrategy,
};
use client::blockchain::HeaderBackend;
use codec::Encode;
use codec::{Decode, Encode};
use sr_primitives::generic::BlockId;
use sr_primitives::traits::{NumberFor, Block as BlockT, DigestFor, Zero};
use keystore::KeyStorePtr;
Expand Down Expand Up @@ -377,9 +378,20 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> GenesisAuthoritySetProvider<Block> for
RA: Send + Sync,
{
fn get(&self) -> Result<AuthorityList, ClientError> {
use finality_proof::AuthoritySetForFinalityProver;

self.authorities(&BlockId::Number(Zero::zero()))
self.executor()
.call(
&BlockId::Number(Zero::zero()),
"GrandpaApi_grandpa_authorities",
&[],
ExecutionStrategy::NativeElseWasm,
None,
)
.and_then(|call_result| {
Decode::decode(&mut &call_result[..])
.map_err(|err| ClientError::CallResultDecode(
"failed to decode GRANDPA authorities set proof".into(), err
))
})
}
}

Expand Down
20 changes: 17 additions & 3 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, AuthorityList};
use fg_primitives::{GRANDPA_ENGINE_ID, AuthorityList, GrandpaApi};
use state_machine::{backend::InMemory, prove_read, read_proof_check};

use authorities::AuthoritySet;
Expand Down Expand Up @@ -199,13 +199,15 @@ impl TestApi {
}
}

pub(crate) struct RuntimeApi;
pub(crate) struct RuntimeApi {
inner: TestApi,
}

impl ProvideRuntimeApi for TestApi {
type Api = RuntimeApi;

fn runtime_api<'a>(&'a self) -> ApiRef<'a, Self::Api> {
RuntimeApi.into()
RuntimeApi { inner: self.clone() }.into()
}
}

Expand Down Expand Up @@ -262,6 +264,18 @@ impl ApiExt<Block> for RuntimeApi {
}
}

impl GrandpaApi<Block> for RuntimeApi {
fn GrandpaApi_grandpa_authorities_runtime_api_impl(
&self,
_: &BlockId<Block>,
_: ExecutionContext,
_: Option<()>,
_: Vec<u8>,
) -> Result<NativeOrEncoded<AuthorityList>> {
Ok(self.inner.genesis_authorities.clone()).map(NativeOrEncoded::Native)
}
}

impl GenesisAuthoritySetProvider<Block> for TestApi {
fn get(&self) -> Result<AuthorityList> {
Ok(self.genesis_authorities.clone())
Expand Down
9 changes: 8 additions & 1 deletion node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use client::{
runtime_api as client_api, impl_runtime_apis
};
use aura_primitives::sr25519::AuthorityId as AuraId;
use grandpa::AuthorityId as GrandpaId;
use grandpa::AuthorityList as GrandpaAuthorityList;
use grandpa::fg_primitives;
use version::RuntimeVersion;
#[cfg(feature = "std")]
use version::NativeVersion;
Expand Down Expand Up @@ -352,4 +353,10 @@ impl_runtime_apis! {
opaque::SessionKeys::generate(seed)
}
}

impl fg_primitives::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> GrandpaAuthorityList {
Grandpa::grandpa_authorities()
}
}
}
9 changes: 8 additions & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ use version::RuntimeVersion;
#[cfg(any(feature = "std", test))]
use version::NativeVersion;
use primitives::OpaqueMetadata;
use grandpa::AuthorityId as GrandpaId;
use grandpa::AuthorityList as GrandpaAuthorityList;
use grandpa::fg_primitives;
use im_online::sr25519::{AuthorityId as ImOnlineId};
use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
use contracts_rpc_runtime_api::ContractExecResult;
Expand Down Expand Up @@ -610,6 +611,12 @@ impl_runtime_apis! {
}
}

impl fg_primitives::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> GrandpaAuthorityList {
Grandpa::grandpa_authorities()
}
}

impl babe_primitives::BabeApi<Block> for Runtime {
fn configuration() -> babe_primitives::BabeConfiguration {
// The choice of `c` parameter (where `1 - c` represents the
Expand Down
3 changes: 3 additions & 0 deletions srml/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
//!
//! In the future, it will also handle misbehavior reports, and on-chain
//! finality notifications.
//!
//! For full integration with GRANDPA, the `GrandpaApi` should be implemented.
//! The necessary items are re-exported via the `fg_primitives` crate.

#![cfg_attr(not(feature = "std"), no_std)]

Expand Down

0 comments on commit b803eb8

Please sign in to comment.