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 33 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`.
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`.
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
59 changes: 44 additions & 15 deletions runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<T: Config> Pallet<T> {
validators: &[ValidatorId],
signed_bitfields: UncheckedSignedAvailabilityBitfields,
core_lookup: F,
) -> Vec<(CoreIndex, CandidateHash)>
) -> (Vec<(CoreIndex, CandidateHash)>, Weight)
where
F: Fn(CoreIndex) -> Option<ParaId>,
{
Expand Down Expand Up @@ -359,6 +359,7 @@ impl<T: Config> Pallet<T> {
<AvailabilityBitfields<T>>::insert(&validator_index, record);
}

let mut enacted_candidate_weight = 0;
let threshold = availability_threshold(validators.len());

let mut freed_cores = Vec::with_capacity(expected_bits);
Expand Down Expand Up @@ -386,14 +387,17 @@ impl<T: Config> Pallet<T> {
descriptor: pending_availability.descriptor,
commitments,
};
let _weight = Self::enact_candidate(

let weight = Self::enact_candidate(
pending_availability.relay_parent_number,
receipt,
pending_availability.backers,
pending_availability.availability_votes,
pending_availability.core,
pending_availability.backing_group,
);

enacted_candidate_weight = enacted_candidate_weight.saturating_add(weight);
}

freed_cores.push((pending_availability.core, pending_availability.hash));
Expand All @@ -402,24 +406,24 @@ impl<T: Config> Pallet<T> {
}
}

freed_cores
(freed_cores, enacted_candidate_weight)
}

/// Process a set of incoming bitfields.
///
/// Returns a `Vec` of `CandidateHash`es and their respective `AvailabilityCore`s that became available,
/// and cores free.
/// and cores free. Additionally returns the weight consumed by enacted candidates.
pub(crate) fn process_bitfields(
expected_bits: usize,
signed_bitfields: UncheckedSignedAvailabilityBitfields,
disputed_bitfield: DisputedBitfield,
core_lookup: impl Fn(CoreIndex) -> Option<ParaId>,
) -> Vec<(CoreIndex, CandidateHash)> {
) -> (Vec<(CoreIndex, CandidateHash)>, Weight) {
emostov marked this conversation as resolved.
Show resolved Hide resolved
let validators = shared::Pallet::<T>::active_validator_keys();
let session_index = shared::Pallet::<T>::session_index();
let parent_hash = frame_system::Pallet::<T>::parent_hash();

let checked_bitfields = sanitize_bitfields::<T>(
let (checked_bitfields, _) = sanitize_bitfields::<T>(
signed_bitfields,
disputed_bitfield,
expected_bits,
Expand All @@ -429,14 +433,15 @@ impl<T: Config> Pallet<T> {
FullCheck::Yes,
);

let freed_cores = Self::update_pending_availability_and_get_freed_cores::<_, true>(
expected_bits,
&validators[..],
checked_bitfields,
core_lookup,
);
let (freed_cores, enacted_candidate_weight) =
Self::update_pending_availability_and_get_freed_cores::<_, true>(
expected_bits,
&validators[..],
checked_bitfields,
core_lookup,
);

freed_cores
(freed_cores, enacted_candidate_weight)
}

/// Process candidates that have been backed. Provide the relay storage root, a set of candidates
Expand Down Expand Up @@ -725,6 +730,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 @@ -742,8 +749,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 @@ -786,6 +791,30 @@ 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)
.saturating_add(<paras::Pallet<T>>::schedule_code_upgrade_weight())
.saturating_add(<dmp::Pallet<T>>::prune_dmq_weight())
.saturating_add(<ump::Pallet<T>>::receive_upward_messages_weight(
max_upward_message_num_per_candidate,
))
.saturating_add(<hrmp::Pallet<T>>::prune_hrmp_weight(
hrmp_max_parachain_inbound_channels,
hrmp_max_parathread_inbound_channels,
))
.saturating_add(<hrmp::Pallet<T>>::queue_outbound_hrmp_weight(
hrmp_max_message_num_per_candidate,
))
.saturating_add(<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
15 changes: 12 additions & 3 deletions runtime/parachains/src/inclusion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ fn bitfield_checks() {
vec![signed.into()],
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
),
)
.0,
vec![]
);
}
Expand All @@ -434,7 +435,8 @@ fn bitfield_checks() {
vec![signed.into()],
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
),
)
.0,
vec![]
);
}
Expand Down Expand Up @@ -472,6 +474,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());

assert_eq!(
Expand Down Expand Up @@ -528,6 +531,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());

assert_eq!(
Expand Down Expand Up @@ -559,6 +563,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());
}

Expand All @@ -579,6 +584,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());
}

Expand Down Expand Up @@ -619,6 +625,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());

<PendingAvailability<Test>>::remove(chain_a);
Expand Down Expand Up @@ -662,6 +669,7 @@ fn bitfield_checks() {
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
)
.0
.is_empty());
}
});
Expand Down Expand Up @@ -805,7 +813,8 @@ fn supermajority_bitfields_trigger_availability() {
signed_bitfields,
DisputedBitfield::zeros(expected_bits()),
&core_lookup,
),
)
.0,
vec![(CoreIndex(0), candidate_a.hash())]
);

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 @@ -972,7 +972,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 @@ -1005,11 +1005,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, 5 + writes)
drahnr marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

/// Worst case weight for `schedule_code_upgrade`.
pub(crate) fn schedule_code_upgrade_weight() -> Weight {
T::DbWeight::get().reads_writes(4, 8)
}

/// 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 @@ -1048,6 +1053,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
Loading