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

Commit b803eb8

Browse files
committed
grandpa: Re-add Grandpa runtime API for genesis authority set.
1 parent c3b1a98 commit b803eb8

File tree

9 files changed

+79
-10
lines changed

9 files changed

+79
-10
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/authority-discovery/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//! 4. Adds the retrieved external addresses as priority nodes to the peerset.
4545
4646
use authority_discovery_primitives::{AuthorityDiscoveryApi, AuthorityId, Signature};
47-
use client::{blockchain::HeaderBackend, runtime_api::StorageProof};
47+
use client::blockchain::HeaderBackend;
4848
use error::{Error, Result};
4949
use futures::{prelude::*, sync::mpsc::Receiver};
5050
use log::{debug, error, log_enabled, warn};

core/finality-grandpa/primitives/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
55
edition = "2018"
66

77
[dependencies]
8+
client = { package = "substrate-client", path = "../../client", default-features = false }
89
app-crypto = { package = "substrate-application-crypto", path = "../../application-crypto", default-features = false }
910
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
1011
sr-primitives = { path = "../../sr-primitives", default-features = false }
@@ -14,6 +15,7 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] }
1415
[features]
1516
default = ["std"]
1617
std = [
18+
"client/std",
1719
"codec/std",
1820
"sr-primitives/std",
1921
"rstd/std",

core/finality-grandpa/primitives/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern crate alloc;
2525
use serde::Serialize;
2626
use codec::{Encode, Decode, Input, Codec};
2727
use sr_primitives::{ConsensusEngineId, RuntimeDebug};
28+
use client::decl_runtime_apis;
2829
use rstd::borrow::Cow;
2930
use rstd::vec::Vec;
3031

@@ -209,3 +210,25 @@ impl<'a> Decode for VersionedAuthorityList<'a> {
209210
Ok(authorities.into())
210211
}
211212
}
213+
214+
decl_runtime_apis! {
215+
/// APIs for integrating the GRANDPA finality gadget into runtimes.
216+
/// This should be implemented on the runtime side.
217+
///
218+
/// This is primarily used for negotiating authority-set changes for the
219+
/// gadget. GRANDPA uses a signaling model of changing authority sets:
220+
/// changes should be signaled with a delay of N blocks, and then automatically
221+
/// applied in the runtime after those N blocks have passed.
222+
///
223+
/// The consensus protocol will coordinate the handoff externally.
224+
#[api_version(2)]
225+
pub trait GrandpaApi {
226+
/// Get the current GRANDPA authorities and weights. This should not change except
227+
/// for when changes are scheduled and the corresponding delay has passed.
228+
///
229+
/// When called at block B, it will return the set of authorities that should be
230+
/// used to finalize descendants of this block (B+1, B+2, ...). The block B itself
231+
/// is finalized by the authorities from block B-1.
232+
fn grandpa_authorities() -> AuthorityList;
233+
}
234+
}

core/finality-grandpa/src/lib.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ use log::{debug, error, info};
5757
use futures::sync::mpsc;
5858
use client::{
5959
BlockchainEvents, CallExecutor, Client, backend::Backend, error::Error as ClientError,
60+
ExecutionStrategy,
6061
};
6162
use client::blockchain::HeaderBackend;
62-
use codec::Encode;
63+
use codec::{Decode, Encode};
6364
use sr_primitives::generic::BlockId;
6465
use sr_primitives::traits::{NumberFor, Block as BlockT, DigestFor, Zero};
6566
use keystore::KeyStorePtr;
@@ -377,9 +378,20 @@ impl<B, E, Block: BlockT<Hash=H256>, RA> GenesisAuthoritySetProvider<Block> for
377378
RA: Send + Sync,
378379
{
379380
fn get(&self) -> Result<AuthorityList, ClientError> {
380-
use finality_proof::AuthoritySetForFinalityProver;
381-
382-
self.authorities(&BlockId::Number(Zero::zero()))
381+
self.executor()
382+
.call(
383+
&BlockId::Number(Zero::zero()),
384+
"GrandpaApi_grandpa_authorities",
385+
&[],
386+
ExecutionStrategy::NativeElseWasm,
387+
None,
388+
)
389+
.and_then(|call_result| {
390+
Decode::decode(&mut &call_result[..])
391+
.map_err(|err| ClientError::CallResultDecode(
392+
"failed to decode GRANDPA authorities set proof".into(), err
393+
))
394+
})
383395
}
384396
}
385397

core/finality-grandpa/src/tests.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use codec::Decode;
3939
use sr_primitives::traits::{ApiRef, ProvideRuntimeApi, Header as HeaderT};
4040
use sr_primitives::generic::{BlockId, DigestItem};
4141
use primitives::{NativeOrEncoded, ExecutionContext, crypto::Public};
42-
use fg_primitives::{GRANDPA_ENGINE_ID, AuthorityList};
42+
use fg_primitives::{GRANDPA_ENGINE_ID, AuthorityList, GrandpaApi};
4343
use state_machine::{backend::InMemory, prove_read, read_proof_check};
4444

4545
use authorities::AuthoritySet;
@@ -199,13 +199,15 @@ impl TestApi {
199199
}
200200
}
201201

202-
pub(crate) struct RuntimeApi;
202+
pub(crate) struct RuntimeApi {
203+
inner: TestApi,
204+
}
203205

204206
impl ProvideRuntimeApi for TestApi {
205207
type Api = RuntimeApi;
206208

207209
fn runtime_api<'a>(&'a self) -> ApiRef<'a, Self::Api> {
208-
RuntimeApi.into()
210+
RuntimeApi { inner: self.clone() }.into()
209211
}
210212
}
211213

@@ -262,6 +264,18 @@ impl ApiExt<Block> for RuntimeApi {
262264
}
263265
}
264266

267+
impl GrandpaApi<Block> for RuntimeApi {
268+
fn GrandpaApi_grandpa_authorities_runtime_api_impl(
269+
&self,
270+
_: &BlockId<Block>,
271+
_: ExecutionContext,
272+
_: Option<()>,
273+
_: Vec<u8>,
274+
) -> Result<NativeOrEncoded<AuthorityList>> {
275+
Ok(self.inner.genesis_authorities.clone()).map(NativeOrEncoded::Native)
276+
}
277+
}
278+
265279
impl GenesisAuthoritySetProvider<Block> for TestApi {
266280
fn get(&self) -> Result<AuthorityList> {
267281
Ok(self.genesis_authorities.clone())

node-template/runtime/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use client::{
2323
runtime_api as client_api, impl_runtime_apis
2424
};
2525
use aura_primitives::sr25519::AuthorityId as AuraId;
26-
use grandpa::AuthorityId as GrandpaId;
26+
use grandpa::AuthorityList as GrandpaAuthorityList;
27+
use grandpa::fg_primitives;
2728
use version::RuntimeVersion;
2829
#[cfg(feature = "std")]
2930
use version::NativeVersion;
@@ -352,4 +353,10 @@ impl_runtime_apis! {
352353
opaque::SessionKeys::generate(seed)
353354
}
354355
}
356+
357+
impl fg_primitives::GrandpaApi<Block> for Runtime {
358+
fn grandpa_authorities() -> GrandpaAuthorityList {
359+
Grandpa::grandpa_authorities()
360+
}
361+
}
355362
}

node/runtime/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ use version::RuntimeVersion;
4545
#[cfg(any(feature = "std", test))]
4646
use version::NativeVersion;
4747
use primitives::OpaqueMetadata;
48-
use grandpa::AuthorityId as GrandpaId;
48+
use grandpa::AuthorityList as GrandpaAuthorityList;
49+
use grandpa::fg_primitives;
4950
use im_online::sr25519::{AuthorityId as ImOnlineId};
5051
use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
5152
use contracts_rpc_runtime_api::ContractExecResult;
@@ -610,6 +611,12 @@ impl_runtime_apis! {
610611
}
611612
}
612613

614+
impl fg_primitives::GrandpaApi<Block> for Runtime {
615+
fn grandpa_authorities() -> GrandpaAuthorityList {
616+
Grandpa::grandpa_authorities()
617+
}
618+
}
619+
613620
impl babe_primitives::BabeApi<Block> for Runtime {
614621
fn configuration() -> babe_primitives::BabeConfiguration {
615622
// The choice of `c` parameter (where `1 - c` represents the

srml/grandpa/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
//!
2222
//! In the future, it will also handle misbehavior reports, and on-chain
2323
//! finality notifications.
24+
//!
25+
//! For full integration with GRANDPA, the `GrandpaApi` should be implemented.
26+
//! The necessary items are re-exported via the `fg_primitives` crate.
2427
2528
#![cfg_attr(not(feature = "std"), no_std)]
2629

0 commit comments

Comments
 (0)