-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
archive: Implement archive_unstable_storage #1846
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7fe61dd
chainHead/storage: Fix typo
lexnv e8d3d8a
rpc-v2: Move hex_string to common
lexnv 56e03db
rpc-v2: Introduce common module for storage queries and events
lexnv 316f116
chainHead/storage: Use common storage object for queries
lexnv c5886c0
rpc-v2: Rename storage params for pagination
lexnv 077f2f5
common/event: Add paginated query type
lexnv cf050f7
archive/storage: Implement archive_storage
lexnv e4de233
archive/storage: Add configurable pagination support
lexnv 044941e
archive/tests: Check hashes and values
lexnv 1cde37d
archive/tests: Test merkle value returns the expected hashes
lexnv d560bb7
archive/tests: Check pagination support produces variable items
lexnv f29b761
archive/tests: Check hash and value queries
lexnv 5914660
archive: Add maximum number of queried items
lexnv 038eb41
Merge branch 'master' into lexnv/archive_storage
lexnv 90f5e9b
Merge remote-tracking branch 'origin/master' into lexnv/archive_storage
lexnv f4f1b23
archive/tests: Port tests to BlockBuilder
lexnv 1de7eb2
spec-v2: Remove the concept of non-queriable keys
lexnv dd70064
Fix build
lexnv 8ad9513
storage: Remove unused imports
lexnv 2be9f52
Merge branch 'master' into lexnv/archive_storage
lexnv 2134bf4
Merge branch 'master' into lexnv/archive_storage
lexnv 553f445
Merge remote-tracking branch 'origin/master' into lexnv/archive_storage
lexnv 6f3920e
rpc-v2/events: Add `StorageQueryType::is_descendant_query` method
lexnv 0580429
rpc-v2/common: Add `ok` and `err` methods for `ArchiveStorageResult`
lexnv 3cf59a8
rpc-v2/archive: Apply cargo fmt
lexnv 899fa7a
Update substrate/client/rpc-spec-v2/src/lib.rs
lexnv 688e3fb
archive: Remove unused imports
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
self.storage_max_queried_items
part of the spec? What is the value we use in production for this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can understand the spec, there's no explicit mention of an upper limit before the pagination kicks in. This is by design, since smoldot / other implementation might be running under hardware constraints.
For
chainHead_storage
we are using 5 items, but we haven't collect yet any data to make a better informed decision:polkadot-sdk/substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
Lines 85 to 87 in f4f1b23
The
archive
class is not exposed by substrate yet, but would keep both classes in syncThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be to good to add a prometheus metric for that or some annoying log for us to know whether this limit is exceeded often or not ^^