mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 23:01:01 +00:00
BlockId removal: refactor: StorageProvider (#12510)
* BlockId removal: refactor: StorageProvider It changes the arguments of `Backend::StorageProvider` trait from: block: `BlockId<Block>` to: hash: `&Block::Hash` This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292) * Apply suggestions from code review Co-authored-by: Bastian Köcher <git@kchr.de> * Update client/api/src/backend.rs Co-authored-by: Dmitrii Markin <dmitry@markin.tech> * GrandpaBlockImport::current_set_id reworked * ExportStateCmd reworked * trigger CI job * trigger CI job Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: Dmitrii Markin <dmitry@markin.tech>
This commit is contained in:
committed by
GitHub
parent
2f341fcf15
commit
b16135f602
@@ -19,25 +19,20 @@
|
||||
use crate::error::Error;
|
||||
use sc_client_api::{StorageProvider, UsageProvider};
|
||||
use sp_core::storage::{well_known_keys, ChildInfo, Storage, StorageChild, StorageKey, StorageMap};
|
||||
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
/// Export the raw state at the given `block`. If `block` is `None`, the
|
||||
/// best block will be used.
|
||||
pub fn export_raw_state<B, BA, C>(
|
||||
client: Arc<C>,
|
||||
block: Option<BlockId<B>>,
|
||||
) -> Result<Storage, Error>
|
||||
pub fn export_raw_state<B, BA, C>(client: Arc<C>, hash: &B::Hash) -> Result<Storage, Error>
|
||||
where
|
||||
C: UsageProvider<B> + StorageProvider<B, BA>,
|
||||
B: BlockT,
|
||||
BA: sc_client_api::backend::Backend<B>,
|
||||
{
|
||||
let block = block.unwrap_or_else(|| BlockId::Hash(client.usage_info().chain.best_hash));
|
||||
|
||||
let empty_key = StorageKey(Vec::new());
|
||||
let mut top_storage = client.storage_pairs(&block, &empty_key)?;
|
||||
let mut top_storage = client.storage_pairs(hash, &empty_key)?;
|
||||
let mut children_default = HashMap::new();
|
||||
|
||||
// Remove all default child storage roots from the top storage and collect the child storage
|
||||
@@ -52,10 +47,10 @@ where
|
||||
StorageKey(key.0[well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX.len()..].to_vec());
|
||||
let child_info = ChildInfo::new_default(&key.0);
|
||||
|
||||
let keys = client.child_storage_keys(&block, &child_info, &empty_key)?;
|
||||
let keys = client.child_storage_keys(hash, &child_info, &empty_key)?;
|
||||
let mut pairs = StorageMap::new();
|
||||
keys.into_iter().try_for_each(|k| {
|
||||
if let Some(value) = client.child_storage(&block, &child_info, &k)? {
|
||||
if let Some(value) = client.child_storage(hash, &child_info, &k)? {
|
||||
pairs.insert(k.0, value.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -420,7 +420,8 @@ where
|
||||
|
||||
/// Get the code at a given block.
|
||||
pub fn code_at(&self, id: &BlockId<Block>) -> sp_blockchain::Result<Vec<u8>> {
|
||||
Ok(StorageProvider::storage(self, id, &StorageKey(well_known_keys::CODE.to_vec()))?
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(id)?;
|
||||
Ok(StorageProvider::storage(self, &hash, &StorageKey(well_known_keys::CODE.to_vec()))?
|
||||
.expect(
|
||||
"None is returned if there's no value stored for the given key;\
|
||||
':code' key is always defined; qed",
|
||||
@@ -1402,21 +1403,19 @@ where
|
||||
{
|
||||
fn storage_keys(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &Block::Hash,
|
||||
key_prefix: &StorageKey,
|
||||
) -> sp_blockchain::Result<Vec<StorageKey>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
let keys = self.state_at(&hash)?.keys(&key_prefix.0).into_iter().map(StorageKey).collect();
|
||||
let keys = self.state_at(hash)?.keys(&key_prefix.0).into_iter().map(StorageKey).collect();
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
fn storage_pairs(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
key_prefix: &StorageKey,
|
||||
) -> sp_blockchain::Result<Vec<(StorageKey, StorageData)>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
let state = self.state_at(&hash)?;
|
||||
let state = self.state_at(hash)?;
|
||||
let keys = state
|
||||
.keys(&key_prefix.0)
|
||||
.into_iter()
|
||||
@@ -1430,37 +1429,34 @@ where
|
||||
|
||||
fn storage_keys_iter<'a>(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
prefix: Option<&'a StorageKey>,
|
||||
start_key: Option<&StorageKey>,
|
||||
) -> sp_blockchain::Result<KeyIterator<'a, B::State, Block>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
let state = self.state_at(&hash)?;
|
||||
let state = self.state_at(hash)?;
|
||||
let start_key = start_key.or(prefix).map(|key| key.0.clone()).unwrap_or_else(Vec::new);
|
||||
Ok(KeyIterator::new(state, prefix, start_key))
|
||||
}
|
||||
|
||||
fn child_storage_keys_iter<'a>(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
child_info: ChildInfo,
|
||||
prefix: Option<&'a StorageKey>,
|
||||
start_key: Option<&StorageKey>,
|
||||
) -> sp_blockchain::Result<KeyIterator<'a, B::State, Block>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
let state = self.state_at(&hash)?;
|
||||
let state = self.state_at(hash)?;
|
||||
let start_key = start_key.or(prefix).map(|key| key.0.clone()).unwrap_or_else(Vec::new);
|
||||
Ok(KeyIterator::new_child(state, child_info, prefix, start_key))
|
||||
}
|
||||
|
||||
fn storage(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &Block::Hash,
|
||||
key: &StorageKey,
|
||||
) -> sp_blockchain::Result<Option<StorageData>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
Ok(self
|
||||
.state_at(&hash)?
|
||||
.state_at(hash)?
|
||||
.storage(&key.0)
|
||||
.map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))?
|
||||
.map(StorageData))
|
||||
@@ -1468,24 +1464,22 @@ where
|
||||
|
||||
fn storage_hash(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
key: &StorageKey,
|
||||
) -> sp_blockchain::Result<Option<Block::Hash>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
self.state_at(&hash)?
|
||||
self.state_at(hash)?
|
||||
.storage_hash(&key.0)
|
||||
.map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))
|
||||
}
|
||||
|
||||
fn child_storage_keys(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
child_info: &ChildInfo,
|
||||
key_prefix: &StorageKey,
|
||||
) -> sp_blockchain::Result<Vec<StorageKey>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
let keys = self
|
||||
.state_at(&hash)?
|
||||
.state_at(hash)?
|
||||
.child_keys(child_info, &key_prefix.0)
|
||||
.into_iter()
|
||||
.map(StorageKey)
|
||||
@@ -1495,13 +1489,12 @@ where
|
||||
|
||||
fn child_storage(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
child_info: &ChildInfo,
|
||||
key: &StorageKey,
|
||||
) -> sp_blockchain::Result<Option<StorageData>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
Ok(self
|
||||
.state_at(&hash)?
|
||||
.state_at(hash)?
|
||||
.child_storage(child_info, &key.0)
|
||||
.map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))?
|
||||
.map(StorageData))
|
||||
@@ -1509,12 +1502,11 @@ where
|
||||
|
||||
fn child_storage_hash(
|
||||
&self,
|
||||
id: &BlockId<Block>,
|
||||
hash: &<Block as BlockT>::Hash,
|
||||
child_info: &ChildInfo,
|
||||
key: &StorageKey,
|
||||
) -> sp_blockchain::Result<Option<Block::Hash>> {
|
||||
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
|
||||
self.state_at(&hash)?
|
||||
self.state_at(hash)?
|
||||
.child_storage_hash(child_info, &key.0)
|
||||
.map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))
|
||||
}
|
||||
|
||||
@@ -1602,12 +1602,14 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
.add_extra_child_storage(&child_info, b"third".to_vec(), vec![0u8; 32])
|
||||
.build();
|
||||
|
||||
let block_hash = client.info().best_hash;
|
||||
|
||||
let child_root = b":child_storage:default:child".to_vec();
|
||||
let prefix = StorageKey(array_bytes::hex2bytes_unchecked("3a"));
|
||||
let child_prefix = StorageKey(b"sec".to_vec());
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(&BlockId::Number(0), Some(&prefix), None)
|
||||
.storage_keys_iter(&block_hash, Some(&prefix), None)
|
||||
.unwrap()
|
||||
.map(|x| x.0)
|
||||
.collect();
|
||||
@@ -1622,7 +1624,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
&block_hash,
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
|
||||
)
|
||||
@@ -1633,7 +1635,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
&block_hash,
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a686561707061676573"))),
|
||||
)
|
||||
@@ -1643,7 +1645,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
assert_eq!(res, Vec::<Vec<u8>>::new());
|
||||
|
||||
let res: Vec<_> = client
|
||||
.child_storage_keys_iter(&BlockId::Number(0), child_info.clone(), Some(&child_prefix), None)
|
||||
.child_storage_keys_iter(&block_hash, child_info.clone(), Some(&child_prefix), None)
|
||||
.unwrap()
|
||||
.map(|x| x.0)
|
||||
.collect();
|
||||
@@ -1651,7 +1653,7 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
|
||||
let res: Vec<_> = client
|
||||
.child_storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
&block_hash,
|
||||
child_info,
|
||||
None,
|
||||
Some(&StorageKey(b"second".to_vec())),
|
||||
@@ -1666,10 +1668,12 @@ fn storage_keys_iter_prefix_and_start_key_works() {
|
||||
fn storage_keys_iter_works() {
|
||||
let client = substrate_test_runtime_client::new();
|
||||
|
||||
let block_hash = client.info().best_hash;
|
||||
|
||||
let prefix = StorageKey(array_bytes::hex2bytes_unchecked(""));
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(&BlockId::Number(0), Some(&prefix), None)
|
||||
.storage_keys_iter(&block_hash, Some(&prefix), None)
|
||||
.unwrap()
|
||||
.take(9)
|
||||
.map(|x| array_bytes::bytes2hex("", &x.0))
|
||||
@@ -1691,7 +1695,7 @@ fn storage_keys_iter_works() {
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
&block_hash,
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked("3a636f6465"))),
|
||||
)
|
||||
@@ -1714,7 +1718,7 @@ fn storage_keys_iter_works() {
|
||||
|
||||
let res: Vec<_> = client
|
||||
.storage_keys_iter(
|
||||
&BlockId::Number(0),
|
||||
&block_hash,
|
||||
Some(&prefix),
|
||||
Some(&StorageKey(array_bytes::hex2bytes_unchecked(
|
||||
"7d5007603a7f5dd729d51d93cf695d6465789443bb967c0d1fe270e388c96eaa",
|
||||
|
||||
Reference in New Issue
Block a user