use std::{marker::PhantomData, sync::Arc};
use sc_client_api::{Backend, ChildInfo, StorageKey, StorageProvider};
use sp_runtime::traits::Block as BlockT;
use super::events::{StorageResult, StorageResultType};
use crate::hex_string;
pub struct Storage<Client, Block, BE> {
client: Arc<Client>,
_phandom: PhantomData<(BE, Block)>,
}
impl<Client, Block, BE> Storage<Client, Block, BE> {
pub fn new(client: Arc<Client>) -> Self {
Self { client, _phandom: PhantomData }
}
}
pub struct QueryIter {
pub query_key: StorageKey,
pub pagination_start_key: Option<StorageKey>,
pub ty: IterQueryType,
}
pub enum IterQueryType {
Value,
Hash,
}
pub type QueryResult = Result<Option<StorageResult>, String>;
pub type QueryIterResult = Result<(Vec<StorageResult>, Option<QueryIter>), String>;
impl<Client, Block, BE> Storage<Client, Block, BE>
where
Block: BlockT + 'static,
BE: Backend<Block> + 'static,
Client: StorageProvider<Block, BE> + 'static,
{
pub fn query_value(
&self,
hash: Block::Hash,
key: &StorageKey,
child_key: Option<&ChildInfo>,
) -> QueryResult {
let result = if let Some(child_key) = child_key {
self.client.child_storage(hash, child_key, key)
} else {
self.client.storage(hash, key)
};
result
.map(|opt| {
QueryResult::Ok(opt.map(|storage_data| StorageResult {
key: hex_string(&key.0),
result: StorageResultType::Value(hex_string(&storage_data.0)),
}))
})
.unwrap_or_else(|error| QueryResult::Err(error.to_string()))
}
pub fn query_hash(
&self,
hash: Block::Hash,
key: &StorageKey,
child_key: Option<&ChildInfo>,
) -> QueryResult {
let result = if let Some(child_key) = child_key {
self.client.child_storage_hash(hash, child_key, key)
} else {
self.client.storage_hash(hash, key)
};
result
.map(|opt| {
QueryResult::Ok(opt.map(|storage_data| StorageResult {
key: hex_string(&key.0),
result: StorageResultType::Hash(hex_string(&storage_data.as_ref())),
}))
})
.unwrap_or_else(|error| QueryResult::Err(error.to_string()))
}
pub fn query_merkle_value(
&self,
hash: Block::Hash,
key: &StorageKey,
child_key: Option<&ChildInfo>,
) -> QueryResult {
let result = if let Some(child_key) = child_key {
self.client.child_closest_merkle_value(hash, child_key, key)
} else {
self.client.closest_merkle_value(hash, key)
};
result
.map(|opt| {
QueryResult::Ok(opt.map(|storage_data| {
let result = match &storage_data {
sc_client_api::MerkleValue::Node(data) => hex_string(&data.as_slice()),
sc_client_api::MerkleValue::Hash(hash) => hex_string(&hash.as_ref()),
};
StorageResult {
key: hex_string(&key.0),
result: StorageResultType::ClosestDescendantMerkleValue(result),
}
}))
})
.unwrap_or_else(|error| QueryResult::Err(error.to_string()))
}
pub fn query_iter_pagination(
&self,
query: QueryIter,
hash: Block::Hash,
child_key: Option<&ChildInfo>,
count: usize,
) -> QueryIterResult {
let QueryIter { ty, query_key, pagination_start_key } = query;
let mut keys_iter = if let Some(child_key) = child_key {
self.client.child_storage_keys(
hash,
child_key.to_owned(),
Some(&query_key),
pagination_start_key.as_ref(),
)
} else {
self.client.storage_keys(hash, Some(&query_key), pagination_start_key.as_ref())
}
.map_err(|err| err.to_string())?;
let mut ret = Vec::with_capacity(count);
let mut next_pagination_key = None;
for _ in 0..count {
let Some(key) = keys_iter.next() else { break };
next_pagination_key = Some(key.clone());
let result = match ty {
IterQueryType::Value => self.query_value(hash, &key, child_key),
IterQueryType::Hash => self.query_hash(hash, &key, child_key),
}?;
if let Some(value) = result {
ret.push(value);
}
}
let maybe_next_query = keys_iter.next().map(|_| QueryIter {
ty,
query_key,
pagination_start_key: next_pagination_key,
});
Ok((ret, maybe_next_query))
}
}