Skip to content

Commit 5ee3a30

Browse files
authored
MaxValues for maps in parachain maps (paritytech#1868)
* MaxValues for maps in parachain maps * fix compilation
1 parent 58e4021 commit 5ee3a30

File tree

5 files changed

+153
-84
lines changed

5 files changed

+153
-84
lines changed

bridges/modules/grandpa/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ mod tests {
662662
assert_err, assert_noop, assert_ok, dispatch::PostDispatchInfo,
663663
storage::generator::StorageValue,
664664
};
665+
use sp_core::Get;
665666
use sp_runtime::{Digest, DigestItem, DispatchError};
666667

667668
fn initialize_substrate_bridge() {
@@ -1242,4 +1243,9 @@ mod tests {
12421243
}
12431244

12441245
generate_owned_bridge_module_tests!(BasicOperatingMode::Normal, BasicOperatingMode::Halted);
1246+
1247+
#[test]
1248+
fn maybe_headers_to_keep_returns_correct_value() {
1249+
assert_eq!(MaybeHeadersToKeep::<TestRuntime, ()>::get(), Some(mock::HeadersToKeep::get()));
1250+
}
12451251
}

bridges/modules/parachains/src/lib.rs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,27 +221,39 @@ pub mod pallet {
221221
/// - the head of the `ImportedParaHashes` ring buffer
222222
#[pallet::storage]
223223
pub type ParasInfo<T: Config<I>, I: 'static = ()> = StorageMap<
224-
_,
225-
<ParasInfoKeyProvider as StorageMapKeyProvider>::Hasher,
226-
<ParasInfoKeyProvider as StorageMapKeyProvider>::Key,
227-
<ParasInfoKeyProvider as StorageMapKeyProvider>::Value,
224+
Hasher = <ParasInfoKeyProvider as StorageMapKeyProvider>::Hasher,
225+
Key = <ParasInfoKeyProvider as StorageMapKeyProvider>::Key,
226+
Value = <ParasInfoKeyProvider as StorageMapKeyProvider>::Value,
227+
QueryKind = OptionQuery,
228+
OnEmpty = GetDefault,
229+
MaxValues = MaybeMaxParachains<T, I>,
228230
>;
229231

230232
/// State roots of parachain heads which have been imported into the pallet.
231233
#[pallet::storage]
232234
pub type ImportedParaHeads<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
233-
_,
234-
<ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Hasher1,
235-
<ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Key1,
236-
<ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Hasher2,
237-
<ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Key2,
238-
StoredParaHeadDataOf<T, I>,
235+
Hasher1 = <ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Hasher1,
236+
Key1 = <ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Key1,
237+
Hasher2 = <ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Hasher2,
238+
Key2 = <ImportedParaHeadsKeyProvider as StorageDoubleMapKeyProvider>::Key2,
239+
Value = StoredParaHeadDataOf<T, I>,
240+
QueryKind = OptionQuery,
241+
OnEmpty = GetDefault,
242+
MaxValues = MaybeMaxTotalParachainHashes<T, I>,
239243
>;
240244

241245
/// A ring buffer of imported parachain head hashes. Ordered by the insertion time.
242246
#[pallet::storage]
243-
pub(super) type ImportedParaHashes<T: Config<I>, I: 'static = ()> =
244-
StorageDoubleMap<_, Blake2_128Concat, ParaId, Twox64Concat, u32, ParaHash>;
247+
pub(super) type ImportedParaHashes<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
248+
Hasher1 = Blake2_128Concat,
249+
Key1 = ParaId,
250+
Hasher2 = Twox64Concat,
251+
Key2 = u32,
252+
Value = ParaHash,
253+
QueryKind = OptionQuery,
254+
OnEmpty = GetDefault,
255+
MaxValues = MaybeMaxTotalParachainHashes<T, I>,
256+
>;
245257

246258
#[pallet::pallet]
247259
#[pallet::generate_store(pub(super) trait Store)]
@@ -646,6 +658,27 @@ pub mod pallet {
646658
}
647659
}
648660
}
661+
662+
/// Returns maximal number of parachains, supported by the pallet.
663+
pub struct MaybeMaxParachains<T, I>(PhantomData<(T, I)>);
664+
665+
impl<T: Config<I>, I: 'static> Get<Option<u32>> for MaybeMaxParachains<T, I> {
666+
fn get() -> Option<u32> {
667+
Some(T::ParaStoredHeaderDataBuilder::supported_parachains())
668+
}
669+
}
670+
671+
/// Returns total number of all parachains hashes/heads, stored by the pallet.
672+
pub struct MaybeMaxTotalParachainHashes<T, I>(PhantomData<(T, I)>);
673+
674+
impl<T: Config<I>, I: 'static> Get<Option<u32>> for MaybeMaxTotalParachainHashes<T, I> {
675+
fn get() -> Option<u32> {
676+
Some(
677+
T::ParaStoredHeaderDataBuilder::supported_parachains()
678+
.saturating_mul(T::HeadsToKeep::get()),
679+
)
680+
}
681+
}
649682
}
650683

651684
/// Single parachain header chain adapter.
@@ -1525,4 +1558,17 @@ mod tests {
15251558
}
15261559

15271560
generate_owned_bridge_module_tests!(BasicOperatingMode::Normal, BasicOperatingMode::Halted);
1561+
1562+
#[test]
1563+
fn maybe_max_parachains_returns_correct_value() {
1564+
assert_eq!(MaybeMaxParachains::<TestRuntime, ()>::get(), Some(mock::TOTAL_PARACHAINS));
1565+
}
1566+
1567+
#[test]
1568+
fn maybe_max_total_parachain_hashes_returns_correct_value() {
1569+
assert_eq!(
1570+
MaybeMaxTotalParachainHashes::<TestRuntime, ()>::get(),
1571+
Some(mock::TOTAL_PARACHAINS * mock::HeadsToKeep::get()),
1572+
);
1573+
}
15281574
}

bridges/modules/parachains/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub const PARAS_PALLET_NAME: &str = "Paras";
3838
pub const UNTRACKED_PARACHAIN_ID: u32 = 10;
3939
// use exact expected encoded size: `vec_len_size + header_number_size + state_root_hash_size`
4040
pub const MAXIMAL_PARACHAIN_HEAD_DATA_SIZE: u32 = 1 + 8 + 32;
41+
// total parachains that we use in tests
42+
pub const TOTAL_PARACHAINS: u32 = 4;
4143

4244
pub type RegularParachainHeader = sp_runtime::testing::Header;
4345
pub type RegularParachainHasher = BlakeTwo256;

bridges/modules/parachains/src/weights.rs

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Autogenerated weights for pallet_bridge_parachains
1818
//!
1919
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
20-
//! DATE: 2023-02-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
20+
//! DATE: 2023-02-09, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
2121
//! WORST CASE MAP SIZE: `1000000`
2222
//! HOSTNAME: `covid`, CPU: `11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz`
2323
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
@@ -67,33 +67,33 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
6767
///
6868
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
6969
///
70-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
71-
/// 2543, mode: MaxEncodedLen)
70+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
71+
/// added: 2048, mode: MaxEncodedLen)
7272
///
7373
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
7474
///
75-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
76-
/// mode: MaxEncodedLen)
75+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
76+
/// 555, mode: MaxEncodedLen)
7777
///
7878
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
7979
///
80-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
81-
/// added: 2539, mode: MaxEncodedLen)
80+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
81+
/// Some(64), added: 2044, mode: MaxEncodedLen)
8282
///
8383
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
8484
///
85-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
86-
/// added: 2671, mode: MaxEncodedLen)
85+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
86+
/// Some(196), added: 2176, mode: MaxEncodedLen)
8787
///
8888
/// The range of component `p` is `[1, 2]`.
8989
fn submit_parachain_heads_with_n_parachains(p: u32) -> Weight {
9090
// Proof Size summary in bytes:
9191
// Measured: `366`
92-
// Estimated: `8113`
93-
// Minimum execution time: 35_348 nanoseconds.
94-
Weight::from_parts(36_906_961, 8113)
95-
// Standard Error: 136_143
96-
.saturating_add(Weight::from_ref_time(148_169).saturating_mul(p.into()))
92+
// Estimated: `5143`
93+
// Minimum execution time: 35_160 nanoseconds.
94+
Weight::from_parts(36_951_585, 5143)
95+
// Standard Error: 336_932
96+
.saturating_add(Weight::from_ref_time(407_557).saturating_mul(p.into()))
9797
.saturating_add(T::DbWeight::get().reads(4_u64))
9898
.saturating_add(T::DbWeight::get().writes(3_u64))
9999
}
@@ -104,29 +104,29 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
104104
///
105105
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
106106
///
107-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
108-
/// 2543, mode: MaxEncodedLen)
107+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
108+
/// added: 2048, mode: MaxEncodedLen)
109109
///
110110
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
111111
///
112-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
113-
/// mode: MaxEncodedLen)
112+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
113+
/// 555, mode: MaxEncodedLen)
114114
///
115115
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
116116
///
117-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
118-
/// added: 2539, mode: MaxEncodedLen)
117+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
118+
/// Some(64), added: 2044, mode: MaxEncodedLen)
119119
///
120120
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
121121
///
122-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
123-
/// added: 2671, mode: MaxEncodedLen)
122+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
123+
/// Some(196), added: 2176, mode: MaxEncodedLen)
124124
fn submit_parachain_heads_with_1kb_proof() -> Weight {
125125
// Proof Size summary in bytes:
126126
// Measured: `366`
127-
// Estimated: `8113`
128-
// Minimum execution time: 43_295 nanoseconds.
129-
Weight::from_parts(48_018_000, 8113)
127+
// Estimated: `5143`
128+
// Minimum execution time: 42_276 nanoseconds.
129+
Weight::from_parts(43_525_000, 5143)
130130
.saturating_add(T::DbWeight::get().reads(4_u64))
131131
.saturating_add(T::DbWeight::get().writes(3_u64))
132132
}
@@ -137,29 +137,29 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
137137
///
138138
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
139139
///
140-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
141-
/// 2543, mode: MaxEncodedLen)
140+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
141+
/// added: 2048, mode: MaxEncodedLen)
142142
///
143143
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
144144
///
145-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
146-
/// mode: MaxEncodedLen)
145+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
146+
/// 555, mode: MaxEncodedLen)
147147
///
148148
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
149149
///
150-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
151-
/// added: 2539, mode: MaxEncodedLen)
150+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
151+
/// Some(64), added: 2044, mode: MaxEncodedLen)
152152
///
153153
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
154154
///
155-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
156-
/// added: 2671, mode: MaxEncodedLen)
155+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
156+
/// Some(196), added: 2176, mode: MaxEncodedLen)
157157
fn submit_parachain_heads_with_16kb_proof() -> Weight {
158158
// Proof Size summary in bytes:
159159
// Measured: `366`
160-
// Estimated: `8113`
161-
// Minimum execution time: 86_112 nanoseconds.
162-
Weight::from_parts(88_901_000, 8113)
160+
// Estimated: `5143`
161+
// Minimum execution time: 85_824 nanoseconds.
162+
Weight::from_parts(87_335_000, 5143)
163163
.saturating_add(T::DbWeight::get().reads(4_u64))
164164
.saturating_add(T::DbWeight::get().writes(3_u64))
165165
}
@@ -174,33 +174,33 @@ impl WeightInfo for () {
174174
///
175175
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
176176
///
177-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
178-
/// 2543, mode: MaxEncodedLen)
177+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
178+
/// added: 2048, mode: MaxEncodedLen)
179179
///
180180
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
181181
///
182-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
183-
/// mode: MaxEncodedLen)
182+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
183+
/// 555, mode: MaxEncodedLen)
184184
///
185185
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
186186
///
187-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
188-
/// added: 2539, mode: MaxEncodedLen)
187+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
188+
/// Some(64), added: 2044, mode: MaxEncodedLen)
189189
///
190190
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
191191
///
192-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
193-
/// added: 2671, mode: MaxEncodedLen)
192+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
193+
/// Some(196), added: 2176, mode: MaxEncodedLen)
194194
///
195195
/// The range of component `p` is `[1, 2]`.
196196
fn submit_parachain_heads_with_n_parachains(p: u32) -> Weight {
197197
// Proof Size summary in bytes:
198198
// Measured: `366`
199-
// Estimated: `8113`
200-
// Minimum execution time: 35_348 nanoseconds.
201-
Weight::from_parts(36_906_961, 8113)
202-
// Standard Error: 136_143
203-
.saturating_add(Weight::from_ref_time(148_169).saturating_mul(p.into()))
199+
// Estimated: `5143`
200+
// Minimum execution time: 35_160 nanoseconds.
201+
Weight::from_parts(36_951_585, 5143)
202+
// Standard Error: 336_932
203+
.saturating_add(Weight::from_ref_time(407_557).saturating_mul(p.into()))
204204
.saturating_add(RocksDbWeight::get().reads(4_u64))
205205
.saturating_add(RocksDbWeight::get().writes(3_u64))
206206
}
@@ -211,29 +211,29 @@ impl WeightInfo for () {
211211
///
212212
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
213213
///
214-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
215-
/// 2543, mode: MaxEncodedLen)
214+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
215+
/// added: 2048, mode: MaxEncodedLen)
216216
///
217217
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
218218
///
219-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
220-
/// mode: MaxEncodedLen)
219+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
220+
/// 555, mode: MaxEncodedLen)
221221
///
222222
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
223223
///
224-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
225-
/// added: 2539, mode: MaxEncodedLen)
224+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
225+
/// Some(64), added: 2044, mode: MaxEncodedLen)
226226
///
227227
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
228228
///
229-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
230-
/// added: 2671, mode: MaxEncodedLen)
229+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
230+
/// Some(196), added: 2176, mode: MaxEncodedLen)
231231
fn submit_parachain_heads_with_1kb_proof() -> Weight {
232232
// Proof Size summary in bytes:
233233
// Measured: `366`
234-
// Estimated: `8113`
235-
// Minimum execution time: 43_295 nanoseconds.
236-
Weight::from_parts(48_018_000, 8113)
234+
// Estimated: `5143`
235+
// Minimum execution time: 42_276 nanoseconds.
236+
Weight::from_parts(43_525_000, 5143)
237237
.saturating_add(RocksDbWeight::get().reads(4_u64))
238238
.saturating_add(RocksDbWeight::get().writes(3_u64))
239239
}
@@ -244,29 +244,29 @@ impl WeightInfo for () {
244244
///
245245
/// Storage: BridgeRialtoGrandpa ImportedHeaders (r:1 w:0)
246246
///
247-
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: None, max_size: Some(68), added:
248-
/// 2543, mode: MaxEncodedLen)
247+
/// Proof: BridgeRialtoGrandpa ImportedHeaders (max_values: Some(14400), max_size: Some(68),
248+
/// added: 2048, mode: MaxEncodedLen)
249249
///
250250
/// Storage: BridgeRialtoParachains ParasInfo (r:1 w:1)
251251
///
252-
/// Proof: BridgeRialtoParachains ParasInfo (max_values: None, max_size: Some(60), added: 2535,
253-
/// mode: MaxEncodedLen)
252+
/// Proof: BridgeRialtoParachains ParasInfo (max_values: Some(1), max_size: Some(60), added:
253+
/// 555, mode: MaxEncodedLen)
254254
///
255255
/// Storage: BridgeRialtoParachains ImportedParaHashes (r:1 w:1)
256256
///
257-
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: None, max_size: Some(64),
258-
/// added: 2539, mode: MaxEncodedLen)
257+
/// Proof: BridgeRialtoParachains ImportedParaHashes (max_values: Some(14400), max_size:
258+
/// Some(64), added: 2044, mode: MaxEncodedLen)
259259
///
260260
/// Storage: BridgeRialtoParachains ImportedParaHeads (r:0 w:1)
261261
///
262-
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: None, max_size: Some(196),
263-
/// added: 2671, mode: MaxEncodedLen)
262+
/// Proof: BridgeRialtoParachains ImportedParaHeads (max_values: Some(14400), max_size:
263+
/// Some(196), added: 2176, mode: MaxEncodedLen)
264264
fn submit_parachain_heads_with_16kb_proof() -> Weight {
265265
// Proof Size summary in bytes:
266266
// Measured: `366`
267-
// Estimated: `8113`
268-
// Minimum execution time: 86_112 nanoseconds.
269-
Weight::from_parts(88_901_000, 8113)
267+
// Estimated: `5143`
268+
// Minimum execution time: 85_824 nanoseconds.
269+
Weight::from_parts(87_335_000, 5143)
270270
.saturating_add(RocksDbWeight::get().reads(4_u64))
271271
.saturating_add(RocksDbWeight::get().writes(3_u64))
272272
}

0 commit comments

Comments
 (0)