Skip to content
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 27 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7fe61dd
chainHead/storage: Fix typo
lexnv Oct 3, 2023
e8d3d8a
rpc-v2: Move hex_string to common
lexnv Oct 3, 2023
56e03db
rpc-v2: Introduce common module for storage queries and events
lexnv Oct 3, 2023
316f116
chainHead/storage: Use common storage object for queries
lexnv Oct 3, 2023
c5886c0
rpc-v2: Rename storage params for pagination
lexnv Oct 3, 2023
077f2f5
common/event: Add paginated query type
lexnv Oct 3, 2023
cf050f7
archive/storage: Implement archive_storage
lexnv Oct 5, 2023
e4de233
archive/storage: Add configurable pagination support
lexnv Oct 5, 2023
044941e
archive/tests: Check hashes and values
lexnv Oct 6, 2023
1cde37d
archive/tests: Test merkle value returns the expected hashes
lexnv Oct 6, 2023
d560bb7
archive/tests: Check pagination support produces variable items
lexnv Oct 6, 2023
f29b761
archive/tests: Check hash and value queries
lexnv Oct 11, 2023
5914660
archive: Add maximum number of queried items
lexnv Oct 11, 2023
038eb41
Merge branch 'master' into lexnv/archive_storage
lexnv Oct 11, 2023
90f5e9b
Merge remote-tracking branch 'origin/master' into lexnv/archive_storage
lexnv Dec 5, 2023
f4f1b23
archive/tests: Port tests to BlockBuilder
lexnv Dec 5, 2023
1de7eb2
spec-v2: Remove the concept of non-queriable keys
lexnv Dec 7, 2023
dd70064
Fix build
lexnv Dec 7, 2023
8ad9513
storage: Remove unused imports
lexnv Dec 7, 2023
2be9f52
Merge branch 'master' into lexnv/archive_storage
lexnv Dec 7, 2023
2134bf4
Merge branch 'master' into lexnv/archive_storage
lexnv Dec 8, 2023
553f445
Merge remote-tracking branch 'origin/master' into lexnv/archive_storage
lexnv Jan 15, 2024
6f3920e
rpc-v2/events: Add `StorageQueryType::is_descendant_query` method
lexnv Jan 15, 2024
0580429
rpc-v2/common: Add `ok` and `err` methods for `ArchiveStorageResult`
lexnv Jan 15, 2024
3cf59a8
rpc-v2/archive: Apply cargo fmt
lexnv Jan 15, 2024
899fa7a
Update substrate/client/rpc-spec-v2/src/lib.rs
lexnv Jan 15, 2024
688e3fb
archive: Remove unused imports
lexnv Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion substrate/client/rpc-spec-v2/src/archive/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

//! API trait of the archive methods.

use crate::MethodResult;
use crate::{
common::events::{ArchiveStorageResult, PaginatedStorageQuery},
MethodResult,
};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

#[rpc(client, server)]
Expand Down Expand Up @@ -88,4 +91,17 @@ pub trait ArchiveApi<Hash> {
function: String,
call_parameters: String,
) -> RpcResult<MethodResult>;

/// Returns storage entries at a specific block's state.
///
/// # Unstable
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_unstable_storage", blocking)]
fn archive_unstable_storage(
&self,
hash: Hash,
items: Vec<PaginatedStorageQuery<String>>,
child_trie: Option<String>,
) -> RpcResult<ArchiveStorageResult>;
}
73 changes: 68 additions & 5 deletions substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

use crate::{
archive::{error::Error as ArchiveError, ArchiveApiServer},
chain_head::hex_string,
MethodResult,
common::events::{ArchiveStorageResult, PaginatedStorageQuery, StorageQueryType},
hex_string, MethodResult,
};

use codec::Encode;
use jsonrpsee::core::{async_trait, RpcResult};
use sc_client_api::{
Backend, BlockBackend, BlockchainEvents, CallExecutor, ExecutorProvider, StorageProvider,
Backend, BlockBackend, BlockchainEvents, CallExecutor, ChildInfo, ExecutorProvider, StorageKey,
StorageProvider,
};
use sp_api::{CallApiAt, CallContext};
use sp_blockchain::{
Expand All @@ -40,6 +41,8 @@ use sp_runtime::{
};
use std::{collections::HashSet, marker::PhantomData, sync::Arc};

use super::archive_storage::ArchiveStorage;

/// An API for archive RPC calls.
pub struct Archive<BE: Backend<Block>, Block: BlockT, Client> {
/// Substrate client.
Expand All @@ -48,8 +51,12 @@ pub struct Archive<BE: Backend<Block>, Block: BlockT, Client> {
backend: Arc<BE>,
/// The hexadecimal encoded hash of the genesis block.
genesis_hash: String,
/// 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,
/// Phantom member to pin the block type.
_phantom: PhantomData<(Block, BE)>,
_phantom: PhantomData<Block>,
}

impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
Expand All @@ -58,9 +65,18 @@ impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
client: Arc<Client>,
backend: Arc<BE>,
genesis_hash: GenesisHash,
storage_max_reported_items: usize,
storage_max_queried_items: usize,
) -> Self {
let genesis_hash = hex_string(&genesis_hash.as_ref());
Self { client, backend, genesis_hash, _phantom: PhantomData }
Self {
client,
backend,
genesis_hash,
storage_max_reported_items,
storage_max_queried_items,
_phantom: PhantomData,
}
}
}

Expand Down Expand Up @@ -185,4 +201,51 @@ where
Err(error) => MethodResult::err(error.to_string()),
})
}

fn archive_unstable_storage(
&self,
hash: Block::Hash,
items: Vec<PaginatedStorageQuery<String>>,
child_trie: Option<String>,
) -> RpcResult<ArchiveStorageResult> {
let items = items
.into_iter()
.map(|query| {
let key = StorageKey(parse_hex_param(query.key)?);
let pagination_start_key = query
.pagination_start_key
.map(|key| parse_hex_param(key).map(|key| StorageKey(key)))
.transpose()?;

// Paginated start key is only supported
if pagination_start_key.is_some() &&
(query.query_type != StorageQueryType::DescendantsValues &&
lexnv marked this conversation as resolved.
Show resolved Hide resolved
query.query_type != StorageQueryType::DescendantsHashes)
{
return Err(ArchiveError::InvalidParam(
"Pagination start key is only supported for descendants queries"
.to_string(),
))
}

Ok(PaginatedStorageQuery {
key,
query_type: query.query_type,
pagination_start_key,
})
})
.collect::<Result<Vec<_>, ArchiveError>>()?;

let child_trie = child_trie
.map(|child_trie| parse_hex_param(child_trie))
.transpose()?
.map(ChildInfo::new_default_from_vec);

let storage_client = ArchiveStorage::new(
self.client.clone(),
self.storage_max_reported_items,
self.storage_max_queried_items,
);
Ok(storage_client.handle_query(hash, items, child_trie))
}
}
136 changes: 136 additions & 0 deletions substrate/client/rpc-spec-v2/src/archive/archive_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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::{
ArchiveStorageMethodErr, ArchiveStorageMethodOk, 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);
Copy link
Contributor

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?

Copy link
Contributor Author

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:

/// The maximum number of items the `chainHead_storage` can return
/// before paginations is required.
const MAX_STORAGE_ITER_ITEMS: usize = 5;

The archive class is not exposed by substrate yet, but would keep both classes in sync

Copy link
Member

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 ^^

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(ArchiveStorageMethodErr { error }),
lexnv marked this conversation as resolved.
Show resolved Hide resolved
}
},
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(ArchiveStorageMethodErr { 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(ArchiveStorageMethodErr { 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(ArchiveStorageMethodErr { 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(ArchiveStorageMethodErr { error }),
}
},
};
}

ArchiveStorageResult::Ok(ArchiveStorageMethodOk {
result: storage_results,
discarded_items,
})
}
}
2 changes: 2 additions & 0 deletions substrate/client/rpc-spec-v2/src/archive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#[cfg(test)]
mod tests;

mod archive_storage;

pub mod api;
pub mod archive;
pub mod error;
Expand Down
Loading