Adds export-state subcommand (#5842)

* Export state cli

* More work

* Fix tests

* Make it work

* Fix compilation

* Apply suggestions from code review
This commit is contained in:
Bastian Köcher
2020-04-30 15:44:40 +02:00
committed by GitHub
parent 929bd07bef
commit 5b8d3607e9
12 changed files with 329 additions and 86 deletions
+45 -3
View File
@@ -33,13 +33,14 @@ use sp_consensus::{
import_queue::{IncomingBlock, Link, BlockImportError, BlockImportResult, ImportQueue},
};
use sc_executor::{NativeExecutor, NativeExecutionDispatch};
use sp_core::storage::{StorageKey, well_known_keys, ChildInfo, Storage, StorageChild, StorageMap};
use sc_client_api::{StorageProvider, BlockBackend, UsageProvider};
use std::{io::{Read, Write, Seek}, pin::Pin};
use sc_client_api::BlockBackend;
use std::{io::{Read, Write, Seek}, pin::Pin, collections::HashMap};
/// Build a chain spec json
pub fn build_spec(spec: &dyn ChainSpec, raw: bool) -> error::Result<String> {
Ok(spec.as_json(raw)?)
spec.as_json(raw).map_err(Into::into)
}
impl<
@@ -298,4 +299,45 @@ impl<
Err(e) => Box::pin(future::err(format!("Error reading block: {:?}", e).into())),
}
}
fn export_raw_state(
&self,
block: Option<BlockId<Self::Block>>,
) -> Result<Storage, Error> {
let block = block.unwrap_or_else(
|| BlockId::Hash(self.client.usage_info().chain.best_hash)
);
let empty_key = StorageKey(Vec::new());
let mut top_storage = self.client.storage_pairs(&block, &empty_key)?;
let mut children_default = HashMap::new();
// Remove all default child storage roots from the top storage and collect the child storage
// pairs.
while let Some(pos) = top_storage
.iter()
.position(|(k, _)| k.0.starts_with(well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX)) {
let (key, _) = top_storage.swap_remove(pos);
let key = StorageKey(
key.0[well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX.len()..].to_vec(),
);
let child_info = ChildInfo::new_default(&key.0);
let keys = self.client.child_storage_keys(&block, &child_info, &empty_key)?;
let mut pairs = StorageMap::new();
keys.into_iter().try_for_each(|k| {
if let Some(value) = self.client.child_storage(&block, &child_info, &k)? {
pairs.insert(k.0, value.0);
}
Ok::<_, Error>(())
})?;
children_default.insert(key.0, StorageChild { child_info, data: pairs });
}
let top = top_storage.into_iter().map(|(k, v)| (k.0, v.0)).collect();
Ok(Storage { top, children_default })
}
}