Skip to content

Commit

Permalink
[subsystem-bench] Add mocks for own assignments triggering (paritytec…
Browse files Browse the repository at this point in the history
…h#5042)

Since paritytech#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 <[email protected]>
  • Loading branch information
alexggh authored and TarekkMA committed Aug 2, 2024
1 parent db0f081 commit 051bb3a
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 5 deletions.
15 changes: 13 additions & 2 deletions polkadot/node/subsystem-bench/src/lib/approval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<Context> 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<Context>(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();
},
},
}
}
}
}
74 changes: 74 additions & 0 deletions polkadot/node/subsystem-bench/src/lib/mock/candidate_validation.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<Context> 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<Context>(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"),
},
}
}
}
}
2 changes: 2 additions & 0 deletions polkadot/node/subsystem-bench/src/lib/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions polkadot/node/subsystem-bench/src/lib/mock/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 051bb3a

Please sign in to comment.