Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Weights for enacting candidates in paras inherent #4172

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7cc81f5
Weights for enacting candidates
emostov Oct 28, 2021
eabde24
Multiply enactments by cores
emostov Oct 28, 2021
6b6b037
Apply suggestions from code review
emostov Oct 28, 2021
4187793
Some improvements
emostov Oct 29, 2021
740f71b
Merge branch 'zeke-track-enact-candidate' of https://github.com/parit…
emostov Oct 29, 2021
8724e9a
Apply suggestions from code review
emostov Oct 29, 2021
5ecc2bb
Apply suggestions from code review
emostov Oct 29, 2021
eeb158e
Apply suggestions from code review
emostov Oct 29, 2021
973ceeb
Update schedule code upgrade
emostov Oct 29, 2021
5c18ff3
Merge branch 'zeke-track-enact-candidate' of https://github.com/parit…
emostov Oct 29, 2021
9b325b3
fmt
emostov Oct 29, 2021
b8a12f2
Add MAX_EXPECTED_CORES_FOR_WEIGHT_CALC
emostov Oct 31, 2021
c72080f
Wip
emostov Nov 17, 2021
244ac54
Update to refund with weight based on enact candidates
emostov Nov 17, 2021
8f6113a
fmt
emostov Nov 17, 2021
fc713d3
Use saturating arith in enact_candidates_weight
emostov Nov 17, 2021
b8bb54e
Try merge origin master
emostov Nov 24, 2021
8443b88
add code comment'
emostov Nov 24, 2021
c8bffed
Merge remote-tracking branch 'origin' into zeke-track-enact-candidate
emostov Nov 26, 2021
7b1bc7a
Account for enact_candidate based on availability bitfields
emostov Nov 28, 2021
7e9dcca
Formatting
emostov Nov 28, 2021
b9e81bf
Account for enact_candidate weight when calculating total weight
emostov Nov 28, 2021
75c8b96
Or together or bitfields
emostov Nov 30, 2021
615e788
Add cheap bitfields checks when counting ones
emostov Dec 1, 2021
14faefb
Track cores voted for in sanitize_bitfields
emostov Dec 2, 2021
ecf1a67
Merge commit '0d92fb4c8b4b418ef1670aab729eeb874f0383a3' into zeke-tra…
emostov Dec 2, 2021
0c0c6a5
Prepare paras_inherent for merging master
emostov Dec 2, 2021
a43f56a
Prepare inclusion for merging master
emostov Dec 2, 2021
86fe00f
fmt
emostov Dec 2, 2021
5accd10
Merge 888162259348c7fa3e9ad7454e8056235565efa6 (#4419)
emostov Dec 2, 2021
dcb91aa
Merge remote-tracking branch 'origin' into zeke-track-enact-candidate
emostov Dec 2, 2021
b97fac2
fmt
emostov Dec 2, 2021
a549397
fix diff with file header
emostov Dec 6, 2021
9efb72f
Remove stale TODOs
emostov Dec 6, 2021
3777172
Try merge origin master
emostov Dec 10, 2021
1c10c27
Update runtime/parachains/src/paras.rs
emostov Dec 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/parachains/src/dmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ impl<T: Config> Pallet<T> {
T::DbWeight::get().reads_writes(1, 1)
}

pub(crate) fn prune_dmq_weight() -> Weight {
T::DbWeight::get().reads_writes(1, 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	// - 1 mutate.

}

/// Returns the Head of Message Queue Chain for the given para or `None` if there is none
/// associated with it.
#[cfg(test)]
Expand Down
22 changes: 21 additions & 1 deletion runtime/parachains/src/hrmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,19 @@ impl<T: Config> Pallet<T> {
weight
}

/// Worst case weight for prune hrmp weight.
emostov marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn prune_hrmp_weight(
emostov marked this conversation as resolved.
Show resolved Hide resolved
hrmp_max_parachain_inbound_channels: u32,
hrmp_max_parathread_inbound_channels: u32,
) -> Weight {
let max_pruneable_channels: u64 = hrmp_max_parachain_inbound_channels
.max(hrmp_max_parathread_inbound_channels)
.into();

T::DbWeight::get()
.reads_writes(1 + 2 * max_pruneable_channels, 2 + 2 * max_pruneable_channels)
}

/// Process the outbound HRMP messages by putting them into the appropriate recipient queues.
///
/// Returns the amount of weight consumed.
Expand Down Expand Up @@ -973,12 +986,19 @@ impl<T: Config> Pallet<T> {
}
<Self as Store>::HrmpChannelDigests::insert(&channel_id.recipient, recipient_digest);

weight += T::DbWeight::get().reads_writes(2, 2);
weight += T::DbWeight::get().reads_writes(3, 2);
}

weight
}

/// Worst case weight for queue outbound hrmp.
emostov marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn queue_outbound_hrmp_weight(hrmp_max_message_num_per_candidate: u32) -> Weight {
let reads = (3 * hrmp_max_message_num_per_candidate).into();
let writes = (2 * hrmp_max_message_num_per_candidate).into();
T::DbWeight::get().reads_writes(reads, writes)
}

/// Initiate opening a channel from a parachain to a given recipient with given channel
/// parameters.
///
Expand Down
47 changes: 36 additions & 11 deletions runtime/parachains/src/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ impl<T: Config> Pallet<T> {

/// Process a set of incoming bitfields.
///
/// Returns a `Vec` of `CandidateHash`es and their respective `AvailabilityCore`s that became available,
/// and cores free.
/// Returns a `Vec` of `CandidateHash`es and their respective `AvailabilityCore`s that became
/// available, cores free, and enactment weight.
pub(crate) fn process_bitfields(
expected_bits: usize,
unchecked_bitfields: UncheckedSignedAvailabilityBitfields,
core_lookup: impl Fn(CoreIndex) -> Option<ParaId>,
) -> Result<Vec<(CoreIndex, CandidateHash)>, DispatchError> {
) -> Result<(Vec<(CoreIndex, CandidateHash)>, Weight), DispatchError> {
let validators = shared::Pallet::<T>::active_validator_keys();
let session_index = shared::Pallet::<T>::session_index();
let mut enacted_candidate_weight = 0;

let mut assigned_paras_record = (0..expected_bits)
.map(|bit_index| core_lookup(CoreIndex::from(bit_index as u32)))
Expand Down Expand Up @@ -383,7 +384,7 @@ impl<T: Config> Pallet<T> {
descriptor: pending_availability.descriptor,
commitments,
};
Self::enact_candidate(
enacted_candidate_weight += Self::enact_candidate(
pending_availability.relay_parent_number,
receipt,
pending_availability.backers,
Expand All @@ -398,7 +399,7 @@ impl<T: Config> Pallet<T> {
}
}

Ok(freed_cores)
Ok((freed_cores, enacted_candidate_weight))
}

/// Process candidates that have been backed. Provide the relay storage root, a set of candidates
Expand Down Expand Up @@ -717,6 +718,8 @@ impl<T: Config> Pallet<T> {
let plain = receipt.to_plain();
let commitments = receipt.commitments;
let config = <configuration::Pallet<T>>::config();
// initial weight is config read.
let mut weight = T::DbWeight::get().reads_writes(1, 0);

T::RewardValidators::reward_backing(
backers
Expand All @@ -734,8 +737,6 @@ impl<T: Config> Pallet<T> {
.map(|(i, _)| ValidatorIndex(i as _)),
);

// initial weight is config read.
let mut weight = T::DbWeight::get().reads_writes(1, 0);
if let Some(new_code) = commitments.new_validation_code {
weight += <paras::Pallet<T>>::schedule_code_upgrade(
receipt.descriptor.para_id,
Expand Down Expand Up @@ -778,6 +779,26 @@ impl<T: Config> Pallet<T> {
)
}

/// Worst case weight for `enact_candidate`.
pub(crate) fn enact_candidate_weight(
hrmp_max_message_num_per_candidate: u32,
max_upward_message_num_per_candidate: u32,
hrmp_max_parachain_inbound_channels: u32,
hrmp_max_parathread_inbound_channels: u32,
) -> Weight {
T::DbWeight::get().reads(1) // initial weight is config read.
// enact the messaging facet of the candidate.
emostov marked this conversation as resolved.
Show resolved Hide resolved
+ <paras::Pallet<T>>::schedule_code_upgrade_weight()
+ <dmp::Pallet<T>>::prune_dmq_weight()
+ <ump::Pallet<T>>::receive_upward_messages_weight(max_upward_message_num_per_candidate)
+ <hrmp::Pallet<T>>::prune_hrmp_weight(
hrmp_max_parachain_inbound_channels,
hrmp_max_parathread_inbound_channels
)
+ <hrmp::Pallet<T>>::queue_outbound_hrmp_weight(hrmp_max_message_num_per_candidate)
+ <paras::Pallet<T>>::note_new_head_weight()
}

/// Cleans up all paras pending availability that the predicate returns true for.
///
/// The predicate accepts the index of the core and the block number the core has been occupied
Expand Down Expand Up @@ -1451,8 +1472,10 @@ mod tests {
expected_bits(),
vec![signed.into()],
&core_lookup,
),
Ok(vec![])
)
.unwrap()
.0,
vec![]
);
}

Expand Down Expand Up @@ -1556,8 +1579,10 @@ mod tests {
expected_bits(),
vec![signed.into()],
&core_lookup,
),
Ok(vec![]),
)
.unwrap()
.0,
vec![],
);
}
});
Expand Down
14 changes: 12 additions & 2 deletions runtime/parachains/src/paras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ impl<T: Config> Pallet<T> {
) -> Weight {
<Self as Store>::FutureCodeUpgrades::mutate(&id, |up| {
if up.is_some() {
T::DbWeight::get().reads_writes(1, 0)
T::DbWeight::get().reads_writes(1, 1)
} else {
let expected_at = relay_parent_number + cfg.validation_upgrade_delay;
let next_possible_upgrade_at =
Expand Down Expand Up @@ -997,11 +997,16 @@ impl<T: Config> Pallet<T> {

let (reads, writes) = Self::increase_code_ref(&new_code_hash, &new_code);
FutureCodeHash::<T>::insert(&id, new_code_hash);
T::DbWeight::get().reads_writes(2 + reads, 3 + writes)
T::DbWeight::get().reads_writes(3 + reads, 3 + writes)
}
})
}

/// Worst case weight for `schedule_code_upgrade`.
pub(crate) fn schedule_code_upgrade_weight() -> Weight {
T::DbWeight::get().reads_writes(4, 5)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	// - mutate 1r, 1w
	// - mutate 1r, 1w
	// - insert 1w
	// - mutate 1r, 1w
	// - deposit_log (append) 1w
	// - increase_code_ref 1r, 2w
	// - insert 1w

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ these are notes about how I counted reads and writes

}

/// Note that a para has progressed to a new head, where the new head was executed in the context
/// of a relay-chain block with given number. This will apply pending code upgrades based
/// on the relay-parent block number provided.
Expand Down Expand Up @@ -1040,6 +1045,11 @@ impl<T: Config> Pallet<T> {
}
}

/// Worst case weight for `note_new_head`.
pub(crate) fn note_new_head_weight() -> Weight {
T::DbWeight::get().reads_writes(6, 6)
}
emostov marked this conversation as resolved.
Show resolved Hide resolved

/// Fetches the validation code hash for the validation code to be used when validating a block
/// in the context of the given relay-chain height. A second block number parameter may be used
/// to tell the lookup to proceed as if an intermediate parablock has been with the given
Expand Down
24 changes: 20 additions & 4 deletions runtime/parachains/src/paras_inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! this module.

use crate::{
configuration,
disputes::DisputesHandler,
inclusion,
scheduler::{self, FreedReason},
Expand Down Expand Up @@ -58,7 +59,7 @@ pub mod pallet {

#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: inclusion::Config + scheduler::Config {}
pub trait Config: inclusion::Config + scheduler::Config + configuration::Config {}

#[pallet::error]
pub enum Error<T> {
Expand Down Expand Up @@ -153,7 +154,21 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Enter the paras inherent. This will process bitfields and backed candidates.
#[pallet::weight((
MINIMAL_INCLUSION_INHERENT_WEIGHT + data.backed_candidates.len() as Weight * BACKED_CANDIDATE_WEIGHT,
{
let c = <configuration::Pallet<T>>::config();
let enact_candidate_weight = <inclusion::Pallet<T>>::enact_candidate_weight(
c.hrmp_max_message_num_per_candidate,
c.max_upward_message_num_per_candidate,
c.hrmp_max_parachain_inbound_channels,
c.hrmp_max_parathread_inbound_channels,
)
// NOTE: this will need to updated if the max number of cores changes.
* 40u64;
emostov marked this conversation as resolved.
Show resolved Hide resolved

MINIMAL_INCLUSION_INHERENT_WEIGHT
+ data.backed_candidates.len() as Weight * BACKED_CANDIDATE_WEIGHT
+ enact_candidate_weight
},
DispatchClass::Mandatory,
))]
pub fn enter(
Expand Down Expand Up @@ -219,7 +234,7 @@ pub mod pallet {
// Process new availability bitfields, yielding any availability cores whose
// work has now concluded.
let expected_bits = <scheduler::Pallet<T>>::availability_cores().len();
let freed_concluded = <inclusion::Pallet<T>>::process_bitfields(
let (freed_concluded, enacted_weight) = <inclusion::Pallet<T>>::process_bitfields(
expected_bits,
signed_bitfields,
<scheduler::Pallet<T>>::core_para,
Expand Down Expand Up @@ -296,7 +311,8 @@ pub mod pallet {
Included::<T>::set(Some(()));

Ok(Some(
MINIMAL_INCLUSION_INHERENT_WEIGHT +
enacted_weight +
MINIMAL_INCLUSION_INHERENT_WEIGHT +
(backed_candidates_len * BACKED_CANDIDATE_WEIGHT),
)
.into())
Expand Down
12 changes: 12 additions & 0 deletions runtime/parachains/src/ump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ impl<T: Config> Pallet<T> {
weight
}

/// Worst case weight for `receive_upward_messages`.
pub(crate) fn receive_upward_messages_weight(
max_upward_message_num_per_candidate: u32,
) -> Weight {
use sp_runtime::traits::Zero;
if !max_upward_message_num_per_candidate.is_zero() {
T::DbWeight::get().reads_writes(3, 3)
} else {
0
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	// - mutate
	// - mutate
	// - mutate


/// Devote some time into dispatching pending upward messages.
pub(crate) fn process_pending_upward_messages() -> Weight {
let mut weight_used = 0;
Expand Down