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/// Remove all `storage_key` key/values
121///
122/// Deletes all keys from the overlay and up to `limit` keys from the backend if
123/// it is set to `Some`. No limit is applied when `limit` is set to `None`.
124///
125/// The limit can be used to partially delete a child trie in case it is too large
126/// to delete in one go (block).
127///
128/// # Note
129///
130/// Please note that keys that are residing in the overlay for that child trie when
131/// issuing this call are all deleted without counting towards the `limit`. Only keys
132/// written during the current block are part of the overlay. Deleting with a `limit`
133/// mostly makes sense with an empty overlay for that child trie.
134///
135/// Calling this function multiple times per block for the same `storage_key` does
136/// not make much sense because it is not cumulative when called inside the same block.
137/// Use this function to distribute the deletion of a single child trie across multiple
138/// blocks.
139#[deprecated = "Use `clear_storage` instead"]
140pub fn kill_storage(child_info: &ChildInfo, limit: Option<u32>) -> KillStorageResult {
141 match child_info.child_type() {
142 ChildType::ParentKeyId =>
143 sp_io::default_child_storage::storage_kill(child_info.storage_key(), limit),
144 }
145}
146
147/// Partially clear the child storage of each key-value pair.
148///
149/// # Limit
150///
151/// A *limit* should always be provided through `maybe_limit`. This is one fewer than the
152/// maximum number of backend iterations which may be done by this operation and as such
153/// represents the maximum number of backend deletions which may happen. A *limit* of zero
154/// implies that no keys will be deleted, though there may be a single iteration done.
155///
156/// The limit can be used to partially delete storage items in case it is too large or costly
157/// to delete all in a single operation.
158///
159/// # Cursor
160///
161/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
162/// passed once (in the initial call) for any attempt to clear storage. In general, subsequent calls
163/// operating on the same prefix should pass `Some` and this value should be equal to the
164/// previous call result's `maybe_cursor` field. The only exception to this is when you can
165/// guarantee that the subsequent call is in a new block; in this case the previous call's result
166/// cursor need not be passed in and a `None` may be passed instead. This exception may be useful
167/// then making this call solely from a block-hook such as `on_initialize`.
168
169/// Returns [`MultiRemovalResults`] to inform about the result. Once the resultant `maybe_cursor`
170/// field is `None`, then no further items remain to be deleted.
171///
172/// NOTE: After the initial call for any given child storage, it is important that no keys further
173/// keys are inserted. If so, then they may or may not be deleted by subsequent calls.
174///
175/// # Note
176///
177/// Please note that keys which are residing in the overlay for the child are deleted without
178/// counting towards the `limit`.
179pub fn clear_storage(
180 child_info: &ChildInfo,
181 maybe_limit: Option<u32>,
182 _maybe_cursor: Option<&[u8]>,
183) -> MultiRemovalResults {
184 // TODO: Once the network has upgraded to include the new host functions, this code can be
185 // enabled.
186 // sp_io::default_child_storage::storage_kill(prefix, maybe_limit, maybe_cursor)
187 let r = match child_info.child_type() {
188 ChildType::ParentKeyId =>
189 sp_io::default_child_storage::storage_kill(child_info.storage_key(), maybe_limit),
190 };
191 use sp_io::KillStorageResult::*;
192 let (maybe_cursor, backend) = match r {
193 AllRemoved(db) => (None, db),
194 SomeRemaining(db) => (Some(child_info.storage_key().to_vec()), db),
195 };
196 MultiRemovalResults { maybe_cursor, backend, unique: backend, loops: backend }
197}
198
199/// Ensure `key` has no explicit entry in storage.
200pub fn kill(child_info: &ChildInfo, key: &[u8]) {
201 match child_info.child_type() {
202 ChildType::ParentKeyId => {
203 sp_io::default_child_storage::clear(child_info.storage_key(), key);
204 },
205 }
206}
207
208/// Get a Vec of bytes from storage.
209pub fn get_raw(child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
210 match child_info.child_type() {
211 ChildType::ParentKeyId => sp_io::default_child_storage::get(child_info.storage_key(), key),
212 }
213}
214
215/// Put a raw byte slice into storage.
216pub fn put_raw(child_info: &ChildInfo, key: &[u8], value: &[u8]) {
217 match child_info.child_type() {
218 ChildType::ParentKeyId =>
219 sp_io::default_child_storage::set(child_info.storage_key(), key, value),
220 }
221}
222
223/// Calculate current child root value.
224pub fn root(child_info: &ChildInfo, version: StateVersion) -> Vec<u8> {
225 match child_info.child_type() {
226 ChildType::ParentKeyId =>
227 sp_io::default_child_storage::root(child_info.storage_key(), version),
228 }
229}
230
231/// Return the length in bytes of the value without reading it. `None` if it does not exist.
232pub fn len(child_info: &ChildInfo, key: &[u8]) -> Option<u32> {
233 match child_info.child_type() {
234 ChildType::ParentKeyId => {
235 let mut buffer = [0; 0];
236 sp_io::default_child_storage::read(child_info.storage_key(), key, &mut buffer, 0)
237 },
238 }
239}