pallet_bridge_parachains/weights_ext.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
16
17//! Weight-related utilities.
18
19use crate::weights::{BridgeWeight, WeightInfo};
20
21use bp_runtime::Size;
22use frame_support::weights::{RuntimeDbWeight, Weight};
23
24/// Size of the regular parachain head.
25///
26/// It's not that we are expecting all parachain heads to share the same size or that we would
27/// reject all heads that have larger/lesser size. It is about head size that we use in benchmarks.
28/// Relayer would need to pay additional fee for extra bytes.
29///
30/// 384 is a bit larger (1.3 times) than the size of the randomly chosen Polkadot block.
31pub const DEFAULT_PARACHAIN_HEAD_SIZE: u32 = 384;
32
33/// Number of extra bytes (excluding size of storage value itself) of storage proof, built at
34/// some generic chain.
35pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
36
37/// Extended weight info.
38pub trait WeightInfoExt: WeightInfo {
39 // Our configuration assumes that the runtime has special signed extensions used to:
40 //
41 // 1) boost priority of `submit_parachain_heads` transactions;
42 //
43 // 2) slash relayer if he submits an invalid transaction.
44 //
45 // We read and update storage values of other pallets (`pallet-bridge-relayers` and
46 // balances/assets pallet). So we need to add this weight to the weight of our call.
47 // Hence two following methods.
48
49 /// Extra weight that is added to the `submit_finality_proof` call weight by signed extensions
50 /// that are declared at runtime level.
51 fn submit_parachain_heads_overhead_from_runtime() -> Weight;
52
53 /// Storage proof overhead, that is included in every storage proof.
54 ///
55 /// The relayer would pay some extra fee for additional proof bytes, since they mean
56 /// more hashing operations.
57 fn expected_extra_storage_proof_size() -> u32;
58
59 /// Weight of the parachain heads delivery extrinsic.
60 fn submit_parachain_heads_weight(
61 db_weight: RuntimeDbWeight,
62 proof: &impl Size,
63 parachains_count: u32,
64 ) -> Weight {
65 // weight of the `submit_parachain_heads` with exactly `parachains_count` parachain
66 // heads of the default size (`DEFAULT_PARACHAIN_HEAD_SIZE`)
67 let base_weight = Self::submit_parachain_heads_with_n_parachains(parachains_count);
68
69 // overhead because of extra storage proof bytes
70 let expected_proof_size = parachains_count
71 .saturating_mul(DEFAULT_PARACHAIN_HEAD_SIZE)
72 .saturating_add(Self::expected_extra_storage_proof_size());
73 let actual_proof_size = proof.size();
74 let proof_size_overhead = Self::storage_proof_size_overhead(
75 actual_proof_size.saturating_sub(expected_proof_size),
76 );
77
78 // potential pruning weight (refunded if hasn't happened)
79 let pruning_weight =
80 Self::parachain_head_pruning_weight(db_weight).saturating_mul(parachains_count as u64);
81
82 base_weight
83 .saturating_add(proof_size_overhead)
84 .saturating_add(pruning_weight)
85 .saturating_add(Self::submit_parachain_heads_overhead_from_runtime())
86 }
87
88 /// Returns weight of single parachain head storage update.
89 ///
90 /// This weight only includes db write operations that happens if parachain head is actually
91 /// updated. All extra weights (weight of storage proof validation, additional checks, ...) is
92 /// not included.
93 fn parachain_head_storage_write_weight(db_weight: RuntimeDbWeight) -> Weight {
94 // it's just a couple of operations - we need to write the hash (`ImportedParaHashes`) and
95 // the head itself (`ImportedParaHeads`. Pruning is not included here
96 db_weight.writes(2)
97 }
98
99 /// Returns weight of single parachain head pruning.
100 fn parachain_head_pruning_weight(db_weight: RuntimeDbWeight) -> Weight {
101 // it's just one write operation, we don't want any benchmarks for that
102 db_weight.writes(1)
103 }
104
105 /// Returns weight that needs to be accounted when storage proof of given size is received.
106 fn storage_proof_size_overhead(extra_proof_bytes: u32) -> Weight {
107 let extra_byte_weight = (Self::submit_parachain_heads_with_16kb_proof() -
108 Self::submit_parachain_heads_with_1kb_proof()) /
109 (15 * 1024);
110 extra_byte_weight.saturating_mul(extra_proof_bytes as u64)
111 }
112}
113
114impl WeightInfoExt for () {
115 fn submit_parachain_heads_overhead_from_runtime() -> Weight {
116 Weight::zero()
117 }
118
119 fn expected_extra_storage_proof_size() -> u32 {
120 EXTRA_STORAGE_PROOF_SIZE
121 }
122}
123
124impl<T: frame_system::Config> WeightInfoExt for BridgeWeight<T> {
125 fn submit_parachain_heads_overhead_from_runtime() -> Weight {
126 Weight::zero()
127 }
128
129 fn expected_extra_storage_proof_size() -> u32 {
130 EXTRA_STORAGE_PROOF_SIZE
131 }
132}