grandpa-rpc: use FinalityProofProvider to check finality for rpc (#6215)

* grandpa-rpc: use FinalityProofProvider to check finality for rpc

* grandpa-rpc: minor tidy

* grandpa-rpc: remove dyn FinalityProofProvider

* grandpa-rpc: remove unused dependencies

* node: move finality_proof_provider setup

* grandpa-rpc: print error reported by finality_proof_provider

* grandpa-rpc: add note about unnecessary encode/decode

* grandpa-rpc: dont encode/decode and use correct hash

* grandpa-rpc: set_id is optional

* grandpa-rpc: create test for prove_finality

* grandpa-rpc: set visibility back to how it was

* grandpa-rpc: remove unused dependency

* grandpa-rpc: minor tidy

* grandpa: doc strings

* grandpa-rpc: rename to prove_finality

* grandpa-rpc: use current set id if none is provided

* grandpa-rpc: remove unnecessary check in test

* node: group finality_proof_provider in rpc_setup

* grandpa: make prove_finality concrete in FinalityProofProvider

* grandpa-rpc: wrap finality output in struct and store as Bytes

* grandpa-rpc: exhaustive error codes and wrap

* grandpa-rpc: let prove_finality take a range instead of a starting point

* grandpa-rpc: fix test for changed API

* grandpa-rpc: fix line length

* grandpa: fix reviewer nits

* node/rpc: fix reviewer comments
This commit is contained in:
Jon Häggblad
2020-09-18 18:39:27 +02:00
committed by GitHub
parent 743cf7543b
commit 8a0ac4c40c
10 changed files with 263 additions and 31 deletions
+2
View File
@@ -3823,6 +3823,7 @@ dependencies = [
"sp-blockchain", "sp-blockchain",
"sp-consensus", "sp-consensus",
"sp-consensus-babe", "sp-consensus-babe",
"sp-runtime",
"sp-transaction-pool", "sp-transaction-pool",
"substrate-frame-rpc-system", "substrate-frame-rpc-system",
] ]
@@ -6821,6 +6822,7 @@ dependencies = [
"log", "log",
"parity-scale-codec", "parity-scale-codec",
"sc-block-builder", "sc-block-builder",
"sc-client-api",
"sc-finality-grandpa", "sc-finality-grandpa",
"sc-network-test", "sc-network-test",
"sc-rpc", "sc-rpc",
+9 -5
View File
@@ -58,7 +58,10 @@ pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponen
grandpa::LinkHalf<Block, FullClient, FullSelectChain>, grandpa::LinkHalf<Block, FullClient, FullSelectChain>,
sc_consensus_babe::BabeLink<Block>, sc_consensus_babe::BabeLink<Block>,
), ),
grandpa::SharedVoterState, (
grandpa::SharedVoterState,
Arc<GrandpaFinalityProofProvider<FullBackend, Block>>,
),
) )
>, ServiceError> { >, ServiceError> {
let (client, backend, keystore, task_manager) = let (client, backend, keystore, task_manager) =
@@ -108,8 +111,10 @@ pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponen
let justification_stream = grandpa_link.justification_stream(); let justification_stream = grandpa_link.justification_stream();
let shared_authority_set = grandpa_link.shared_authority_set().clone(); let shared_authority_set = grandpa_link.shared_authority_set().clone();
let shared_voter_state = grandpa::SharedVoterState::empty(); let shared_voter_state = grandpa::SharedVoterState::empty();
let finality_proof_provider =
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());
let rpc_setup = shared_voter_state.clone(); let rpc_setup = (shared_voter_state.clone(), finality_proof_provider.clone());
let babe_config = babe_link.config().clone(); let babe_config = babe_link.config().clone();
let shared_epoch_changes = babe_link.epoch_changes().clone(); let shared_epoch_changes = babe_link.epoch_changes().clone();
@@ -135,6 +140,7 @@ pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponen
shared_authority_set: shared_authority_set.clone(), shared_authority_set: shared_authority_set.clone(),
justification_stream: justification_stream.clone(), justification_stream: justification_stream.clone(),
subscription_executor, subscription_executor,
finality_provider: finality_proof_provider.clone(),
}, },
}; };
@@ -174,8 +180,7 @@ pub fn new_full_base(
other: (rpc_extensions_builder, import_setup, rpc_setup), other: (rpc_extensions_builder, import_setup, rpc_setup),
} = new_partial(&config)?; } = new_partial(&config)?;
let finality_proof_provider = let (shared_voter_state, finality_proof_provider) = rpc_setup;
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());
let (network, network_status_sinks, system_rpc_tx, network_starter) = let (network, network_status_sinks, system_rpc_tx, network_starter) =
sc_service::build_network(sc_service::BuildNetworkParams { sc_service::build_network(sc_service::BuildNetworkParams {
@@ -220,7 +225,6 @@ pub fn new_full_base(
})?; })?;
let (block_import, grandpa_link, babe_link) = import_setup; let (block_import, grandpa_link, babe_link) = import_setup;
let shared_voter_state = rpc_setup;
(with_startup_data)(&block_import, &babe_link); (with_startup_data)(&block_import, &babe_link);
+1
View File
@@ -30,5 +30,6 @@ sp-block-builder = { version = "2.0.0-rc6", path = "../../../primitives/block-bu
sp-blockchain = { version = "2.0.0-rc6", path = "../../../primitives/blockchain" } sp-blockchain = { version = "2.0.0-rc6", path = "../../../primitives/blockchain" }
sp-consensus = { version = "0.8.0-rc6", path = "../../../primitives/consensus/common" } sp-consensus = { version = "0.8.0-rc6", path = "../../../primitives/consensus/common" }
sp-consensus-babe = { version = "0.8.0-rc6", path = "../../../primitives/consensus/babe" } sp-consensus-babe = { version = "0.8.0-rc6", path = "../../../primitives/consensus/babe" }
sp-runtime = { version = "2.0.0-rc6", path = "../../../primitives/runtime" }
sp-transaction-pool = { version = "2.0.0-rc6", path = "../../../primitives/transaction-pool" } sp-transaction-pool = { version = "2.0.0-rc6", path = "../../../primitives/transaction-pool" }
substrate-frame-rpc-system = { version = "2.0.0-rc6", path = "../../../utils/frame/rpc/system" } substrate-frame-rpc-system = { version = "2.0.0-rc6", path = "../../../utils/frame/rpc/system" }
+14 -6
View File
@@ -36,7 +36,9 @@ use node_primitives::{Block, BlockNumber, AccountId, Index, Balance, Hash};
use sc_consensus_babe::{Config, Epoch}; use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe_rpc::BabeRpcHandler; use sc_consensus_babe_rpc::BabeRpcHandler;
use sc_consensus_epochs::SharedEpochChanges; use sc_consensus_epochs::SharedEpochChanges;
use sc_finality_grandpa::{SharedVoterState, SharedAuthoritySet, GrandpaJustificationStream}; use sc_finality_grandpa::{
SharedVoterState, SharedAuthoritySet, FinalityProofProvider, GrandpaJustificationStream
};
use sc_finality_grandpa_rpc::GrandpaRpcHandler; use sc_finality_grandpa_rpc::GrandpaRpcHandler;
use sc_keystore::KeyStorePtr; use sc_keystore::KeyStorePtr;
pub use sc_rpc_api::DenyUnsafe; pub use sc_rpc_api::DenyUnsafe;
@@ -71,7 +73,7 @@ pub struct BabeDeps {
} }
/// Extra dependencies for GRANDPA /// Extra dependencies for GRANDPA
pub struct GrandpaDeps { pub struct GrandpaDeps<B> {
/// Voting round info. /// Voting round info.
pub shared_voter_state: SharedVoterState, pub shared_voter_state: SharedVoterState,
/// Authority set info. /// Authority set info.
@@ -80,10 +82,12 @@ pub struct GrandpaDeps {
pub justification_stream: GrandpaJustificationStream<Block>, pub justification_stream: GrandpaJustificationStream<Block>,
/// Executor to drive the subscription manager in the Grandpa RPC handler. /// Executor to drive the subscription manager in the Grandpa RPC handler.
pub subscription_executor: SubscriptionTaskExecutor, pub subscription_executor: SubscriptionTaskExecutor,
/// Finality proof provider.
pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
} }
/// Full client dependencies. /// Full client dependencies.
pub struct FullDeps<C, P, SC> { pub struct FullDeps<C, P, SC, B> {
/// The client instance to use. /// The client instance to use.
pub client: Arc<C>, pub client: Arc<C>,
/// Transaction pool instance. /// Transaction pool instance.
@@ -95,15 +99,15 @@ pub struct FullDeps<C, P, SC> {
/// BABE specific dependencies. /// BABE specific dependencies.
pub babe: BabeDeps, pub babe: BabeDeps,
/// GRANDPA specific dependencies. /// GRANDPA specific dependencies.
pub grandpa: GrandpaDeps, pub grandpa: GrandpaDeps<B>,
} }
/// A IO handler that uses all Full RPC extensions. /// A IO handler that uses all Full RPC extensions.
pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>; pub type IoHandler = jsonrpc_core::IoHandler<sc_rpc::Metadata>;
/// Instantiate all Full RPC extensions. /// Instantiate all Full RPC extensions.
pub fn create_full<C, P, SC>( pub fn create_full<C, P, SC, B>(
deps: FullDeps<C, P, SC>, deps: FullDeps<C, P, SC, B>,
) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata> where ) -> jsonrpc_core::IoHandler<sc_rpc_api::Metadata> where
C: ProvideRuntimeApi<Block>, C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError> + 'static, C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError> + 'static,
@@ -115,6 +119,8 @@ pub fn create_full<C, P, SC>(
C::Api: BlockBuilder<Block>, C::Api: BlockBuilder<Block>,
P: TransactionPool + 'static, P: TransactionPool + 'static,
SC: SelectChain<Block> +'static, SC: SelectChain<Block> +'static,
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,
{ {
use substrate_frame_rpc_system::{FullSystem, SystemApi}; use substrate_frame_rpc_system::{FullSystem, SystemApi};
use pallet_contracts_rpc::{Contracts, ContractsApi}; use pallet_contracts_rpc::{Contracts, ContractsApi};
@@ -140,6 +146,7 @@ pub fn create_full<C, P, SC>(
shared_authority_set, shared_authority_set,
justification_stream, justification_stream,
subscription_executor, subscription_executor,
finality_provider,
} = grandpa; } = grandpa;
io.extend_with( io.extend_with(
@@ -173,6 +180,7 @@ pub fn create_full<C, P, SC>(
shared_voter_state, shared_voter_state,
justification_stream, justification_stream,
subscription_executor, subscription_executor,
finality_provider,
) )
) )
); );
@@ -10,6 +10,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies] [dependencies]
sc-finality-grandpa = { version = "0.8.0-rc6", path = "../" } sc-finality-grandpa = { version = "0.8.0-rc6", path = "../" }
sc-rpc = { version = "2.0.0-rc6", path = "../../rpc" } sc-rpc = { version = "2.0.0-rc6", path = "../../rpc" }
sp-blockchain = { version = "2.0.0-rc6", path = "../../../primitives/blockchain" }
sp-core = { version = "2.0.0-rc6", path = "../../../primitives/core" } sp-core = { version = "2.0.0-rc6", path = "../../../primitives/core" }
sp-runtime = { version = "2.0.0-rc6", path = "../../../primitives/runtime" } sp-runtime = { version = "2.0.0-rc6", path = "../../../primitives/runtime" }
finality-grandpa = { version = "0.12.3", features = ["derive-codec"] } finality-grandpa = { version = "0.12.3", features = ["derive-codec"] }
@@ -23,12 +24,12 @@ serde_json = "1.0.50"
log = "0.4.8" log = "0.4.8"
derive_more = "0.99.2" derive_more = "0.99.2"
parity-scale-codec = { version = "1.3.0", features = ["derive"] } parity-scale-codec = { version = "1.3.0", features = ["derive"] }
sc-client-api = { version = "2.0.0-rc6", path = "../../api" }
[dev-dependencies] [dev-dependencies]
sc-block-builder = { version = "0.8.0-rc6", path = "../../block-builder" } sc-block-builder = { version = "0.8.0-rc6", path = "../../block-builder" }
sc-network-test = { version = "0.8.0-rc6", path = "../../network/test" } sc-network-test = { version = "0.8.0-rc6", path = "../../network/test" }
sc-rpc = { version = "2.0.0-rc6", path = "../../rpc", features = ["test-helpers"] } sc-rpc = { version = "2.0.0-rc6", path = "../../rpc", features = ["test-helpers"] }
sp-blockchain = { version = "2.0.0-rc6", path = "../../../primitives/blockchain" }
sp-consensus = { version = "0.8.0-rc6", path = "../../../primitives/consensus/common" } sp-consensus = { version = "0.8.0-rc6", path = "../../../primitives/consensus/common" }
sp-core = { version = "2.0.0-rc6", path = "../../../primitives/core" } sp-core = { version = "2.0.0-rc6", path = "../../../primitives/core" }
sp-finality-grandpa = { version = "2.0.0-rc6", path = "../../../primitives/finality-grandpa" } sp-finality-grandpa = { version = "2.0.0-rc6", path = "../../../primitives/finality-grandpa" }
@@ -16,8 +16,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::NOT_READY_ERROR_CODE;
#[derive(derive_more::Display, derive_more::From)] #[derive(derive_more::Display, derive_more::From)]
/// Top-level error type for the RPC handler /// Top-level error type for the RPC handler
pub enum Error { pub enum Error {
@@ -30,13 +28,41 @@ pub enum Error {
/// GRANDPA reports voter state with round id or weights larger than 32-bits. /// GRANDPA reports voter state with round id or weights larger than 32-bits.
#[display(fmt = "GRANDPA reports voter state as unreasonably large")] #[display(fmt = "GRANDPA reports voter state as unreasonably large")]
VoterStateReportsUnreasonablyLargeNumbers, VoterStateReportsUnreasonablyLargeNumbers,
/// GRANDPA prove finality failed.
#[display(fmt = "GRANDPA prove finality rpc failed: {}", _0)]
ProveFinalityFailed(sp_blockchain::Error),
}
/// The error codes returned by jsonrpc.
pub enum ErrorCode {
/// Returned when Grandpa RPC endpoint is not ready.
NotReady = 1,
/// Authority set ID is larger than 32-bits.
AuthoritySetTooLarge,
/// Voter state with round id or weights larger than 32-bits.
VoterStateTooLarge,
/// Failed to prove finality.
ProveFinality,
}
impl From<Error> for ErrorCode {
fn from(error: Error) -> Self {
match error {
Error::EndpointNotReady => ErrorCode::NotReady,
Error::AuthoritySetIdReportedAsUnreasonablyLarge => ErrorCode::AuthoritySetTooLarge,
Error::VoterStateReportsUnreasonablyLargeNumbers => ErrorCode::VoterStateTooLarge,
Error::ProveFinalityFailed(_) => ErrorCode::ProveFinality,
}
}
} }
impl From<Error> for jsonrpc_core::Error { impl From<Error> for jsonrpc_core::Error {
fn from(error: Error) -> Self { fn from(error: Error) -> Self {
let message = format!("{}", error);
let code = ErrorCode::from(error);
jsonrpc_core::Error { jsonrpc_core::Error {
message: format!("{}", error), message,
code: jsonrpc_core::ErrorCode::ServerError(NOT_READY_ERROR_CODE), code: jsonrpc_core::ErrorCode::ServerError(code as i64),
data: None, data: None,
} }
} }
@@ -0,0 +1,54 @@
// This file is part of Substrate.
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use serde::{Serialize, Deserialize};
use sc_finality_grandpa::FinalityProofProvider;
use sp_runtime::traits::{Block as BlockT, NumberFor};
#[derive(Serialize, Deserialize)]
pub struct EncodedFinalityProofs(pub sp_core::Bytes);
/// Local trait mainly to allow mocking in tests.
pub trait RpcFinalityProofProvider<Block: BlockT> {
/// Return finality proofs for the given authorities set id, if it is provided, otherwise the
/// current one will be used.
fn rpc_prove_finality(
&self,
begin: Block::Hash,
end: Block::Hash,
authorities_set_id: u64,
) -> Result<Option<EncodedFinalityProofs>, sp_blockchain::Error>;
}
impl<B, Block> RpcFinalityProofProvider<Block> for FinalityProofProvider<B, Block>
where
Block: BlockT,
NumberFor<Block>: finality_grandpa::BlockNumberOps,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
{
fn rpc_prove_finality(
&self,
begin: Block::Hash,
end: Block::Hash,
authorities_set_id: u64,
) -> Result<Option<EncodedFinalityProofs>, sp_blockchain::Error> {
self.prove_finality(begin, end, authorities_set_id)
.map(|x| x.map(|y| EncodedFinalityProofs(y.into())))
}
}
+125 -12
View File
@@ -32,24 +32,23 @@ use jsonrpc_core::futures::{
}; };
mod error; mod error;
mod finality;
mod notification; mod notification;
mod report; mod report;
use sc_finality_grandpa::GrandpaJustificationStream; use sc_finality_grandpa::GrandpaJustificationStream;
use sp_runtime::traits::Block as BlockT; use sp_runtime::traits::Block as BlockT;
use finality::{EncodedFinalityProofs, RpcFinalityProofProvider};
use report::{ReportAuthoritySet, ReportVoterState, ReportedRoundStates}; use report::{ReportAuthoritySet, ReportVoterState, ReportedRoundStates};
use notification::JustificationNotification; use notification::JustificationNotification;
/// Returned when Grandpa RPC endpoint is not ready.
pub const NOT_READY_ERROR_CODE: i64 = 1;
type FutureResult<T> = type FutureResult<T> =
Box<dyn jsonrpc_core::futures::Future<Item = T, Error = jsonrpc_core::Error> + Send>; Box<dyn jsonrpc_core::futures::Future<Item = T, Error = jsonrpc_core::Error> + Send>;
/// Provides RPC methods for interacting with GRANDPA. /// Provides RPC methods for interacting with GRANDPA.
#[rpc] #[rpc]
pub trait GrandpaApi<Notification> { pub trait GrandpaApi<Notification, Hash> {
/// RPC Metadata /// RPC Metadata
type Metadata; type Metadata;
@@ -82,23 +81,37 @@ pub trait GrandpaApi<Notification> {
metadata: Option<Self::Metadata>, metadata: Option<Self::Metadata>,
id: SubscriptionId id: SubscriptionId
) -> jsonrpc_core::Result<bool>; ) -> jsonrpc_core::Result<bool>;
/// Prove finality for the range (begin; end] hash. Returns None if there are no finalized blocks
/// unknown in the range. If no authorities set is provided, the current one will be attempted.
#[rpc(name = "grandpa_proveFinality")]
fn prove_finality(
&self,
begin: Hash,
end: Hash,
authorities_set_id: Option<u64>,
) -> FutureResult<Option<EncodedFinalityProofs>>;
} }
/// Implements the GrandpaApi RPC trait for interacting with GRANDPA. /// Implements the GrandpaApi RPC trait for interacting with GRANDPA.
pub struct GrandpaRpcHandler<AuthoritySet, VoterState, Block: BlockT> { pub struct GrandpaRpcHandler<AuthoritySet, VoterState, Block: BlockT, ProofProvider> {
authority_set: AuthoritySet, authority_set: AuthoritySet,
voter_state: VoterState, voter_state: VoterState,
justification_stream: GrandpaJustificationStream<Block>, justification_stream: GrandpaJustificationStream<Block>,
manager: SubscriptionManager, manager: SubscriptionManager,
finality_proof_provider: Arc<ProofProvider>,
} }
impl<AuthoritySet, VoterState, Block: BlockT> GrandpaRpcHandler<AuthoritySet, VoterState, Block> { impl<AuthoritySet, VoterState, Block: BlockT, ProofProvider>
GrandpaRpcHandler<AuthoritySet, VoterState, Block, ProofProvider>
{
/// Creates a new GrandpaRpcHandler instance. /// Creates a new GrandpaRpcHandler instance.
pub fn new<E>( pub fn new<E>(
authority_set: AuthoritySet, authority_set: AuthoritySet,
voter_state: VoterState, voter_state: VoterState,
justification_stream: GrandpaJustificationStream<Block>, justification_stream: GrandpaJustificationStream<Block>,
executor: E, executor: E,
finality_proof_provider: Arc<ProofProvider>,
) -> Self ) -> Self
where where
E: Executor01<Box<dyn Future01<Item = (), Error = ()> + Send>> + Send + Sync + 'static, E: Executor01<Box<dyn Future01<Item = (), Error = ()> + Send>> + Send + Sync + 'static,
@@ -109,16 +122,18 @@ impl<AuthoritySet, VoterState, Block: BlockT> GrandpaRpcHandler<AuthoritySet, Vo
voter_state, voter_state,
justification_stream, justification_stream,
manager, manager,
finality_proof_provider,
} }
} }
} }
impl<AuthoritySet, VoterState, Block> GrandpaApi<JustificationNotification> impl<AuthoritySet, VoterState, Block, ProofProvider> GrandpaApi<JustificationNotification, Block::Hash>
for GrandpaRpcHandler<AuthoritySet, VoterState, Block> for GrandpaRpcHandler<AuthoritySet, VoterState, Block, ProofProvider>
where where
VoterState: ReportVoterState + Send + Sync + 'static, VoterState: ReportVoterState + Send + Sync + 'static,
AuthoritySet: ReportAuthoritySet + Send + Sync + 'static, AuthoritySet: ReportAuthoritySet + Send + Sync + 'static,
Block: BlockT, Block: BlockT,
ProofProvider: RpcFinalityProofProvider<Block> + Send + Sync + 'static,
{ {
type Metadata = sc_rpc::Metadata; type Metadata = sc_rpc::Metadata;
@@ -153,6 +168,30 @@ where
) -> jsonrpc_core::Result<bool> { ) -> jsonrpc_core::Result<bool> {
Ok(self.manager.cancel(id)) Ok(self.manager.cancel(id))
} }
fn prove_finality(
&self,
begin: Block::Hash,
end: Block::Hash,
authorities_set_id: Option<u64>,
) -> FutureResult<Option<EncodedFinalityProofs>> {
// If we are not provided a set_id, try with the current one.
let authorities_set_id = authorities_set_id
.unwrap_or_else(|| self.authority_set.get().0);
let result = self
.finality_proof_provider
.rpc_prove_finality(begin, end, authorities_set_id);
let future = async move { result }.boxed();
Box::new(
future
.map_err(|e| {
warn!("Error proving finality: {}", e);
error::Error::ProveFinalityFailed(e)
})
.map_err(jsonrpc_core::Error::from)
.compat()
)
}
} }
#[cfg(test)] #[cfg(test)]
@@ -161,16 +200,19 @@ mod tests {
use std::{collections::HashSet, convert::TryInto, sync::Arc}; use std::{collections::HashSet, convert::TryInto, sync::Arc};
use jsonrpc_core::{Notification, Output, types::Params}; use jsonrpc_core::{Notification, Output, types::Params};
use parity_scale_codec::Decode; use parity_scale_codec::{Encode, Decode};
use sc_block_builder::BlockBuilder; use sc_block_builder::BlockBuilder;
use sc_finality_grandpa::{report, AuthorityId, GrandpaJustificationSender, GrandpaJustification}; use sc_finality_grandpa::{
report, AuthorityId, GrandpaJustificationSender, GrandpaJustification,
FinalityProofFragment,
};
use sp_blockchain::HeaderBackend; use sp_blockchain::HeaderBackend;
use sp_consensus::RecordProof; use sp_consensus::RecordProof;
use sp_core::crypto::Public; use sp_core::crypto::Public;
use sp_keyring::Ed25519Keyring; use sp_keyring::Ed25519Keyring;
use sp_runtime::traits::Header as HeaderT; use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use substrate_test_runtime_client::{ use substrate_test_runtime_client::{
runtime::Block, runtime::{Block, Header, H256},
DefaultTestClientBuilderExt, DefaultTestClientBuilderExt,
TestClientBuilderExt, TestClientBuilderExt,
TestClientBuilder, TestClientBuilder,
@@ -180,6 +222,10 @@ mod tests {
struct TestVoterState; struct TestVoterState;
struct EmptyVoterState; struct EmptyVoterState;
struct TestFinalityProofProvider {
finality_proofs: Vec<FinalityProofFragment<Header>>,
}
fn voters() -> HashSet<AuthorityId> { fn voters() -> HashSet<AuthorityId> {
let voter_id_1 = AuthorityId::from_slice(&[1; 32]); let voter_id_1 = AuthorityId::from_slice(&[1; 32]);
let voter_id_2 = AuthorityId::from_slice(&[2; 32]); let voter_id_2 = AuthorityId::from_slice(&[2; 32]);
@@ -199,6 +245,31 @@ mod tests {
} }
} }
fn header(number: u64) -> Header {
let parent_hash = match number {
0 => Default::default(),
_ => header(number - 1).hash(),
};
Header::new(
number,
H256::from_low_u64_be(0),
H256::from_low_u64_be(0),
parent_hash,
Default::default(),
)
}
impl<Block: BlockT> RpcFinalityProofProvider<Block> for TestFinalityProofProvider {
fn rpc_prove_finality(
&self,
_begin: Block::Hash,
_end: Block::Hash,
_authoritites_set_id: u64,
) -> Result<Option<EncodedFinalityProofs>, sp_blockchain::Error> {
Ok(Some(EncodedFinalityProofs(self.finality_proofs.encode().into())))
}
}
impl ReportVoterState for TestVoterState { impl ReportVoterState for TestVoterState {
fn get(&self) -> Option<report::VoterState<AuthorityId>> { fn get(&self) -> Option<report::VoterState<AuthorityId>> {
let voter_id_1 = AuthorityId::from_slice(&[1; 32]); let voter_id_1 = AuthorityId::from_slice(&[1; 32]);
@@ -236,14 +307,28 @@ mod tests {
GrandpaJustificationSender<Block>, GrandpaJustificationSender<Block>,
) where ) where
VoterState: ReportVoterState + Send + Sync + 'static, VoterState: ReportVoterState + Send + Sync + 'static,
{
setup_io_handler_with_finality_proofs(voter_state, Default::default())
}
fn setup_io_handler_with_finality_proofs<VoterState>(
voter_state: VoterState,
finality_proofs: Vec<FinalityProofFragment<Header>>,
) -> (
jsonrpc_core::MetaIoHandler<sc_rpc::Metadata>,
GrandpaJustificationSender<Block>,
) where
VoterState: ReportVoterState + Send + Sync + 'static,
{ {
let (justification_sender, justification_stream) = GrandpaJustificationStream::channel(); let (justification_sender, justification_stream) = GrandpaJustificationStream::channel();
let finality_proof_provider = Arc::new(TestFinalityProofProvider { finality_proofs });
let handler = GrandpaRpcHandler::new( let handler = GrandpaRpcHandler::new(
TestAuthoritySet, TestAuthoritySet,
voter_state, voter_state,
justification_stream, justification_stream,
sc_rpc::testing::TaskExecutor, sc_rpc::testing::TaskExecutor,
finality_proof_provider,
); );
let mut io = jsonrpc_core::MetaIoHandler::default(); let mut io = jsonrpc_core::MetaIoHandler::default();
@@ -432,4 +517,32 @@ mod tests {
assert_eq!(recv_sub_id, sub_id); assert_eq!(recv_sub_id, sub_id);
assert_eq!(recv_justification, justification); assert_eq!(recv_justification, justification);
} }
#[test]
fn prove_finality_with_test_finality_proof_provider() {
let finality_proofs = vec![FinalityProofFragment {
block: header(42).hash(),
justification: create_justification().encode(),
unknown_headers: vec![header(2)],
authorities_proof: None,
}];
let (io, _) = setup_io_handler_with_finality_proofs(
TestVoterState,
finality_proofs.clone(),
);
let request = "{\"jsonrpc\":\"2.0\",\"method\":\"grandpa_proveFinality\",\"params\":[\
\"0x0000000000000000000000000000000000000000000000000000000000000000\",\
\"0x0000000000000000000000000000000000000000000000000000000000000001\",\
42\
],\"id\":1}";
let meta = sc_rpc::Metadata::default();
let resp = io.handle_request_sync(request, meta);
let mut resp: serde_json::Value = serde_json::from_str(&resp.unwrap()).unwrap();
let result: sp_core::Bytes = serde_json::from_value(resp["result"].take()).unwrap();
let fragments: Vec<FinalityProofFragment<Header>> =
Decode::decode(&mut &result[..]).unwrap();
assert_eq!(fragments, finality_proofs);
}
} }
@@ -180,7 +180,30 @@ impl<B, Block: BlockT> FinalityProofProvider<B, Block>
) -> Arc<Self> { ) -> Arc<Self> {
Arc::new(Self::new(backend, storage_and_proof_provider)) Arc::new(Self::new(backend, storage_and_proof_provider))
} }
}
impl<B, Block> FinalityProofProvider<B, Block>
where
Block: BlockT,
NumberFor<Block>: BlockNumberOps,
B: Backend<Block> + Send + Sync + 'static,
{
/// Prove finality for the range (begin; end] hash. Returns None if there are no finalized blocks
/// unknown in the range.
pub fn prove_finality(
&self,
begin: Block::Hash,
end: Block::Hash,
authorities_set_id: u64,
) -> Result<Option<Vec<u8>>, ClientError> {
prove_finality::<_, _, GrandpaJustification<Block>>(
&*self.backend.blockchain(),
&*self.authority_provider,
authorities_set_id,
begin,
end,
)
}
} }
impl<B, Block> sc_network::config::FinalityProofProvider<Block> for FinalityProofProvider<B, Block> impl<B, Block> sc_network::config::FinalityProofProvider<Block> for FinalityProofProvider<B, Block>
@@ -232,8 +255,8 @@ pub struct FinalityEffects<Header: HeaderT> {
/// 1) the justification for the descendant block F; /// 1) the justification for the descendant block F;
/// 2) headers sub-chain (B; F] if B != F; /// 2) headers sub-chain (B; F] if B != F;
/// 3) proof of GRANDPA::authorities() if the set changes at block F. /// 3) proof of GRANDPA::authorities() if the set changes at block F.
#[derive(Debug, PartialEq, Encode, Decode)] #[derive(Debug, PartialEq, Encode, Decode, Clone)]
pub(crate) struct FinalityProofFragment<Header: HeaderT> { pub struct FinalityProofFragment<Header: HeaderT> {
/// The hash of block F for which justification is provided. /// The hash of block F for which justification is provided.
pub block: Header::Hash, pub block: Header::Hash,
/// Justification of the block F. /// Justification of the block F.
+1 -1
View File
@@ -125,7 +125,7 @@ mod until_imported;
mod voting_rule; mod voting_rule;
pub use authorities::SharedAuthoritySet; pub use authorities::SharedAuthoritySet;
pub use finality_proof::{FinalityProofProvider, StorageAndProofProvider}; pub use finality_proof::{FinalityProofFragment, FinalityProofProvider, StorageAndProofProvider};
pub use notification::{GrandpaJustificationSender, GrandpaJustificationStream}; pub use notification::{GrandpaJustificationSender, GrandpaJustificationStream};
pub use import::GrandpaBlockImport; pub use import::GrandpaBlockImport;
pub use justification::GrandpaJustification; pub use justification::GrandpaJustification;