From 82a307827ae382f035a13cbfe8ee6828f536b124 Mon Sep 17 00:00:00 2001 From: Alexandru Gheorghe <49718502+alexggh@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:39:39 +0300 Subject: [PATCH] [subsystem-bench] Add mocks for own assignments triggering (#5042) Since https://github.com/paritytech/polkadot-sdk/pull/4772, the benchamrks triggers its own assignments, but the mocks weren't properly hooked up, so that the approval is sent as well, so they would have counted as no-shows and impact the time it takes for a block to be approved. Fixed by adding mocks for availability recovery and candidate-validation and hook those into the benchmark. Signed-off-by: Alexandru Gheorghe --- .../subsystem-bench/src/lib/approval/mod.rs | 15 +++- .../src/lib/mock/availability_recovery.rs | 73 ++++++++++++++++++ .../src/lib/mock/candidate_validation.rs | 74 +++++++++++++++++++ .../node/subsystem-bench/src/lib/mock/mod.rs | 2 + .../src/lib/mock/runtime_api.rs | 13 +++- 5 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 polkadot/node/subsystem-bench/src/lib/mock/availability_recovery.rs create mode 100644 polkadot/node/subsystem-bench/src/lib/mock/candidate_validation.rs diff --git a/polkadot/node/subsystem-bench/src/lib/approval/mod.rs b/polkadot/node/subsystem-bench/src/lib/approval/mod.rs index 4ac044ea3459..404df2f8c653 100644 --- a/polkadot/node/subsystem-bench/src/lib/approval/mod.rs +++ b/polkadot/node/subsystem-bench/src/lib/approval/mod.rs @@ -28,6 +28,8 @@ use crate::{ dummy_builder, environment::{TestEnvironment, TestEnvironmentDependencies, MAX_TIME_OF_FLIGHT}, mock::{ + availability_recovery::MockAvailabilityRecovery, + candidate_validation::MockCandidateValidation, chain_api::{ChainApiState, MockChainApi}, network_bridge::{MockNetworkBridgeRx, MockNetworkBridgeTx}, runtime_api::{MockRuntimeApi, MockRuntimeApiCoreState}, @@ -59,13 +61,14 @@ use polkadot_node_subsystem_types::messages::{ApprovalDistributionMessage, Appro use polkadot_node_subsystem_util::metrics::Metrics; use polkadot_overseer::Handle as OverseerHandleReal; use polkadot_primitives::{ - BlockNumber, CandidateEvent, CandidateIndex, CandidateReceipt, Hash, Header, Slot, + BlockNumber, CandidateEvent, CandidateIndex, CandidateReceipt, Hash, Header, Slot, ValidatorId, ValidatorIndex, ASSIGNMENT_KEY_TYPE_ID, }; use prometheus::Registry; use sc_keystore::LocalKeystore; use sc_service::SpawnTaskHandle; use serde::{Deserialize, Serialize}; +use sp_application_crypto::AppCrypto; use sp_consensus_babe::Epoch as BabeEpoch; use sp_core::H256; use sp_keystore::Keystore; @@ -792,6 +795,12 @@ fn build_overseer( Some(state.test_authorities.key_seeds.get(NODE_UNDER_TEST as usize).unwrap().as_str()), ) .unwrap(); + keystore + .sr25519_generate_new( + ValidatorId::ID, + Some(state.test_authorities.key_seeds.get(NODE_UNDER_TEST as usize).unwrap().as_str()), + ) + .unwrap(); let system_clock = PastSystemClock::new(SystemClock {}, state.delta_tick_from_generated.clone()); @@ -831,7 +840,9 @@ fn build_overseer( .replace_chain_selection(|_| mock_chain_selection) .replace_runtime_api(|_| mock_runtime_api) .replace_network_bridge_tx(|_| mock_tx_bridge) - .replace_network_bridge_rx(|_| mock_rx_bridge); + .replace_network_bridge_rx(|_| mock_rx_bridge) + .replace_availability_recovery(|_| MockAvailabilityRecovery::new()) + .replace_candidate_validation(|_| MockCandidateValidation::new()); let (overseer, raw_handle) = dummy.build_with_connector(overseer_connector).expect("Should not fail"); diff --git a/polkadot/node/subsystem-bench/src/lib/mock/availability_recovery.rs b/polkadot/node/subsystem-bench/src/lib/mock/availability_recovery.rs new file mode 100644 index 000000000000..713226de6ad8 --- /dev/null +++ b/polkadot/node/subsystem-bench/src/lib/mock/availability_recovery.rs @@ -0,0 +1,73 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot 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. + +// Polkadot 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 Polkadot. If not, see . + +//! A generic mock availability recovery suitable to be used in benchmarks. + +use std::sync::Arc; + +use futures::FutureExt; +use polkadot_node_primitives::{AvailableData, BlockData, PoV}; +use polkadot_node_subsystem::{ + messages::AvailabilityRecoveryMessage, overseer, SpawnedSubsystem, SubsystemError, +}; +use polkadot_node_subsystem_types::OverseerSignal; +use polkadot_primitives::{Hash, HeadData, PersistedValidationData}; + +pub struct MockAvailabilityRecovery {} + +impl MockAvailabilityRecovery { + pub fn new() -> Self { + Self {} + } +} + +#[overseer::subsystem(AvailabilityRecovery, error=SubsystemError, prefix=self::overseer)] +impl MockAvailabilityRecovery { + fn start(self, ctx: Context) -> SpawnedSubsystem { + let future = self.run(ctx).map(|_| Ok(())).boxed(); + + SpawnedSubsystem { name: "test-environment", future } + } +} + +#[overseer::contextbounds(AvailabilityRecovery, prefix = self::overseer)] +impl MockAvailabilityRecovery { + async fn run(self, mut ctx: Context) { + loop { + let msg = ctx.recv().await.expect("Overseer never fails us"); + match msg { + orchestra::FromOrchestra::Signal(signal) => + if signal == OverseerSignal::Conclude { + return + }, + orchestra::FromOrchestra::Communication { msg } => match msg { + AvailabilityRecoveryMessage::RecoverAvailableData(_, _, _, _, tx) => { + let available_data = AvailableData { + pov: Arc::new(PoV { block_data: BlockData(Vec::new()) }), + validation_data: PersistedValidationData { + parent_head: HeadData(Vec::new()), + relay_parent_number: 0, + relay_parent_storage_root: Hash::default(), + max_pov_size: 2, + }, + }; + tx.send(Ok(available_data)).unwrap(); + }, + }, + } + } + } +} diff --git a/polkadot/node/subsystem-bench/src/lib/mock/candidate_validation.rs b/polkadot/node/subsystem-bench/src/lib/mock/candidate_validation.rs new file mode 100644 index 000000000000..941fac2a38c6 --- /dev/null +++ b/polkadot/node/subsystem-bench/src/lib/mock/candidate_validation.rs @@ -0,0 +1,74 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot 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. + +// Polkadot 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 Polkadot. If not, see . + +//! A generic mock candidate validation subsystem suitable for using in benchmarks, it +//! is responding with candidate valid for every request. + +use futures::FutureExt; +use polkadot_node_primitives::ValidationResult; +use polkadot_node_subsystem::{ + messages::CandidateValidationMessage, overseer, SpawnedSubsystem, SubsystemError, +}; +use polkadot_node_subsystem_types::OverseerSignal; +use polkadot_primitives::{CandidateCommitments, Hash, HeadData, PersistedValidationData}; + +pub struct MockCandidateValidation {} + +impl MockCandidateValidation { + pub fn new() -> Self { + Self {} + } +} + +#[overseer::subsystem(CandidateValidation, error=SubsystemError, prefix=self::overseer)] +impl MockCandidateValidation { + fn start(self, ctx: Context) -> SpawnedSubsystem { + let future = self.run(ctx).map(|_| Ok(())).boxed(); + + SpawnedSubsystem { name: "test-environment", future } + } +} + +#[overseer::contextbounds(CandidateValidation, prefix = self::overseer)] +impl MockCandidateValidation { + async fn run(self, mut ctx: Context) { + loop { + let msg = ctx.recv().await.expect("Overseer never fails us"); + match msg { + orchestra::FromOrchestra::Signal(signal) => + if signal == OverseerSignal::Conclude { + return + }, + orchestra::FromOrchestra::Communication { msg } => match msg { + CandidateValidationMessage::ValidateFromExhaustive { + response_sender, .. + } => response_sender + .send(Ok(ValidationResult::Valid( + CandidateCommitments::default(), + PersistedValidationData { + parent_head: HeadData(Vec::new()), + relay_parent_number: 0, + relay_parent_storage_root: Hash::default(), + max_pov_size: 2, + }, + ))) + .unwrap(), + _ => unimplemented!("Unexpected chain-api message"), + }, + } + } + } +} diff --git a/polkadot/node/subsystem-bench/src/lib/mock/mod.rs b/polkadot/node/subsystem-bench/src/lib/mock/mod.rs index 12766374bfa9..da4ac05e33b7 100644 --- a/polkadot/node/subsystem-bench/src/lib/mock/mod.rs +++ b/polkadot/node/subsystem-bench/src/lib/mock/mod.rs @@ -19,7 +19,9 @@ use polkadot_node_subsystem_types::Hash; use sp_consensus::SyncOracle; pub mod av_store; +pub mod availability_recovery; pub mod candidate_backing; +pub mod candidate_validation; pub mod chain_api; pub mod dummy; pub mod network_bridge; diff --git a/polkadot/node/subsystem-bench/src/lib/mock/runtime_api.rs b/polkadot/node/subsystem-bench/src/lib/mock/runtime_api.rs index ee45ea05c925..61523de1f1b5 100644 --- a/polkadot/node/subsystem-bench/src/lib/mock/runtime_api.rs +++ b/polkadot/node/subsystem-bench/src/lib/mock/runtime_api.rs @@ -26,9 +26,9 @@ use polkadot_node_subsystem::{ }; use polkadot_node_subsystem_types::OverseerSignal; use polkadot_primitives::{ - node_features, AsyncBackingParams, CandidateEvent, CandidateReceipt, CoreState, GroupIndex, - GroupRotationInfo, IndexedVec, NodeFeatures, OccupiedCore, ScheduledCore, SessionIndex, - SessionInfo, ValidationCode, ValidatorIndex, + node_features, ApprovalVotingParams, AsyncBackingParams, CandidateEvent, CandidateReceipt, + CoreState, GroupIndex, GroupRotationInfo, IndexedVec, NodeFeatures, OccupiedCore, + ScheduledCore, SessionIndex, SessionInfo, ValidationCode, ValidatorIndex, }; use sp_consensus_babe::Epoch as BabeEpoch; use sp_core::H256; @@ -297,6 +297,13 @@ impl MockRuntimeApi { gum::error!(target: LOG_TARGET, ?err, "validation code wasn't received"); } }, + RuntimeApiMessage::Request( + _parent, + RuntimeApiRequest::ApprovalVotingParams(_, tx), + ) => + if let Err(err) = tx.send(Ok(ApprovalVotingParams::default())) { + gum::error!(target: LOG_TARGET, ?err, "Voting params weren't received"); + }, // Long term TODO: implement more as needed. message => { unimplemented!("Unexpected runtime-api message: {:?}", message)