Skip to main content

topsoil_core/storage/
mod.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Stuff to do with the runtime's storage.
8
9use crate::{
10	hash::{ReversibleStorageHasher, StorageHasher},
11	storage::types::{
12		EncodeLikeTuple, HasKeyPrefix, HasReversibleKeyPrefix, KeyGenerator,
13		ReversibleKeyGenerator, TupleToEncodedIter,
14	},
15};
16use alloc::{collections::btree_set::BTreeSet, vec::Vec};
17use codec::{Decode, Encode, EncodeLike, FullCodec, FullEncode};
18use core::marker::PhantomData;
19use subsoil::core::storage::ChildInfo;
20use subsoil::runtime::generic::{Digest, DigestItem};
21
22pub use self::{
23	stream_iter::StorageStreamIter,
24	transactional::{
25		in_storage_layer, with_storage_layer, with_transaction, with_transaction_unchecked,
26	},
27	types::StorageEntryMetadataBuilder,
28};
29pub use subsoil::runtime::TransactionOutcome;
30pub use types::Key;
31
32pub mod bounded_btree_map;
33pub mod bounded_btree_set;
34pub mod bounded_vec;
35pub mod child;
36#[doc(hidden)]
37pub mod generator;
38pub mod hashed;
39pub mod migration;
40pub mod storage_noop_guard;
41mod stream_iter;
42pub mod transactional;
43pub mod types;
44pub mod unhashed;
45pub mod weak_bounded_vec;
46
47/// Utility type for converting a storage map into a `Get<u32>` impl which returns the maximum
48/// key size.
49pub struct KeyLenOf<M>(PhantomData<M>);
50
51/// A trait for working with macro-generated storage values under the substrate storage API.
52///
53/// Details on implementation can be found at [`generator::StorageValue`].
54pub trait StorageValue<T: FullCodec> {
55	/// The type that get/take return.
56	type Query;
57
58	/// Get the storage key.
59	fn hashed_key() -> [u8; 32];
60
61	/// Does the value (explicitly) exist in storage?
62	fn exists() -> bool;
63
64	/// Load the value from the provided storage instance.
65	fn get() -> Self::Query;
66
67	/// Try to get the underlying value from the provided storage instance.
68	///
69	/// Returns `Ok` if it exists, `Err` if not.
70	fn try_get() -> Result<T, ()>;
71
72	/// Translate a value from some previous type (`O`) to the current type.
73	///
74	/// `f: F` is the translation function.
75	///
76	/// Returns `Err` if the storage item could not be interpreted as the old type, and Ok, along
77	/// with the new value if it could.
78	///
79	/// NOTE: This operates from and to `Option<_>` types; no effort is made to respect the default
80	/// value of the original type.
81	///
82	/// # Warning
83	///
84	/// This function must be used with care, before being updated the storage still contains the
85	/// old type, thus other calls (such as `get`) will fail at decoding it.
86	///
87	/// # Usage
88	///
89	/// This would typically be called inside the module implementation of on_runtime_upgrade, while
90	/// ensuring **no usage of this storage are made before the call to `on_runtime_upgrade`**.
91	/// (More precisely prior initialized modules doesn't make use of this storage).
92	fn translate<O: Decode, F: FnOnce(Option<O>) -> Option<T>>(f: F) -> Result<Option<T>, ()>;
93
94	/// Store a value under this key into the provided storage instance.
95	fn put<Arg: EncodeLike<T>>(val: Arg);
96
97	/// Store a value under this key into the provided storage instance; this uses the query
98	/// type rather than the underlying value.
99	fn set(val: Self::Query);
100
101	/// Mutate the value
102	fn mutate<R, F: FnOnce(&mut Self::Query) -> R>(f: F) -> R;
103
104	/// Mutate the value under a key if the value already exists. Do nothing and return the default
105	/// value if not.
106	fn mutate_extant<R: Default, F: FnOnce(&mut T) -> R>(f: F) -> R {
107		Self::mutate_exists(|maybe_v| match maybe_v {
108			Some(ref mut value) => f(value),
109			None => R::default(),
110		})
111	}
112
113	/// Mutate the value if closure returns `Ok`
114	fn try_mutate<R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(f: F) -> Result<R, E>;
115
116	/// Mutate the value. Deletes the item if mutated to a `None`.
117	fn mutate_exists<R, F: FnOnce(&mut Option<T>) -> R>(f: F) -> R;
118
119	/// Mutate the value if closure returns `Ok`. Deletes the item if mutated to a `None`.
120	fn try_mutate_exists<R, E, F: FnOnce(&mut Option<T>) -> Result<R, E>>(f: F) -> Result<R, E>;
121
122	/// Clear the storage value.
123	fn kill();
124
125	/// Take a value from storage, removing it afterwards.
126	fn take() -> Self::Query;
127
128	/// Append the given item to the value in the storage.
129	///
130	/// `T` is required to implement [`StorageAppend`].
131	///
132	/// # Warning
133	///
134	/// If the storage item is not encoded properly, the storage item will be overwritten
135	/// and set to `[item]`. Any default value set for the storage item will be ignored
136	/// on overwrite.
137	fn append<Item, EncodeLikeItem>(item: EncodeLikeItem)
138	where
139		Item: Encode,
140		EncodeLikeItem: EncodeLike<Item>,
141		T: StorageAppend<Item>;
142
143	/// Read the length of the storage value without decoding the entire value.
144	///
145	/// `T` is required to implement [`StorageDecodeLength`].
146	///
147	/// If the value does not exists or it fails to decode the length, `None` is returned.
148	/// Otherwise `Some(len)` is returned.
149	///
150	/// # Warning
151	///
152	/// `None` does not mean that `get()` does not return a value. The default value is completely
153	/// ignored by this function.
154	fn decode_len() -> Option<usize>
155	where
156		T: StorageDecodeLength,
157	{
158		T::decode_len(&Self::hashed_key())
159	}
160
161	/// Read the length of the storage value without decoding the entire value.
162	///
163	/// `T` is required to implement [`StorageDecodeNonDedupLength`].
164	///
165	/// If the value does not exists or it fails to decode the length, `None` is returned.
166	/// Otherwise `Some(len)` is returned.
167	///
168	/// # Warning
169	///
170	/// - The value returned is the non-deduplicated length of the underlying Vector in storage.This
171	/// means that any duplicate items are included.
172	///
173	/// - `None` does not mean that `get()` does not return a value. The default value is completely
174	/// ignored by this function.
175	///
176	/// # Example
177	#[doc = docify::embed!("src/storage/mod.rs", btree_set_decode_non_dedup_len)]
178	/// This demonstrates how `decode_non_dedup_len` will count even the duplicate values in the
179	/// storage (in this case, the number `4` is counted twice).
180	fn decode_non_dedup_len() -> Option<usize>
181	where
182		T: StorageDecodeNonDedupLength,
183	{
184		T::decode_non_dedup_len(&Self::hashed_key())
185	}
186}
187
188/// A non-continuous container type.
189pub trait StorageList<V: FullCodec> {
190	/// Iterator for normal and draining iteration.
191	type Iterator: Iterator<Item = V>;
192
193	/// Append iterator for fast append operations.
194	type Appender: StorageAppender<V>;
195
196	/// List the elements in append order.
197	fn iter() -> Self::Iterator;
198
199	/// Drain the elements in append order.
200	///
201	/// Note that this drains a value as soon as it is being inspected. For example `take_while(|_|
202	/// false)` still drains the first element. This also applies to `peek()`.
203	fn drain() -> Self::Iterator;
204
205	/// A fast append iterator.
206	fn appender() -> Self::Appender;
207
208	/// Append a single element.
209	///
210	/// Should not be called repeatedly; use `append_many` instead.
211	/// Worst case linear `O(len)` with `len` being the number if elements in the list.
212	fn append_one<EncodeLikeValue>(item: EncodeLikeValue)
213	where
214		EncodeLikeValue: EncodeLike<V>,
215	{
216		Self::append_many(core::iter::once(item));
217	}
218
219	/// Append many elements.
220	///
221	/// Should not be called repeatedly; use `appender` instead.
222	/// Worst case linear `O(len + items.count())` with `len` beings the number if elements in the
223	/// list.
224	fn append_many<EncodeLikeValue, I>(items: I)
225	where
226		EncodeLikeValue: EncodeLike<V>,
227		I: IntoIterator<Item = EncodeLikeValue>,
228	{
229		let mut ap = Self::appender();
230		ap.append_many(items);
231	}
232}
233
234/// Append iterator to append values to a storage struct.
235///
236/// Can be used in situations where appending does not have constant time complexity.
237pub trait StorageAppender<V: FullCodec> {
238	/// Append a single item in constant time `O(1)`.
239	fn append<EncodeLikeValue>(&mut self, item: EncodeLikeValue)
240	where
241		EncodeLikeValue: EncodeLike<V>;
242
243	/// Append many items in linear time `O(items.count())`.
244	// Note: a default impl is provided since `Self` is already assumed to be optimal for single
245	// append operations.
246	fn append_many<EncodeLikeValue, I>(&mut self, items: I)
247	where
248		EncodeLikeValue: EncodeLike<V>,
249		I: IntoIterator<Item = EncodeLikeValue>,
250	{
251		for item in items.into_iter() {
252			self.append(item);
253		}
254	}
255}
256
257/// A strongly-typed map in storage.
258///
259/// Details on implementation can be found at [`generator::StorageMap`].
260pub trait StorageMap<K: FullEncode, V: FullCodec> {
261	/// The type that get/take return.
262	type Query;
263
264	/// Get the storage key used to fetch a value corresponding to a specific key.
265	fn hashed_key_for<KeyArg: EncodeLike<K>>(key: KeyArg) -> Vec<u8>;
266
267	/// Does the value (explicitly) exist in storage?
268	fn contains_key<KeyArg: EncodeLike<K>>(key: KeyArg) -> bool;
269
270	/// Load the value associated with the given key from the map.
271	fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
272
273	/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
274	fn set<KeyArg: EncodeLike<K>>(key: KeyArg, query: Self::Query);
275
276	/// Try to get the value for the given key from the map.
277	///
278	/// Returns `Ok` if it exists, `Err` if not.
279	fn try_get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<V, ()>;
280
281	/// Swap the values of two keys.
282	fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2);
283
284	/// Store a value to be associated with the given key from the map.
285	fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg);
286
287	/// Remove the value under a key.
288	fn remove<KeyArg: EncodeLike<K>>(key: KeyArg);
289
290	/// Mutate the value under a key.
291	fn mutate<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R;
292
293	/// Mutate the item, only if an `Ok` value is returned.
294	fn try_mutate<KeyArg: EncodeLike<K>, R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(
295		key: KeyArg,
296		f: F,
297	) -> Result<R, E>;
298
299	/// Mutate the value under a key if the value already exists. Do nothing and return the default
300	/// value if not.
301	fn mutate_extant<KeyArg: EncodeLike<K>, R: Default, F: FnOnce(&mut V) -> R>(
302		key: KeyArg,
303		f: F,
304	) -> R {
305		Self::mutate_exists(key, |maybe_v| match maybe_v {
306			Some(ref mut value) => f(value),
307			None => R::default(),
308		})
309	}
310
311	/// Mutate the value under a key.
312	///
313	/// Deletes the item if mutated to a `None`.
314	fn mutate_exists<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Option<V>) -> R>(
315		key: KeyArg,
316		f: F,
317	) -> R;
318
319	/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
320	/// `f` will always be called with an option representing if the storage item exists (`Some<V>`)
321	/// or if the storage item does not exist (`None`), independent of the `QueryType`.
322	fn try_mutate_exists<KeyArg: EncodeLike<K>, R, E, F: FnOnce(&mut Option<V>) -> Result<R, E>>(
323		key: KeyArg,
324		f: F,
325	) -> Result<R, E>;
326
327	/// Take the value under a key.
328	fn take<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
329
330	/// Append the given items to the value in the storage.
331	///
332	/// `V` is required to implement `codec::EncodeAppend`.
333	///
334	/// # Warning
335	///
336	/// If the storage item is not encoded properly, the storage will be overwritten
337	/// and set to `[item]`. Any default value set for the storage item will be ignored
338	/// on overwrite.
339	fn append<Item, EncodeLikeItem, EncodeLikeKey>(key: EncodeLikeKey, item: EncodeLikeItem)
340	where
341		EncodeLikeKey: EncodeLike<K>,
342		Item: Encode,
343		EncodeLikeItem: EncodeLike<Item>,
344		V: StorageAppend<Item>;
345
346	/// Read the length of the storage value without decoding the entire value under the
347	/// given `key`.
348	///
349	/// `V` is required to implement [`StorageDecodeLength`].
350	///
351	/// If the value does not exists or it fails to decode the length, `None` is returned.
352	/// Otherwise `Some(len)` is returned.
353	///
354	/// # Warning
355	///
356	/// `None` does not mean that `get()` does not return a value. The default value is completely
357	/// ignored by this function.
358	fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Option<usize>
359	where
360		V: StorageDecodeLength,
361	{
362		V::decode_len(&Self::hashed_key_for(key))
363	}
364
365	/// Read the length of the storage value without decoding the entire value.
366	///
367	/// `V` is required to implement [`StorageDecodeNonDedupLength`].
368	///
369	/// If the value does not exists or it fails to decode the length, `None` is returned.
370	/// Otherwise `Some(len)` is returned.
371	///
372	/// # Warning
373	///
374	///  - `None` does not mean that `get()` does not return a value. The default value is
375	///    completely
376	/// ignored by this function.
377	///
378	/// - The value returned is the non-deduplicated length of the underlying Vector in storage.This
379	/// means that any duplicate items are included.
380	fn decode_non_dedup_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Option<usize>
381	where
382		V: StorageDecodeNonDedupLength,
383	{
384		V::decode_non_dedup_len(&Self::hashed_key_for(key))
385	}
386
387	/// Migrate an item with the given `key` from a defunct `OldHasher` to the current hasher.
388	///
389	/// If the key doesn't exist, then it's a no-op. If it does, then it returns its value.
390	fn migrate_key<OldHasher: StorageHasher, KeyArg: EncodeLike<K>>(key: KeyArg) -> Option<V>;
391
392	/// Migrate an item with the given `key` from a `blake2_256` hasher to the current hasher.
393	///
394	/// If the key doesn't exist, then it's a no-op. If it does, then it returns its value.
395	fn migrate_key_from_blake<KeyArg: EncodeLike<K>>(key: KeyArg) -> Option<V> {
396		Self::migrate_key::<crate::hash::Blake2_256, KeyArg>(key)
397	}
398}
399
400/// A strongly-typed map in storage whose keys and values can be iterated over.
401pub trait IterableStorageMap<K: FullEncode, V: FullCodec>: StorageMap<K, V> {
402	/// The type that iterates over all `(key, value)`.
403	type Iterator: Iterator<Item = (K, V)>;
404	/// The type that iterates over all `key`s.
405	type KeyIterator: Iterator<Item = K>;
406
407	/// Enumerate all elements in the map in lexicographical order of the encoded key. If you
408	/// alter the map while doing this, you'll get undefined results.
409	fn iter() -> Self::Iterator;
410
411	/// Enumerate all elements in the map after a specified `starting_raw_key` in lexicographical
412	/// order of the encoded key. If you alter the map while doing this, you'll get undefined
413	/// results.
414	fn iter_from(starting_raw_key: Vec<u8>) -> Self::Iterator;
415
416	/// Enumerate all keys in the map in lexicographical order of the encoded key, skipping over
417	/// the elements. If you alter the map while doing this, you'll get undefined results.
418	fn iter_keys() -> Self::KeyIterator;
419
420	/// Enumerate all keys in the map after a specified `starting_raw_key` in lexicographical order
421	/// of the encoded key. If you alter the map while doing this, you'll get undefined results.
422	fn iter_keys_from(starting_raw_key: Vec<u8>) -> Self::KeyIterator;
423
424	/// Remove all elements from the map and iterate through them in lexicographical order of the
425	/// encoded key. If you add elements to the map while doing this, you'll get undefined results.
426	fn drain() -> Self::Iterator;
427
428	/// Translate the values of all elements by a function `f`, in the map in lexicographical order
429	/// of the encoded key.
430	/// By returning `None` from `f` for an element, you'll remove it from the map.
431	///
432	/// NOTE: If a value fail to decode because storage is corrupted then it is skipped.
433	fn translate<O: Decode, F: FnMut(K, O) -> Option<V>>(f: F);
434
435	/// Translate the next entry following `previous_key` by a function `f`.
436	/// By returning `None` from `f` for an element, you'll remove it from the map.
437	///
438	/// Returns the next key to iterate from in lexicographical order of the encoded key.
439	fn translate_next<O: Decode, F: FnMut(K, O) -> Option<V>>(
440		previous_key: Option<Vec<u8>>,
441		f: F,
442	) -> Option<Vec<u8>>;
443}
444
445/// A strongly-typed double map in storage whose secondary keys and values can be iterated over.
446pub trait IterableStorageDoubleMap<K1: FullCodec, K2: FullCodec, V: FullCodec>:
447	StorageDoubleMap<K1, K2, V>
448{
449	/// The type that iterates over all `key2`.
450	type PartialKeyIterator: Iterator<Item = K2>;
451
452	/// The type that iterates over all `(key2, value)`.
453	type PrefixIterator: Iterator<Item = (K2, V)>;
454
455	/// The type that iterates over all `(key1, key2)`.
456	type FullKeyIterator: Iterator<Item = (K1, K2)>;
457
458	/// The type that iterates over all `(key1, key2, value)`.
459	type Iterator: Iterator<Item = (K1, K2, V)>;
460
461	/// Enumerate all elements in the map with first key `k1` in lexicographical order of the
462	/// encoded key. If you add or remove values whose first key is `k1` to the map while doing
463	/// this, you'll get undefined results.
464	fn iter_prefix(k1: impl EncodeLike<K1>) -> Self::PrefixIterator;
465
466	/// Enumerate all elements in the map with first key `k1` after a specified `starting_raw_key`
467	/// in lexicographical order of the encoded key. If you add or remove values whose first key is
468	/// `k1` to the map while doing this, you'll get undefined results.
469	fn iter_prefix_from(k1: impl EncodeLike<K1>, starting_raw_key: Vec<u8>)
470		-> Self::PrefixIterator;
471
472	/// Enumerate all second keys `k2` in the map with the same first key `k1` in lexicographical
473	/// order of the encoded key. If you add or remove values whose first key is `k1` to the map
474	/// while doing this, you'll get undefined results.
475	fn iter_key_prefix(k1: impl EncodeLike<K1>) -> Self::PartialKeyIterator;
476
477	/// Enumerate all second keys `k2` in the map with the same first key `k1` after a specified
478	/// `starting_raw_key` in lexicographical order of the encoded key. If you add or remove values
479	/// whose first key is `k1` to the map while doing this, you'll get undefined results.
480	fn iter_key_prefix_from(
481		k1: impl EncodeLike<K1>,
482		starting_raw_key: Vec<u8>,
483	) -> Self::PartialKeyIterator;
484
485	/// Remove all elements from the map with first key `k1` and iterate through them in
486	/// lexicographical order of the encoded key. If you add elements with first key `k1` to the
487	/// map while doing this, you'll get undefined results.
488	fn drain_prefix(k1: impl EncodeLike<K1>) -> Self::PrefixIterator;
489
490	/// Enumerate all elements in the map in lexicographical order of the encoded key. If you add
491	/// or remove values to the map while doing this, you'll get undefined results.
492	fn iter() -> Self::Iterator;
493
494	/// Enumerate all elements in the map after a specified `starting_raw_key` in lexicographical
495	/// order of the encoded key. If you add or remove values to the map while doing this, you'll
496	/// get undefined results.
497	fn iter_from(starting_raw_key: Vec<u8>) -> Self::Iterator;
498
499	/// Enumerate all keys `k1` and `k2` in the map in lexicographical order of the encoded key. If
500	/// you add or remove values to the map while doing this, you'll get undefined results.
501	fn iter_keys() -> Self::FullKeyIterator;
502
503	/// Enumerate all keys `k1` and `k2` in the map after a specified `starting_raw_key` in
504	/// lexicographical order of the encoded key. If you add or remove values to the map while
505	/// doing this, you'll get undefined results.
506	fn iter_keys_from(starting_raw_key: Vec<u8>) -> Self::FullKeyIterator;
507
508	/// Remove all elements from the map and iterate through them in lexicographical order of the
509	/// encoded key. If you add elements to the map while doing this, you'll get undefined results.
510	fn drain() -> Self::Iterator;
511
512	/// Translate the values of all elements by a function `f`, in the map in lexicographical order
513	/// of the encoded key.
514	/// By returning `None` from `f` for an element, you'll remove it from the map.
515	///
516	/// NOTE: If a value fail to decode because storage is corrupted then it is skipped.
517	fn translate<O: Decode, F: FnMut(K1, K2, O) -> Option<V>>(f: F);
518}
519
520/// A strongly-typed map with arbitrary number of keys in storage whose keys and values can be
521/// iterated over.
522pub trait IterableStorageNMap<K: ReversibleKeyGenerator, V: FullCodec>: StorageNMap<K, V> {
523	/// The type that iterates over all `(key1, key2, key3, ... keyN)` tuples.
524	type KeyIterator: Iterator<Item = K::Key>;
525
526	/// The type that iterates over all `(key1, key2, key3, ... keyN), value)` tuples.
527	type Iterator: Iterator<Item = (K::Key, V)>;
528
529	/// Enumerate all elements in the map with prefix key `kp` in lexicographical order of the
530	/// encoded key. If you add or remove values whose prefix is `kp` to the map while doing this,
531	/// you'll get undefined results.
532	fn iter_prefix<KP>(kp: KP) -> PrefixIterator<(<K as HasKeyPrefix<KP>>::Suffix, V)>
533	where
534		K: HasReversibleKeyPrefix<KP>;
535
536	/// Enumerate all elements in the map with prefix key `kp` after a specified `starting_raw_key`
537	/// in lexicographical order of the encoded key. If you add or remove values whose prefix is
538	/// `kp` to the map while doing this, you'll get undefined results.
539	fn iter_prefix_from<KP>(
540		kp: KP,
541		starting_raw_key: Vec<u8>,
542	) -> PrefixIterator<(<K as HasKeyPrefix<KP>>::Suffix, V)>
543	where
544		K: HasReversibleKeyPrefix<KP>;
545
546	/// Enumerate all suffix keys in the map with prefix key `kp` in lexicographical order of the
547	/// encoded key. If you add or remove values whose prefix is `kp` to the map while doing this,
548	/// you'll get undefined results.
549	fn iter_key_prefix<KP>(kp: KP) -> KeyPrefixIterator<<K as HasKeyPrefix<KP>>::Suffix>
550	where
551		K: HasReversibleKeyPrefix<KP>;
552
553	/// Enumerate all suffix keys in the map with prefix key `kp` after a specified
554	/// `starting_raw_key` in lexicographical order of the encoded key. If you add or remove values
555	/// whose prefix is `kp` to the map while doing this, you'll get undefined results.
556	fn iter_key_prefix_from<KP>(
557		kp: KP,
558		starting_raw_key: Vec<u8>,
559	) -> KeyPrefixIterator<<K as HasKeyPrefix<KP>>::Suffix>
560	where
561		K: HasReversibleKeyPrefix<KP>;
562
563	/// Remove all elements from the map with prefix key `kp` and iterate through them in
564	/// lexicographical order of the encoded key. If you add elements with prefix key `kp` to the
565	/// map while doing this, you'll get undefined results.
566	fn drain_prefix<KP>(kp: KP) -> PrefixIterator<(<K as HasKeyPrefix<KP>>::Suffix, V)>
567	where
568		K: HasReversibleKeyPrefix<KP>;
569
570	/// Enumerate all elements in the map in lexicographical order of the encoded key. If you add
571	/// or remove values to the map while doing this, you'll get undefined results.
572	fn iter() -> Self::Iterator;
573
574	/// Enumerate all elements in the map after a specified `starting_raw_key` in lexicographical
575	/// order of the encoded key. If you add or remove values to the map while doing this, you'll
576	/// get undefined results.
577	fn iter_from(starting_raw_key: Vec<u8>) -> Self::Iterator;
578
579	/// Enumerate all keys in the map in lexicographical order of the encoded key. If you add or
580	/// remove values to the map while doing this, you'll get undefined results.
581	fn iter_keys() -> Self::KeyIterator;
582
583	/// Enumerate all keys in the map after `starting_raw_key` in lexicographical order of the
584	/// encoded key. If you add or remove values to the map while doing this, you'll get undefined
585	/// results.
586	fn iter_keys_from(starting_raw_key: Vec<u8>) -> Self::KeyIterator;
587
588	/// Remove all elements from the map and iterate through them in lexicographical order of the
589	/// encoded key. If you add elements to the map while doing this, you'll get undefined results.
590	fn drain() -> Self::Iterator;
591
592	/// Translate the values of all elements by a function `f`, in the map in lexicographical order
593	/// of the encoded key.
594	/// By returning `None` from `f` for an element, you'll remove it from the map.
595	///
596	/// NOTE: If a value fail to decode because storage is corrupted then it is skipped.
597	fn translate<O: Decode, F: FnMut(K::Key, O) -> Option<V>>(f: F);
598}
599
600/// An implementation of a map with a two keys.
601///
602/// Details on implementation can be found at [`generator::StorageDoubleMap`].
603pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
604	/// The type that get/take returns.
605	type Query;
606
607	/// Get the storage key used to fetch a value corresponding to a specific key.
608	fn hashed_key_for<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Vec<u8>
609	where
610		KArg1: EncodeLike<K1>,
611		KArg2: EncodeLike<K2>;
612
613	/// Does the value (explicitly) exist in storage?
614	fn contains_key<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> bool
615	where
616		KArg1: EncodeLike<K1>,
617		KArg2: EncodeLike<K2>;
618
619	/// Load the value associated with the given key from the double map.
620	fn get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
621	where
622		KArg1: EncodeLike<K1>,
623		KArg2: EncodeLike<K2>;
624
625	/// Try to get the value for the given key from the double map.
626	///
627	/// Returns `Ok` if it exists, `Err` if not.
628	fn try_get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Result<V, ()>
629	where
630		KArg1: EncodeLike<K1>,
631		KArg2: EncodeLike<K2>;
632
633	/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
634	fn set<KArg1: EncodeLike<K1>, KArg2: EncodeLike<K2>>(k1: KArg1, k2: KArg2, query: Self::Query);
635
636	/// Take a value from storage, removing it afterwards.
637	fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
638	where
639		KArg1: EncodeLike<K1>,
640		KArg2: EncodeLike<K2>;
641
642	/// Swap the values of two key-pairs.
643	fn swap<XKArg1, XKArg2, YKArg1, YKArg2>(x_k1: XKArg1, x_k2: XKArg2, y_k1: YKArg1, y_k2: YKArg2)
644	where
645		XKArg1: EncodeLike<K1>,
646		XKArg2: EncodeLike<K2>,
647		YKArg1: EncodeLike<K1>,
648		YKArg2: EncodeLike<K2>;
649
650	/// Store a value to be associated with the given keys from the double map.
651	fn insert<KArg1, KArg2, VArg>(k1: KArg1, k2: KArg2, val: VArg)
652	where
653		KArg1: EncodeLike<K1>,
654		KArg2: EncodeLike<K2>,
655		VArg: EncodeLike<V>;
656
657	/// Remove the value under the given keys.
658	fn remove<KArg1, KArg2>(k1: KArg1, k2: KArg2)
659	where
660		KArg1: EncodeLike<K1>,
661		KArg2: EncodeLike<K2>;
662
663	/// Remove all values under the first key `k1` in the overlay and up to `limit` in the
664	/// backend.
665	///
666	/// All values in the client overlay will be deleted, if there is some `limit` then up to
667	/// `limit` values are deleted from the client backend, if `limit` is none then all values in
668	/// the client backend are deleted.
669	///
670	/// # Note
671	///
672	/// Calling this multiple times per block with a `limit` set leads always to the same keys being
673	/// removed and the same result being returned. This happens because the keys to delete in the
674	/// overlay are not taken into account when deleting keys in the backend.
675	#[deprecated = "Use `clear_prefix` instead"]
676	fn remove_prefix<KArg1>(k1: KArg1, limit: Option<u32>) -> subsoil::io::KillStorageResult
677	where
678		KArg1: ?Sized + EncodeLike<K1>;
679
680	/// Remove all values under the first key `k1` in the overlay and up to `maybe_limit` in the
681	/// backend.
682	///
683	/// All values in the client overlay will be deleted, if `maybe_limit` is `Some` then up to
684	/// that number of values are deleted from the client backend, otherwise all values in the
685	/// client backend are deleted.
686	///
687	/// ## Cursors
688	///
689	/// The `maybe_cursor` parameter should be `None` for the first call to initial removal.
690	/// If the resultant `maybe_cursor` is `Some`, then another call is required to complete the
691	/// removal operation. This value must be passed in as the subsequent call's `maybe_cursor`
692	/// parameter. If the resultant `maybe_cursor` is `None`, then the operation is complete and no
693	/// items remain in storage provided that no items were added between the first calls and the
694	/// final call.
695	fn clear_prefix<KArg1>(
696		k1: KArg1,
697		limit: u32,
698		maybe_cursor: Option<&[u8]>,
699	) -> subsoil::io::MultiRemovalResults
700	where
701		KArg1: ?Sized + EncodeLike<K1>;
702
703	/// Does any value under the first key `k1` (explicitly) exist in storage?
704	/// Might have unexpected behaviour with empty keys, e.g. `[]`.
705	fn contains_prefix<KArg1>(k1: KArg1) -> bool
706	where
707		KArg1: EncodeLike<K1>;
708
709	/// Iterate over values that share the first key.
710	fn iter_prefix_values<KArg1>(k1: KArg1) -> PrefixIterator<V>
711	where
712		KArg1: ?Sized + EncodeLike<K1>;
713
714	/// Mutate the value under the given keys.
715	fn mutate<KArg1, KArg2, R, F>(k1: KArg1, k2: KArg2, f: F) -> R
716	where
717		KArg1: EncodeLike<K1>,
718		KArg2: EncodeLike<K2>,
719		F: FnOnce(&mut Self::Query) -> R;
720
721	/// Mutate the value under the given keys when the closure returns `Ok`.
722	fn try_mutate<KArg1, KArg2, R, E, F>(k1: KArg1, k2: KArg2, f: F) -> Result<R, E>
723	where
724		KArg1: EncodeLike<K1>,
725		KArg2: EncodeLike<K2>,
726		F: FnOnce(&mut Self::Query) -> Result<R, E>;
727
728	/// Mutate the value under the given keys. Deletes the item if mutated to a `None`.
729	fn mutate_exists<KArg1, KArg2, R, F>(k1: KArg1, k2: KArg2, f: F) -> R
730	where
731		KArg1: EncodeLike<K1>,
732		KArg2: EncodeLike<K2>,
733		F: FnOnce(&mut Option<V>) -> R;
734
735	/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
736	/// `f` will always be called with an option representing if the storage item exists (`Some<V>`)
737	/// or if the storage item does not exist (`None`), independent of the `QueryType`.
738	fn try_mutate_exists<KArg1, KArg2, R, E, F>(k1: KArg1, k2: KArg2, f: F) -> Result<R, E>
739	where
740		KArg1: EncodeLike<K1>,
741		KArg2: EncodeLike<K2>,
742		F: FnOnce(&mut Option<V>) -> Result<R, E>;
743
744	/// Append the given item to the value in the storage.
745	///
746	/// `V` is required to implement [`StorageAppend`].
747	///
748	/// # Warning
749	///
750	/// If the storage item is not encoded properly, the storage will be overwritten
751	/// and set to `[item]`. Any default value set for the storage item will be ignored
752	/// on overwrite.
753	fn append<Item, EncodeLikeItem, KArg1, KArg2>(k1: KArg1, k2: KArg2, item: EncodeLikeItem)
754	where
755		KArg1: EncodeLike<K1>,
756		KArg2: EncodeLike<K2>,
757		Item: Encode,
758		EncodeLikeItem: EncodeLike<Item>,
759		V: StorageAppend<Item>;
760
761	/// Read the length of the storage value without decoding the entire value under the
762	/// given `key1` and `key2`.
763	///
764	/// `V` is required to implement [`StorageDecodeLength`].
765	///
766	/// If the value does not exists or it fails to decode the length, `None` is returned.
767	/// Otherwise `Some(len)` is returned.
768	///
769	/// # Warning
770	///
771	/// `None` does not mean that `get()` does not return a value. The default value is completely
772	/// ignored by this function.
773	fn decode_len<KArg1, KArg2>(key1: KArg1, key2: KArg2) -> Option<usize>
774	where
775		KArg1: EncodeLike<K1>,
776		KArg2: EncodeLike<K2>,
777		V: StorageDecodeLength,
778	{
779		V::decode_len(&Self::hashed_key_for(key1, key2))
780	}
781
782	/// Read the length of the storage value without decoding the entire value under the
783	/// given `key1` and `key2`.
784	///
785	/// `V` is required to implement [`StorageDecodeNonDedupLength`].
786	///
787	/// If the value does not exists or it fails to decode the length, `None` is returned.
788	/// Otherwise `Some(len)` is returned.
789	///
790	/// # Warning
791	///
792	/// `None` does not mean that `get()` does not return a value. The default value is completely
793	/// ignored by this function.
794	fn decode_non_dedup_len<KArg1, KArg2>(key1: KArg1, key2: KArg2) -> Option<usize>
795	where
796		KArg1: EncodeLike<K1>,
797		KArg2: EncodeLike<K2>,
798		V: StorageDecodeNonDedupLength,
799	{
800		V::decode_non_dedup_len(&Self::hashed_key_for(key1, key2))
801	}
802
803	/// Migrate an item with the given `key1` and `key2` from defunct `OldHasher1` and
804	/// `OldHasher2` to the current hashers.
805	///
806	/// If the key doesn't exist, then it's a no-op. If it does, then it returns its value.
807	fn migrate_keys<
808		OldHasher1: StorageHasher,
809		OldHasher2: StorageHasher,
810		KeyArg1: EncodeLike<K1>,
811		KeyArg2: EncodeLike<K2>,
812	>(
813		key1: KeyArg1,
814		key2: KeyArg2,
815	) -> Option<V>;
816}
817
818/// An implementation of a map with an arbitrary number of keys.
819///
820/// Details of implementation can be found at [`generator::StorageNMap`].
821pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
822	/// The type that get/take returns.
823	type Query;
824
825	/// Get the storage key used to fetch a value corresponding to a specific key.
826	fn hashed_key_for<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8>;
827
828	/// Does the value (explicitly) exist in storage?
829	fn contains_key<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> bool;
830
831	/// Load the value associated with the given key from the map.
832	fn get<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query;
833
834	/// Try to get the value for the given key from the map.
835	///
836	/// Returns `Ok` if it exists, `Err` if not.
837	fn try_get<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()>;
838
839	/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
840	fn set<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg, query: Self::Query);
841
842	/// Swap the values of two keys.
843	fn swap<KOther, KArg1, KArg2>(key1: KArg1, key2: KArg2)
844	where
845		KOther: KeyGenerator,
846		KArg1: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
847		KArg2: EncodeLikeTuple<KOther::KArg> + TupleToEncodedIter;
848
849	/// Store a value to be associated with the given key from the map.
850	fn insert<KArg, VArg>(key: KArg, val: VArg)
851	where
852		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
853		VArg: EncodeLike<V>;
854
855	/// Remove the value under a key.
856	fn remove<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg);
857
858	/// Remove all values starting with `partial_key` in the overlay and up to `limit` in the
859	/// backend.
860	///
861	/// All values in the client overlay will be deleted, if there is some `limit` then up to
862	/// `limit` values are deleted from the client backend, if `limit` is none then all values in
863	/// the client backend are deleted.
864	///
865	/// # Note
866	///
867	/// Calling this multiple times per block with a `limit` set leads always to the same keys being
868	/// removed and the same result being returned. This happens because the keys to delete in the
869	/// overlay are not taken into account when deleting keys in the backend.
870	#[deprecated = "Use `clear_prefix` instead"]
871	fn remove_prefix<KP>(partial_key: KP, limit: Option<u32>) -> subsoil::io::KillStorageResult
872	where
873		K: HasKeyPrefix<KP>;
874
875	/// Attempt to remove items from the map matching a `partial_key` prefix.
876	///
877	/// Returns [`MultiRemovalResults`](subsoil::io::MultiRemovalResults) to inform about the result. Once
878	/// the resultant `maybe_cursor` field is `None`, then no further items remain to be deleted.
879	///
880	/// NOTE: After the initial call for any given map, it is important that no further items
881	/// are inserted into the map which match the `partial key`. If so, then the map may not be
882	/// empty when the resultant `maybe_cursor` is `None`.
883	///
884	/// # Limit
885	///
886	/// A `limit` must be provided in order to cap the maximum
887	/// amount of deletions done in a single call. This is one fewer than the
888	/// maximum number of backend iterations which may be done by this operation and as such
889	/// represents the maximum number of backend deletions which may happen. A `limit` of zero
890	/// implies that no keys will be deleted, though there may be a single iteration done.
891	///
892	/// # Cursor
893	///
894	/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
895	/// passed once (in the initial call) for any given storage map and `partial_key`. Subsequent
896	/// calls operating on the same map/`partial_key` should always pass `Some`, and this should be
897	/// equal to the previous call result's `maybe_cursor` field.
898	fn clear_prefix<KP>(
899		partial_key: KP,
900		limit: u32,
901		maybe_cursor: Option<&[u8]>,
902	) -> subsoil::io::MultiRemovalResults
903	where
904		K: HasKeyPrefix<KP>;
905
906	/// Does any value under a `partial_key` prefix (explicitly) exist in storage?
907	/// Might have unexpected behaviour with empty keys, e.g. `[]`.
908	fn contains_prefix<KP>(partial_key: KP) -> bool
909	where
910		K: HasKeyPrefix<KP>;
911
912	/// Iterate over values that share the partial prefix key.
913	fn iter_prefix_values<KP>(partial_key: KP) -> PrefixIterator<V>
914	where
915		K: HasKeyPrefix<KP>;
916
917	/// Mutate the value under a key.
918	fn mutate<KArg, R, F>(key: KArg, f: F) -> R
919	where
920		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
921		F: FnOnce(&mut Self::Query) -> R;
922
923	/// Mutate the item, only if an `Ok` value is returned.
924	fn try_mutate<KArg, R, E, F>(key: KArg, f: F) -> Result<R, E>
925	where
926		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
927		F: FnOnce(&mut Self::Query) -> Result<R, E>;
928
929	/// Mutate the value under a key.
930	///
931	/// Deletes the item if mutated to a `None`.
932	fn mutate_exists<KArg, R, F>(key: KArg, f: F) -> R
933	where
934		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
935		F: FnOnce(&mut Option<V>) -> R;
936
937	/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
938	/// `f` will always be called with an option representing if the storage item exists (`Some<V>`)
939	/// or if the storage item does not exist (`None`), independent of the `QueryType`.
940	fn try_mutate_exists<KArg, R, E, F>(key: KArg, f: F) -> Result<R, E>
941	where
942		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
943		F: FnOnce(&mut Option<V>) -> Result<R, E>;
944
945	/// Take the value under a key.
946	fn take<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query;
947
948	/// Append the given items to the value in the storage.
949	///
950	/// `V` is required to implement `codec::EncodeAppend`.
951	///
952	/// # Warning
953	///
954	/// If the storage item is not encoded properly, the storage will be overwritten
955	/// and set to `[item]`. Any default value set for the storage item will be ignored
956	/// on overwrite.
957	fn append<Item, EncodeLikeItem, KArg>(key: KArg, item: EncodeLikeItem)
958	where
959		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter,
960		Item: Encode,
961		EncodeLikeItem: EncodeLike<Item>,
962		V: StorageAppend<Item>;
963
964	/// Read the length of the storage value without decoding the entire value under the
965	/// given `key`.
966	///
967	/// `V` is required to implement [`StorageDecodeLength`].
968	///
969	/// If the value does not exists or it fails to decode the length, `None` is returned.
970	/// Otherwise `Some(len)` is returned.
971	///
972	/// # Warning
973	///
974	/// `None` does not mean that `get()` does not return a value. The default value is completely
975	/// ignored by this function.
976	fn decode_len<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Option<usize>
977	where
978		V: StorageDecodeLength,
979	{
980		V::decode_len(&Self::hashed_key_for(key))
981	}
982
983	/// Migrate an item with the given `key` from defunct `hash_fns` to the current hashers.
984	///
985	/// If the key doesn't exist, then it's a no-op. If it does, then it returns its value.
986	fn migrate_keys<KArg>(key: KArg, hash_fns: K::HArg) -> Option<V>
987	where
988		KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter;
989}
990
991/// Iterate or drain over a prefix and decode raw_key and raw_value into `T`.
992///
993/// If any decoding fails it skips it and continues to the next key.
994///
995/// If draining, then the hook `OnRemoval::on_removal` is called after each removal.
996pub struct PrefixIterator<T, OnRemoval = ()> {
997	prefix: Vec<u8>,
998	previous_key: Vec<u8>,
999	/// If true then value are removed while iterating
1000	drain: bool,
1001	/// Function that take `(raw_key_without_prefix, raw_value)` and decode `T`.
1002	/// `raw_key_without_prefix` is the raw storage key without the prefix iterated on.
1003	closure: fn(&[u8], &[u8]) -> Result<T, codec::Error>,
1004	phantom: core::marker::PhantomData<OnRemoval>,
1005}
1006
1007impl<T, OnRemoval1> PrefixIterator<T, OnRemoval1> {
1008	/// Converts to the same iterator but with the different 'OnRemoval' type
1009	pub fn convert_on_removal<OnRemoval2>(self) -> PrefixIterator<T, OnRemoval2> {
1010		PrefixIterator::<T, OnRemoval2> {
1011			prefix: self.prefix,
1012			previous_key: self.previous_key,
1013			drain: self.drain,
1014			closure: self.closure,
1015			phantom: Default::default(),
1016		}
1017	}
1018}
1019
1020/// Trait for specialising on removal logic of [`PrefixIterator`].
1021pub trait PrefixIteratorOnRemoval {
1022	/// This function is called whenever a key/value is removed.
1023	fn on_removal(key: &[u8], value: &[u8]);
1024}
1025
1026/// No-op implementation.
1027impl PrefixIteratorOnRemoval for () {
1028	fn on_removal(_key: &[u8], _value: &[u8]) {}
1029}
1030
1031impl<T, OnRemoval> PrefixIterator<T, OnRemoval> {
1032	/// Creates a new `PrefixIterator`, iterating after `previous_key` and filtering out keys that
1033	/// are not prefixed with `prefix`.
1034	///
1035	/// A `decode_fn` function must also be supplied, and it takes in two `&[u8]` parameters,
1036	/// returning a `Result` containing the decoded type `T` if successful, and a `codec::Error` on
1037	/// failure. The first `&[u8]` argument represents the raw, undecoded key without the prefix of
1038	/// the current item, while the second `&[u8]` argument denotes the corresponding raw,
1039	/// undecoded value.
1040	pub fn new(
1041		prefix: Vec<u8>,
1042		previous_key: Vec<u8>,
1043		decode_fn: fn(&[u8], &[u8]) -> Result<T, codec::Error>,
1044	) -> Self {
1045		PrefixIterator {
1046			prefix,
1047			previous_key,
1048			drain: false,
1049			closure: decode_fn,
1050			phantom: Default::default(),
1051		}
1052	}
1053
1054	/// Get the last key that has been iterated upon and return it.
1055	pub fn last_raw_key(&self) -> &[u8] {
1056		&self.previous_key
1057	}
1058
1059	/// Get the prefix that is being iterated upon for this iterator and return it.
1060	pub fn prefix(&self) -> &[u8] {
1061		&self.prefix
1062	}
1063
1064	/// Set the key that the iterator should start iterating after.
1065	pub fn set_last_raw_key(&mut self, previous_key: Vec<u8>) {
1066		self.previous_key = previous_key;
1067	}
1068
1069	/// Mutate this iterator into a draining iterator; items iterated are removed from storage.
1070	pub fn drain(mut self) -> Self {
1071		self.drain = true;
1072		self
1073	}
1074}
1075
1076impl<T, OnRemoval: PrefixIteratorOnRemoval> Iterator for PrefixIterator<T, OnRemoval> {
1077	type Item = T;
1078
1079	fn next(&mut self) -> Option<Self::Item> {
1080		loop {
1081			let maybe_next = subsoil::io::storage::next_key(&self.previous_key)
1082				.filter(|n| n.starts_with(&self.prefix));
1083			break match maybe_next {
1084				Some(next) => {
1085					self.previous_key = next;
1086					let raw_value = match unhashed::get_raw(&self.previous_key) {
1087						Some(raw_value) => raw_value,
1088						None => {
1089							log::error!(
1090								"next_key returned a key with no value at {:?}",
1091								self.previous_key,
1092							);
1093							continue;
1094						},
1095					};
1096					if self.drain {
1097						unhashed::kill(&self.previous_key);
1098						OnRemoval::on_removal(&self.previous_key, &raw_value);
1099					}
1100					let raw_key_without_prefix = &self.previous_key[self.prefix.len()..];
1101					let item = match (self.closure)(raw_key_without_prefix, &raw_value[..]) {
1102						Ok(item) => item,
1103						Err(e) => {
1104							log::error!(
1105								"(key, value) failed to decode at {:?}: {:?}",
1106								self.previous_key,
1107								e,
1108							);
1109							continue;
1110						},
1111					};
1112
1113					Some(item)
1114				},
1115				None => None,
1116			};
1117		}
1118	}
1119}
1120
1121/// Iterate over a prefix and decode raw_key into `T`.
1122///
1123/// If any decoding fails it skips it and continues to the next key.
1124pub struct KeyPrefixIterator<T> {
1125	prefix: Vec<u8>,
1126	previous_key: Vec<u8>,
1127	/// If true then value are removed while iterating
1128	drain: bool,
1129	/// Function that take `raw_key_without_prefix` and decode `T`.
1130	/// `raw_key_without_prefix` is the raw storage key without the prefix iterated on.
1131	closure: fn(&[u8]) -> Result<T, codec::Error>,
1132}
1133
1134impl<T> KeyPrefixIterator<T> {
1135	/// Creates a new `KeyPrefixIterator`, iterating after `previous_key` and filtering out keys
1136	/// that are not prefixed with `prefix`.
1137	///
1138	/// A `decode_fn` function must also be supplied, and it takes in a `&[u8]` parameter, returning
1139	/// a `Result` containing the decoded key type `T` if successful, and a `codec::Error` on
1140	/// failure. The `&[u8]` argument represents the raw, undecoded key without the prefix of the
1141	/// current item.
1142	pub fn new(
1143		prefix: Vec<u8>,
1144		previous_key: Vec<u8>,
1145		decode_fn: fn(&[u8]) -> Result<T, codec::Error>,
1146	) -> Self {
1147		KeyPrefixIterator { prefix, previous_key, drain: false, closure: decode_fn }
1148	}
1149
1150	/// Get the last key that has been iterated upon and return it.
1151	pub fn last_raw_key(&self) -> &[u8] {
1152		&self.previous_key
1153	}
1154
1155	/// Get the prefix that is being iterated upon for this iterator and return it.
1156	pub fn prefix(&self) -> &[u8] {
1157		&self.prefix
1158	}
1159
1160	/// Set the key that the iterator should start iterating after.
1161	pub fn set_last_raw_key(&mut self, previous_key: Vec<u8>) {
1162		self.previous_key = previous_key;
1163	}
1164
1165	/// Mutate this iterator into a draining iterator; items iterated are removed from storage.
1166	pub fn drain(mut self) -> Self {
1167		self.drain = true;
1168		self
1169	}
1170}
1171
1172impl<T> Iterator for KeyPrefixIterator<T> {
1173	type Item = T;
1174
1175	fn next(&mut self) -> Option<Self::Item> {
1176		loop {
1177			let maybe_next = subsoil::io::storage::next_key(&self.previous_key)
1178				.filter(|n| n.starts_with(&self.prefix));
1179
1180			if let Some(next) = maybe_next {
1181				self.previous_key = next;
1182				if self.drain {
1183					unhashed::kill(&self.previous_key);
1184				}
1185				let raw_key_without_prefix = &self.previous_key[self.prefix.len()..];
1186
1187				match (self.closure)(raw_key_without_prefix) {
1188					Ok(item) => return Some(item),
1189					Err(e) => {
1190						log::error!("key failed to decode at {:?}: {:?}", self.previous_key, e);
1191						continue;
1192					},
1193				}
1194			}
1195
1196			return None;
1197		}
1198	}
1199}
1200
1201/// Iterate over a prefix of a child trie and decode raw_key and raw_value into `T`.
1202///
1203/// If any decoding fails it skips the key and continues to the next one.
1204pub struct ChildTriePrefixIterator<T> {
1205	/// The prefix iterated on
1206	prefix: Vec<u8>,
1207	/// child info for child trie
1208	child_info: ChildInfo,
1209	/// The last key iterated on
1210	previous_key: Vec<u8>,
1211	/// If true then values are removed while iterating
1212	drain: bool,
1213	/// Whether or not we should fetch the previous key
1214	fetch_previous_key: bool,
1215	/// Function that takes `(raw_key_without_prefix, raw_value)` and decode `T`.
1216	/// `raw_key_without_prefix` is the raw storage key without the prefix iterated on.
1217	closure: fn(&[u8], &[u8]) -> Result<T, codec::Error>,
1218}
1219
1220impl<T> ChildTriePrefixIterator<T> {
1221	/// Mutate this iterator into a draining iterator; items iterated are removed from storage.
1222	pub fn drain(mut self) -> Self {
1223		self.drain = true;
1224		self
1225	}
1226}
1227
1228impl<T: Decode + Sized> ChildTriePrefixIterator<(Vec<u8>, T)> {
1229	/// Construct iterator to iterate over child trie items in `child_info` with the prefix
1230	/// `prefix`.
1231	///
1232	/// NOTE: Iterator with [`Self::drain`] will remove any value who failed to decode
1233	pub fn with_prefix(child_info: &ChildInfo, prefix: &[u8]) -> Self {
1234		let prefix = prefix.to_vec();
1235		let previous_key = prefix.clone();
1236		let closure = |raw_key_without_prefix: &[u8], mut raw_value: &[u8]| {
1237			let value = T::decode(&mut raw_value)?;
1238			Ok((raw_key_without_prefix.to_vec(), value))
1239		};
1240
1241		Self {
1242			prefix,
1243			child_info: child_info.clone(),
1244			previous_key,
1245			drain: false,
1246			fetch_previous_key: true,
1247			closure,
1248		}
1249	}
1250}
1251
1252impl<K: Decode + Sized, T: Decode + Sized> ChildTriePrefixIterator<(K, T)> {
1253	/// Construct iterator to iterate over child trie items in `child_info` with the prefix
1254	/// `prefix`.
1255	///
1256	/// NOTE: Iterator with [`Self::drain`] will remove any key or value who failed to decode
1257	pub fn with_prefix_over_key<H: ReversibleStorageHasher>(
1258		child_info: &ChildInfo,
1259		prefix: &[u8],
1260	) -> Self {
1261		let prefix = prefix.to_vec();
1262		let previous_key = prefix.clone();
1263		let closure = |raw_key_without_prefix: &[u8], mut raw_value: &[u8]| {
1264			let mut key_material = H::reverse(raw_key_without_prefix);
1265			let key = K::decode(&mut key_material)?;
1266			let value = T::decode(&mut raw_value)?;
1267			Ok((key, value))
1268		};
1269
1270		Self {
1271			prefix,
1272			child_info: child_info.clone(),
1273			previous_key,
1274			drain: false,
1275			fetch_previous_key: true,
1276			closure,
1277		}
1278	}
1279}
1280
1281impl<T> Iterator for ChildTriePrefixIterator<T> {
1282	type Item = T;
1283
1284	fn next(&mut self) -> Option<Self::Item> {
1285		loop {
1286			let maybe_next = if self.fetch_previous_key {
1287				self.fetch_previous_key = false;
1288				Some(self.previous_key.clone())
1289			} else {
1290				subsoil::io::default_child_storage::next_key(
1291					self.child_info.storage_key(),
1292					&self.previous_key,
1293				)
1294				.filter(|n| n.starts_with(&self.prefix))
1295			};
1296			break match maybe_next {
1297				Some(next) => {
1298					self.previous_key = next;
1299					let raw_value = match child::get_raw(&self.child_info, &self.previous_key) {
1300						Some(raw_value) => raw_value,
1301						None => {
1302							log::error!(
1303								"next_key returned a key with no value at {:?}",
1304								self.previous_key,
1305							);
1306							continue;
1307						},
1308					};
1309					if self.drain {
1310						child::kill(&self.child_info, &self.previous_key)
1311					}
1312					let raw_key_without_prefix = &self.previous_key[self.prefix.len()..];
1313					let item = match (self.closure)(raw_key_without_prefix, &raw_value[..]) {
1314						Ok(item) => item,
1315						Err(e) => {
1316							log::error!(
1317								"(key, value) failed to decode at {:?}: {:?}",
1318								self.previous_key,
1319								e,
1320							);
1321							continue;
1322						},
1323					};
1324
1325					Some(item)
1326				},
1327				None => None,
1328			};
1329		}
1330	}
1331}
1332
1333/// Trait for storage types that store all its value after a unique prefix.
1334pub trait StoragePrefixedContainer {
1335	/// Pallet prefix. Used for generating final key.
1336	fn pallet_prefix() -> &'static [u8];
1337
1338	/// Storage prefix. Used for generating final key.
1339	fn storage_prefix() -> &'static [u8];
1340
1341	/// Final full prefix that prefixes all keys.
1342	fn final_prefix() -> [u8; 32] {
1343		crate::storage::storage_prefix(Self::pallet_prefix(), Self::storage_prefix())
1344	}
1345}
1346
1347/// Trait for maps that store all its value after a unique prefix.
1348///
1349/// By default the final prefix is:
1350/// ```nocompile
1351/// Twox128(pallet_prefix) ++ Twox128(storage_prefix)
1352/// ```
1353pub trait StoragePrefixedMap<Value: FullCodec> {
1354	/// Pallet prefix. Used for generating final key.
1355	fn pallet_prefix() -> &'static [u8]; // TODO move to StoragePrefixedContainer
1356
1357	/// Storage prefix. Used for generating final key.
1358	fn storage_prefix() -> &'static [u8];
1359
1360	/// Final full prefix that prefixes all keys.
1361	fn final_prefix() -> [u8; 32] {
1362		crate::storage::storage_prefix(Self::pallet_prefix(), Self::storage_prefix())
1363	}
1364
1365	/// Remove all values in the overlay and up to `limit` in the backend.
1366	///
1367	/// All values in the client overlay will be deleted, if there is some `limit` then up to
1368	/// `limit` values are deleted from the client backend, if `limit` is none then all values in
1369	/// the client backend are deleted.
1370	///
1371	/// # Note
1372	///
1373	/// Calling this multiple times per block with a `limit` set leads always to the same keys being
1374	/// removed and the same result being returned. This happens because the keys to delete in the
1375	/// overlay are not taken into account when deleting keys in the backend.
1376	#[deprecated = "Use `clear` instead"]
1377	fn remove_all(limit: Option<u32>) -> subsoil::io::KillStorageResult {
1378		unhashed::clear_prefix(&Self::final_prefix(), limit, None).into()
1379	}
1380
1381	/// Attempt to remove all items from the map.
1382	///
1383	/// Returns [`MultiRemovalResults`](subsoil::io::MultiRemovalResults) to inform about the result. Once
1384	/// the resultant `maybe_cursor` field is `None`, then no further items remain to be deleted.
1385	///
1386	/// NOTE: After the initial call for any given map, it is important that no further items
1387	/// are inserted into the map. If so, then the map may not be empty when the resultant
1388	/// `maybe_cursor` is `None`.
1389	///
1390	/// # Limit
1391	///
1392	/// A `limit` must always be provided through in order to cap the maximum
1393	/// amount of deletions done in a single call. This is one fewer than the
1394	/// maximum number of backend iterations which may be done by this operation and as such
1395	/// represents the maximum number of backend deletions which may happen. A `limit` of zero
1396	/// implies that no keys will be deleted, though there may be a single iteration done.
1397	///
1398	/// # Cursor
1399	///
1400	/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
1401	/// passed once (in the initial call) for any given storage map. Subsequent calls
1402	/// operating on the same map should always pass `Some`, and this should be equal to the
1403	/// previous call result's `maybe_cursor` field.
1404	fn clear(limit: u32, maybe_cursor: Option<&[u8]>) -> subsoil::io::MultiRemovalResults {
1405		unhashed::clear_prefix(&Self::final_prefix(), Some(limit), maybe_cursor)
1406	}
1407
1408	/// Iter over all value of the storage.
1409	///
1410	/// NOTE: If a value failed to decode because storage is corrupted then it is skipped.
1411	fn iter_values() -> PrefixIterator<Value> {
1412		let prefix = Self::final_prefix();
1413		PrefixIterator {
1414			prefix: prefix.to_vec(),
1415			previous_key: prefix.to_vec(),
1416			drain: false,
1417			closure: |_raw_key, mut raw_value| Value::decode(&mut raw_value),
1418			phantom: Default::default(),
1419		}
1420	}
1421
1422	/// Translate the values of all elements by a function `f`, in the map in no particular order.
1423	/// By returning `None` from `f` for an element, you'll remove it from the map.
1424	///
1425	/// NOTE: If a value fail to decode because storage is corrupted then it is skipped.
1426	///
1427	/// # Warning
1428	///
1429	/// This function must be used with care, before being updated the storage still contains the
1430	/// old type, thus other calls (such as `get`) will fail at decoding it.
1431	///
1432	/// # Usage
1433	///
1434	/// This would typically be called inside the module implementation of on_runtime_upgrade.
1435	fn translate_values<OldValue: Decode, F: FnMut(OldValue) -> Option<Value>>(mut f: F) {
1436		let prefix = Self::final_prefix();
1437		let mut previous_key = prefix.clone().to_vec();
1438		while let Some(next) =
1439			subsoil::io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
1440		{
1441			previous_key = next;
1442			let maybe_value = unhashed::get::<OldValue>(&previous_key);
1443			match maybe_value {
1444				Some(value) => match f(value) {
1445					Some(new) => unhashed::put::<Value>(&previous_key, &new),
1446					None => unhashed::kill(&previous_key),
1447				},
1448				None => {
1449					log::error!("old key failed to decode at {:?}", previous_key);
1450					continue;
1451				},
1452			}
1453		}
1454	}
1455}
1456
1457/// Marker trait that will be implemented for types that support the `storage::append` api.
1458///
1459/// This trait is sealed.
1460pub trait StorageAppend<Item: Encode>: private::Sealed {}
1461
1462/// It is expected that the length is at the beginning of the encoded object
1463/// and that the length is a `Compact<u32>`.
1464///
1465/// This trait is sealed.
1466pub trait StorageDecodeLength: private::Sealed + codec::DecodeLength {
1467	/// Decode the length of the storage value at `key`.
1468	///
1469	/// This function assumes that the length is at the beginning of the encoded object
1470	/// and is a `Compact<u32>`.
1471	///
1472	/// Returns `None` if the storage value does not exist or the decoding failed.
1473	fn decode_len(key: &[u8]) -> Option<usize> {
1474		// `Compact<u32>` is 5 bytes in maximum.
1475		let mut data = [0u8; 5];
1476		let len = subsoil::io::storage::read(key, &mut data, 0)?;
1477		let len = data.len().min(len as usize);
1478		<Self as codec::DecodeLength>::len(&data[..len]).ok()
1479	}
1480}
1481
1482/// It is expected that the length is at the beginning of the encoded object and that the length is
1483/// a `Compact<u32>`.
1484///
1485/// # Note
1486/// The length returned by this trait is not deduplicated, i.e. it is the length of the underlying
1487/// stored Vec.
1488///
1489/// This trait is sealed.
1490pub trait StorageDecodeNonDedupLength: private::Sealed + codec::DecodeLength {
1491	/// Decode the length of the storage value at `key`.
1492	///
1493	/// This function assumes that the length is at the beginning of the encoded object and is a
1494	/// `Compact<u32>`.
1495	///
1496	/// Returns `None` if the storage value does not exist or the decoding failed.
1497	fn decode_non_dedup_len(key: &[u8]) -> Option<usize> {
1498		let mut data = [0u8; 5];
1499		let len = subsoil::io::storage::read(key, &mut data, 0)?;
1500		let len = data.len().min(len as usize);
1501		<Self as codec::DecodeLength>::len(&data[..len]).ok()
1502	}
1503}
1504
1505/// Provides `Sealed` trait to prevent implementing trait `StorageAppend` & `StorageDecodeLength`
1506/// & `EncodeLikeTuple` outside of this crate.
1507mod private {
1508	use super::*;
1509	use bounded_vec::BoundedVec;
1510	use weak_bounded_vec::WeakBoundedVec;
1511
1512	pub trait Sealed {}
1513
1514	impl<T: Encode> Sealed for Vec<T> {}
1515	impl Sealed for Digest {}
1516	impl<T, S> Sealed for BoundedVec<T, S> {}
1517	impl<T, S> Sealed for WeakBoundedVec<T, S> {}
1518	impl<K, V, S> Sealed for bounded_btree_map::BoundedBTreeMap<K, V, S> {}
1519	impl<T, S> Sealed for bounded_btree_set::BoundedBTreeSet<T, S> {}
1520	impl<T: Encode> Sealed for BTreeSet<T> {}
1521	impl<'a, T: EncodeLike<U>, U: Encode> Sealed for codec::Ref<'a, T, U> {}
1522
1523	macro_rules! impl_sealed_for_tuple {
1524		($($elem:ident),+) => {
1525			paste::paste! {
1526				impl<$($elem: Encode,)+> Sealed for ($($elem,)+) {}
1527				impl<$($elem: Encode,)+> Sealed for &($($elem,)+) {}
1528			}
1529		};
1530	}
1531
1532	impl_sealed_for_tuple!(A);
1533	impl_sealed_for_tuple!(A, B);
1534	impl_sealed_for_tuple!(A, B, C);
1535	impl_sealed_for_tuple!(A, B, C, D);
1536	impl_sealed_for_tuple!(A, B, C, D, E);
1537	impl_sealed_for_tuple!(A, B, C, D, E, F);
1538	impl_sealed_for_tuple!(A, B, C, D, E, F, G);
1539	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H);
1540	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I);
1541	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J);
1542	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K);
1543	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
1544	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
1545	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, O);
1546	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P);
1547	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P, Q);
1548	impl_sealed_for_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, O, P, Q, R);
1549}
1550
1551impl<T: Encode> StorageAppend<T> for Vec<T> {}
1552impl<T: Encode> StorageDecodeLength for Vec<T> {}
1553
1554impl<T: Encode> StorageAppend<T> for BTreeSet<T> {}
1555impl<T: Encode> StorageDecodeNonDedupLength for BTreeSet<T> {}
1556
1557// Blanket implementation StorageDecodeNonDedupLength for all types that are StorageDecodeLength.
1558impl<T: StorageDecodeLength> StorageDecodeNonDedupLength for T {
1559	fn decode_non_dedup_len(key: &[u8]) -> Option<usize> {
1560		T::decode_len(key)
1561	}
1562}
1563
1564/// We abuse the fact that SCALE does not put any marker into the encoding, i.e. we only encode the
1565/// internal vec and we can append to this vec. We have a test that ensures that if the `Digest`
1566/// format ever changes, we need to remove this here.
1567impl StorageAppend<DigestItem> for Digest {}
1568
1569/// Marker trait that is implemented for types that support the `storage::append` api with a limit
1570/// on the number of element.
1571///
1572/// This trait is sealed.
1573pub trait StorageTryAppend<Item>: StorageDecodeLength + private::Sealed {
1574	fn bound() -> usize;
1575}
1576
1577/// Storage value that is capable of [`StorageTryAppend`].
1578pub trait TryAppendValue<T: StorageTryAppend<I>, I: Encode> {
1579	/// Try and append the `item` into the storage item.
1580	///
1581	/// This might fail if bounds are not respected.
1582	fn try_append<LikeI: EncodeLike<I>>(item: LikeI) -> Result<(), ()>;
1583}
1584
1585impl<T, I, StorageValueT> TryAppendValue<T, I> for StorageValueT
1586where
1587	I: Encode,
1588	T: FullCodec + StorageTryAppend<I>,
1589	StorageValueT: generator::StorageValue<T>,
1590{
1591	fn try_append<LikeI: EncodeLike<I>>(item: LikeI) -> Result<(), ()> {
1592		let bound = T::bound();
1593		let current = Self::decode_len().unwrap_or_default();
1594		if current < bound {
1595			// NOTE: we cannot reuse the implementation for `Vec<T>` here because we never want to
1596			// mark `BoundedVec<T, S>` as `StorageAppend`.
1597			let key = Self::storage_value_final_key();
1598			subsoil::io::storage::append(&key, item.encode());
1599			Ok(())
1600		} else {
1601			Err(())
1602		}
1603	}
1604}
1605
1606/// Storage map that is capable of [`StorageTryAppend`].
1607pub trait TryAppendMap<K: Encode, T: StorageTryAppend<I>, I: Encode> {
1608	/// Try and append the `item` into the storage map at the given `key`.
1609	///
1610	/// This might fail if bounds are not respected.
1611	fn try_append<LikeK: EncodeLike<K> + Clone, LikeI: EncodeLike<I>>(
1612		key: LikeK,
1613		item: LikeI,
1614	) -> Result<(), ()>;
1615}
1616
1617impl<K, T, I, StorageMapT> TryAppendMap<K, T, I> for StorageMapT
1618where
1619	K: FullCodec,
1620	T: FullCodec + StorageTryAppend<I>,
1621	I: Encode,
1622	StorageMapT: generator::StorageMap<K, T>,
1623{
1624	fn try_append<LikeK: EncodeLike<K> + Clone, LikeI: EncodeLike<I>>(
1625		key: LikeK,
1626		item: LikeI,
1627	) -> Result<(), ()> {
1628		let bound = T::bound();
1629		let current = Self::decode_len(key.clone()).unwrap_or_default();
1630		if current < bound {
1631			let key = Self::storage_map_final_key(key);
1632			subsoil::io::storage::append(&key, item.encode());
1633			Ok(())
1634		} else {
1635			Err(())
1636		}
1637	}
1638}
1639
1640/// Storage double map that is capable of [`StorageTryAppend`].
1641pub trait TryAppendDoubleMap<K1: Encode, K2: Encode, T: StorageTryAppend<I>, I: Encode> {
1642	/// Try and append the `item` into the storage double map at the given `key`.
1643	///
1644	/// This might fail if bounds are not respected.
1645	fn try_append<
1646		LikeK1: EncodeLike<K1> + Clone,
1647		LikeK2: EncodeLike<K2> + Clone,
1648		LikeI: EncodeLike<I>,
1649	>(
1650		key1: LikeK1,
1651		key2: LikeK2,
1652		item: LikeI,
1653	) -> Result<(), ()>;
1654}
1655
1656impl<K1, K2, T, I, StorageDoubleMapT> TryAppendDoubleMap<K1, K2, T, I> for StorageDoubleMapT
1657where
1658	K1: FullCodec,
1659	K2: FullCodec,
1660	T: FullCodec + StorageTryAppend<I>,
1661	I: Encode,
1662	StorageDoubleMapT: generator::StorageDoubleMap<K1, K2, T>,
1663{
1664	fn try_append<
1665		LikeK1: EncodeLike<K1> + Clone,
1666		LikeK2: EncodeLike<K2> + Clone,
1667		LikeI: EncodeLike<I>,
1668	>(
1669		key1: LikeK1,
1670		key2: LikeK2,
1671		item: LikeI,
1672	) -> Result<(), ()> {
1673		let bound = T::bound();
1674		let current = Self::decode_len(key1.clone(), key2.clone()).unwrap_or_default();
1675		if current < bound {
1676			let double_map_key = Self::storage_double_map_final_key(key1, key2);
1677			subsoil::io::storage::append(&double_map_key, item.encode());
1678			Ok(())
1679		} else {
1680			Err(())
1681		}
1682	}
1683}
1684
1685/// Storage N map that is capable of [`StorageTryAppend`].
1686pub trait TryAppendNMap<K: KeyGenerator, T: StorageTryAppend<I>, I: Encode> {
1687	/// Try and append the `item` into the storage N map at the given `key`.
1688	///
1689	/// This might fail if bounds are not respected.
1690	fn try_append<
1691		LikeK: EncodeLikeTuple<K::KArg> + TupleToEncodedIter + Clone,
1692		LikeI: EncodeLike<I>,
1693	>(
1694		key: LikeK,
1695		item: LikeI,
1696	) -> Result<(), ()>;
1697}
1698
1699impl<K, T, I, StorageNMapT> TryAppendNMap<K, T, I> for StorageNMapT
1700where
1701	K: KeyGenerator,
1702	T: FullCodec + StorageTryAppend<I>,
1703	I: Encode,
1704	StorageNMapT: generator::StorageNMap<K, T>,
1705{
1706	fn try_append<
1707		LikeK: EncodeLikeTuple<K::KArg> + TupleToEncodedIter + Clone,
1708		LikeI: EncodeLike<I>,
1709	>(
1710		key: LikeK,
1711		item: LikeI,
1712	) -> Result<(), ()> {
1713		let bound = T::bound();
1714		let current = Self::decode_len(key.clone()).unwrap_or_default();
1715		if current < bound {
1716			let key = Self::storage_n_map_final_key::<K, _>(key);
1717			subsoil::io::storage::append(&key, item.encode());
1718			Ok(())
1719		} else {
1720			Err(())
1721		}
1722	}
1723}
1724
1725/// Returns the storage prefix for a specific pallet name and storage name.
1726///
1727/// The storage prefix is `concat(twox_128(pallet_name), twox_128(storage_name))`.
1728pub fn storage_prefix(pallet_name: &[u8], storage_name: &[u8]) -> [u8; 32] {
1729	let pallet_hash = subsoil::io::hashing::twox_128(pallet_name);
1730	let storage_hash = subsoil::io::hashing::twox_128(storage_name);
1731
1732	let mut final_key = [0u8; 32];
1733	final_key[..16].copy_from_slice(&pallet_hash);
1734	final_key[16..].copy_from_slice(&storage_hash);
1735
1736	final_key
1737}
1738
1739#[cfg(test)]
1740mod test {
1741	use super::*;
1742	use crate::{assert_ok, hash::Identity, pallet_prelude::NMapKey, Twox128};
1743	use bounded_vec::BoundedVec;
1744	use generator::StorageValue as _;
1745	use subsoil::io::TestExternalities;
1746	use subsoil_crypto_hashing::twox_128;
1747	use topsoil_core::traits::ConstU32;
1748	use weak_bounded_vec::WeakBoundedVec;
1749
1750	#[test]
1751	fn prefixed_map_works() {
1752		TestExternalities::default().execute_with(|| {
1753			struct MyStorage;
1754			impl StoragePrefixedMap<u64> for MyStorage {
1755				fn pallet_prefix() -> &'static [u8] {
1756					b"MyModule"
1757				}
1758
1759				fn storage_prefix() -> &'static [u8] {
1760					b"MyStorage"
1761				}
1762			}
1763
1764			let key_before = {
1765				let mut k = MyStorage::final_prefix();
1766				let last = k.iter_mut().last().unwrap();
1767				*last = last.checked_sub(1).unwrap();
1768				k
1769			};
1770			let key_after = {
1771				let mut k = MyStorage::final_prefix();
1772				let last = k.iter_mut().last().unwrap();
1773				*last = last.checked_add(1).unwrap();
1774				k
1775			};
1776
1777			unhashed::put(&key_before[..], &32u64);
1778			unhashed::put(&key_after[..], &33u64);
1779
1780			let k = [twox_128(b"MyModule"), twox_128(b"MyStorage")].concat();
1781			assert_eq!(MyStorage::final_prefix().to_vec(), k);
1782
1783			// test iteration
1784			assert!(MyStorage::iter_values().collect::<Vec<_>>().is_empty());
1785
1786			unhashed::put(&[&k[..], &vec![1][..]].concat(), &1u64);
1787			unhashed::put(&[&k[..], &vec![1, 1][..]].concat(), &2u64);
1788			unhashed::put(&[&k[..], &vec![8][..]].concat(), &3u64);
1789			unhashed::put(&[&k[..], &vec![10][..]].concat(), &4u64);
1790
1791			assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3, 4]);
1792
1793			// test removal
1794			let _ = MyStorage::clear(u32::max_value(), None);
1795			assert!(MyStorage::iter_values().collect::<Vec<_>>().is_empty());
1796
1797			// test migration
1798			unhashed::put(&[&k[..], &vec![1][..]].concat(), &1u32);
1799			unhashed::put(&[&k[..], &vec![8][..]].concat(), &2u32);
1800
1801			assert!(MyStorage::iter_values().collect::<Vec<_>>().is_empty());
1802			MyStorage::translate_values(|v: u32| Some(v as u64));
1803			assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2]);
1804			let _ = MyStorage::clear(u32::max_value(), None);
1805
1806			// test migration 2
1807			unhashed::put(&[&k[..], &vec![1][..]].concat(), &1u128);
1808			unhashed::put(&[&k[..], &vec![1, 1][..]].concat(), &2u64);
1809			unhashed::put(&[&k[..], &vec![8][..]].concat(), &3u128);
1810			unhashed::put(&[&k[..], &vec![10][..]].concat(), &4u32);
1811
1812			// (contains some value that successfully decoded to u64)
1813			assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3]);
1814			MyStorage::translate_values(|v: u128| Some(v as u64));
1815			assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3]);
1816			let _ = MyStorage::clear(u32::max_value(), None);
1817
1818			// test that other values are not modified.
1819			assert_eq!(unhashed::get(&key_before[..]), Some(32u64));
1820			assert_eq!(unhashed::get(&key_after[..]), Some(33u64));
1821		});
1822	}
1823
1824	// This test ensures that the Digest encoding does not change without being noticed.
1825	#[test]
1826	fn digest_storage_append_works_as_expected() {
1827		TestExternalities::default().execute_with(|| {
1828			struct Storage;
1829			impl generator::StorageValue<Digest> for Storage {
1830				type Query = Digest;
1831
1832				fn pallet_prefix() -> &'static [u8] {
1833					b"MyModule"
1834				}
1835
1836				fn storage_prefix() -> &'static [u8] {
1837					b"Storage"
1838				}
1839
1840				fn from_optional_value_to_query(v: Option<Digest>) -> Self::Query {
1841					v.unwrap()
1842				}
1843
1844				fn from_query_to_optional_value(v: Self::Query) -> Option<Digest> {
1845					Some(v)
1846				}
1847
1848				fn storage_value_final_key() -> [u8; 32] {
1849					storage_prefix(Self::pallet_prefix(), Self::storage_prefix())
1850				}
1851			}
1852
1853			Storage::append(DigestItem::Other(Vec::new()));
1854
1855			let value = unhashed::get_raw(&Storage::storage_value_final_key()).unwrap();
1856
1857			let expected = Digest { logs: vec![DigestItem::Other(Vec::new())] };
1858			assert_eq!(Digest::decode(&mut &value[..]).unwrap(), expected);
1859		});
1860	}
1861
1862	#[test]
1863	fn key_prefix_iterator_works() {
1864		TestExternalities::default().execute_with(|| {
1865			use crate::{hash::Twox64Concat, storage::generator::StorageMap};
1866			struct MyStorageMap;
1867			impl StorageMap<u64, u64> for MyStorageMap {
1868				type Query = u64;
1869				type Hasher = Twox64Concat;
1870
1871				fn pallet_prefix() -> &'static [u8] {
1872					b"MyModule"
1873				}
1874
1875				fn storage_prefix() -> &'static [u8] {
1876					b"MyStorageMap"
1877				}
1878
1879				fn prefix_hash() -> [u8; 32] {
1880					storage_prefix(Self::pallet_prefix(), Self::storage_prefix())
1881				}
1882
1883				fn from_optional_value_to_query(v: Option<u64>) -> Self::Query {
1884					v.unwrap_or_default()
1885				}
1886
1887				fn from_query_to_optional_value(v: Self::Query) -> Option<u64> {
1888					Some(v)
1889				}
1890			}
1891
1892			let k = [twox_128(b"MyModule"), twox_128(b"MyStorageMap")].concat();
1893			assert_eq!(MyStorageMap::prefix_hash().to_vec(), k);
1894
1895			// empty to start
1896			assert!(MyStorageMap::iter_keys().collect::<Vec<_>>().is_empty());
1897
1898			MyStorageMap::insert(1, 10);
1899			MyStorageMap::insert(2, 20);
1900			MyStorageMap::insert(3, 30);
1901			MyStorageMap::insert(4, 40);
1902
1903			// just looking
1904			let mut keys = MyStorageMap::iter_keys().collect::<Vec<_>>();
1905			keys.sort();
1906			assert_eq!(keys, vec![1, 2, 3, 4]);
1907
1908			// draining the keys and values
1909			let mut drained_keys = MyStorageMap::iter_keys().drain().collect::<Vec<_>>();
1910			drained_keys.sort();
1911			assert_eq!(drained_keys, vec![1, 2, 3, 4]);
1912
1913			// empty again
1914			assert!(MyStorageMap::iter_keys().collect::<Vec<_>>().is_empty());
1915		});
1916	}
1917
1918	#[test]
1919	fn prefix_iterator_pagination_works() {
1920		TestExternalities::default().execute_with(|| {
1921			use crate::{hash::Identity, storage::generator::map::StorageMap};
1922			#[crate::storage_alias]
1923			type MyStorageMap = StorageMap<MyModule, Identity, u64, u64>;
1924
1925			MyStorageMap::insert(1, 10);
1926			MyStorageMap::insert(2, 20);
1927			MyStorageMap::insert(3, 30);
1928			MyStorageMap::insert(4, 40);
1929			MyStorageMap::insert(5, 50);
1930			MyStorageMap::insert(6, 60);
1931			MyStorageMap::insert(7, 70);
1932			MyStorageMap::insert(8, 80);
1933			MyStorageMap::insert(9, 90);
1934			MyStorageMap::insert(10, 100);
1935
1936			let op = |(_, v)| v / 10;
1937			let mut final_vec = vec![];
1938			let mut iter = MyStorageMap::iter();
1939
1940			let elem = iter.next().unwrap();
1941			assert_eq!(elem, (1, 10));
1942			final_vec.push(op(elem));
1943
1944			let elem = iter.next().unwrap();
1945			assert_eq!(elem, (2, 20));
1946			final_vec.push(op(elem));
1947
1948			let stored_key = iter.last_raw_key().to_owned();
1949			assert_eq!(stored_key, MyStorageMap::storage_map_final_key(2));
1950
1951			let mut iter = MyStorageMap::iter_from(stored_key.clone());
1952
1953			final_vec.push(op(iter.next().unwrap()));
1954			final_vec.push(op(iter.next().unwrap()));
1955			final_vec.push(op(iter.next().unwrap()));
1956
1957			assert_eq!(final_vec, vec![1, 2, 3, 4, 5]);
1958
1959			let mut iter = PrefixIterator::<_>::new(
1960				iter.prefix().to_vec(),
1961				stored_key,
1962				|mut raw_key_without_prefix, mut raw_value| {
1963					let key = u64::decode(&mut raw_key_without_prefix)?;
1964					Ok((key, u64::decode(&mut raw_value)?))
1965				},
1966			);
1967			let previous_key = MyStorageMap::storage_map_final_key(5);
1968			iter.set_last_raw_key(previous_key);
1969
1970			let remaining = iter.map(op).collect::<Vec<_>>();
1971			assert_eq!(remaining.len(), 5);
1972			assert_eq!(remaining, vec![6, 7, 8, 9, 10]);
1973
1974			final_vec.extend_from_slice(&remaining);
1975
1976			assert_eq!(final_vec, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1977		});
1978	}
1979
1980	#[test]
1981	fn child_trie_prefixed_map_works() {
1982		TestExternalities::default().execute_with(|| {
1983			let child_info_a = child::ChildInfo::new_default(b"a");
1984			child::put(&child_info_a, &[1, 2, 3], &8u16);
1985			child::put(&child_info_a, &[2], &8u16);
1986			child::put(&child_info_a, &[2, 1, 3], &8u8);
1987			child::put(&child_info_a, &[2, 2, 3], &8u16);
1988			child::put(&child_info_a, &[3], &8u16);
1989
1990			assert_eq!(
1991				ChildTriePrefixIterator::with_prefix(&child_info_a, &[2])
1992					.collect::<Vec<(Vec<u8>, u16)>>(),
1993				vec![(vec![], 8), (vec![2, 3], 8),],
1994			);
1995
1996			assert_eq!(
1997				ChildTriePrefixIterator::with_prefix(&child_info_a, &[2])
1998					.drain()
1999					.collect::<Vec<(Vec<u8>, u16)>>(),
2000				vec![(vec![], 8), (vec![2, 3], 8),],
2001			);
2002
2003			// The only remaining is the ones outside prefix
2004			assert_eq!(
2005				ChildTriePrefixIterator::with_prefix(&child_info_a, &[])
2006					.collect::<Vec<(Vec<u8>, u8)>>(),
2007				vec![(vec![1, 2, 3], 8), (vec![3], 8),],
2008			);
2009
2010			child::put(&child_info_a, &[1, 2, 3], &8u16);
2011			child::put(&child_info_a, &[2], &8u16);
2012			child::put(&child_info_a, &[2, 1, 3], &8u8);
2013			child::put(&child_info_a, &[2, 2, 3], &8u16);
2014			child::put(&child_info_a, &[3], &8u16);
2015
2016			assert_eq!(
2017				ChildTriePrefixIterator::with_prefix_over_key::<Identity>(&child_info_a, &[2])
2018					.collect::<Vec<(u16, u16)>>(),
2019				vec![(u16::decode(&mut &[2, 3][..]).unwrap(), 8),],
2020			);
2021
2022			assert_eq!(
2023				ChildTriePrefixIterator::with_prefix_over_key::<Identity>(&child_info_a, &[2])
2024					.drain()
2025					.collect::<Vec<(u16, u16)>>(),
2026				vec![(u16::decode(&mut &[2, 3][..]).unwrap(), 8),],
2027			);
2028
2029			// The only remaining is the ones outside prefix
2030			assert_eq!(
2031				ChildTriePrefixIterator::with_prefix(&child_info_a, &[])
2032					.collect::<Vec<(Vec<u8>, u8)>>(),
2033				vec![(vec![1, 2, 3], 8), (vec![3], 8),],
2034			);
2035		});
2036	}
2037
2038	#[crate::storage_alias]
2039	type Foo = StorageValue<Prefix, WeakBoundedVec<u32, ConstU32<7>>>;
2040	#[crate::storage_alias]
2041	type FooMap = StorageMap<Prefix, Twox128, u32, BoundedVec<u32, ConstU32<7>>>;
2042	#[crate::storage_alias]
2043	type FooDoubleMap =
2044		StorageDoubleMap<Prefix, Twox128, u32, Twox128, u32, BoundedVec<u32, ConstU32<7>>>;
2045	#[crate::storage_alias]
2046	type FooTripleMap = StorageNMap<
2047		Prefix,
2048		(NMapKey<Twox128, u32>, NMapKey<Twox128, u32>, NMapKey<Twox128, u32>),
2049		u64,
2050	>;
2051	#[crate::storage_alias]
2052	type FooQuadMap = StorageNMap<
2053		Prefix,
2054		(
2055			NMapKey<Twox128, u32>,
2056			NMapKey<Twox128, u32>,
2057			NMapKey<Twox128, u32>,
2058			NMapKey<Twox128, u32>,
2059		),
2060		BoundedVec<u32, ConstU32<7>>,
2061	>;
2062
2063	#[test]
2064	fn contains_prefix_works() {
2065		TestExternalities::default().execute_with(|| {
2066			// Test double maps
2067			assert!(FooDoubleMap::iter_prefix_values(1).next().is_none());
2068			assert_eq!(FooDoubleMap::contains_prefix(1), false);
2069
2070			assert_ok!(FooDoubleMap::try_append(1, 1, 4));
2071			assert_ok!(FooDoubleMap::try_append(2, 1, 4));
2072			assert!(FooDoubleMap::iter_prefix_values(1).next().is_some());
2073			assert!(FooDoubleMap::contains_prefix(1));
2074			FooDoubleMap::remove(1, 1);
2075			assert_eq!(FooDoubleMap::contains_prefix(1), false);
2076
2077			// Test N Maps
2078			assert!(FooTripleMap::iter_prefix_values((1,)).next().is_none());
2079			assert_eq!(FooTripleMap::contains_prefix((1,)), false);
2080
2081			FooTripleMap::insert((1, 1, 1), 4);
2082			FooTripleMap::insert((2, 1, 1), 4);
2083			assert!(FooTripleMap::iter_prefix_values((1,)).next().is_some());
2084			assert!(FooTripleMap::contains_prefix((1,)));
2085			FooTripleMap::remove((1, 1, 1));
2086			assert_eq!(FooTripleMap::contains_prefix((1,)), false);
2087		});
2088	}
2089
2090	#[test]
2091	fn try_append_works() {
2092		TestExternalities::default().execute_with(|| {
2093			let bounded: WeakBoundedVec<u32, ConstU32<7>> = vec![1, 2, 3].try_into().unwrap();
2094			Foo::put(bounded);
2095			assert_ok!(Foo::try_append(4));
2096			assert_ok!(Foo::try_append(5));
2097			assert_ok!(Foo::try_append(6));
2098			assert_ok!(Foo::try_append(7));
2099			assert_eq!(Foo::decode_len().unwrap(), 7);
2100			assert!(Foo::try_append(8).is_err());
2101		});
2102
2103		TestExternalities::default().execute_with(|| {
2104			let bounded: BoundedVec<u32, ConstU32<7>> = vec![1, 2, 3].try_into().unwrap();
2105			FooMap::insert(1, bounded);
2106
2107			assert_ok!(FooMap::try_append(1, 4));
2108			assert_ok!(FooMap::try_append(1, 5));
2109			assert_ok!(FooMap::try_append(1, 6));
2110			assert_ok!(FooMap::try_append(1, 7));
2111			assert_eq!(FooMap::decode_len(1).unwrap(), 7);
2112			assert!(FooMap::try_append(1, 8).is_err());
2113
2114			// append to a non-existing
2115			assert!(FooMap::get(2).is_none());
2116			assert_ok!(FooMap::try_append(2, 4));
2117			assert_eq!(
2118				FooMap::get(2).unwrap(),
2119				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4]).unwrap(),
2120			);
2121			assert_ok!(FooMap::try_append(2, 5));
2122			assert_eq!(
2123				FooMap::get(2).unwrap(),
2124				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4, 5]).unwrap(),
2125			);
2126		});
2127
2128		TestExternalities::default().execute_with(|| {
2129			let bounded: BoundedVec<u32, ConstU32<7>> = vec![1, 2, 3].try_into().unwrap();
2130			FooDoubleMap::insert(1, 1, bounded);
2131
2132			assert_ok!(FooDoubleMap::try_append(1, 1, 4));
2133			assert_ok!(FooDoubleMap::try_append(1, 1, 5));
2134			assert_ok!(FooDoubleMap::try_append(1, 1, 6));
2135			assert_ok!(FooDoubleMap::try_append(1, 1, 7));
2136			assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 7);
2137			assert!(FooDoubleMap::try_append(1, 1, 8).is_err());
2138
2139			// append to a non-existing
2140			assert!(FooDoubleMap::get(2, 1).is_none());
2141			assert_ok!(FooDoubleMap::try_append(2, 1, 4));
2142			assert_eq!(
2143				FooDoubleMap::get(2, 1).unwrap(),
2144				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4]).unwrap(),
2145			);
2146			assert_ok!(FooDoubleMap::try_append(2, 1, 5));
2147			assert_eq!(
2148				FooDoubleMap::get(2, 1).unwrap(),
2149				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4, 5]).unwrap(),
2150			);
2151		});
2152
2153		TestExternalities::default().execute_with(|| {
2154			let bounded: BoundedVec<u32, ConstU32<7>> = vec![1, 2, 3].try_into().unwrap();
2155			FooQuadMap::insert((1, 1, 1, 1), bounded);
2156
2157			assert_ok!(FooQuadMap::try_append((1, 1, 1, 1), 4));
2158			assert_ok!(FooQuadMap::try_append((1, 1, 1, 1), 5));
2159			assert_ok!(FooQuadMap::try_append((1, 1, 1, 1), 6));
2160			assert_ok!(FooQuadMap::try_append((1, 1, 1, 1), 7));
2161			assert_eq!(FooQuadMap::decode_len((1, 1, 1, 1)).unwrap(), 7);
2162			assert!(FooQuadMap::try_append((1, 1, 1, 1), 8).is_err());
2163
2164			// append to a non-existing
2165			assert!(FooQuadMap::get((2, 1, 1, 1)).is_none());
2166			assert_ok!(FooQuadMap::try_append((2, 1, 1, 1), 4));
2167			assert_eq!(
2168				FooQuadMap::get((2, 1, 1, 1)).unwrap(),
2169				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4]).unwrap(),
2170			);
2171			assert_ok!(FooQuadMap::try_append((2, 1, 1, 1), 5));
2172			assert_eq!(
2173				FooQuadMap::get((2, 1, 1, 1)).unwrap(),
2174				BoundedVec::<u32, ConstU32<7>>::try_from(vec![4, 5]).unwrap(),
2175			);
2176		});
2177	}
2178
2179	#[crate::storage_alias]
2180	type FooSet = StorageValue<Prefix, BTreeSet<u32>>;
2181
2182	#[test]
2183	fn btree_set_append_and_decode_len_works() {
2184		TestExternalities::default().execute_with(|| {
2185			let btree = BTreeSet::from([1, 2, 3]);
2186			FooSet::put(btree);
2187
2188			FooSet::append(4);
2189			FooSet::append(5);
2190			FooSet::append(6);
2191			FooSet::append(7);
2192
2193			assert_eq!(FooSet::decode_non_dedup_len().unwrap(), 7);
2194		});
2195	}
2196
2197	#[docify::export]
2198	#[test]
2199	fn btree_set_decode_non_dedup_len() {
2200		#[crate::storage_alias]
2201		type Store = StorageValue<Prefix, BTreeSet<u32>>;
2202
2203		TestExternalities::default().execute_with(|| {
2204			Store::append(4);
2205			Store::append(4); // duplicate value
2206			Store::append(5);
2207
2208			let length_with_dup_items = 3;
2209
2210			assert_eq!(Store::decode_non_dedup_len().unwrap(), length_with_dup_items);
2211		});
2212	}
2213}