forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
archive: Implement archive_unstable_storage (paritytech#1846)
This PR implements the `archive_unstable_storage` method that offers support for: - fetching values - fetching hashes - iterating over keys and values - iterating over keys and hashes - fetching merkle values from the trie-db A common component dedicated to RPC-V2 storage queries is created to bridge the gap between `chainHead/storage` and `archive/storage`. Query pagination is supported by `paginationStartKey`, similar to the old APIs. Similarly to the `chainHead/storage`, the `archive/storage` method accepts a maximum number of queried items. The design builds upon: paritytech/json-rpc-interface-spec#94. Closes paritytech#1512. cc @paritytech/subxt-team --------- Signed-off-by: Alexandru Vasile <[email protected]> Co-authored-by: Niklas Adolfsson <[email protected]>
- Loading branch information
Showing
15 changed files
with
1,278 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
substrate/client/rpc-spec-v2/src/archive/archive_storage.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Implementation of the `archive_storage` method. | ||
use std::sync::Arc; | ||
|
||
use sc_client_api::{Backend, ChildInfo, StorageKey, StorageProvider}; | ||
use sp_runtime::traits::Block as BlockT; | ||
|
||
use crate::common::{ | ||
events::{ArchiveStorageResult, PaginatedStorageQuery, StorageQueryType}, | ||
storage::{IterQueryType, QueryIter, Storage}, | ||
}; | ||
|
||
/// Generates the events of the `chainHead_storage` method. | ||
pub struct ArchiveStorage<Client, Block, BE> { | ||
/// Storage client. | ||
client: Storage<Client, Block, BE>, | ||
/// The maximum number of reported items by the `archive_storage` at a time. | ||
storage_max_reported_items: usize, | ||
/// The maximum number of queried items allowed for the `archive_storage` at a time. | ||
storage_max_queried_items: usize, | ||
} | ||
|
||
impl<Client, Block, BE> ArchiveStorage<Client, Block, BE> { | ||
/// Constructs a new [`ArchiveStorage`]. | ||
pub fn new( | ||
client: Arc<Client>, | ||
storage_max_reported_items: usize, | ||
storage_max_queried_items: usize, | ||
) -> Self { | ||
Self { client: Storage::new(client), storage_max_reported_items, storage_max_queried_items } | ||
} | ||
} | ||
|
||
impl<Client, Block, BE> ArchiveStorage<Client, Block, BE> | ||
where | ||
Block: BlockT + 'static, | ||
BE: Backend<Block> + 'static, | ||
Client: StorageProvider<Block, BE> + 'static, | ||
{ | ||
/// Generate the response of the `archive_storage` method. | ||
pub fn handle_query( | ||
&self, | ||
hash: Block::Hash, | ||
mut items: Vec<PaginatedStorageQuery<StorageKey>>, | ||
child_key: Option<ChildInfo>, | ||
) -> ArchiveStorageResult { | ||
let discarded_items = items.len().saturating_sub(self.storage_max_queried_items); | ||
items.truncate(self.storage_max_queried_items); | ||
|
||
let mut storage_results = Vec::with_capacity(items.len()); | ||
for item in items { | ||
match item.query_type { | ||
StorageQueryType::Value => { | ||
match self.client.query_value(hash, &item.key, child_key.as_ref()) { | ||
Ok(Some(value)) => storage_results.push(value), | ||
Ok(None) => continue, | ||
Err(error) => return ArchiveStorageResult::err(error), | ||
} | ||
}, | ||
StorageQueryType::Hash => | ||
match self.client.query_hash(hash, &item.key, child_key.as_ref()) { | ||
Ok(Some(value)) => storage_results.push(value), | ||
Ok(None) => continue, | ||
Err(error) => return ArchiveStorageResult::err(error), | ||
}, | ||
StorageQueryType::ClosestDescendantMerkleValue => | ||
match self.client.query_merkle_value(hash, &item.key, child_key.as_ref()) { | ||
Ok(Some(value)) => storage_results.push(value), | ||
Ok(None) => continue, | ||
Err(error) => return ArchiveStorageResult::err(error), | ||
}, | ||
StorageQueryType::DescendantsValues => { | ||
match self.client.query_iter_pagination( | ||
QueryIter { | ||
query_key: item.key, | ||
ty: IterQueryType::Value, | ||
pagination_start_key: item.pagination_start_key, | ||
}, | ||
hash, | ||
child_key.as_ref(), | ||
self.storage_max_reported_items, | ||
) { | ||
Ok((results, _)) => storage_results.extend(results), | ||
Err(error) => return ArchiveStorageResult::err(error), | ||
} | ||
}, | ||
StorageQueryType::DescendantsHashes => { | ||
match self.client.query_iter_pagination( | ||
QueryIter { | ||
query_key: item.key, | ||
ty: IterQueryType::Hash, | ||
pagination_start_key: item.pagination_start_key, | ||
}, | ||
hash, | ||
child_key.as_ref(), | ||
self.storage_max_reported_items, | ||
) { | ||
Ok((results, _)) => storage_results.extend(results), | ||
Err(error) => return ArchiveStorageResult::err(error), | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
ArchiveStorageResult::ok(storage_results, discarded_items) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
#[cfg(test)] | ||
mod tests; | ||
|
||
mod archive_storage; | ||
|
||
pub mod api; | ||
pub mod archive; | ||
pub mod error; | ||
|
Oops, something went wrong.