sp_state_machine/
read_only.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//! Read-only version of Externalities.
19
20use crate::{Backend, StorageKey, StorageValue};
21use codec::Encode;
22use hash_db::Hasher;
23use sp_core::{
24	storage::{ChildInfo, StateVersion, TrackedStorageKey},
25	traits::Externalities,
26};
27use sp_externalities::MultiRemovalResults;
28use std::{
29	any::{Any, TypeId},
30	marker::PhantomData,
31};
32
33/// Trait for inspecting state in any backend.
34///
35/// Implemented for any backend.
36pub trait InspectState<H: Hasher, B: Backend<H>> {
37	/// Inspect state with a closure.
38	///
39	/// Self will be set as read-only externalities and inspection
40	/// closure will be run against it.
41	///
42	/// Returns the result of the closure.
43	fn inspect_state<F: FnOnce() -> R, R>(&self, f: F) -> R;
44}
45
46impl<H: Hasher, B: Backend<H>> InspectState<H, B> for B
47where
48	H::Out: Encode,
49{
50	fn inspect_state<F: FnOnce() -> R, R>(&self, f: F) -> R {
51		ReadOnlyExternalities::from(self).execute_with(f)
52	}
53}
54
55/// Simple read-only externalities for any backend.
56///
57/// To be used in test for state inspection. Will panic if something writes
58/// to the storage.
59#[derive(Debug)]
60pub struct ReadOnlyExternalities<'a, H: Hasher, B: 'a + Backend<H>> {
61	backend: &'a B,
62	_phantom: PhantomData<H>,
63}
64
65impl<'a, H: Hasher, B: 'a + Backend<H>> From<&'a B> for ReadOnlyExternalities<'a, H, B> {
66	fn from(backend: &'a B) -> Self {
67		ReadOnlyExternalities { backend, _phantom: PhantomData }
68	}
69}
70
71impl<'a, H: Hasher, B: 'a + Backend<H>> ReadOnlyExternalities<'a, H, B>
72where
73	H::Out: Encode,
74{
75	/// Execute the given closure while `self` is set as externalities.
76	///
77	/// Returns the result of the given closure.
78	pub fn execute_with<R>(&mut self, f: impl FnOnce() -> R) -> R {
79		sp_externalities::set_and_run_with_externalities(self, f)
80	}
81}
82
83impl<'a, H: Hasher, B: 'a + Backend<H>> Externalities for ReadOnlyExternalities<'a, H, B>
84where
85	H::Out: Encode,
86{
87	fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {
88		panic!("Should not be used in read-only externalities!")
89	}
90
91	fn storage(&mut self, key: &[u8]) -> Option<StorageValue> {
92		self.backend
93			.storage(key)
94			.expect("Backed failed for storage in ReadOnlyExternalities")
95	}
96
97	fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>> {
98		self.backend
99			.storage_hash(key)
100			.expect("Backed failed for storage_hash in ReadOnlyExternalities")
101			.map(|h| h.encode())
102	}
103
104	fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
105		self.backend
106			.child_storage(child_info, key)
107			.expect("Backed failed for child_storage in ReadOnlyExternalities")
108	}
109
110	fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
111		self.backend
112			.child_storage_hash(child_info, key)
113			.expect("Backed failed for child_storage_hash in ReadOnlyExternalities")
114			.map(|h| h.encode())
115	}
116
117	fn next_storage_key(&mut self, key: &[u8]) -> Option<StorageKey> {
118		self.backend
119			.next_storage_key(key)
120			.expect("Backed failed for next_storage_key in ReadOnlyExternalities")
121	}
122
123	fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
124		self.backend
125			.next_child_storage_key(child_info, key)
126			.expect("Backed failed for next_child_storage_key in ReadOnlyExternalities")
127	}
128
129	fn place_storage(&mut self, _key: StorageKey, _maybe_value: Option<StorageValue>) {
130		unimplemented!("place_storage not supported in ReadOnlyExternalities")
131	}
132
133	fn place_child_storage(
134		&mut self,
135		_child_info: &ChildInfo,
136		_key: StorageKey,
137		_value: Option<StorageValue>,
138	) {
139		unimplemented!("place_child_storage not supported in ReadOnlyExternalities")
140	}
141
142	fn kill_child_storage(
143		&mut self,
144		_child_info: &ChildInfo,
145		_maybe_limit: Option<u32>,
146		_maybe_cursor: Option<&[u8]>,
147	) -> MultiRemovalResults {
148		unimplemented!("kill_child_storage is not supported in ReadOnlyExternalities")
149	}
150
151	fn clear_prefix(
152		&mut self,
153		_prefix: &[u8],
154		_maybe_limit: Option<u32>,
155		_maybe_cursor: Option<&[u8]>,
156	) -> MultiRemovalResults {
157		unimplemented!("clear_prefix is not supported in ReadOnlyExternalities")
158	}
159
160	fn clear_child_prefix(
161		&mut self,
162		_child_info: &ChildInfo,
163		_prefix: &[u8],
164		_maybe_limit: Option<u32>,
165		_maybe_cursor: Option<&[u8]>,
166	) -> MultiRemovalResults {
167		unimplemented!("clear_child_prefix is not supported in ReadOnlyExternalities")
168	}
169
170	fn storage_append(&mut self, _key: Vec<u8>, _value: Vec<u8>) {
171		unimplemented!("storage_append is not supported in ReadOnlyExternalities")
172	}
173
174	fn storage_root(&mut self, _state_version: StateVersion) -> Vec<u8> {
175		unimplemented!("storage_root is not supported in ReadOnlyExternalities")
176	}
177
178	fn child_storage_root(
179		&mut self,
180		_child_info: &ChildInfo,
181		_state_version: StateVersion,
182	) -> Vec<u8> {
183		unimplemented!("child_storage_root is not supported in ReadOnlyExternalities")
184	}
185
186	fn storage_start_transaction(&mut self) {
187		unimplemented!("Transactions are not supported by ReadOnlyExternalities");
188	}
189
190	fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
191		unimplemented!("Transactions are not supported by ReadOnlyExternalities");
192	}
193
194	fn storage_commit_transaction(&mut self) -> Result<(), ()> {
195		unimplemented!("Transactions are not supported by ReadOnlyExternalities");
196	}
197
198	fn wipe(&mut self) {}
199
200	fn commit(&mut self) {}
201
202	fn read_write_count(&self) -> (u32, u32, u32, u32) {
203		unimplemented!("read_write_count is not supported in ReadOnlyExternalities")
204	}
205
206	fn reset_read_write_count(&mut self) {
207		unimplemented!("reset_read_write_count is not supported in ReadOnlyExternalities")
208	}
209
210	fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
211		unimplemented!("get_whitelist is not supported in ReadOnlyExternalities")
212	}
213
214	fn set_whitelist(&mut self, _: Vec<TrackedStorageKey>) {
215		unimplemented!("set_whitelist is not supported in ReadOnlyExternalities")
216	}
217
218	fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
219		unimplemented!("get_read_and_written_keys is not supported in ReadOnlyExternalities")
220	}
221}
222
223impl<'a, H: Hasher, B: 'a + Backend<H>> sp_externalities::ExtensionStore
224	for ReadOnlyExternalities<'a, H, B>
225{
226	fn extension_by_type_id(&mut self, _type_id: TypeId) -> Option<&mut dyn Any> {
227		unimplemented!("extension_by_type_id is not supported in ReadOnlyExternalities")
228	}
229
230	fn register_extension_with_type_id(
231		&mut self,
232		_type_id: TypeId,
233		_extension: Box<dyn sp_externalities::Extension>,
234	) -> Result<(), sp_externalities::Error> {
235		unimplemented!("register_extension_with_type_id is not supported in ReadOnlyExternalities")
236	}
237
238	fn deregister_extension_by_type_id(
239		&mut self,
240		_type_id: TypeId,
241	) -> Result<(), sp_externalities::Error> {
242		unimplemented!("deregister_extension_by_type_id is not supported in ReadOnlyExternalities")
243	}
244}