backing: move the min votes threshold to the runtime (#1200)

* move min backing votes const to runtime

also cache it per-session in the backing subsystem

Signed-off-by: alindima <alin@parity.io>

* add runtime migration

* introduce api versioning for min_backing votes

also enable it for rococo/versi for testing

* also add min_backing_votes runtime calls to statement-distribution

this dependency has been recently introduced by async backing

* remove explicit version runtime API call

this is not needed, as the RuntimeAPISubsystem already takes care
of versioning and will return NotSupported if the version is not
right.

* address review comments

- parametrise backing votes runtime API with session index
- remove RuntimeInfo usage in backing subsystem, as runtime API
caches the min backing votes by session index anyway.
- move the logic for adjusting the configured needed backing votes with the size of the backing group
to a primitives helper.
- move the legacy min backing votes value to a primitives helper.
- mark JoinMultiple error as fatal, since the Canceled (non-multiple) counterpart is also fatal.
- make backing subsystem handle fatal errors for new leaves update.
- add HostConfiguration consistency check for zeroed backing votes threshold
- add cumulus accompanying change

* fix cumulus test compilation

* fix tests

* more small fixes

* fix merge

* bump runtime api version for westend and rollback version for rococo

---------

Signed-off-by: alindima <alin@parity.io>
Co-authored-by: Javier Viola <javier@parity.io>
This commit is contained in:
Alin Dima
2023-08-31 14:01:36 +03:00
committed by GitHub
parent f1845f725d
commit d6af073aa5
37 changed files with 920 additions and 255 deletions
@@ -24,8 +24,8 @@ use frame_system::pallet_prelude::*;
use parity_scale_codec::{Decode, Encode};
use polkadot_parachain::primitives::{MAX_HORIZONTAL_MESSAGE_NUM, MAX_UPWARD_MESSAGE_NUM};
use primitives::{
vstaging::AsyncBackingParams, Balance, ExecutorParams, SessionIndex, MAX_CODE_SIZE,
MAX_HEAD_DATA_SIZE, MAX_POV_SIZE, ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE,
vstaging::AsyncBackingParams, Balance, ExecutorParams, SessionIndex, LEGACY_MIN_BACKING_VOTES,
MAX_CODE_SIZE, MAX_HEAD_DATA_SIZE, MAX_POV_SIZE, ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE,
};
use sp_runtime::{traits::Zero, Perbill};
use sp_std::prelude::*;
@@ -245,6 +245,9 @@ pub struct HostConfiguration<BlockNumber> {
/// This value should be greater than
/// [`paras_availability_period`](Self::paras_availability_period).
pub minimum_validation_upgrade_delay: BlockNumber,
/// The minimum number of valid backing statements required to consider a parachain candidate
/// backable.
pub minimum_backing_votes: u32,
}
impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber> {
@@ -295,6 +298,7 @@ impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber
on_demand_fee_variability: Perbill::from_percent(3),
on_demand_target_queue_utilization: Perbill::from_percent(25),
on_demand_ttl: 5u32.into(),
minimum_backing_votes: LEGACY_MIN_BACKING_VOTES,
}
}
}
@@ -331,6 +335,8 @@ pub enum InconsistentError<BlockNumber> {
MaxHrmpOutboundChannelsExceeded,
/// Maximum number of HRMP inbound channels exceeded.
MaxHrmpInboundChannelsExceeded,
/// `minimum_backing_votes` is set to zero.
ZeroMinimumBackingVotes,
}
impl<BlockNumber> HostConfiguration<BlockNumber>
@@ -411,6 +417,10 @@ where
return Err(MaxHrmpInboundChannelsExceeded)
}
if self.minimum_backing_votes.is_zero() {
return Err(ZeroMinimumBackingVotes)
}
Ok(())
}
@@ -477,7 +487,8 @@ pub mod pallet {
/// v5-v6: <https://github.com/paritytech/polkadot/pull/6271> (remove UMP dispatch queue)
/// v6-v7: <https://github.com/paritytech/polkadot/pull/7396>
/// v7-v8: <https://github.com/paritytech/polkadot/pull/6969>
const STORAGE_VERSION: StorageVersion = StorageVersion::new(8);
/// v8-v9: <https://github.com/paritytech/polkadot/pull/7577>
const STORAGE_VERSION: StorageVersion = StorageVersion::new(9);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
@@ -1153,6 +1164,18 @@ pub mod pallet {
config.on_demand_ttl = new;
})
}
/// Set the minimum backing votes threshold.
#[pallet::call_index(52)]
#[pallet::weight((
T::WeightInfo::set_config_with_u32(),
DispatchClass::Operational
))]
pub fn set_minimum_backing_votes(origin: OriginFor<T>, new: u32) -> DispatchResult {
ensure_root(origin)?;
Self::schedule_config_update(|config| {
config.minimum_backing_votes = new;
})
}
}
#[pallet::hooks]
@@ -19,3 +19,4 @@
pub mod v6;
pub mod v7;
pub mod v8;
pub mod v9;
@@ -23,14 +23,114 @@ use frame_support::{
weights::Weight,
};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::SessionIndex;
use primitives::{
vstaging::AsyncBackingParams, Balance, ExecutorParams, SessionIndex,
ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE,
};
use sp_runtime::Perbill;
use sp_std::vec::Vec;
use frame_support::traits::OnRuntimeUpgrade;
use super::v7::V7HostConfiguration;
type V8HostConfiguration<BlockNumber> = configuration::HostConfiguration<BlockNumber>;
/// All configuration of the runtime with respect to paras.
#[derive(Clone, Encode, Decode, Debug)]
pub struct V8HostConfiguration<BlockNumber> {
pub max_code_size: u32,
pub max_head_data_size: u32,
pub max_upward_queue_count: u32,
pub max_upward_queue_size: u32,
pub max_upward_message_size: u32,
pub max_upward_message_num_per_candidate: u32,
pub hrmp_max_message_num_per_candidate: u32,
pub validation_upgrade_cooldown: BlockNumber,
pub validation_upgrade_delay: BlockNumber,
pub async_backing_params: AsyncBackingParams,
pub max_pov_size: u32,
pub max_downward_message_size: u32,
pub hrmp_max_parachain_outbound_channels: u32,
pub hrmp_sender_deposit: Balance,
pub hrmp_recipient_deposit: Balance,
pub hrmp_channel_max_capacity: u32,
pub hrmp_channel_max_total_size: u32,
pub hrmp_max_parachain_inbound_channels: u32,
pub hrmp_channel_max_message_size: u32,
pub executor_params: ExecutorParams,
pub code_retention_period: BlockNumber,
pub on_demand_cores: u32,
pub on_demand_retries: u32,
pub on_demand_queue_max_size: u32,
pub on_demand_target_queue_utilization: Perbill,
pub on_demand_fee_variability: Perbill,
pub on_demand_base_fee: Balance,
pub on_demand_ttl: BlockNumber,
pub group_rotation_frequency: BlockNumber,
pub paras_availability_period: BlockNumber,
pub scheduling_lookahead: u32,
pub max_validators_per_core: Option<u32>,
pub max_validators: Option<u32>,
pub dispute_period: SessionIndex,
pub dispute_post_conclusion_acceptance_period: BlockNumber,
pub no_show_slots: u32,
pub n_delay_tranches: u32,
pub zeroth_delay_tranche_width: u32,
pub needed_approvals: u32,
pub relay_vrf_modulo_samples: u32,
pub pvf_voting_ttl: SessionIndex,
pub minimum_validation_upgrade_delay: BlockNumber,
}
impl<BlockNumber: Default + From<u32>> Default for V8HostConfiguration<BlockNumber> {
fn default() -> Self {
Self {
async_backing_params: AsyncBackingParams {
max_candidate_depth: 0,
allowed_ancestry_len: 0,
},
group_rotation_frequency: 1u32.into(),
paras_availability_period: 1u32.into(),
no_show_slots: 1u32.into(),
validation_upgrade_cooldown: Default::default(),
validation_upgrade_delay: 2u32.into(),
code_retention_period: Default::default(),
max_code_size: Default::default(),
max_pov_size: Default::default(),
max_head_data_size: Default::default(),
on_demand_cores: Default::default(),
on_demand_retries: Default::default(),
scheduling_lookahead: 1,
max_validators_per_core: Default::default(),
max_validators: None,
dispute_period: 6,
dispute_post_conclusion_acceptance_period: 100.into(),
n_delay_tranches: Default::default(),
zeroth_delay_tranche_width: Default::default(),
needed_approvals: Default::default(),
relay_vrf_modulo_samples: Default::default(),
max_upward_queue_count: Default::default(),
max_upward_queue_size: Default::default(),
max_downward_message_size: Default::default(),
max_upward_message_size: Default::default(),
max_upward_message_num_per_candidate: Default::default(),
hrmp_sender_deposit: Default::default(),
hrmp_recipient_deposit: Default::default(),
hrmp_channel_max_capacity: Default::default(),
hrmp_channel_max_total_size: Default::default(),
hrmp_max_parachain_inbound_channels: Default::default(),
hrmp_channel_max_message_size: Default::default(),
hrmp_max_parachain_outbound_channels: Default::default(),
hrmp_max_message_num_per_candidate: Default::default(),
pvf_voting_ttl: 2u32.into(),
minimum_validation_upgrade_delay: 2.into(),
executor_params: Default::default(),
on_demand_queue_max_size: ON_DEMAND_DEFAULT_QUEUE_MAX_SIZE,
on_demand_base_fee: 10_000_000u128,
on_demand_fee_variability: Perbill::from_percent(3),
on_demand_target_queue_utilization: Perbill::from_percent(25),
on_demand_ttl: 5u32.into(),
}
}
}
mod v7 {
use super::*;
@@ -0,0 +1,321 @@
// 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 module that is responsible for migration of storage.
use crate::configuration::{self, Config, Pallet};
use frame_support::{
pallet_prelude::*,
traits::{Defensive, StorageVersion},
weights::Weight,
};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::{SessionIndex, LEGACY_MIN_BACKING_VOTES};
use sp_runtime::Perbill;
use sp_std::vec::Vec;
use frame_support::traits::OnRuntimeUpgrade;
use super::v8::V8HostConfiguration;
type V9HostConfiguration<BlockNumber> = configuration::HostConfiguration<BlockNumber>;
mod v8 {
use super::*;
#[frame_support::storage_alias]
pub(crate) type ActiveConfig<T: Config> =
StorageValue<Pallet<T>, V8HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
#[frame_support::storage_alias]
pub(crate) type PendingConfigs<T: Config> = StorageValue<
Pallet<T>,
Vec<(SessionIndex, V8HostConfiguration<BlockNumberFor<T>>)>,
OptionQuery,
>;
}
mod v9 {
use super::*;
#[frame_support::storage_alias]
pub(crate) type ActiveConfig<T: Config> =
StorageValue<Pallet<T>, V9HostConfiguration<BlockNumberFor<T>>, OptionQuery>;
#[frame_support::storage_alias]
pub(crate) type PendingConfigs<T: Config> = StorageValue<
Pallet<T>,
Vec<(SessionIndex, V9HostConfiguration<BlockNumberFor<T>>)>,
OptionQuery,
>;
}
pub struct MigrateToV9<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV9<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
log::trace!(target: crate::configuration::LOG_TARGET, "Running pre_upgrade() for HostConfiguration MigrateToV9");
Ok(Vec::new())
}
fn on_runtime_upgrade() -> Weight {
log::info!(target: configuration::LOG_TARGET, "HostConfiguration MigrateToV9 started");
if StorageVersion::get::<Pallet<T>>() == 8 {
let weight_consumed = migrate_to_v9::<T>();
log::info!(target: configuration::LOG_TARGET, "HostConfiguration MigrateToV9 executed successfully");
StorageVersion::new(9).put::<Pallet<T>>();
weight_consumed
} else {
log::warn!(target: configuration::LOG_TARGET, "HostConfiguration MigrateToV9 should be removed.");
T::DbWeight::get().reads(1)
}
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
log::trace!(target: crate::configuration::LOG_TARGET, "Running post_upgrade() for HostConfiguration MigrateToV9");
ensure!(
StorageVersion::get::<Pallet<T>>() >= 9,
"Storage version should be >= 9 after the migration"
);
Ok(())
}
}
fn migrate_to_v9<T: Config>() -> Weight {
// Unusual formatting is justified:
// - make it easier to verify that fields assign what they supposed to assign.
// - this code is transient and will be removed after all migrations are done.
// - this code is important enough to optimize for legibility sacrificing consistency.
#[rustfmt::skip]
let translate =
|pre: V8HostConfiguration<BlockNumberFor<T>>| ->
V9HostConfiguration<BlockNumberFor<T>>
{
V9HostConfiguration {
max_code_size : pre.max_code_size,
max_head_data_size : pre.max_head_data_size,
max_upward_queue_count : pre.max_upward_queue_count,
max_upward_queue_size : pre.max_upward_queue_size,
max_upward_message_size : pre.max_upward_message_size,
max_upward_message_num_per_candidate : pre.max_upward_message_num_per_candidate,
hrmp_max_message_num_per_candidate : pre.hrmp_max_message_num_per_candidate,
validation_upgrade_cooldown : pre.validation_upgrade_cooldown,
validation_upgrade_delay : pre.validation_upgrade_delay,
max_pov_size : pre.max_pov_size,
max_downward_message_size : pre.max_downward_message_size,
hrmp_sender_deposit : pre.hrmp_sender_deposit,
hrmp_recipient_deposit : pre.hrmp_recipient_deposit,
hrmp_channel_max_capacity : pre.hrmp_channel_max_capacity,
hrmp_channel_max_total_size : pre.hrmp_channel_max_total_size,
hrmp_max_parachain_inbound_channels : pre.hrmp_max_parachain_inbound_channels,
hrmp_max_parachain_outbound_channels : pre.hrmp_max_parachain_outbound_channels,
hrmp_channel_max_message_size : pre.hrmp_channel_max_message_size,
code_retention_period : pre.code_retention_period,
on_demand_cores : pre.on_demand_cores,
on_demand_retries : pre.on_demand_retries,
group_rotation_frequency : pre.group_rotation_frequency,
paras_availability_period : pre.paras_availability_period,
scheduling_lookahead : pre.scheduling_lookahead,
max_validators_per_core : pre.max_validators_per_core,
max_validators : pre.max_validators,
dispute_period : pre.dispute_period,
dispute_post_conclusion_acceptance_period: pre.dispute_post_conclusion_acceptance_period,
no_show_slots : pre.no_show_slots,
n_delay_tranches : pre.n_delay_tranches,
zeroth_delay_tranche_width : pre.zeroth_delay_tranche_width,
needed_approvals : pre.needed_approvals,
relay_vrf_modulo_samples : pre.relay_vrf_modulo_samples,
pvf_voting_ttl : pre.pvf_voting_ttl,
minimum_validation_upgrade_delay : pre.minimum_validation_upgrade_delay,
async_backing_params : pre.async_backing_params,
executor_params : pre.executor_params,
on_demand_queue_max_size : 10_000u32,
on_demand_base_fee : 10_000_000u128,
on_demand_fee_variability : Perbill::from_percent(3),
on_demand_target_queue_utilization : Perbill::from_percent(25),
on_demand_ttl : 5u32.into(),
minimum_backing_votes : LEGACY_MIN_BACKING_VOTES
}
};
let v8 = v8::ActiveConfig::<T>::get()
.defensive_proof("Could not decode old config")
.unwrap_or_default();
let v9 = translate(v8);
v9::ActiveConfig::<T>::set(Some(v9));
// Allowed to be empty.
let pending_v8 = v8::PendingConfigs::<T>::get().unwrap_or_default();
let mut pending_v9 = Vec::new();
for (session, v8) in pending_v8.into_iter() {
let v9 = translate(v8);
pending_v9.push((session, v9));
}
v9::PendingConfigs::<T>::set(Some(pending_v9.clone()));
let num_configs = (pending_v9.len() + 1) as u64;
T::DbWeight::get().reads_writes(num_configs, num_configs)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mock::{new_test_ext, Test};
#[test]
fn v9_deserialized_from_actual_data() {
// Example how to get new `raw_config`:
// We'll obtain the raw_config at a specified a block
// Steps:
// 1. Go to Polkadot.js -> Developer -> Chain state -> Storage: https://polkadot.js.org/apps/#/chainstate
// 2. Set these parameters:
// 2.1. selected state query: configuration; activeConfig():
// PolkadotRuntimeParachainsConfigurationHostConfiguration
// 2.2. blockhash to query at:
// 0xf89d3ab5312c5f70d396dc59612f0aa65806c798346f9db4b35278baed2e0e53 (the hash of
// the block)
// 2.3. Note the value of encoded storage key ->
// 0x06de3d8a54d27e44a9d5ce189618f22db4b49d95320d9021994c850f25b8e385 for the
// referenced block.
// 2.4. You'll also need the decoded values to update the test.
// 3. Go to Polkadot.js -> Developer -> Chain state -> Raw storage
// 3.1 Enter the encoded storage key and you get the raw config.
// This exceeds the maximal line width length, but that's fine, since this is not code and
// doesn't need to be read and also leaving it as one line allows to easily copy it.
let raw_config =
hex_literal::hex!["
0000300000800000080000000000100000c8000005000000050000000200000002000000000000000000000000005000000010000400000000000000000000000000000000000000000000000000000000000000000000000800000000200000040000000000100000b004000000000000000000001027000080b2e60e80c3c901809698000000000000000000000000000500000014000000040000000100000001010000000006000000640000000200000019000000000000000300000002000000020000000500000002000000"
];
let v9 =
V9HostConfiguration::<primitives::BlockNumber>::decode(&mut &raw_config[..]).unwrap();
// We check only a sample of the values here. If we missed any fields or messed up data
// types that would skew all the fields coming after.
assert_eq!(v9.max_code_size, 3_145_728);
assert_eq!(v9.validation_upgrade_cooldown, 2);
assert_eq!(v9.max_pov_size, 5_242_880);
assert_eq!(v9.hrmp_channel_max_message_size, 1_048_576);
assert_eq!(v9.n_delay_tranches, 25);
assert_eq!(v9.minimum_validation_upgrade_delay, 5);
assert_eq!(v9.group_rotation_frequency, 20);
assert_eq!(v9.on_demand_cores, 0);
assert_eq!(v9.on_demand_base_fee, 10_000_000);
assert_eq!(v9.minimum_backing_votes, LEGACY_MIN_BACKING_VOTES);
}
#[test]
fn test_migrate_to_v9() {
// Host configuration has lots of fields. However, in this migration we only add one
// field. The most important part to check are a couple of the last fields. We also pick
// extra fields to check arbitrarily, e.g. depending on their position (i.e. the middle) and
// also their type.
//
// We specify only the picked fields and the rest should be provided by the `Default`
// implementation. That implementation is copied over between the two types and should work
// fine.
let v8 = V8HostConfiguration::<primitives::BlockNumber> {
needed_approvals: 69,
paras_availability_period: 55,
hrmp_recipient_deposit: 1337,
max_pov_size: 1111,
minimum_validation_upgrade_delay: 20,
..Default::default()
};
let mut pending_configs = Vec::new();
pending_configs.push((100, v8.clone()));
pending_configs.push((300, v8.clone()));
new_test_ext(Default::default()).execute_with(|| {
// Implant the v8 version in the state.
v8::ActiveConfig::<Test>::set(Some(v8));
v8::PendingConfigs::<Test>::set(Some(pending_configs));
migrate_to_v9::<Test>();
let v9 = v9::ActiveConfig::<Test>::get().unwrap();
let mut configs_to_check = v9::PendingConfigs::<Test>::get().unwrap();
configs_to_check.push((0, v9.clone()));
for (_, v8) in configs_to_check {
#[rustfmt::skip]
{
assert_eq!(v8.max_code_size , v9.max_code_size);
assert_eq!(v8.max_head_data_size , v9.max_head_data_size);
assert_eq!(v8.max_upward_queue_count , v9.max_upward_queue_count);
assert_eq!(v8.max_upward_queue_size , v9.max_upward_queue_size);
assert_eq!(v8.max_upward_message_size , v9.max_upward_message_size);
assert_eq!(v8.max_upward_message_num_per_candidate , v9.max_upward_message_num_per_candidate);
assert_eq!(v8.hrmp_max_message_num_per_candidate , v9.hrmp_max_message_num_per_candidate);
assert_eq!(v8.validation_upgrade_cooldown , v9.validation_upgrade_cooldown);
assert_eq!(v8.validation_upgrade_delay , v9.validation_upgrade_delay);
assert_eq!(v8.max_pov_size , v9.max_pov_size);
assert_eq!(v8.max_downward_message_size , v9.max_downward_message_size);
assert_eq!(v8.hrmp_max_parachain_outbound_channels , v9.hrmp_max_parachain_outbound_channels);
assert_eq!(v8.hrmp_sender_deposit , v9.hrmp_sender_deposit);
assert_eq!(v8.hrmp_recipient_deposit , v9.hrmp_recipient_deposit);
assert_eq!(v8.hrmp_channel_max_capacity , v9.hrmp_channel_max_capacity);
assert_eq!(v8.hrmp_channel_max_total_size , v9.hrmp_channel_max_total_size);
assert_eq!(v8.hrmp_max_parachain_inbound_channels , v9.hrmp_max_parachain_inbound_channels);
assert_eq!(v8.hrmp_channel_max_message_size , v9.hrmp_channel_max_message_size);
assert_eq!(v8.code_retention_period , v9.code_retention_period);
assert_eq!(v8.on_demand_cores , v9.on_demand_cores);
assert_eq!(v8.on_demand_retries , v9.on_demand_retries);
assert_eq!(v8.group_rotation_frequency , v9.group_rotation_frequency);
assert_eq!(v8.paras_availability_period , v9.paras_availability_period);
assert_eq!(v8.scheduling_lookahead , v9.scheduling_lookahead);
assert_eq!(v8.max_validators_per_core , v9.max_validators_per_core);
assert_eq!(v8.max_validators , v9.max_validators);
assert_eq!(v8.dispute_period , v9.dispute_period);
assert_eq!(v8.no_show_slots , v9.no_show_slots);
assert_eq!(v8.n_delay_tranches , v9.n_delay_tranches);
assert_eq!(v8.zeroth_delay_tranche_width , v9.zeroth_delay_tranche_width);
assert_eq!(v8.needed_approvals , v9.needed_approvals);
assert_eq!(v8.relay_vrf_modulo_samples , v9.relay_vrf_modulo_samples);
assert_eq!(v8.pvf_voting_ttl , v9.pvf_voting_ttl);
assert_eq!(v8.minimum_validation_upgrade_delay , v9.minimum_validation_upgrade_delay);
assert_eq!(v8.async_backing_params.allowed_ancestry_len, v9.async_backing_params.allowed_ancestry_len);
assert_eq!(v8.async_backing_params.max_candidate_depth , v9.async_backing_params.max_candidate_depth);
assert_eq!(v8.executor_params , v9.executor_params);
assert_eq!(v8.minimum_backing_votes , v9.minimum_backing_votes);
}; // ; makes this a statement. `rustfmt::skip` cannot be put on an expression.
}
});
}
// Test that migration doesn't panic in case there're no pending configurations upgrades in
// pallet's storage.
#[test]
fn test_migrate_to_v9_no_pending() {
let v8 = V8HostConfiguration::<primitives::BlockNumber>::default();
new_test_ext(Default::default()).execute_with(|| {
// Implant the v8 version in the state.
v8::ActiveConfig::<Test>::set(Some(v8));
// Ensure there're no pending configs.
v8::PendingConfigs::<Test>::set(None);
// Shouldn't fail.
migrate_to_v9::<Test>();
});
}
}
@@ -317,6 +317,7 @@ fn setting_pending_config_members() {
on_demand_fee_variability: Perbill::from_percent(3),
on_demand_target_queue_utilization: Perbill::from_percent(25),
on_demand_ttl: 5u32,
minimum_backing_votes: 5,
};
Configuration::set_validation_upgrade_cooldown(
@@ -467,6 +468,11 @@ fn setting_pending_config_members() {
.unwrap();
Configuration::set_pvf_voting_ttl(RuntimeOrigin::root(), new_config.pvf_voting_ttl)
.unwrap();
Configuration::set_minimum_backing_votes(
RuntimeOrigin::root(),
new_config.minimum_backing_votes,
)
.unwrap();
assert_eq!(PendingConfigs::<Test>::get(), vec![(shared::SESSION_DELAY, new_config)],);
})
@@ -36,11 +36,11 @@ use frame_system::pallet_prelude::*;
use pallet_message_queue::OnQueueChanged;
use parity_scale_codec::{Decode, Encode};
use primitives::{
supermajority_threshold, well_known_keys, AvailabilityBitfield, BackedCandidate,
CandidateCommitments, CandidateDescriptor, CandidateHash, CandidateReceipt,
CommittedCandidateReceipt, CoreIndex, GroupIndex, Hash, HeadData, Id as ParaId,
SignedAvailabilityBitfields, SigningContext, UpwardMessage, ValidatorId, ValidatorIndex,
ValidityAttestation,
effective_minimum_backing_votes, supermajority_threshold, well_known_keys,
AvailabilityBitfield, BackedCandidate, CandidateCommitments, CandidateDescriptor,
CandidateHash, CandidateReceipt, CommittedCandidateReceipt, CoreIndex, GroupIndex, Hash,
HeadData, Id as ParaId, SignedAvailabilityBitfields, SigningContext, UpwardMessage,
ValidatorId, ValidatorIndex, ValidityAttestation,
};
use scale_info::TypeInfo;
use sp_runtime::{traits::One, DispatchError, SaturatedConversion, Saturating};
@@ -199,17 +199,6 @@ impl<H> Default for ProcessedCandidates<H> {
}
}
/// Number of backing votes we need for a valid backing.
///
/// WARNING: This check has to be kept in sync with the node side checks.
pub fn minimum_backing_votes(n_validators: usize) -> usize {
// For considerations on this value see:
// https://github.com/paritytech/polkadot/pull/1656#issuecomment-999734650
// and
// https://github.com/paritytech/polkadot/issues/4386
sp_std::cmp::min(n_validators, 2)
}
/// Reads the footprint of queues for a specific origin type.
pub trait QueueFootprinter {
type Origin;
@@ -622,6 +611,7 @@ impl<T: Config> Pallet<T> {
return Ok(ProcessedCandidates::default())
}
let minimum_backing_votes = configuration::Pallet::<T>::config().minimum_backing_votes;
let validators = shared::Pallet::<T>::active_validator_keys();
// Collect candidate receipts with backers.
@@ -738,7 +728,11 @@ impl<T: Config> Pallet<T> {
match maybe_amount_validated {
Ok(amount_validated) => ensure!(
amount_validated >= minimum_backing_votes(group_vals.len()),
amount_validated >=
effective_minimum_backing_votes(
group_vals.len(),
minimum_backing_votes
),
Error::<T>::InsufficientBacking,
),
Err(()) => {
@@ -26,7 +26,10 @@ use crate::{
paras_inherent::DisputedBitfield,
shared::AllowedRelayParentsTracker,
};
use primitives::{SignedAvailabilityBitfields, UncheckedSignedAvailabilityBitfields};
use primitives::{
effective_minimum_backing_votes, SignedAvailabilityBitfields,
UncheckedSignedAvailabilityBitfields,
};
use assert_matches::assert_matches;
use frame_support::assert_noop;
@@ -120,11 +123,14 @@ pub(crate) fn back_candidate(
kind: BackingKind,
) -> BackedCandidate {
let mut validator_indices = bitvec::bitvec![u8, BitOrderLsb0; 0; group.len()];
let threshold = minimum_backing_votes(group.len());
let threshold = effective_minimum_backing_votes(
group.len(),
configuration::Pallet::<Test>::config().minimum_backing_votes,
);
let signing = match kind {
BackingKind::Unanimous => group.len(),
BackingKind::Threshold => threshold,
BackingKind::Threshold => threshold as usize,
BackingKind::Lacking => threshold.saturating_sub(1),
};
@@ -1609,7 +1615,10 @@ fn backing_works() {
);
let backers = {
let num_backers = minimum_backing_votes(group_validators(GroupIndex(0)).unwrap().len());
let num_backers = effective_minimum_backing_votes(
group_validators(GroupIndex(0)).unwrap().len(),
configuration::Pallet::<Test>::config().minimum_backing_votes,
);
backing_bitfield(&(0..num_backers).collect::<Vec<_>>())
};
assert_eq!(
@@ -1631,7 +1640,10 @@ fn backing_works() {
);
let backers = {
let num_backers = minimum_backing_votes(group_validators(GroupIndex(0)).unwrap().len());
let num_backers = effective_minimum_backing_votes(
group_validators(GroupIndex(0)).unwrap().len(),
configuration::Pallet::<Test>::config().minimum_backing_votes,
);
backing_bitfield(&(0..num_backers).map(|v| v + 2).collect::<Vec<_>>())
};
assert_eq!(
@@ -1765,7 +1777,10 @@ fn can_include_candidate_with_ok_code_upgrade() {
assert_eq!(occupied_cores, vec![(CoreIndex::from(0), chain_a)]);
let backers = {
let num_backers = minimum_backing_votes(group_validators(GroupIndex(0)).unwrap().len());
let num_backers = effective_minimum_backing_votes(
group_validators(GroupIndex(0)).unwrap().len(),
configuration::Pallet::<Test>::config().minimum_backing_votes,
);
backing_bitfield(&(0..num_backers).collect::<Vec<_>>())
};
assert_eq!(
@@ -948,8 +948,11 @@ fn default_header() -> primitives::Header {
mod sanitizers {
use super::*;
use crate::inclusion::tests::{
back_candidate, collator_sign_candidate, BackingKind, TestCandidateBuilder,
use crate::{
inclusion::tests::{
back_candidate, collator_sign_candidate, BackingKind, TestCandidateBuilder,
},
mock::{new_test_ext, MockGenesisConfig},
};
use bitvec::order::Lsb0;
use primitives::{
@@ -1207,131 +1210,133 @@ mod sanitizers {
#[test]
fn candidates() {
const RELAY_PARENT_NUM: u32 = 3;
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
const RELAY_PARENT_NUM: u32 = 3;
let header = default_header();
let relay_parent = header.hash();
let session_index = SessionIndex::from(0_u32);
let header = default_header();
let relay_parent = header.hash();
let session_index = SessionIndex::from(0_u32);
let keystore = LocalKeystore::in_memory();
let keystore = Arc::new(keystore) as KeystorePtr;
let signing_context = SigningContext { parent_hash: relay_parent, session_index };
let keystore = LocalKeystore::in_memory();
let keystore = Arc::new(keystore) as KeystorePtr;
let signing_context = SigningContext { parent_hash: relay_parent, session_index };
let validators = vec![
keyring::Sr25519Keyring::Alice,
keyring::Sr25519Keyring::Bob,
keyring::Sr25519Keyring::Charlie,
keyring::Sr25519Keyring::Dave,
];
for validator in validators.iter() {
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
)
.unwrap();
}
let has_concluded_invalid =
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
let entry_ttl = 10_000;
let scheduled = (0_usize..2)
.into_iter()
.map(|idx| {
let core_idx = CoreIndex::from(idx as u32);
let ca = CoreAssignment {
paras_entry: ParasEntry::new(
Assignment::new(ParaId::from(1_u32 + idx as u32)),
entry_ttl,
),
core: core_idx,
};
ca
})
.collect::<Vec<_>>();
let group_validators = |group_index: GroupIndex| {
match group_index {
group_index if group_index == GroupIndex::from(0) => Some(vec![0, 1]),
group_index if group_index == GroupIndex::from(1) => Some(vec![2, 3]),
_ => panic!("Group index out of bounds for 2 parachains and 1 parathread core"),
let validators = vec![
keyring::Sr25519Keyring::Alice,
keyring::Sr25519Keyring::Bob,
keyring::Sr25519Keyring::Charlie,
keyring::Sr25519Keyring::Dave,
];
for validator in validators.iter() {
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
)
.unwrap();
}
.map(|m| m.into_iter().map(ValidatorIndex).collect::<Vec<_>>())
};
let backed_candidates = (0_usize..2)
.into_iter()
.map(|idx0| {
let idx1 = idx0 + 1;
let mut candidate = TestCandidateBuilder {
para_id: ParaId::from(idx1),
relay_parent,
pov_hash: Hash::repeat_byte(idx1 as u8),
persisted_validation_data_hash: [42u8; 32].into(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(idx0 as u32)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
);
backed
})
.collect::<Vec<_>>();
// happy path
assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
),
backed_candidates
);
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
{
let scheduled = &Vec::new();
assert!(sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
)
.is_empty());
}
// candidates that have concluded as invalid are filtered out
{
// mark every second one as concluded invalid
let set = {
let mut set = std::collections::HashSet::new();
for (idx, backed_candidate) in backed_candidates.iter().enumerate() {
if idx & 0x01 == 0 {
set.insert(backed_candidate.hash());
}
}
set
};
let has_concluded_invalid =
|_idx: usize, candidate: &BackedCandidate| set.contains(&candidate.hash());
|_idx: usize, _backed_candidate: &BackedCandidate| -> bool { false };
let entry_ttl = 10_000;
let scheduled = (0_usize..2)
.into_iter()
.map(|idx| {
let core_idx = CoreIndex::from(idx as u32);
let ca = CoreAssignment {
paras_entry: ParasEntry::new(
Assignment::new(ParaId::from(1_u32 + idx as u32)),
entry_ttl,
),
core: core_idx,
};
ca
})
.collect::<Vec<_>>();
let group_validators = |group_index: GroupIndex| {
match group_index {
group_index if group_index == GroupIndex::from(0) => Some(vec![0, 1]),
group_index if group_index == GroupIndex::from(1) => Some(vec![2, 3]),
_ => panic!("Group index out of bounds for 2 parachains and 1 parathread core"),
}
.map(|m| m.into_iter().map(ValidatorIndex).collect::<Vec<_>>())
};
let backed_candidates = (0_usize..2)
.into_iter()
.map(|idx0| {
let idx1 = idx0 + 1;
let mut candidate = TestCandidateBuilder {
para_id: ParaId::from(idx1),
relay_parent,
pov_hash: Hash::repeat_byte(idx1 as u8),
persisted_validation_data_hash: [42u8; 32].into(),
hrmp_watermark: RELAY_PARENT_NUM,
..Default::default()
}
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(idx0 as u32)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
);
backed
})
.collect::<Vec<_>>();
// happy path
assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
)
.len(),
backed_candidates.len() / 2
),
backed_candidates
);
}
// nothing is scheduled, so no paraids match, thus all backed candidates are skipped
{
let scheduled = &Vec::new();
assert!(sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
)
.is_empty());
}
// candidates that have concluded as invalid are filtered out
{
// mark every second one as concluded invalid
let set = {
let mut set = std::collections::HashSet::new();
for (idx, backed_candidate) in backed_candidates.iter().enumerate() {
if idx & 0x01 == 0 {
set.insert(backed_candidate.hash());
}
}
set
};
let has_concluded_invalid =
|_idx: usize, candidate: &BackedCandidate| set.contains(&candidate.hash());
assert_eq!(
sanitize_backed_candidates::<Test, _>(
backed_candidates.clone(),
has_concluded_invalid,
&scheduled
)
.len(),
backed_candidates.len() / 2
);
}
});
}
}
@@ -118,3 +118,8 @@ pub fn backing_state<T: initializer::Config>(
pub fn async_backing_params<T: configuration::Config>() -> AsyncBackingParams {
<configuration::Pallet<T>>::config().async_backing_params
}
/// Return the min backing votes threshold from the configuration.
pub fn minimum_backing_votes<T: initializer::Config>() -> u32 {
<configuration::Pallet<T>>::config().minimum_backing_votes
}