referrerpolicy=no-referrer-when-downgrade

pallet_on_demand_para/
weightinfo_extension.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::WeightInfo;
19use frame_support::weights::Weight;
20
21pub trait WeightInfoExt {
22	/// Returns the fixed base cost of finalize_block operations.
23	///
24	/// This represents the constant overhead incurred during `on_finalize()` regardless
25	/// of order count. Includes setup costs, storage reads/writes, and other fixed operations.
26	fn on_finalize_block_fixed() -> Weight;
27
28	/// Returns the additional cost of finalize_block per placed order.
29	fn on_finalize_block_per_order() -> Weight;
30}
31
32/// Implementation of `WeightInfoExt` that derives high-level weights from `WeightInfo`
33/// benchmarks.
34///
35/// This implementation solves the linear dependency problem by splitting the finalization weight
36/// into a fixed part and a part linearly growing with the number of orders.
37///
38/// **Weight Formula:**
39/// ```text
40/// Total weight = fixed_part + n_orders * per_order_part(payload_i)
41/// ```
42///
43/// **Benchmark Sources:**
44/// - Fixed cost: `on_finalize_with_orders(0)`
45/// - Per-order cost: `on_finalize_with_orders(1) - on_finalize_per_transaction(0)`
46///
47/// Uses differential calculation to isolate marginal costs from benchmark measurements.
48impl<W: WeightInfo> WeightInfoExt for W {
49	fn on_finalize_block_fixed() -> Weight {
50		// The constant factor in linearly growing weight of finalization of the block.
51		W::on_finalize_with_orders(0)
52	}
53
54	fn on_finalize_block_per_order() -> Weight {
55		W::on_finalize_with_orders(1).saturating_sub(W::on_finalize_with_orders(0))
56	}
57}