From 2e4b8996c4924fc39f85198019039cf0987f89ec Mon Sep 17 00:00:00 2001 From: Serban Iorga <serban@parity.io> Date: Sat, 6 Jan 2024 12:18:38 +0100 Subject: [PATCH] Kitchensink chain: Add BEEFY support (#2856) Related to https://github.com/paritytech/polkadot-sdk/issues/2787 Adding BEEFY support to the kitchensink chain in order to be able to extend the current warp sync zombienet tests with BEEFY enabled --- Cargo.lock | 9 + polkadot/runtime/rococo/src/lib.rs | 13 -- polkadot/runtime/westend/src/lib.rs | 13 -- substrate/bin/node/cli/Cargo.toml | 4 + substrate/bin/node/cli/src/chain_spec.rs | 54 +++++- substrate/bin/node/cli/src/service.rs | 167 ++++++++++++++---- .../cli/tests/res/default_genesis_config.json | 4 + substrate/bin/node/rpc/Cargo.toml | 2 + substrate/bin/node/rpc/src/lib.rs | 28 ++- substrate/bin/node/runtime/Cargo.toml | 8 + substrate/bin/node/runtime/src/lib.rs | 76 +++++++- substrate/bin/node/testing/src/genesis.rs | 13 +- substrate/bin/node/testing/src/keyring.rs | 20 +-- .../primitives/consensus/beefy/src/mmr.rs | 2 +- 14 files changed, 320 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc80c77ae60..23a0bf8fd35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7048,6 +7048,8 @@ dependencies = [ "pallet-babe", "pallet-bags-list", "pallet-balances", + "pallet-beefy", + "pallet-beefy-mmr", "pallet-bounties", "pallet-broker", "pallet-child-bounties", @@ -7121,6 +7123,7 @@ dependencies = [ "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", + "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", "sp-genesis-builder", @@ -8619,6 +8622,8 @@ dependencies = [ "sc-client-api", "sc-consensus-babe", "sc-consensus-babe-rpc", + "sc-consensus-beefy", + "sc-consensus-beefy-rpc", "sc-consensus-grandpa", "sc-consensus-grandpa-rpc", "sc-mixnet", @@ -19091,6 +19096,7 @@ dependencies = [ "jsonrpsee", "kitchensink-runtime", "log", + "mmr-gadget", "nix 0.26.2", "node-primitives", "node-rpc", @@ -19121,6 +19127,7 @@ dependencies = [ "sc-client-db", "sc-consensus", "sc-consensus-babe", + "sc-consensus-beefy", "sc-consensus-epochs", "sc-consensus-grandpa", "sc-consensus-slots", @@ -19152,6 +19159,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", + "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", "sp-externalities 0.19.0", @@ -19160,6 +19168,7 @@ dependencies = [ "sp-keyring", "sp-keystore", "sp-mixnet", + "sp-mmr-primitives", "sp-runtime", "sp-state-machine", "sp-statement-store", diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 4d0e537f6db..67caa347bc3 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -1237,19 +1237,6 @@ impl pallet_mmr::Config for Runtime { } parameter_types! { - /// Version of the produced MMR leaf. - /// - /// The version consists of two parts; - /// - `major` (3 bits) - /// - `minor` (5 bits) - /// - /// `major` should be updated only if decoding the previous MMR Leaf format from the payload - /// is not possible (i.e. backward incompatible change). - /// `minor` should be updated if fields are added to the previous MMR Leaf, which given SCALE - /// encoding does not prevent old leafs from being decoded. - /// - /// Hence we expect `major` to be changed really rarely (think never). - /// See [`MmrLeafVersion`] type documentation for more details. pub LeafVersion: MmrLeafVersion = MmrLeafVersion::new(0, 0); } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index fb54bec509b..e0b9fd0fb53 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -342,19 +342,6 @@ mod mmr { } parameter_types! { - /// Version of the produced MMR leaf. - /// - /// The version consists of two parts; - /// - `major` (3 bits) - /// - `minor` (5 bits) - /// - /// `major` should be updated only if decoding the previous MMR Leaf format from the payload - /// is not possible (i.e. backward incompatible change). - /// `minor` should be updated if fields are added to the previous MMR Leaf, which given SCALE - /// encoding does not prevent old leafs from being decoded. - /// - /// Hence we expect `major` to be changed really rarely (think never). - /// See [`MmrLeafVersion`] type documentation for more details. pub LeafVersion: MmrLeafVersion = MmrLeafVersion::new(0, 0); } diff --git a/substrate/bin/node/cli/Cargo.toml b/substrate/bin/node/cli/Cargo.toml index a01c35a3b63..35d632865ee 100644 --- a/substrate/bin/node/cli/Cargo.toml +++ b/substrate/bin/node/cli/Cargo.toml @@ -52,6 +52,7 @@ rand = "0.8" # primitives sp-authority-discovery = { path = "../../../primitives/authority-discovery" } sp-consensus-babe = { path = "../../../primitives/consensus/babe" } +beefy-primitives = { package = "sp-consensus-beefy", path = "../../../primitives/consensus/beefy" } grandpa-primitives = { package = "sp-consensus-grandpa", path = "../../../primitives/consensus/grandpa" } sp-api = { path = "../../../primitives/api" } sp-core = { path = "../../../primitives/core" } @@ -64,6 +65,7 @@ sp-consensus = { path = "../../../primitives/consensus/common" } sp-transaction-storage-proof = { path = "../../../primitives/transaction-storage-proof" } sp-io = { path = "../../../primitives/io" } sp-mixnet = { path = "../../../primitives/mixnet" } +sp-mmr-primitives = { path = "../../../primitives/merkle-mountain-range" } sp-statement-store = { path = "../../../primitives/statement-store" } # client dependencies @@ -79,7 +81,9 @@ sc-network-sync = { path = "../../../client/network/sync" } sc-network-statement = { path = "../../../client/network/statement" } sc-consensus-slots = { path = "../../../client/consensus/slots" } sc-consensus-babe = { path = "../../../client/consensus/babe" } +beefy = { package = "sc-consensus-beefy", path = "../../../client/consensus/beefy" } grandpa = { package = "sc-consensus-grandpa", path = "../../../client/consensus/grandpa" } +mmr-gadget = { path = "../../../client/merkle-mountain-range" } sc-rpc = { path = "../../../client/rpc" } sc-basic-authorship = { path = "../../../client/basic-authorship" } sc-service = { path = "../../../client/service", default-features = false } diff --git a/substrate/bin/node/cli/src/chain_spec.rs b/substrate/bin/node/cli/src/chain_spec.rs index 3559348d188..b6e8fb8a14e 100644 --- a/substrate/bin/node/cli/src/chain_spec.rs +++ b/substrate/bin/node/cli/src/chain_spec.rs @@ -18,6 +18,7 @@ //! Substrate chain configurations. +use beefy_primitives::ecdsa_crypto::AuthorityId as BeefyId; use grandpa_primitives::AuthorityId as GrandpaId; use kitchensink_runtime::{ constants::currency::*, wasm_binary_unwrap, Block, MaxNominations, SessionKeys, StakerStatus, @@ -73,23 +74,37 @@ fn session_keys( im_online: ImOnlineId, authority_discovery: AuthorityDiscoveryId, mixnet: MixnetId, + beefy: BeefyId, ) -> SessionKeys { - SessionKeys { grandpa, babe, im_online, authority_discovery, mixnet } + SessionKeys { grandpa, babe, im_online, authority_discovery, mixnet, beefy } } fn configure_accounts_for_staging_testnet() -> ( - Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId)>, + Vec<( + AccountId, + AccountId, + GrandpaId, + BabeId, + ImOnlineId, + AuthorityDiscoveryId, + MixnetId, + BeefyId, + )>, AccountId, Vec<AccountId>, ) { #[rustfmt::skip] - // stash, controller, session-key + // stash, controller, session-key, beefy id // generated with secret: // for i in 1 2 3 4 ; do for j in stash controller; do subkey inspect "$secret"/fir/$j/$i; done; done // // and // - // for i in 1 2 3 4 ; do for j in session; do subkey --ed25519 inspect "$secret"//fir//$j//$i; done; done + // for i in 1 2 3 4 ; do for j in session; do subkey inspect --scheme ed25519 "$secret"//fir//$j//$i; done; done + // + // and + // + // for i in 1 2 3 4 ; do for j in session; do subkey inspect --scheme ecdsa "$secret"//fir//$j//$i; done; done let initial_authorities: Vec<( AccountId, @@ -99,6 +114,7 @@ fn configure_accounts_for_staging_testnet() -> ( ImOnlineId, AuthorityDiscoveryId, MixnetId, + BeefyId, )> = vec![ ( // 5Fbsd6WXDGiLTxunqeK5BATNiocfCqu9bS1yArVjCgeBLkVy @@ -120,6 +136,9 @@ fn configure_accounts_for_staging_testnet() -> ( // 5EZaeQ8djPcq9pheJUhgerXQZt9YaHnMJpiHMRhwQeinqUW8 array_bytes::hex2array_unchecked("6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106") .unchecked_into(), + // 5DMLFcDdLLQbw696YfHaWBpQR99HwR456ycSCfr6L7KXGYK8 + array_bytes::hex2array_unchecked("035560fafa241739869360aa4b32bc98953172ceb41a19c6cc1a27962fb3d1ecec") + .unchecked_into(), ), ( // 5ERawXCzCWkjVq3xz1W5KGNtVx2VdefvZ62Bw1FEuZW4Vny2 @@ -141,6 +160,9 @@ fn configure_accounts_for_staging_testnet() -> ( // 5DhLtiaQd1L1LU9jaNeeu9HJkP6eyg3BwXA7iNMzKm7qqruQ array_bytes::hex2array_unchecked("482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e") .unchecked_into(), + // 5FYk11kNtB4178wLKJ2RNoUzzcjgRUciFe3SJDVZXhqX4dzG + array_bytes::hex2array_unchecked("02da1ab255ed888ee3e19b73d335fc13160b3eb10456c2d17c6a8ea7de403d2445") + .unchecked_into(), ), ( // 5DyVtKWPidondEu8iHZgi6Ffv9yrJJ1NDNLom3X9cTDi98qp @@ -162,6 +184,9 @@ fn configure_accounts_for_staging_testnet() -> ( // 5DhKqkHRkndJu8vq7pi2Q5S3DfftWJHGxbEUNH43b46qNspH array_bytes::hex2array_unchecked("482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a") .unchecked_into(), + // 5GQx4FToRBPqfani6o7owFJE1UstiviqbPP7HPWyvtXWWukn + array_bytes::hex2array_unchecked("036a818b3f59579c5fbbe4fede64f49dbf090ba883eb2a175d5ca90e5adb5f0b3e") + .unchecked_into(), ), ( // 5HYZnKWe5FVZQ33ZRJK1rG3WaLMztxWrrNDb1JRwaHHVWyP9 @@ -183,6 +208,9 @@ fn configure_accounts_for_staging_testnet() -> ( // 5C4vDQxA8LTck2xJEy4Yg1hM9qjDt4LvTQaMo4Y8ne43aU6x array_bytes::hex2array_unchecked("00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378") .unchecked_into(), + // 5FCu2pY928VVHPgnNVJssvxFJZECyNe1CyH3WTG79Wisx58B + array_bytes::hex2array_unchecked("020ce02b963548f9f8ade8765f7a4a06638c17819c78422a1cc35b647873583eef") + .unchecked_into(), ), ]; @@ -234,7 +262,8 @@ where /// Helper function to generate stash, controller and session key from seed. pub fn authority_keys_from_seed( seed: &str, -) -> (AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId) { +) -> (AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId, BeefyId) +{ ( get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)), get_account_id_from_seed::<sr25519::Public>(seed), @@ -243,6 +272,7 @@ pub fn authority_keys_from_seed( get_from_seed::<ImOnlineId>(seed), get_from_seed::<AuthorityDiscoveryId>(seed), get_from_seed::<MixnetId>(seed), + get_from_seed::<BeefyId>(seed), ) } @@ -255,12 +285,22 @@ fn configure_accounts( ImOnlineId, AuthorityDiscoveryId, MixnetId, + BeefyId, )>, initial_nominators: Vec<AccountId>, endowed_accounts: Option<Vec<AccountId>>, stash: Balance, ) -> ( - Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId)>, + Vec<( + AccountId, + AccountId, + GrandpaId, + BabeId, + ImOnlineId, + AuthorityDiscoveryId, + MixnetId, + BeefyId, + )>, Vec<AccountId>, usize, Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>, @@ -326,6 +366,7 @@ pub fn testnet_genesis( ImOnlineId, AuthorityDiscoveryId, MixnetId, + BeefyId, )>, initial_nominators: Vec<AccountId>, root_key: AccountId, @@ -351,6 +392,7 @@ pub fn testnet_genesis( x.4.clone(), x.5.clone(), x.6.clone(), + x.7.clone(), ), ) }) diff --git a/substrate/bin/node/cli/src/service.rs b/substrate/bin/node/cli/src/service.rs index 4f8c6198cdc..5e6e794f732 100644 --- a/substrate/bin/node/cli/src/service.rs +++ b/substrate/bin/node/cli/src/service.rs @@ -63,6 +63,8 @@ type FullBackend = sc_service::TFullBackend<Block>; type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>; type FullGrandpaBlockImport = grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>; +type FullBeefyBlockImport<InnerBlockImport> = + beefy::import::BeefyBlockImport<Block, FullBackend, FullClient, InnerBlockImport>; /// The transaction pool type definition. pub type TransactionPool = sc_transaction_pool::FullPool<Block, FullClient>; @@ -165,9 +167,14 @@ pub fn new_partial( sc_rpc::SubscriptionTaskExecutor, ) -> Result<jsonrpsee::RpcModule<()>, sc_service::Error>, ( - sc_consensus_babe::BabeBlockImport<Block, FullClient, FullGrandpaBlockImport>, + sc_consensus_babe::BabeBlockImport< + Block, + FullClient, + FullBeefyBlockImport<FullGrandpaBlockImport>, + >, grandpa::LinkHalf<Block, FullClient, FullSelectChain>, sc_consensus_babe::BabeLink<Block>, + beefy::BeefyVoterLinks<Block>, ), grandpa::SharedVoterState, Option<Telemetry>, @@ -222,9 +229,17 @@ pub fn new_partial( )?; let justification_import = grandpa_block_import.clone(); + let (beefy_block_import, beefy_voter_links, beefy_rpc_links) = + beefy::beefy_block_import_and_links( + grandpa_block_import, + backend.clone(), + client.clone(), + config.prometheus_registry().cloned(), + ); + let (block_import, babe_link) = sc_consensus_babe::block_import( sc_consensus_babe::configuration(&*client)?, - grandpa_block_import, + beefy_block_import, client.clone(), )?; @@ -253,7 +268,7 @@ pub fn new_partial( offchain_tx_pool_factory: OffchainTransactionPoolFactory::new(transaction_pool.clone()), })?; - let import_setup = (block_import, grandpa_link, babe_link); + let import_setup = (block_import, grandpa_link, babe_link, beefy_voter_links); let statement_store = sc_statement_store::Store::new_shared( &config.data_path, @@ -268,7 +283,7 @@ pub fn new_partial( let (mixnet_api, mixnet_api_backend) = mixnet_config.map(sc_mixnet::Api::new).unzip(); let (rpc_extensions_builder, rpc_setup) = { - let (_, grandpa_link, _) = &import_setup; + let (_, grandpa_link, _, _) = &import_setup; let justification_stream = grandpa_link.justification_stream(); let shared_authority_set = grandpa_link.shared_authority_set().clone(); @@ -288,31 +303,41 @@ pub fn new_partial( let rpc_backend = backend.clone(); let rpc_statement_store = statement_store.clone(); - let rpc_extensions_builder = move |deny_unsafe, subscription_executor| { - let deps = node_rpc::FullDeps { - client: client.clone(), - pool: pool.clone(), - select_chain: select_chain.clone(), - chain_spec: chain_spec.cloned_box(), - deny_unsafe, - babe: node_rpc::BabeDeps { - keystore: keystore.clone(), - babe_worker_handle: babe_worker_handle.clone(), - }, - grandpa: node_rpc::GrandpaDeps { - shared_voter_state: shared_voter_state.clone(), - shared_authority_set: shared_authority_set.clone(), - justification_stream: justification_stream.clone(), - subscription_executor, - finality_provider: finality_proof_provider.clone(), - }, - statement_store: rpc_statement_store.clone(), - backend: rpc_backend.clone(), - mixnet_api: mixnet_api.as_ref().cloned(), - }; + let rpc_extensions_builder = + move |deny_unsafe, subscription_executor: node_rpc::SubscriptionTaskExecutor| { + let deps = node_rpc::FullDeps { + client: client.clone(), + pool: pool.clone(), + select_chain: select_chain.clone(), + chain_spec: chain_spec.cloned_box(), + deny_unsafe, + babe: node_rpc::BabeDeps { + keystore: keystore.clone(), + babe_worker_handle: babe_worker_handle.clone(), + }, + grandpa: node_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(), + }, + beefy: node_rpc::BeefyDeps { + beefy_finality_proof_stream: beefy_rpc_links + .from_voter_justif_stream + .clone(), + beefy_best_block_stream: beefy_rpc_links + .from_voter_best_beefy_stream + .clone(), + subscription_executor, + }, + statement_store: rpc_statement_store.clone(), + backend: rpc_backend.clone(), + mixnet_api: mixnet_api.as_ref().cloned(), + }; - node_rpc::create_full(deps).map_err(Into::into) - }; + node_rpc::create_full(deps).map_err(Into::into) + }; (rpc_extensions_builder, shared_voter_state2) }; @@ -358,10 +383,24 @@ pub fn new_full_base( mixnet_config: Option<sc_mixnet::Config>, disable_hardware_benchmarks: bool, with_startup_data: impl FnOnce( - &sc_consensus_babe::BabeBlockImport<Block, FullClient, FullGrandpaBlockImport>, + &sc_consensus_babe::BabeBlockImport< + Block, + FullClient, + FullBeefyBlockImport<FullGrandpaBlockImport>, + >, &sc_consensus_babe::BabeLink<Block>, ), ) -> Result<NewFullBase, ServiceError> { + let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled; + let role = config.role.clone(); + let force_authoring = config.force_authoring; + let backoff_authoring_blocks = + Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default()); + let name = config.network.node_name.clone(); + let enable_grandpa = !config.disable_grandpa; + let prometheus_registry = config.prometheus_registry().cloned(); + let enable_offchain_worker = config.offchain_worker.enabled; + let hwbench = (!disable_hardware_benchmarks) .then_some(config.database.path().map(|database_path| { let _ = std::fs::create_dir_all(&database_path); @@ -391,6 +430,24 @@ pub fn new_full_base( grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone()); net_config.add_notification_protocol(grandpa_protocol_config); + let beefy_gossip_proto_name = + beefy::gossip_protocol_name(&genesis_hash, config.chain_spec.fork_id()); + // `beefy_on_demand_justifications_handler` is given to `beefy-gadget` task to be run, + // while `beefy_req_resp_cfg` is added to `config.network.request_response_protocols`. + let (beefy_on_demand_justifications_handler, beefy_req_resp_cfg) = + beefy::communication::request_response::BeefyJustifsRequestHandler::new( + &genesis_hash, + config.chain_spec.fork_id(), + client.clone(), + prometheus_registry.clone(), + ); + + let (beefy_notification_config, beefy_notification_service) = + beefy::communication::beefy_peers_set_config(beefy_gossip_proto_name.clone()); + + net_config.add_notification_protocol(beefy_notification_config); + net_config.add_request_response_protocol(beefy_req_resp_cfg); + let (statement_handler_proto, statement_config) = sc_network_statement::StatementHandlerPrototype::new( genesis_hash, @@ -442,15 +499,6 @@ pub fn new_full_base( task_manager.spawn_handle().spawn("mixnet", None, mixnet); } - let role = config.role.clone(); - let force_authoring = config.force_authoring; - let backoff_authoring_blocks = - Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default()); - let name = config.network.node_name.clone(); - let enable_grandpa = !config.disable_grandpa; - let prometheus_registry = config.prometheus_registry().cloned(); - let enable_offchain_worker = config.offchain_worker.enabled; - let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { config, backend: backend.clone(), @@ -488,7 +536,7 @@ pub fn new_full_base( } } - let (block_import, grandpa_link, babe_link) = import_setup; + let (block_import, grandpa_link, babe_link, beefy_links) = import_setup; (with_startup_data)(&block_import, &babe_link); @@ -582,6 +630,47 @@ pub fn new_full_base( // need a keystore, regardless of which protocol we use below. let keystore = if role.is_authority() { Some(keystore_container.keystore()) } else { None }; + // beefy is enabled if its notification service exists + let network_params = beefy::BeefyNetworkParams { + network: network.clone(), + sync: sync_service.clone(), + gossip_protocol_name: beefy_gossip_proto_name, + justifications_protocol_name: beefy_on_demand_justifications_handler.protocol_name(), + notification_service: beefy_notification_service, + _phantom: core::marker::PhantomData::<Block>, + }; + let beefy_params = beefy::BeefyParams { + client: client.clone(), + backend: backend.clone(), + payload_provider: beefy_primitives::mmr::MmrRootProvider::new(client.clone()), + runtime: client.clone(), + key_store: keystore.clone(), + network_params, + min_block_delta: 8, + prometheus_registry: prometheus_registry.clone(), + links: beefy_links, + on_demand_justifications_handler: beefy_on_demand_justifications_handler, + }; + + let beefy_gadget = beefy::start_beefy_gadget::<_, _, _, _, _, _, _>(beefy_params); + // BEEFY is part of consensus, if it fails we'll bring the node down with it to make sure it + // is noticed. + task_manager + .spawn_essential_handle() + .spawn_blocking("beefy-gadget", None, beefy_gadget); + // When offchain indexing is enabled, MMR gadget should also run. + if is_offchain_indexing_enabled { + task_manager.spawn_essential_handle().spawn_blocking( + "mmr-gadget", + None, + mmr_gadget::MmrGadget::start( + client.clone(), + backend.clone(), + sp_mmr_primitives::INDEXING_PREFIX.to_vec(), + ), + ); + } + let grandpa_config = grandpa::Config { // FIXME #1578 make this available through chainspec gossip_duration: std::time::Duration::from_millis(333), diff --git a/substrate/bin/node/cli/tests/res/default_genesis_config.json b/substrate/bin/node/cli/tests/res/default_genesis_config.json index caf12a443d3..1465a6497ca 100644 --- a/substrate/bin/node/cli/tests/res/default_genesis_config.json +++ b/substrate/bin/node/cli/tests/res/default_genesis_config.json @@ -45,6 +45,10 @@ "grandpa": { "authorities": [] }, + "beefy": { + "authorities": [], + "genesisBlock": 1 + }, "treasury": {}, "sudo": { "key": null diff --git a/substrate/bin/node/rpc/Cargo.toml b/substrate/bin/node/rpc/Cargo.toml index a4a361fadbc..66bd6e9a0a5 100644 --- a/substrate/bin/node/rpc/Cargo.toml +++ b/substrate/bin/node/rpc/Cargo.toml @@ -24,6 +24,8 @@ sc-chain-spec = { path = "../../../client/chain-spec" } sc-client-api = { path = "../../../client/api" } sc-consensus-babe = { path = "../../../client/consensus/babe" } sc-consensus-babe-rpc = { path = "../../../client/consensus/babe/rpc" } +sc-consensus-beefy = { path = "../../../client/consensus/beefy" } +sc-consensus-beefy-rpc = { path = "../../../client/consensus/beefy/rpc" } sc-consensus-grandpa = { path = "../../../client/consensus/grandpa" } sc-consensus-grandpa-rpc = { path = "../../../client/consensus/grandpa/rpc" } sc-mixnet = { path = "../../../client/mixnet" } diff --git a/substrate/bin/node/rpc/src/lib.rs b/substrate/bin/node/rpc/src/lib.rs index acc58777e91..4646524a25b 100644 --- a/substrate/bin/node/rpc/src/lib.rs +++ b/substrate/bin/node/rpc/src/lib.rs @@ -37,10 +37,13 @@ use jsonrpsee::RpcModule; use node_primitives::{AccountId, Balance, Block, BlockNumber, Hash, Nonce}; use sc_client_api::AuxStore; use sc_consensus_babe::BabeWorkerHandle; +use sc_consensus_beefy::communication::notification::{ + BeefyBestBlockStream, BeefyVersionedFinalityProofStream, +}; use sc_consensus_grandpa::{ FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState, }; -use sc_rpc::SubscriptionTaskExecutor; +pub use sc_rpc::SubscriptionTaskExecutor; pub use sc_rpc_api::DenyUnsafe; use sc_transaction_pool_api::TransactionPool; use sp_api::ProvideRuntimeApi; @@ -72,6 +75,16 @@ pub struct GrandpaDeps<B> { pub finality_provider: Arc<FinalityProofProvider<B, Block>>, } +/// Dependencies for BEEFY +pub struct BeefyDeps { + /// Receives notifications about finality proof events from BEEFY. + pub beefy_finality_proof_stream: BeefyVersionedFinalityProofStream<Block>, + /// Receives notifications about best block events from BEEFY. + pub beefy_best_block_stream: BeefyBestBlockStream<Block>, + /// Executor to drive the subscription manager in the BEEFY RPC handler. + pub subscription_executor: SubscriptionTaskExecutor, +} + /// Full client dependencies. pub struct FullDeps<C, P, SC, B> { /// The client instance to use. @@ -88,6 +101,8 @@ pub struct FullDeps<C, P, SC, B> { pub babe: BabeDeps, /// GRANDPA specific dependencies. pub grandpa: GrandpaDeps<B>, + /// BEEFY specific dependencies. + pub beefy: BeefyDeps, /// Shared statement store reference. pub statement_store: Arc<dyn sp_statement_store::StatementStore>, /// The backend used by the node. @@ -106,6 +121,7 @@ pub fn create_full<C, P, SC, B>( deny_unsafe, babe, grandpa, + beefy, statement_store, backend, mixnet_api, @@ -133,6 +149,7 @@ where use mmr_rpc::{Mmr, MmrApiServer}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; use sc_consensus_babe_rpc::{Babe, BabeApiServer}; + use sc_consensus_beefy_rpc::{Beefy, BeefyApiServer}; use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer}; use sc_rpc::{ dev::{Dev, DevApiServer}, @@ -205,5 +222,14 @@ where io.merge(mixnet)?; } + io.merge( + Beefy::<Block>::new( + beefy.beefy_finality_proof_stream, + beefy.beefy_best_block_stream, + beefy.subscription_executor, + )? + .into_rpc(), + )?; + Ok(io) } diff --git a/substrate/bin/node/runtime/Cargo.toml b/substrate/bin/node/runtime/Cargo.toml index 5f77f72af49..93695912c52 100644 --- a/substrate/bin/node/runtime/Cargo.toml +++ b/substrate/bin/node/runtime/Cargo.toml @@ -34,6 +34,7 @@ primitive-types = { version = "0.12.0", default-features = false, features = ["c # primitives sp-authority-discovery = { path = "../../../primitives/authority-discovery", default-features = false, features = ["serde"] } sp-consensus-babe = { path = "../../../primitives/consensus/babe", default-features = false, features = ["serde"] } +sp-consensus-beefy = { path = "../../../primitives/consensus/beefy", default-features = false } sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false, features = ["serde"] } sp-block-builder = { path = "../../../primitives/block-builder", default-features = false } sp-genesis-builder = { default-features = false, path = "../../../primitives/genesis-builder" } @@ -72,6 +73,8 @@ pallet-authorship = { path = "../../../frame/authorship", default-features = fal pallet-babe = { path = "../../../frame/babe", default-features = false } pallet-bags-list = { path = "../../../frame/bags-list", default-features = false } pallet-balances = { path = "../../../frame/balances", default-features = false } +pallet-beefy = { path = "../../../frame/beefy", default-features = false } +pallet-beefy-mmr = { path = "../../../frame/beefy-mmr", default-features = false } pallet-bounties = { path = "../../../frame/bounties", default-features = false } pallet-broker = { path = "../../../frame/broker", default-features = false } pallet-child-bounties = { path = "../../../frame/child-bounties", default-features = false } @@ -170,6 +173,8 @@ std = [ "pallet-babe/std", "pallet-bags-list/std", "pallet-balances/std", + "pallet-beefy-mmr/std", + "pallet-beefy/std", "pallet-bounties/std", "pallet-broker/std", "pallet-child-bounties/std", @@ -241,6 +246,7 @@ std = [ "sp-authority-discovery/std", "sp-block-builder/std", "sp-consensus-babe/std", + "sp-consensus-beefy/std", "sp-consensus-grandpa/std", "sp-core/std", "sp-genesis-builder/std", @@ -349,6 +355,8 @@ try-runtime = [ "pallet-babe/try-runtime", "pallet-bags-list/try-runtime", "pallet-balances/try-runtime", + "pallet-beefy-mmr/try-runtime", + "pallet-beefy/try-runtime", "pallet-bounties/try-runtime", "pallet-broker/try-runtime", "pallet-child-bounties/try-runtime", diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 4d409a791ba..6e7bfb4f4b4 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -75,6 +75,10 @@ use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; use pallet_tx_pause::RuntimeCallNameOf; use sp_api::impl_runtime_apis; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; +use sp_consensus_beefy::{ + ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature}, + mmr::MmrLeafVersion, +}; use sp_consensus_grandpa::AuthorityId as GrandpaId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_inherents::{CheckInherentsResult, InherentData}; @@ -131,7 +135,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); /// Max size for serialized extrinsic params for this testing runtime. /// This is a quite arbitrary but empirically battle tested value. #[cfg(test)] -pub const CALL_PARAMS_MAX_SIZE: usize = 208; +pub const CALL_PARAMS_MAX_SIZE: usize = 244; /// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics. #[cfg(feature = "std")] @@ -602,6 +606,7 @@ impl_opaque_keys! { pub im_online: ImOnline, pub authority_discovery: AuthorityDiscovery, pub mixnet: Mixnet, + pub beefy: Beefy, } } @@ -1573,6 +1578,17 @@ impl pallet_mmr::Config for Runtime { type WeightInfo = (); } +parameter_types! { + pub LeafVersion: MmrLeafVersion = MmrLeafVersion::new(0, 0); +} + +impl pallet_beefy_mmr::Config for Runtime { + type LeafVersion = LeafVersion; + type BeefyAuthorityToMerkleLeaf = pallet_beefy_mmr::BeefyEcdsaToEthereum; + type LeafExtra = Vec<u8>; + type BeefyDataProvider = (); +} + parameter_types! { pub const LotteryPalletId: PalletId = PalletId(*b"py/lotto"); pub const MaxCalls: u32 = 10; @@ -2077,6 +2093,11 @@ construct_runtime!( AssetConversionTxPayment: pallet_asset_conversion_tx_payment, ElectionProviderMultiPhase: pallet_election_provider_multi_phase, Staking: pallet_staking, + Beefy: pallet_beefy::{Pallet, Call, Storage, Config<T>, ValidateUnsigned}, + // MMR leaf construction must be before session in order to have leaf contents + // refer to block<N-1> consistently. see substrate issue #11797 for details. + Mmr: pallet_mmr::{Pallet, Storage}, + MmrLeaf: pallet_beefy_mmr::{Pallet, Storage}, Session: pallet_session, Democracy: pallet_democracy, Council: pallet_collective::<Instance1>, @@ -2106,7 +2127,6 @@ construct_runtime!( Tips: pallet_tips, Assets: pallet_assets::<Instance1>, PoolAssets: pallet_assets::<Instance2>, - Mmr: pallet_mmr, Lottery: pallet_lottery, Nis: pallet_nis, Uniques: pallet_uniques, @@ -2202,6 +2222,22 @@ type EventRecord = frame_system::EventRecord< <Runtime as frame_system::Config>::Hash, >; +parameter_types! { + pub const BeefySetIdSessionEntries: u32 = BondingDuration::get() * SessionsPerEra::get(); +} + +impl pallet_beefy::Config for Runtime { + type BeefyId = BeefyId; + type MaxAuthorities = MaxAuthorities; + type MaxNominators = ConstU32<0>; + type MaxSetIdSessionEntries = BeefySetIdSessionEntries; + type OnNewValidatorSet = MmrLeaf; + type WeightInfo = (); + type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, BeefyId)>>::Proof; + type EquivocationReportSystem = + pallet_beefy::EquivocationReportSystem<Self, Offences, Historical, ReportLongevity>; +} + /// MMR helper types. mod mmr { use super::Runtime; @@ -2656,6 +2692,42 @@ impl_runtime_apis! { } } + #[api_version(3)] + impl sp_consensus_beefy::BeefyApi<Block, BeefyId> for Runtime { + fn beefy_genesis() -> Option<BlockNumber> { + Beefy::genesis_block() + } + + fn validator_set() -> Option<sp_consensus_beefy::ValidatorSet<BeefyId>> { + Beefy::validator_set() + } + + fn submit_report_equivocation_unsigned_extrinsic( + equivocation_proof: sp_consensus_beefy::EquivocationProof< + BlockNumber, + BeefyId, + BeefySignature, + >, + key_owner_proof: sp_consensus_beefy::OpaqueKeyOwnershipProof, + ) -> Option<()> { + let key_owner_proof = key_owner_proof.decode()?; + + Beefy::submit_unsigned_equivocation_report( + equivocation_proof, + key_owner_proof, + ) + } + + fn generate_key_ownership_proof( + _set_id: sp_consensus_beefy::ValidatorSetId, + authority_id: BeefyId, + ) -> Option<sp_consensus_beefy::OpaqueKeyOwnershipProof> { + Historical::prove((sp_consensus_beefy::KEY_TYPE, authority_id)) + .map(|p| p.encode()) + .map(sp_consensus_beefy::OpaqueKeyOwnershipProof::new) + } + } + impl pallet_mmr::primitives::MmrApi< Block, mmr::Hash, diff --git a/substrate/bin/node/testing/src/genesis.rs b/substrate/bin/node/testing/src/genesis.rs index eecbf64775b..6ec21fbe093 100644 --- a/substrate/bin/node/testing/src/genesis.rs +++ b/substrate/bin/node/testing/src/genesis.rs @@ -24,7 +24,7 @@ use kitchensink_runtime::{ GrandpaConfig, IndicesConfig, RuntimeGenesisConfig, SessionConfig, SocietyConfig, StakerStatus, StakingConfig, BABE_GENESIS_EPOCH_CONFIG, }; -use sp_keyring::{Ed25519Keyring, Sr25519Keyring}; +use sp_keyring::Ed25519Keyring; use sp_runtime::Perbill; /// Create genesis runtime configuration for tests. @@ -52,13 +52,9 @@ pub fn config_endowed(extra_endowed: Vec<AccountId>) -> RuntimeGenesisConfig { balances: BalancesConfig { balances: endowed }, session: SessionConfig { keys: vec![ - (alice(), dave(), to_session_keys(&Ed25519Keyring::Alice, &Sr25519Keyring::Alice)), - (bob(), eve(), to_session_keys(&Ed25519Keyring::Bob, &Sr25519Keyring::Bob)), - ( - charlie(), - ferdie(), - to_session_keys(&Ed25519Keyring::Charlie, &Sr25519Keyring::Charlie), - ), + (alice(), dave(), session_keys_from_seed(Ed25519Keyring::Alice.into())), + (bob(), eve(), session_keys_from_seed(Ed25519Keyring::Bob.into())), + (charlie(), ferdie(), session_keys_from_seed(Ed25519Keyring::Charlie.into())), ], }, staking: StakingConfig { @@ -79,6 +75,7 @@ pub fn config_endowed(extra_endowed: Vec<AccountId>) -> RuntimeGenesisConfig { ..Default::default() }, grandpa: GrandpaConfig { authorities: vec![], _config: Default::default() }, + beefy: Default::default(), im_online: Default::default(), authority_discovery: Default::default(), democracy: Default::default(), diff --git a/substrate/bin/node/testing/src/keyring.rs b/substrate/bin/node/testing/src/keyring.rs index 9940077c9da..6c885cc039a 100644 --- a/substrate/bin/node/testing/src/keyring.rs +++ b/substrate/bin/node/testing/src/keyring.rs @@ -20,8 +20,10 @@ use codec::Encode; use kitchensink_runtime::{CheckedExtrinsic, SessionKeys, SignedExtra, UncheckedExtrinsic}; +use node_cli::chain_spec::get_from_seed; use node_primitives::{AccountId, Balance, Nonce}; -use sp_keyring::{AccountKeyring, Ed25519Keyring, Sr25519Keyring}; +use sp_core::{ecdsa, ed25519, sr25519}; +use sp_keyring::AccountKeyring; use sp_runtime::generic::Era; /// Alice's account id. @@ -55,16 +57,14 @@ pub fn ferdie() -> AccountId { } /// Convert keyrings into `SessionKeys`. -pub fn to_session_keys( - ed25519_keyring: &Ed25519Keyring, - sr25519_keyring: &Sr25519Keyring, -) -> SessionKeys { +pub fn session_keys_from_seed(seed: &str) -> SessionKeys { SessionKeys { - grandpa: ed25519_keyring.to_owned().public().into(), - babe: sr25519_keyring.to_owned().public().into(), - im_online: sr25519_keyring.to_owned().public().into(), - authority_discovery: sr25519_keyring.to_owned().public().into(), - mixnet: sr25519_keyring.to_owned().public().into(), + grandpa: get_from_seed::<ed25519::Public>(seed).into(), + babe: get_from_seed::<sr25519::Public>(seed).into(), + im_online: get_from_seed::<sr25519::Public>(seed).into(), + authority_discovery: get_from_seed::<sr25519::Public>(seed).into(), + mixnet: get_from_seed::<sr25519::Public>(seed).into(), + beefy: get_from_seed::<ecdsa::Public>(seed).into(), } } diff --git a/substrate/primitives/consensus/beefy/src/mmr.rs b/substrate/primitives/consensus/beefy/src/mmr.rs index 9ac1624ca75..1b9a45f8687 100644 --- a/substrate/primitives/consensus/beefy/src/mmr.rs +++ b/substrate/primitives/consensus/beefy/src/mmr.rs @@ -66,7 +66,7 @@ pub struct MmrLeaf<BlockNumber, Hash, MerkleRoot, ExtraData> { /// An MMR leaf versioning scheme. /// -/// Version is a single byte that constist of two components: +/// Version is a single byte that consists of two components: /// - `major` - 3 bits /// - `minor` - 5 bits /// -- GitLab