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 21 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::{self, FinalityProofProvider as GrandpaFinalityProofProvider,
StorageAndProofProvider, SharedVoterState,
octol marked this conversation as resolved.
Show resolved Hide resolved
};

// 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 @@ -155,7 +157,8 @@ pub fn new_full(config: Configuration)
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::new(None),
};

// 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
1 change: 1 addition & 0 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use node_runtime::{Block, RuntimeApi};
use node_transaction_factory::RuntimeAdapter;
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams, SubstrateCli};
use sc_service::Configuration;
use grandpa::SharedVoterState;

impl SubstrateCli for Cli {
fn impl_name() -> &'static str {
Expand Down
27 changes: 23 additions & 4 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use std::sync::Arc;

use sc_consensus_babe;
use sc_client::{self, LongestChain};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider};
use grandpa::{
self, FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider,
SharedVoterState,
};
use node_executor;
use node_primitives::Block;
use node_runtime::RuntimeApi;
Expand All @@ -48,6 +51,7 @@ macro_rules! new_full_start {
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 @@ -91,6 +95,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 = SharedVoterState::new(None);
let deps = node_rpc::FullDeps {
client: builder.client().clone(),
pool: builder.pool(),
Expand All @@ -100,12 +108,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 @@ -131,7 +144,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 @@ -144,6 +158,9 @@ macro_rules! new_full {
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");

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

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

if let sc_service::config::Role::Authority { .. } = &role {
Expand Down Expand Up @@ -243,6 +260,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 Expand Up @@ -408,6 +426,7 @@ mod tests {
use crate::service::{new_full, new_light};
use sp_runtime::traits::IdentifyAccount;
use sp_transaction_pool::{MaintainedTransactionPool, ChainEvent};
use grandpa::SharedVoterState;

type AccountPublic = <Signature as Verify>::Signer;

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_voter_state, shared_authority_set)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything preventing this RPC working on light clients? Would it be worth adding there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The light client does not participate in the GRANDPA protocol, currently it does not participate in gossip at all. In the future it might be possible to have the light client listen to just the global GRANDPA gossip topic (rather than rounds) and run the GRANDPA observer instead (the lightweight observer is already implemented). But even in that scenario it won't be running a full voter and therefore we won't need this API.

)
);

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 <admin@parity.io>"]
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.11.2", features = ["derive-codec"] }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
futures = "0.3.1"
serde = { version = "1.0.105", features = ["derive"] }
serde_json = "1.0.50"
log = "0.4.8"
derive_more = "0.99.2"

[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0-dev", path = "../../../test-utils/runtime/client" }
Loading