1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
pub use crate::sp_io::KillStorageResult;
use crate::sp_std::prelude::*;
use codec::{Codec, Decode, Encode};
pub use sp_core::storage::{ChildInfo, ChildType};
pub fn get<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
match child_info.child_type() {
ChildType::ParentKeyId => {
let storage_key = child_info.storage_key();
sp_io::default_child_storage::get(storage_key, key).and_then(|v| {
Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
crate::runtime_print!(
"ERROR: Corrupted state in child trie at {:?}/{:?}",
storage_key,
key,
);
None
})
})
},
}
}
pub fn get_or_default<T: Decode + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
get(child_info, key).unwrap_or_else(Default::default)
}
pub fn get_or<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
get(child_info, key).unwrap_or(default_value)
}
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
get(child_info, key).unwrap_or_else(default_value)
}
pub fn put<T: Encode>(child_info: &ChildInfo, key: &[u8], value: &T) {
match child_info.child_type() {
ChildType::ParentKeyId => value.using_encoded(|slice| {
sp_io::default_child_storage::set(child_info.storage_key(), key, slice)
}),
}
}
pub fn take<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
let r = get(child_info, key);
if r.is_some() {
kill(child_info, key);
}
r
}
pub fn take_or_default<T: Codec + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
take(child_info, key).unwrap_or_else(Default::default)
}
pub fn take_or<T: Codec + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
take(child_info, key).unwrap_or(default_value)
}
pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
take(child_info, key).unwrap_or_else(default_value)
}
pub fn exists(child_info: &ChildInfo, key: &[u8]) -> bool {
match child_info.child_type() {
ChildType::ParentKeyId =>
sp_io::default_child_storage::read(child_info.storage_key(), key, &mut [0; 0][..], 0)
.is_some(),
}
}
pub fn kill_storage(child_info: &ChildInfo, limit: Option<u32>) -> KillStorageResult {
match child_info.child_type() {
ChildType::ParentKeyId =>
sp_io::default_child_storage::storage_kill(child_info.storage_key(), limit),
}
}
pub fn kill(child_info: &ChildInfo, key: &[u8]) {
match child_info.child_type() {
ChildType::ParentKeyId => {
sp_io::default_child_storage::clear(child_info.storage_key(), key);
},
}
}
pub fn get_raw(child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::get(child_info.storage_key(), key),
}
}
pub fn put_raw(child_info: &ChildInfo, key: &[u8], value: &[u8]) {
match child_info.child_type() {
ChildType::ParentKeyId =>
sp_io::default_child_storage::set(child_info.storage_key(), key, value),
}
}
pub fn root(child_info: &ChildInfo) -> Vec<u8> {
match child_info.child_type() {
ChildType::ParentKeyId => sp_io::default_child_storage::root(child_info.storage_key()),
}
}
pub fn len(child_info: &ChildInfo, key: &[u8]) -> Option<u32> {
match child_info.child_type() {
ChildType::ParentKeyId => {
let mut buffer = [0; 0];
sp_io::default_child_storage::read(child_info.storage_key(), key, &mut buffer, 0)
},
}
}