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

Expose GRANDPA round state through RPC #5375

Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5f6bb70
grandpa: wire up basic RPC call
octol Apr 1, 2020
4ef39fa
grandpa: make it compile against GRANDPA with expose round state
andresilva Apr 1, 2020
eea294d
grandpa: use shared voter state to expose RPC endpoint
octol Apr 7, 2020
a8109a2
grandpa: restructure into nested structs
octol Apr 7, 2020
04fca7e
grandpa: return background rounds too
octol Apr 8, 2020
627c7a8
grandpa: return error when endpoint not ready
octol Apr 8, 2020
123e249
grandpa: collect grandpa rpc deps
octol Apr 8, 2020
10b3266
grandpa: decide to use concrete AuthorityId in finality-grandpa-rpc
octol Apr 17, 2020
eb5d87d
grandpa: remove unncessary type annotation
octol Apr 23, 2020
6edfa13
grandpa: move error code to const
octol Apr 23, 2020
bcb206d
grandpa: remove unnecessary WIP comment
octol Apr 23, 2020
4ca212d
grandpa: remove Id type parameter for SharedVoterState
octol Apr 23, 2020
b2af4f7
grandpa: update tests to add shared_voter_state in parameters
octol Apr 23, 2020
b04616f
grandpa: remove old deprecated test
octol Apr 23, 2020
c9bcbf8
grandpa: fix getting the correct set_id
octol Apr 24, 2020
fedfdcc
grandpa: make SharedVoterState a struct
octol Apr 24, 2020
4e75855
grandpa: wrap shared_voter_state in rpc_setup
octol Apr 24, 2020
f4140b6
grandpa: replace spaces with tabs
octol Apr 24, 2020
7586652
grandpa: limit RwLock write attempt to 1 sec
octol Apr 24, 2020
3d6f1c2
grandpa: add missing doc comments and remove some pub
octol Apr 27, 2020
4872fcb
Apply suggestions from code review
Demi-Marie Apr 28, 2020
75efc8e
grandpa: update function name call after change in finality-grandpa
octol Apr 29, 2020
26b0959
grandpa: group pub use and only export voter::report
octol Apr 29, 2020
9c55fa5
grandpa: add missing docs
octol Apr 29, 2020
d636c28
grandpa: extract out structs used for json serialization
octol Apr 29, 2020
e63c3be
grandpa: stick to u32 for fields intended for js
octol Apr 29, 2020
c02211c
grandpa: move Error type to its own file
octol Apr 29, 2020
fef11d3
grandpa: group pub use better
octol Apr 30, 2020
f1c2103
Apply code review suggestion
octol Apr 30, 2020
bc68092
Merge 'upstream/master' into jon/issue-4921-expose-grandpa-round-stat…
octol Apr 30, 2020
b809ee1
grandpa: use correct version of finality-granpda in rpc crate
octol Apr 30, 2020
988bbee
grandpa: add back basic rpc unit test
octol Apr 29, 2020
f9d9b1c
grandpa: replace SharedVoterState::new() with empty()
andresilva Apr 30, 2020
9eb5fa3
node: cleanup grandpa::SharedVoterState usage in macro
andresilva Apr 30, 2020
12004bc
grandpa: remove VoterState error variant
andresilva Apr 30, 2020
1ef01df
grandpa: enable missing futures compat feature
andresilva Apr 30, 2020
a97bb87
grandpa: fix typo in error variant
andresilva May 1, 2020
7aed9bb
grandpa: remove test_utils
andresilva May 1, 2020
65f8674
grandpa: allow mocking rpc handler components
andresilva May 1, 2020
a3e477d
grandpa: rename serialized to report in rpc module
andresilva May 1, 2020
23536a0
grandpa: add proper test for RPC
andresilva May 1, 2020
aabacb8
grandpa: update to finality-grandpa v0.12.1
andresilva May 1, 2020
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
21 changes: 21 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ name = "node-template"
futures = "0.3.4"
log = "0.4.8"
structopt = "0.3.8"
parking_lot = "0.10.0"

sc-cli = { version = "0.8.0-dev", path = "../../../client/cli" }
sp-core = { version = "2.0.0-dev", path = "../../../primitives/core" }
Expand Down
9 changes: 6 additions & 3 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use sp_inherents::InherentDataProviders;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_finality_grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
use sc_finality_grandpa::{
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider, SharedVoterState,
};

// Our native executor instance.
native_executor_instance!(
Expand All @@ -27,7 +29,7 @@ macro_rules! new_full_start {
($config:expr) => {{
use std::sync::Arc;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;

let mut import_setup = None;
let inherent_data_providers = sp_inherents::InherentDataProviders::new();

Expand Down Expand Up @@ -156,7 +158,8 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
inherent_data_providers: inherent_data_providers.clone(),
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
prometheus_registry: service.prometheus_registry()
prometheus_registry: service.prometheus_registry(),
shared_voter_state: SharedVoterState::empty(),
};

// the GRANDPA voter task is considered infallible, i.e.
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ log = "0.4.8"
rand = "0.7.2"
structopt = { version = "0.3.8", optional = true }
tracing = "0.1.10"
parking_lot = "0.10.0"

# primitives
sp-authority-discovery = { version = "2.0.0-dev", path = "../../../primitives/authority-discovery" }
Expand Down
28 changes: 23 additions & 5 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use std::sync::Arc;

use sc_consensus_babe;
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
use grandpa::{
self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider,
};
use node_executor;
use node_primitives::Block;
use node_runtime::RuntimeApi;
Expand All @@ -38,8 +40,10 @@ use sc_consensus::LongestChain;
macro_rules! new_full_start {
($config:expr) => {{
use std::sync::Arc;

type RpcExtension = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
let mut import_setup = None;
let mut rpc_setup = None;
let inherent_data_providers = sp_inherents::InherentDataProviders::new();

let builder = sc_service::ServiceBuilder::new_full::<
Expand Down Expand Up @@ -86,6 +90,10 @@ macro_rules! new_full_start {
.with_rpc_extensions(|builder| -> std::result::Result<RpcExtension, _> {
let babe_link = import_setup.as_ref().map(|s| &s.2)
.expect("BabeLink is present for full services or set up failed; qed.");
let grandpa_link = import_setup.as_ref().map(|s| &s.1)
.expect("GRANDPA LinkHalf is present for full services or set up failed; qed.");
let shared_authority_set = grandpa_link.shared_authority_set();
let shared_voter_state = grandpa::SharedVoterState::empty();
let deps = node_rpc::FullDeps {
client: builder.client().clone(),
pool: builder.pool(),
Expand All @@ -95,12 +103,17 @@ macro_rules! new_full_start {
keystore: builder.keystore(),
babe_config: sc_consensus_babe::BabeLink::config(babe_link).clone(),
shared_epoch_changes: sc_consensus_babe::BabeLink::epoch_changes(babe_link).clone()
}
},
grandpa: node_rpc::GrandpaDeps {
shared_voter_state: shared_voter_state.clone(),
shared_authority_set: shared_authority_set.clone(),
},
};
rpc_setup = Some((shared_voter_state));
Ok(node_rpc::create_full(deps))
})?;

(builder, import_setup, inherent_data_providers)
(builder, import_setup, inherent_data_providers, rpc_setup)
}}
}

Expand All @@ -126,7 +139,8 @@ macro_rules! new_full {
$config.disable_grandpa,
);

let (builder, mut import_setup, inherent_data_providers) = new_full_start!($config);
let (builder, mut import_setup, inherent_data_providers, mut rpc_setup) =
new_full_start!($config);

let service = builder
.with_finality_proof_provider(|client, backend| {
Expand All @@ -137,7 +151,10 @@ macro_rules! new_full {
.build()?;

let (block_import, grandpa_link, babe_link) = import_setup.take()
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");

let shared_voter_state = rpc_setup.take()
.expect("The SharedVoterState is present for Full Services or setup failed before. qed");

($with_startup_data)(&block_import, &babe_link);

Expand Down Expand Up @@ -238,6 +255,7 @@ macro_rules! new_full {
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
voting_rule: grandpa::VotingRulesBuilder::default().build(),
prometheus_registry: service.prometheus_registry(),
shared_voter_state,
};

// the GRANDPA voter task is considered infallible, i.e.
Expand Down
2 changes: 2 additions & 0 deletions bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ sc-keystore = { version = "2.0.0-dev", path = "../../../client/keystore" }
sc-consensus-epochs = { version = "0.8.0-dev", path = "../../../client/consensus/epochs" }
sp-consensus = { version = "0.8.0-dev", path = "../../../primitives/consensus/common" }
sp-blockchain = { version = "2.0.0-dev", path = "../../../primitives/blockchain" }
sc-finality-grandpa = { version = "0.8.0-dev", path = "../../../client/finality-grandpa" }
sc-finality-grandpa-rpc = { version = "0.8.0-dev", path = "../../../client/finality-grandpa/rpc" }
26 changes: 24 additions & 2 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

use std::{sync::Arc, fmt};

use node_primitives::{Block, BlockNumber, AccountId, Index, Balance};
use node_primitives::{Block, BlockNumber, AccountId, Index, Balance, Hash};
use node_runtime::UncheckedExtrinsic;
use sp_api::ProvideRuntimeApi;
use sp_transaction_pool::TransactionPool;
Expand All @@ -42,6 +42,8 @@ use sp_consensus_babe::BabeApi;
use sc_consensus_epochs::SharedEpochChanges;
use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe_rpc::BabeRPCHandler;
use sc_finality_grandpa::{SharedVoterState, SharedAuthoritySet};
use sc_finality_grandpa_rpc::GrandpaRpcHandler;

/// Light client extra dependencies.
pub struct LightDeps<C, F, P> {
Expand All @@ -65,6 +67,14 @@ pub struct BabeDeps {
pub keystore: KeyStorePtr,
}

/// Extra dependencies for GRANDPA
pub struct GrandpaDeps {
/// Voting round info.
pub shared_voter_state: SharedVoterState,
/// Authority set info.
pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>,
}

/// Full client dependencies.
pub struct FullDeps<C, P, SC> {
/// The client instance to use.
Expand All @@ -75,6 +85,8 @@ pub struct FullDeps<C, P, SC> {
pub select_chain: SC,
/// BABE specific dependencies.
pub babe: BabeDeps,
/// GRANDPA specific dependencies.
pub grandpa: GrandpaDeps,
}

/// Instantiate all Full RPC extensions.
Expand Down Expand Up @@ -102,13 +114,18 @@ pub fn create_full<C, P, M, SC>(
client,
pool,
select_chain,
babe
babe,
grandpa,
} = deps;
let BabeDeps {
keystore,
babe_config,
shared_epoch_changes,
} = babe;
let GrandpaDeps {
shared_voter_state,
shared_authority_set,
} = grandpa;

io.extend_with(
SystemApi::to_delegate(FullSystem::new(client.clone(), pool))
Expand All @@ -127,6 +144,11 @@ pub fn create_full<C, P, M, SC>(
BabeRPCHandler::new(client, shared_epoch_changes, keystore, babe_config, select_chain)
)
);
io.extend_with(
sc_finality_grandpa_rpc::GrandpaApi::to_delegate(
GrandpaRpcHandler::new(shared_authority_set, shared_voter_state)
)
);

io
}
Expand Down
22 changes: 22 additions & 0 deletions client/finality-grandpa/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "sc-finality-grandpa-rpc"
version = "0.8.0-dev"
authors = ["Parity Technologies <[email protected]>"]
description = "RPC extensions for the GRANDPA finality gadget"
edition = "2018"
license = "GPL-3.0"

[dependencies]
sc-finality-grandpa = { version = "0.8.0-dev", path = "../" }
finality-grandpa = { version = "0.12.0", features = ["derive-codec"] }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
futures = { version = "0.3.4", features = ["compat"] }
serde = { version = "1.0.105", features = ["derive"] }
serde_json = "1.0.50"
log = "0.4.8"
derive_more = "0.99.2"

[dev-dependencies]
sp-core = { version = "2.0.0-dev", path = "../../../primitives/core" }
47 changes: 47 additions & 0 deletions client/finality-grandpa/rpc/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use crate::NOT_READY_ERROR_CODE;

#[derive(derive_more::Display, derive_more::From)]
/// Top-level error type for the RPC handler
pub enum Error {
/// The GRANDPA RPC endpoint is not ready.
#[display(fmt = "GRANDPA RPC endpoint not ready")]
EndpointNotReady,
/// GRANDPA reports the authority set id to be larger than 32-bits.
#[display(fmt = "GRANDPA reports authority set id unreasonably large")]
AuthoritySetIdReportedAsUnreasonablyLarge,
/// GRANDPA reports voter state with round id or weights larger than 32-bits.
#[display(fmt = "GRANDPA reports voter state as unreasonably large")]
VoterStateReportsUnreasonablyLargeNumbers,
}

impl From<Error> for jsonrpc_core::Error {
fn from(error: Error) -> Self {
jsonrpc_core::Error {
message: format!("{}", error).into(),
code: jsonrpc_core::ErrorCode::ServerError(NOT_READY_ERROR_CODE),
data: None,
}
}
}

impl From<std::num::TryFromIntError> for Error {
fn from(_error: std::num::TryFromIntError) -> Self {
Error::VoterStateReportsUnreasonablyLargeNumbers
}
}
Loading