referrerpolicy=no-referrer-when-downgrade

sc_service/chain_ops/
export_raw_state.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::error::Error;
20use sc_client_api::{StorageProvider, UsageProvider};
21use sp_core::storage::{well_known_keys, ChildInfo, Storage, StorageChild, StorageKey, StorageMap};
22use sp_runtime::traits::Block as BlockT;
23
24use std::{
25	collections::{BTreeMap, HashMap},
26	sync::Arc,
27};
28
29/// Export the raw state at the given `block`. If `block` is `None`, the
30/// best block will be used.
31pub fn export_raw_state<B, BA, C>(client: Arc<C>, hash: B::Hash) -> Result<Storage, Error>
32where
33	C: UsageProvider<B> + StorageProvider<B, BA>,
34	B: BlockT,
35	BA: sc_client_api::backend::Backend<B>,
36{
37	let mut top = BTreeMap::new();
38	let mut children_default = HashMap::new();
39
40	for (key, value) in client.storage_pairs(hash, None, None)? {
41		// Remove all default child storage roots from the top storage and collect the child storage
42		// pairs.
43		if key.0.starts_with(well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX) {
44			let child_root_key = StorageKey(
45				key.0[well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX.len()..].to_vec(),
46			);
47			let child_info = ChildInfo::new_default(&child_root_key.0);
48			let mut pairs = StorageMap::new();
49			for child_key in client.child_storage_keys(hash, child_info.clone(), None, None)? {
50				if let Some(child_value) = client.child_storage(hash, &child_info, &child_key)? {
51					pairs.insert(child_key.0, child_value.0);
52				}
53			}
54
55			children_default.insert(child_root_key.0, StorageChild { child_info, data: pairs });
56			continue
57		}
58
59		top.insert(key.0, value.0);
60	}
61
62	Ok(Storage { top, children_default })
63}