Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
chainHead/storage: Implement queries for hashes of values
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Jul 13, 2023
1 parent 9379e3c commit 4c57700
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/rpc-spec-v2/src/chain_head/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ where
let items = items
.into_iter()
.map(|query| {
if query.ty != StorageQueryType::Value {
if query.ty != StorageQueryType::Value && query.ty != StorageQueryType::Hash {
// Note: remove this once all types are implemented.
let _ = sink.reject(ChainHeadRpcError::InvalidParam(
"Storage query type not supported".into(),
Expand Down
30 changes: 30 additions & 0 deletions client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ where
})
}

/// Fetch the hash of a value from storage.
fn query_storage_hash(
&self,
hash: Block::Hash,
key: &StorageKey,
child_key: Option<&ChildInfo>,
) -> Option<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| {
opt.map(|storage_data| {
QueryResult::Buffered(StorageResult::<String> {
key: format!("0x{:?}", HexDisplay::from(&key.0)),
result: StorageResultType::Hash(format!("{:?}", storage_data)),
})
})
})
.unwrap_or_else(|err| {
Some(QueryResult::Immediate(ChainHeadStorageEvent::<String>::Error(ErrorEvent {
error: err.to_string(),
})))
})
}

/// Make the storage query.
fn query_storage(
&self,
Expand All @@ -116,6 +145,7 @@ where

match query.ty {
StorageQueryType::Value => self.query_storage_value(hash, &query.key, child_key),
StorageQueryType::Hash => self.query_storage_hash(hash, &query.key, child_key),
_ => None,
}
}
Expand Down

0 comments on commit 4c57700

Please sign in to comment.