polkadot_runtime_parachains/paras_inherent/
weights.rs1use crate::{configuration, inclusion};
23use codec::{Encode, WrapperTypeEncode};
24use polkadot_primitives::{
25 CheckedMultiDisputeStatementSet, MultiDisputeStatementSet, UncheckedSignedAvailabilityBitfield,
26 UncheckedSignedAvailabilityBitfields,
27};
28
29use super::{BackedCandidate, Config, DisputeStatementSet, Weight};
30
31pub trait WeightInfo {
32 fn enter_empty() -> Weight;
34 fn enter_variable_disputes(v: u32) -> Weight;
37 fn enter_bitfields() -> Weight;
39 fn enter_backed_candidates_variable(v: u32) -> Weight;
42 fn enter_backed_candidate_code_upgrade() -> Weight;
44}
45
46pub struct TestWeightInfo;
47#[cfg(not(feature = "runtime-benchmarks"))]
50impl WeightInfo for TestWeightInfo {
51 fn enter_empty() -> Weight {
52 Weight::zero()
53 }
54 fn enter_variable_disputes(v: u32) -> Weight {
55 Weight::from_parts(80_000 * v as u64 + 80_000, 0)
57 }
58 fn enter_bitfields() -> Weight {
59 Weight::from_parts(40_000u64, 0)
61 }
62 fn enter_backed_candidates_variable(v: u32) -> Weight {
63 Weight::from_parts(40_000 * v as u64 + 40_000, 0)
65 }
66 fn enter_backed_candidate_code_upgrade() -> Weight {
67 Weight::zero()
68 }
69}
70#[cfg(feature = "runtime-benchmarks")]
74impl WeightInfo for TestWeightInfo {
75 fn enter_empty() -> Weight {
76 Weight::zero()
77 }
78 fn enter_variable_disputes(_v: u32) -> Weight {
79 Weight::zero()
80 }
81 fn enter_bitfields() -> Weight {
82 Weight::zero()
83 }
84 fn enter_backed_candidates_variable(_v: u32) -> Weight {
85 Weight::zero()
86 }
87 fn enter_backed_candidate_code_upgrade() -> Weight {
88 Weight::zero()
89 }
90}
91
92pub fn paras_inherent_total_weight<T: Config>(
93 backed_candidates: &[BackedCandidate<<T as frame_system::Config>::Hash>],
94 bitfields: &UncheckedSignedAvailabilityBitfields,
95 disputes: &MultiDisputeStatementSet,
96) -> Weight {
97 let weight = backed_candidates_weight::<T>(backed_candidates)
98 .saturating_add(signed_bitfields_weight::<T>(bitfields))
99 .saturating_add(multi_dispute_statement_sets_weight::<T>(disputes))
100 .saturating_add(enact_candidates_max_weight::<T>(bitfields));
101 weight.set_proof_size(u64::MAX)
104}
105
106pub fn multi_dispute_statement_sets_weight<T: Config>(
107 disputes: &MultiDisputeStatementSet,
108) -> Weight {
109 set_proof_size_to_tx_size(
110 disputes
111 .iter()
112 .map(|d| dispute_statement_set_weight::<T, _>(d))
113 .fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)),
114 disputes,
115 )
116}
117
118pub fn checked_multi_dispute_statement_sets_weight<T: Config>(
119 disputes: &CheckedMultiDisputeStatementSet,
120) -> Weight {
121 set_proof_size_to_tx_size(
122 disputes
123 .iter()
124 .map(|d| dispute_statement_set_weight::<T, _>(d))
125 .fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)),
126 disputes,
127 )
128}
129
130pub fn dispute_statement_set_weight<T, D>(statement_set: D) -> Weight
132where
133 T: Config,
134 D: AsRef<DisputeStatementSet> + WrapperTypeEncode + Sized + Encode,
135{
136 set_proof_size_to_tx_size(
137 <<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
138 statement_set.as_ref().statements.len() as u32,
139 )
140 .saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
141 statement_set,
142 )
143}
144
145pub fn signed_bitfields_weight<T: Config>(
146 bitfields: &UncheckedSignedAvailabilityBitfields,
147) -> Weight {
148 set_proof_size_to_tx_size(
149 <<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
150 .saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty())
151 .saturating_mul(bitfields.len() as u64),
152 bitfields,
153 )
154}
155
156pub fn signed_bitfield_weight<T: Config>(bitfield: &UncheckedSignedAvailabilityBitfield) -> Weight {
157 set_proof_size_to_tx_size(
158 <<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
159 .saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
160 bitfield,
161 )
162}
163
164pub fn enact_candidates_max_weight<T: Config>(
167 bitfields: &UncheckedSignedAvailabilityBitfields,
168) -> Weight {
169 let config = configuration::ActiveConfig::<T>::get();
170 let max_ump_msgs = config.max_upward_message_num_per_candidate;
171 let max_hrmp_msgs = config.hrmp_max_message_num_per_candidate;
172 let bitfield_size = bitfields.first().map(|b| b.unchecked_payload().0.len()).unwrap_or(0);
174 set_proof_size_to_tx_size(
175 <<T as inclusion::Config>::WeightInfo as inclusion::WeightInfo>::enact_candidate(
176 max_ump_msgs,
177 max_hrmp_msgs,
178 1, )
180 .saturating_mul(bitfield_size as u64),
181 bitfields,
182 )
183}
184
185pub fn backed_candidate_weight<T: frame_system::Config + Config>(
186 candidate: &BackedCandidate<T::Hash>,
187) -> Weight {
188 set_proof_size_to_tx_size(
189 if candidate.candidate().commitments.new_validation_code.is_some() {
190 <<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidate_code_upgrade()
191 } else {
192 <<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidates_variable(
193 candidate.validity_votes().len() as u32,
194 )
195 }
196 .saturating_sub(<<T as Config>::WeightInfo as WeightInfo>::enter_empty()),
197 candidate,
198 )
199}
200
201pub fn backed_candidates_weight<T: frame_system::Config + Config>(
202 candidates: &[BackedCandidate<T::Hash>],
203) -> Weight {
204 candidates
205 .iter()
206 .map(|c| backed_candidate_weight::<T>(c))
207 .fold(Weight::zero(), |acc, x| acc.saturating_add(x))
208}
209
210fn set_proof_size_to_tx_size<Arg: Encode>(weight: Weight, arg: Arg) -> Weight {
212 weight.set_proof_size(arg.encoded_size() as u64)
213}