referrerpolicy=no-referrer-when-downgrade

frame_support/storage/
child.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//! Operation on runtime child storages.
19//!
20//! This module is a currently only a variant of unhashed with additional `child_info`.
21// NOTE: could replace unhashed by having only one kind of storage (top trie being the child info
22// of null length parent storage key).
23
24use alloc::vec::Vec;
25use codec::{Codec, Decode, Encode};
26pub use sp_core::storage::{ChildInfo, ChildType, StateVersion};
27pub use sp_io::{KillStorageResult, MultiRemovalResults};
28
29/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
30pub fn get<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
31	match child_info.child_type() {
32		ChildType::ParentKeyId => {
33			let storage_key = child_info.storage_key();
34			sp_io::default_child_storage::get(storage_key, key).and_then(|v| {
35				Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
36					// TODO #3700: error should be handleable.
37					log::error!(
38						target: "runtime::storage",
39						"Corrupted state in child trie at {:?}/{:?}",
40						storage_key,
41						key,
42					);
43					None
44				})
45			})
46		},
47	}
48}
49
50/// Return the value of the item in storage under `key`, or the type's default if there is no
51/// explicit entry.
52pub fn get_or_default<T: Decode + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
53	get(child_info, key).unwrap_or_default()
54}
55
56/// Return the value of the item in storage under `key`, or `default_value` if there is no
57/// explicit entry.
58pub fn get_or<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
59	get(child_info, key).unwrap_or(default_value)
60}
61
62/// Return the value of the item in storage under `key`, or `default_value()` if there is no
63/// explicit entry.
64pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
65	child_info: &ChildInfo,
66	key: &[u8],
67	default_value: F,
68) -> T {
69	get(child_info, key).unwrap_or_else(default_value)
70}
71
72/// Put `value` in storage under `key`.
73pub fn put<T: Encode>(child_info: &ChildInfo, key: &[u8], value: &T) {
74	match child_info.child_type() {
75		ChildType::ParentKeyId => value.using_encoded(|slice| {
76			sp_io::default_child_storage::set(child_info.storage_key(), key, slice)
77		}),
78	}
79}
80
81/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
82pub fn take<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
83	let r = get(child_info, key);
84	if r.is_some() {
85		kill(child_info, key);
86	}
87	r
88}
89
90/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
91/// the default for its type.
92pub fn take_or_default<T: Codec + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
93	take(child_info, key).unwrap_or_default()
94}
95
96/// Return the value of the item in storage under `key`, or `default_value` if there is no
97/// explicit entry. Ensure there is no explicit entry on return.
98pub fn take_or<T: Codec + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
99	take(child_info, key).unwrap_or(default_value)
100}
101
102/// Return the value of the item in storage under `key`, or `default_value()` if there is no
103/// explicit entry. Ensure there is no explicit entry on return.
104pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
105	child_info: &ChildInfo,
106	key: &[u8],
107	default_value: F,
108) -> T {
109	take(child_info, key).unwrap_or_else(default_value)
110}
111
112/// Check to see if `key` has an explicit entry in storage.
113pub fn exists(child_info: &ChildInfo, key: &[u8]) -> bool {
114	match child_info.child_type() {
115		ChildType::ParentKeyId => {
116			sp_io::default_child_storage::exists(child_info.storage_key(), key)
117		},
118	}
119}
120
121/// Remove all `storage_key` key/values
122///
123/// Deletes all keys from the overlay and up to `limit` keys from the backend if
124/// it is set to `Some`. No limit is applied when `limit` is set to `None`.
125///
126/// The limit can be used to partially delete a child trie in case it is too large
127/// to delete in one go (block).
128///
129/// # Note
130///
131/// Please note that keys that are residing in the overlay for that child trie when
132/// issuing this call are all deleted without counting towards the `limit`. Only keys
133/// written during the current block are part of the overlay. Deleting with a `limit`
134/// mostly makes sense with an empty overlay for that child trie.
135///
136/// Calling this function multiple times per block for the same `storage_key` does
137/// not make much sense because it is not cumulative when called inside the same block.
138/// Use this function to distribute the deletion of a single child trie across multiple
139/// blocks.
140#[deprecated = "Use `clear_storage` instead"]
141pub fn kill_storage(child_info: &ChildInfo, limit: Option<u32>) -> KillStorageResult {
142	match child_info.child_type() {
143		ChildType::ParentKeyId => {
144			sp_io::default_child_storage::storage_kill(child_info.storage_key(), limit)
145		},
146	}
147}
148
149/// Partially clear the child storage of each key-value pair.
150///
151/// # Limit
152///
153/// A *limit* should always be provided through `maybe_limit`. This is one fewer than the
154/// maximum number of backend iterations which may be done by this operation and as such
155/// represents the maximum number of backend deletions which may happen. A *limit* of zero
156/// implies that no keys will be deleted, though there may be a single iteration done.
157///
158/// The limit can be used to partially delete storage items in case it is too large or costly
159/// to delete all in a single operation.
160///
161/// # Cursor
162///
163/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
164/// passed once (in the initial call) for any attempt to clear storage. In general, subsequent calls
165/// operating on the same prefix should pass `Some` and this value should be equal to the
166/// previous call result's `maybe_cursor` field. The only exception to this is when you can
167/// guarantee that the subsequent call is in a new block; in this case the previous call's result
168/// cursor need not be passed in and a `None` may be passed instead. This exception may be useful
169/// then making this call solely from a block-hook such as `on_initialize`.
170
171/// Returns [`MultiRemovalResults`] to inform about the result. Once the resultant `maybe_cursor`
172/// field is `None`, then no further items remain to be deleted.
173///
174/// NOTE: After the initial call for any given child storage, it is important that no keys further
175/// keys are inserted. If so, then they may or may not be deleted by subsequent calls.
176///
177/// # Note
178///
179/// Please note that keys which are residing in the overlay for the child are deleted without
180/// counting towards the `limit`.
181pub fn clear_storage(
182	child_info: &ChildInfo,
183	maybe_limit: Option<u32>,
184	_maybe_cursor: Option<&[u8]>,
185) -> MultiRemovalResults {
186	// TODO: Once the network has upgraded to include the new host functions, this code can be
187	// enabled.
188	// sp_io::default_child_storage::storage_kill(prefix, maybe_limit, maybe_cursor)
189	let r = match child_info.child_type() {
190		ChildType::ParentKeyId => {
191			sp_io::default_child_storage::storage_kill(child_info.storage_key(), maybe_limit)
192		},
193	};
194	use sp_io::KillStorageResult::*;
195	let (maybe_cursor, backend) = match r {
196		AllRemoved(db) => (None, db),
197		SomeRemaining(db) => (Some(child_info.storage_key().to_vec()), db),
198	};
199	MultiRemovalResults { maybe_cursor, backend, unique: backend, loops: backend }
200}
201
202/// Ensure `key` has no explicit entry in storage.
203pub fn kill(child_info: &ChildInfo, key: &[u8]) {
204	match child_info.child_type() {
205		ChildType::ParentKeyId => {
206			sp_io::default_child_storage::clear(child_info.storage_key(), key);
207		},
208	}
209}
210
211/// Get a Vec of bytes from storage.
212pub fn get_raw(child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
213	match child_info.child_type() {
214		ChildType::ParentKeyId => sp_io::default_child_storage::get(child_info.storage_key(), key),
215	}
216}
217
218/// Put a raw byte slice into storage.
219pub fn put_raw(child_info: &ChildInfo, key: &[u8], value: &[u8]) {
220	match child_info.child_type() {
221		ChildType::ParentKeyId => {
222			sp_io::default_child_storage::set(child_info.storage_key(), key, value)
223		},
224	}
225}
226
227/// Calculate current child root value.
228pub fn root(child_info: &ChildInfo, version: StateVersion) -> Vec<u8> {
229	match child_info.child_type() {
230		ChildType::ParentKeyId => {
231			sp_io::default_child_storage::root(child_info.storage_key(), version)
232		},
233	}
234}
235
236/// Return the length in bytes of the value without reading it. `None` if it does not exist.
237pub fn len(child_info: &ChildInfo, key: &[u8]) -> Option<u32> {
238	match child_info.child_type() {
239		ChildType::ParentKeyId => {
240			let mut buffer = [0; 0];
241			sp_io::default_child_storage::read(child_info.storage_key(), key, &mut buffer, 0)
242		},
243	}
244}