From a55f5b4dfa86611ba5c60711e03e90289dd860f2 Mon Sep 17 00:00:00 2001 From: bgallois <benjamin@gallois.cc> Date: Wed, 25 Sep 2024 13:39:16 +0200 Subject: [PATCH] add grandpa rpc --- Cargo.lock | 69 ++++++++++++++++++++++++++++++++++----------- Cargo.toml | 2 ++ node/Cargo.toml | 2 ++ node/src/rpc.rs | 51 ++++++++++++++++++++++++++++++--- node/src/service.rs | 45 ++++++++++++++++++++--------- 5 files changed, 135 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65ec4ffb1..bec05c6ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,13 +429,13 @@ dependencies = [ [[package]] name = "ark-secret-scalar" version = "0.0.2" -source = "git+https://github.com/duniter/ring-vrf#06b60fdb7308c127448ef5799414c45516f6c42b" +source = "git+https://github.com/duniter/ring-vrf#27a6b9f6aa0c576bc481947dd5e4f3beff0fe327" dependencies = [ "ark-ec", "ark-ff", "ark-serialize", "ark-std", - "ark-transcript", + "ark-transcript 0.0.2 (git+https://github.com/duniter/ring-vrf)", "digest 0.10.7", "getrandom_or_panic", "zeroize", @@ -478,7 +478,20 @@ dependencies = [ [[package]] name = "ark-transcript" version = "0.0.2" -source = "git+https://github.com/duniter/ring-vrf#06b60fdb7308c127448ef5799414c45516f6c42b" +source = "git+https://github.com/duniter/ring-vrf#27a6b9f6aa0c576bc481947dd5e4f3beff0fe327" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "digest 0.10.7", + "rand_core", + "sha3", +] + +[[package]] +name = "ark-transcript" +version = "0.0.2" +source = "git+https://github.com/w3f/ring-vrf#0fef8266d851932ad25d6b41bc4b34d834d1e11d" dependencies = [ "ark-ff", "ark-serialize", @@ -808,7 +821,7 @@ dependencies = [ [[package]] name = "bandersnatch_vrfs" version = "0.0.4" -source = "git+https://github.com/duniter/ring-vrf#06b60fdb7308c127448ef5799414c45516f6c42b" +source = "git+https://github.com/duniter/ring-vrf#27a6b9f6aa0c576bc481947dd5e4f3beff0fe327" dependencies = [ "ark-bls12-381", "ark-ec", @@ -1487,7 +1500,7 @@ dependencies = [ [[package]] name = "common" version = "0.1.0" -source = "git+https://github.com/w3f/ring-proof#b273d33f9981e2bb3375ab45faeb537f7ee35224" +source = "git+https://github.com/w3f/ring-proof#1472ce9cd87cee49c56ce7869a0aba872d837c51" dependencies = [ "ark-ec", "ark-ff", @@ -1496,8 +1509,7 @@ dependencies = [ "ark-std", "fflonk", "getrandom_or_panic", - "merlin", - "rand_chacha", + "rand_core", ] [[package]] @@ -2401,7 +2413,7 @@ dependencies = [ [[package]] name = "dleq_vrf" version = "0.0.2" -source = "git+https://github.com/duniter/ring-vrf#06b60fdb7308c127448ef5799414c45516f6c42b" +source = "git+https://github.com/duniter/ring-vrf#27a6b9f6aa0c576bc481947dd5e4f3beff0fe327" dependencies = [ "ark-ec", "ark-ff", @@ -2409,7 +2421,7 @@ dependencies = [ "ark-secret-scalar", "ark-serialize", "ark-std", - "ark-transcript", + "ark-transcript 0.0.2 (git+https://github.com/duniter/ring-vrf)", "arrayvec 0.7.4", "zeroize", ] @@ -2516,11 +2528,13 @@ dependencies = [ "sc-consensus-babe", "sc-consensus-babe-rpc", "sc-consensus-grandpa", + "sc-consensus-grandpa-rpc", "sc-consensus-manual-seal", "sc-executor", "sc-keystore", "sc-network", "sc-offchain", + "sc-rpc", "sc-rpc-api", "sc-service", "sc-telemetry", @@ -4334,14 +4348,14 @@ checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http 1.1.0", - "hyper 1.4.0", + "hyper 1.3.1", "hyper-util", "rustls 0.23.10", "rustls-pki-types", "tokio", "tokio-rustls 0.26.0", "tower-service", - "webpki-roots 0.26.3", + "webpki-roots 0.26.5", ] [[package]] @@ -8175,7 +8189,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c7c5fdde3cdae7203427dc4f0a68fe0ed09833edc525a03456b153b79828684" dependencies = [ "bytes", - "pin-project-lite 0.2.14", + "pin-project-lite", "quinn-proto 0.11.8", "quinn-udp 0.5.4", "rustc-hash 2.0.0", @@ -8550,7 +8564,7 @@ dependencies = [ "http 1.1.0", "http-body 1.0.0", "http-body-util", - "hyper 1.4.0", + "hyper 1.3.1", "hyper-rustls 0.27.3", "hyper-util", "ipnet", @@ -8559,7 +8573,7 @@ dependencies = [ "mime", "once_cell", "percent-encoding", - "pin-project-lite 0.2.14", + "pin-project-lite", "quinn 0.11.5", "rustls 0.23.10", "rustls-pemfile 2.1.2", @@ -8575,7 +8589,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots 0.26.3", + "webpki-roots 0.26.5", "winreg 0.52.0", ] @@ -8602,17 +8616,18 @@ dependencies = [ [[package]] name = "ring" version = "0.1.0" -source = "git+https://github.com/w3f/ring-proof#b273d33f9981e2bb3375ab45faeb537f7ee35224" +source = "git+https://github.com/w3f/ring-proof#1472ce9cd87cee49c56ce7869a0aba872d837c51" dependencies = [ "ark-ec", "ark-ff", "ark-poly", "ark-serialize", "ark-std", + "ark-transcript 0.0.2 (git+https://github.com/w3f/ring-vrf)", + "arrayvec 0.7.4", "blake2 0.10.6", "common", "fflonk", - "merlin", ] [[package]] @@ -9316,6 +9331,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "sc-consensus-grandpa-rpc" +version = "0.19.0" +source = "git+https://github.com/duniter/duniter-polkadot-sdk?branch=duniter-substrate-v1.14.0#bcc60f3e4170c3908689252242f40761270c9a51" +dependencies = [ + "finality-grandpa", + "futures 0.3.30", + "jsonrpsee 0.23.2", + "log", + "parity-scale-codec", + "sc-client-api", + "sc-consensus-grandpa", + "sc-rpc", + "serde", + "sp-blockchain", + "sp-core", + "sp-runtime", + "thiserror", +] + [[package]] name = "sc-consensus-manual-seal" version = "0.35.0" diff --git a/Cargo.toml b/Cargo.toml index 648c2d734..6703cc209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -169,6 +169,8 @@ sp-block-builder = { git = 'https://github.com/duniter/duniter-polkadot-sdk', br sc-client-db = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } sc-client-api = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } sc-consensus-grandpa = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } +sc-rpc = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } +sc-consensus-grandpa-rpc = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } sc-consensus-babe = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } sc-consensus-babe-rpc = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } sp-blockchain = { git = 'https://github.com/duniter/duniter-polkadot-sdk', branch = 'duniter-substrate-v1.14.0', default-features = false } diff --git a/node/Cargo.toml b/node/Cargo.toml index 7c5db8247..97e51a664 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -139,9 +139,11 @@ sc-chain-spec = { workspace = true, default-features = true } sc-client-api = { workspace = true, default-features = true } sc-client-db = { workspace = true, default-features = true } sc-consensus = { workspace = true, default-features = true } +sc-rpc = { workspace = true, default-features = true } sc-consensus-babe = { workspace = true, default-features = true } sc-consensus-babe-rpc = { workspace = true, default-features = true } sc-consensus-grandpa = { workspace = true, default-features = true } +sc-consensus-grandpa-rpc = { workspace = true, default-features = true } sc-consensus-manual-seal = { workspace = true, default-features = true } sc-executor = { workspace = true, default-features = true } sc-keystore = { workspace = true, default-features = true } diff --git a/node/src/rpc.rs b/node/src/rpc.rs index d70694e23..203e22d08 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -23,9 +23,13 @@ pub use sc_rpc_api::DenyUnsafe; -use common_runtime::{AccountId, Balance, Block, Index}; +use common_runtime::{AccountId, Balance, Block, BlockNumber, Hash, Index}; use jsonrpsee::RpcModule; use sc_consensus_babe::{BabeApi, BabeWorkerHandle}; +use sc_consensus_grandpa::{ + self, FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState, +}; +use sc_rpc::SubscriptionTaskExecutor; use sc_transaction_pool_api::TransactionPool; use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; @@ -43,8 +47,23 @@ pub struct BabeDeps { pub keystore: KeystorePtr, } +/// Dependencies for GRANDPA +#[derive(Clone)] +pub struct GrandpaDeps<B> { + /// Voting round info. + pub shared_voter_state: SharedVoterState, + /// Authority set info. + pub shared_authority_set: SharedAuthoritySet<Hash, BlockNumber>, + /// Receives notifications about justification events from Grandpa. + pub justification_stream: GrandpaJustificationStream<Block>, + /// Executor to drive the subscription manager in the Grandpa RPC handler. + pub subscription_executor: SubscriptionTaskExecutor, + /// Finality proof provider. + pub finality_provider: Arc<FinalityProofProvider<B, Block>>, +} + /// Full client dependencies. -pub struct FullDeps<C, P, SC> { +pub struct FullDeps<C, P, SC, B> { /// The client instance to use. pub client: Arc<C>, /// Transaction pool instance. @@ -59,11 +78,13 @@ pub struct FullDeps<C, P, SC> { >, /// BABE specific dependencies. pub babe: Option<BabeDeps>, + /// GRANDPA specific dependencies. + pub grandpa: GrandpaDeps<B>, } /// Instantiate all full RPC extensions. -pub fn create_full<C, P, SC>( - deps: FullDeps<C, P, SC>, +pub fn create_full<C, P, SC, B>( + deps: FullDeps<C, P, SC, B>, ) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>> where C: ProvideRuntimeApi<Block>, @@ -75,9 +96,11 @@ where C::Api: BlockBuilder<Block>, P: TransactionPool + 'static, SC: SelectChain<Block> + 'static, + B: sc_client_api::Backend<Block> + 'static, { use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; use sc_consensus_babe_rpc::{Babe, BabeApiServer}; + use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer}; use sc_consensus_manual_seal::rpc::{ManualSeal, ManualSealApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; @@ -89,6 +112,7 @@ where deny_unsafe, command_sink_opt, babe, + grandpa, } = deps; if let Some(babe) = babe { @@ -107,6 +131,25 @@ where .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, + shared_voter_state, + justification_stream, + finality_provider, + ) + .into_rpc(), + )?; + module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; module.merge(TransactionPayment::new(client).into_rpc())?; if let Some(command_sink) = command_sink_opt { diff --git a/node/src/service.rs b/node/src/service.rs index 048f0f565..6893dc373 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -23,8 +23,9 @@ use async_io::Timer; use common_runtime::Block; use futures::{Stream, StreamExt}; use sc_client_api::{client::BlockBackend, Backend}; -use sc_consensus_grandpa::SharedVoterState; +use sc_consensus_grandpa::{FinalityProofProvider, SharedVoterState}; use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams}; +use sc_rpc::SubscriptionTaskExecutor; use sc_service::{ error::Error as ServiceError, Configuration, PartialComponents, TaskManager, WarpSyncParams, }; @@ -572,6 +573,12 @@ where } } + let justification_stream = grandpa_link.justification_stream(); + let shared_authority_set = grandpa_link.shared_authority_set().clone(); + let shared_voter_state = SharedVoterState::empty(); + let finality_proof_provider = + FinalityProofProvider::new_for_service(backend.clone(), Some(shared_authority_set.clone())); + let rpc_extensions_builder = { let client = client.clone(); let pool = transaction_pool.clone(); @@ -581,19 +588,31 @@ where babe_worker_handle, keystore: keystore.clone(), }); + let rpc_setup = shared_voter_state.clone(); + + Box::new( + move |deny_unsafe, subscription_task_executor: SubscriptionTaskExecutor| { + let grandpa_deps = crate::rpc::GrandpaDeps { + shared_voter_state: rpc_setup.clone(), + shared_authority_set: shared_authority_set.clone(), + justification_stream: justification_stream.clone(), + subscription_executor: subscription_task_executor.clone(), + finality_provider: finality_proof_provider.clone(), + }; - Box::new(move |deny_unsafe, _| { - let deps = crate::rpc::FullDeps { - client: client.clone(), - pool: pool.clone(), - select_chain: select_chain.clone(), - deny_unsafe, - babe: babe_deps.clone(), - command_sink_opt: command_sink_opt.clone(), - }; + let deps = crate::rpc::FullDeps { + client: client.clone(), + pool: pool.clone(), + select_chain: select_chain.clone(), + deny_unsafe, + babe: babe_deps.clone(), + grandpa: grandpa_deps, + command_sink_opt: command_sink_opt.clone(), + }; - crate::rpc::create_full(deps).map_err(Into::into) - }) + crate::rpc::create_full(deps).map_err(Into::into) + }, + ) }; let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { @@ -644,7 +663,7 @@ where network, voting_rule: sc_consensus_grandpa::VotingRulesBuilder::default().build(), prometheus_registry, - shared_voter_state: SharedVoterState::empty(), + shared_voter_state, telemetry: telemetry.as_ref().map(|x| x.handle()), notification_service: grandpa_notification_service, offchain_tx_pool_factory: sc_transaction_pool_api::OffchainTransactionPoolFactory::new( -- GitLab