referrerpolicy=no-referrer-when-downgrade

frame_support/storage/
bounded_btree_map.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Traits, types and structs to support a bounded BTreeMap.
19
20use crate::storage::StorageDecodeLength;
21pub use sp_runtime::BoundedBTreeMap;
22
23impl<K, V, S> StorageDecodeLength for BoundedBTreeMap<K, V, S> {}
24
25#[cfg(test)]
26pub mod test {
27	use super::*;
28	use crate::Twox128;
29	use alloc::collections::btree_map::BTreeMap;
30	use frame_support::traits::{ConstU32, Get};
31	use sp_io::TestExternalities;
32
33	#[crate::storage_alias]
34	type Foo = StorageValue<Prefix, BoundedBTreeMap<u32, (), ConstU32<7>>>;
35
36	#[crate::storage_alias]
37	type FooMap = StorageMap<Prefix, Twox128, u32, BoundedBTreeMap<u32, (), ConstU32<7>>>;
38
39	#[crate::storage_alias]
40	type FooDoubleMap =
41		StorageDoubleMap<Prefix, Twox128, u32, Twox128, u32, BoundedBTreeMap<u32, (), ConstU32<7>>>;
42
43	fn map_from_keys<K>(keys: &[K]) -> BTreeMap<K, ()>
44	where
45		K: Ord + Copy,
46	{
47		keys.iter().copied().zip(std::iter::repeat(())).collect()
48	}
49
50	fn boundedmap_from_keys<K, S>(keys: &[K]) -> BoundedBTreeMap<K, (), S>
51	where
52		K: Ord + Copy,
53		S: Get<u32>,
54	{
55		map_from_keys(keys).try_into().unwrap()
56	}
57
58	#[test]
59	fn decode_len_works() {
60		TestExternalities::default().execute_with(|| {
61			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
62			Foo::put(bounded);
63			assert_eq!(Foo::decode_len().unwrap(), 3);
64		});
65
66		TestExternalities::default().execute_with(|| {
67			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
68			FooMap::insert(1, bounded);
69			assert_eq!(FooMap::decode_len(1).unwrap(), 3);
70			assert!(FooMap::decode_len(0).is_none());
71			assert!(FooMap::decode_len(2).is_none());
72		});
73
74		TestExternalities::default().execute_with(|| {
75			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
76			FooDoubleMap::insert(1, 1, bounded);
77			assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 3);
78			assert!(FooDoubleMap::decode_len(2, 1).is_none());
79			assert!(FooDoubleMap::decode_len(1, 2).is_none());
80			assert!(FooDoubleMap::decode_len(2, 2).is_none());
81		});
82
83		TestExternalities::default().execute_with(|| {
84			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
85			FooDoubleMap::insert(1, 1, bounded.clone());
86			FooDoubleMap::insert(2, 2, bounded); // duplicate value
87
88			assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 3);
89			assert_eq!(FooDoubleMap::decode_len(2, 2).unwrap(), 3);
90			assert!(FooDoubleMap::decode_len(2, 1).is_none());
91			assert!(FooDoubleMap::decode_len(1, 2).is_none());
92		});
93	}
94}