From 1f28c0c6aa1d8bd1a55e9a940528340c2e49ad6b Mon Sep 17 00:00:00 2001 From: MaciejBaj Date: Wed, 10 Jan 2024 10:36:59 +0000 Subject: [PATCH 1/2] chore: plug GRANDPA proveFinality RPC to standalone node --- Cargo.lock | 21 +++++++++++++++++++ node/Cargo.toml | 1 + node/src/rpc.rs | 50 +++++++++++++++++++++++++++++++++++++++++---- node/src/service.rs | 25 +++++++++++++++++++++-- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77ba885b1..a051088d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4182,6 +4182,7 @@ dependencies = [ "sc-consensus-aura", "sc-executor", "sc-finality-grandpa", + "sc-finality-grandpa-rpc", "sc-keystore", "sc-rpc", "sc-rpc-api", @@ -6315,6 +6316,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "sc-finality-grandpa-rpc" +version = "0.10.0-dev" +source = "git+https://github.com/paritytech/substrate.git?branch=polkadot-v0.9.39#8c4b84520cee2d7de53cc33cb67605ce4efefba8" +dependencies = [ + "finality-grandpa", + "futures", + "jsonrpsee", + "log", + "parity-scale-codec", + "sc-client-api", + "sc-finality-grandpa", + "sc-rpc", + "serde", + "sp-blockchain", + "sp-core", + "sp-runtime", + "thiserror", +] + [[package]] name = "sc-informant" version = "0.10.0-dev" diff --git a/node/Cargo.toml b/node/Cargo.toml index ede9f21cf..ddd39a168 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -38,6 +38,7 @@ sp-consensus-aura = { version = "0.10.0-dev", git = "https://github.com/parityte sp-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } sc-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } sc-finality-grandpa = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } +sc-finality-grandpa-rpc = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } sp-finality-grandpa = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } sc-client-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } sp-runtime = { version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" } diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 9752c379a..9f9b31c76 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -8,27 +8,46 @@ use std::sync::Arc; use jsonrpsee::RpcModule; -use node_subtensor_runtime::{opaque::Block, AccountId, Balance, Index}; +use node_subtensor_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Index, Hash}; use sc_transaction_pool_api::TransactionPool; use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata}; +use sc_finality_grandpa::FinalityProofProvider; pub use sc_rpc_api::DenyUnsafe; +/// Dependencies for GRANDPA +pub struct GrandpaDeps { + /// Voting round info. + pub shared_voter_state: sc_finality_grandpa::SharedVoterState, + /// Authority set info. + pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet, + /// Receives notifications about justification events from Grandpa. + pub justification_stream: sc_finality_grandpa::GrandpaJustificationStream, + /// Executor to drive the subscription manager in the Grandpa RPC handler. + pub subscription_executor: sc_rpc::SubscriptionTaskExecutor, + /// Finality proof provider. + pub finality_provider: Arc>, +} + /// Full client dependencies. -pub struct FullDeps { +pub struct FullDeps { /// The client instance to use. pub client: Arc, /// Transaction pool instance. pub pool: Arc

, /// Whether to deny unsafe calls pub deny_unsafe: DenyUnsafe, + /// Grandpa block import setup. + pub grandpa: GrandpaDeps, + /// Backend used by the node. + pub backend: Arc, } /// Instantiate all full RPC extensions. -pub fn create_full( - deps: FullDeps, +pub fn create_full( + deps: FullDeps, ) -> Result, Box> where C: ProvideRuntimeApi, @@ -41,17 +60,21 @@ where C::Api: subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi, C::Api: subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi, C::Api: subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi, + B: sc_client_api::Backend + Send + Sync + 'static, P: TransactionPool + 'static, { use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; use subtensor_custom_rpc::{SubtensorCustom, SubtensorCustomApiServer}; + use sc_finality_grandpa_rpc::{Grandpa, GrandpaApiServer}; let mut module = RpcModule::new(()); let FullDeps { client, pool, deny_unsafe, + grandpa, + backend: _, } = deps; // Custom RPC methods for Paratensor @@ -60,6 +83,25 @@ where module.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?; module.merge(TransactionPayment::new(client).into_rpc())?; + let GrandpaDeps { + shared_voter_state, + shared_authority_set, + justification_stream, + subscription_executor, + finality_provider, + } = grandpa; + + module.merge( + Grandpa::new( + subscription_executor, + shared_authority_set.clone(), + shared_voter_state, + justification_stream, + finality_provider, + ) + .into_rpc(), + )?; + // Extend this RPC with a custom API by using the following syntax. // `YourRpcStruct` should have a reference to a client, which is needed // to call into the runtime. diff --git a/node/src/service.rs b/node/src/service.rs index d789c022f..bb60697e1 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -219,13 +219,34 @@ pub fn new_full(mut config: Configuration) -> Result let enable_grandpa = !config.disable_grandpa; let prometheus_registry = config.prometheus_registry().cloned(); + let finality_proof_provider = sc_finality_grandpa::FinalityProofProvider::new_for_service( + backend.clone(), + Some(grandpa_link.shared_authority_set().clone()), + ); + let rpc_backend = backend.clone(); + let justification_stream = grandpa_link.justification_stream(); + let shared_authority_set = grandpa_link.shared_authority_set().clone(); + let shared_voter_state = SharedVoterState::empty(); + let rpc_extensions_builder = { let client = client.clone(); let pool = transaction_pool.clone(); - Box::new(move |deny_unsafe, _| { + Box::new(move |deny_unsafe, subscription_executor: sc_rpc::SubscriptionTaskExecutor| { let deps = - crate::rpc::FullDeps { client: client.clone(), pool: pool.clone(), deny_unsafe }; + crate::rpc::FullDeps { + client: client.clone(), + pool: pool.clone(), + deny_unsafe, + grandpa: crate::rpc::GrandpaDeps { + shared_voter_state: shared_voter_state.clone(), + shared_authority_set: shared_authority_set.clone(), + justification_stream: justification_stream.clone(), + subscription_executor: subscription_executor.clone(), + finality_provider: finality_proof_provider.clone(), + }, + backend: rpc_backend.clone(), + }; crate::rpc::create_full(deps).map_err(Into::into) }) }; From a85075fe7f2c4e391b7181f72f425635a29b1fe8 Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 9 Apr 2024 09:44:52 +0800 Subject: [PATCH 2/2] remove commented code --- node/src/service.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/node/src/service.rs b/node/src/service.rs index d59a19946..58d58e77b 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -260,21 +260,6 @@ pub fn new_full(config: Configuration) -> Result { crate::rpc::create_full(deps).map_err(Into::into) }) }; - - - // let rpc_extensions_builder = { - // let client = client.clone(); - // let pool = transaction_pool.clone(); - - // Box::new(move |deny_unsafe, _| { - // let deps = crate::rpc::FullDeps { - // client: client.clone(), - // pool: pool.clone(), - // deny_unsafe, - // }; - // crate::rpc::create_full(deps).map_err(Into::into) - // }) - // }; let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { network: network.clone(),